in reply to decimal to fraction
Having made the odd tweak to it myself in passing, it continues to make me want to rewrite it in PP, which would probably go much quicker and would also allow pthreading for big batches of numbers. Probably it would live most naturally in either PDL::Math (the core), or maybe a PDL::NumericalAnalysis?sub lentzCF { # continued fraction using lentz method Numerical Recipes # p. 171. Arguments are as, bs, maximum number of iterations and # smallness parameter # a0+b1/a1+b2+... my $as=shift; my $bs=shift; my $max=shift; my $small=shift; my $tiny=r2C(1.e-30); my $converged=0; my $fn=$as->slice(0); $fn=$tiny if all($fn==0); my $n=1; my ($fnm1, $Cnm1, $Dnm1)=($fn, $fn, r2C(0)); #previous coeffs. my ($Cn, $Dn); #current coeffs. my $Deltan; while($n<$max){ $Dn=$as->slice($n)+$bs->slice($n)*$Dnm1; $Dn=$tiny if all($Dn==0); $Cn=$as->slice($n)+$bs->slice($n)/$Cnm1; $Cn=$tiny if all($Cn==0); $Dn=1/$Dn; $Deltan=$Cn*$Dn; $fn=$fnm1*$Deltan; last if $converged=$Deltan->approx(1, $small)->all; $fnm1=$fn; $Dnm1=$Dn; $Cnm1=$Cn; $n++; } $fn = $fn->slice("(0)"); return wantarray? ($fn, $n): $fn; }
|
|---|