Thiele's interpolation formula: Difference between revisions
→{{header|Perl 6}}: modernize rho a bit
(Python solution) |
(→{{header|Perl 6}}: modernize rho a bit) |
||
Line 682:
=={{header|Perl 6}}==
{{works with|Rakudo|
Implemented to parallel the (generalized) formula. (i.e. clearer, but naive and very slow.)
<lang perl6>use v6;
# reciprocal difference:
multi sub
multi sub
multi sub
+ ρ(&f, @x[
▲ / ( rho($f, @x[^($ord -1)]) # / ( rho[n-1](x[0], ..., x[n-1])
▲ - rho($f, @x[1..^($ord)]) ) # - rho[n-1](x[1], ..., x[n]) )
▲ + rho($f, @x[1..^($ord -1)]); # + rho[n-2](x[1], ..., x[n-1])
}
# Thiele:
multi sub thiele($x, %f, $ord where { $ord == +%f }) { 1 } # Identity
multi sub thiele($x, %f, $ord) {
my
# Caveat: depends on the fact that Rakudo maintains key order within hashes
my $a =
my $b =
my $num = $x - %f.keys[$ord];
my $cont = thiele($x, %f, $ord +1);
# Thiele always takes this form:
return $a - $b + ( $num / $cont );
}
## Demo
sub mk-inv(
my %h;
for 0..$lim { %h{
return %h;
}
sub MAIN($tblsz) {
my %invsin = mk-inv(&sin, 0.05, $tblsz);
my %invcos = mk-inv(&cos, 0.05, $tblsz);
my %invtan = mk-inv(&tan, 0.05, $tblsz);
my $sin_pi = 6 * thiele(0.5, %invsin, 0);
my $cos_pi = 3 * thiele(0.5, %invcos, 0);
my $tan_pi = 4 * thiele(1.0, %invtan, 0);
say "pi = {pi}";
say "estimations using a table of $tblsz elements:";
Line 740 ⟶ 737:
Output (table size of 6 for want of resources):
<pre>pi = 3.14159265358979▼
▲pi = 3.14159265358979
estimations using a table of 6 elements:
sin interpolation: 3.
cos interpolation: 1.
tan interpolation: 3.
=={{header|PicoLisp}}==
|