Math-Algebra-Symbols
view release on metacpan or search on metacpan
lib/Math/Algebra/Symbols/Sum.pm view on Meta::CPAN
#_______________________________________________________________________
my %S = ();
for my $t(@s)
{my $s = $t->Sqrt;
my $S = $s->signature;
push @{$S{$S}}, $t;
}
#_______________________________________________________________________
# Square each partitions, as required by the formulae below.
#_______________________________________________________________________
my @t;
push @t, sigma(@n)->power($two) if scalar(@n); # Non sqrt partition
for my $s(keys(%S))
{push @t, sigma(@{$S{$s}})->power($two); # Sqrt partition
}
#_______________________________________________________________________
# I can multiply out upto 4 square roots using the formulae below.
# There are formula to multiply out more than 4 sqrts, but they are big.
# These formulae are obtained by squaring out and rearranging:
# sqrt(a)+sqrt(b)+sqrt(c)+sqrt(d) == 0 until no sqrts remain, and
# then matching terms to produce optimal execution.
# This remarkable result was obtained with the help of this package:
# demonstrating its utility in optimizing complex calculations written
# in Perl: which in of itself cannot optimize broadly.
#_______________________________________________________________________
my $ns = scalar(@t);
# 2016/01/26 12:29:28 No need to die
# $ns < 5 or die "There are $ns square roots present. I can handle less than 5";
t/bug_2004_6_1.t view on Meta::CPAN
# Convert miles per gallon to liters per 100 kilometers symbolically.
# PhilipRBrenan@yahoo.com, 2004
#______________________________________________________________________
($gallons, $miles) = symbols(qw(gallons miles));
$liters = $gallons * 3.8; # 4: Have to use fraction, not decimal, improvement needed.
$kilometers = $miles * 1.6; # 5: Have to use fraction, not decimal, improvement needed.
$consumption = $liters / $kilometers * 100; # 6: Correct precedence
print "Liters per 100 kilometers = $consumption\n"; # 7: As a general formula.
$miles = 40; # 8: This expresses that the mileage is 40. Mike points out that
$gallons = 1; # it would be more natural to express this as $miles/$gallons == 40,
print
"$miles miles per $gallons gallon = ",
eval "$consumption", # 9: Evaluate for a specific example
" liters per 100 kilometers\n";
# Liters per 100 kilometers = 475/2*$gallons/$miles # 10: The general formula
# 40 miles per 1 gallon = 5.9375 liters per 100 kilometers # 11: A specific example
ok("$consumption" eq '475/2*$gallons/$miles');
ok(eval "$consumption" == 5.9375);
my ($gallons, $miles) = symbols(qw(gallons miles));
ok($gallons/$miles == 1/($miles/$gallons)); # 12: Check inverse of $miles/$gallon
( run in 0.312 second using v1.01-cache-2.11-cpan-26ccb49234f )