Thiele's interpolation formula: Difference between revisions

Added FreeBASIC
(→‎{{header|Haskell}}: hlint, hindent + a couple of reductions)
(Added FreeBASIC)
 
(24 intermediate revisions by 12 users not shown)
Line 26:
#* &nbsp; <big><big> 4 &times; tan<sup>-1</sup> 1 = <math>\pi</math></big></big>
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F thieleInterpolator(x, y)
V ρ = enumerate(y).map((i, yi) -> [yi] * (@y.len - i))
L(i) 0 .< ρ.len - 1
ρ[i][1] = (x[i] - x[i + 1]) / (ρ[i][0] - ρ[i + 1][0])
L(i) 2 .< ρ.len
L(j) 0 .< ρ.len - i
ρ[j][i] = (x[j] - x[j + i]) / (ρ[j][i - 1] - ρ[j + 1][i - 1]) + ρ[j + 1][i - 2]
V ρ0 = ρ[0]
F t(xin)
V a = 0.0
L(i) (@=ρ0.len - 1 .< 1).step(-1)
a = (xin - @=x[i - 1]) / (@=ρ0[i] - @=ρ0[i - 2] + a)
R @=y[0] + (xin - @=x[0]) / (@=ρ0[1] + a)
R t
 
V xVal = (0.<32).map(i -> i * 0.05)
V tSin = xVal.map(x -> sin(x))
V tCos = xVal.map(x -> cos(x))
V tTan = xVal.map(x -> tan(x))
V iSin = thieleInterpolator(tSin, xVal)
V iCos = thieleInterpolator(tCos, xVal)
V iTan = thieleInterpolator(tTan, xVal)
print(‘#.14’.format(6 * iSin(0.5)))
print(‘#.14’.format(3 * iCos(0.5)))
print(‘#.14’.format(4 * iTan(1)))</syntaxhighlight>
 
{{out}}
<pre>
3.14159265358979
3.14159265358979
3.14159265358980
</pre>
 
=={{header|Ada}}==
thiele.ads:
<langsyntaxhighlight Adalang="ada">with Ada.Numerics.Generic_Real_Arrays;
 
generic
Line 45 ⟶ 81:
X, Y, RhoX : Real_Array (1 .. Length);
end record;
end Thiele;</langsyntaxhighlight>
 
thiele.adb:
<langsyntaxhighlight Adalang="ada">package body Thiele is
use type Real_Array;
 
Line 103 ⟶ 139:
end Inverse;
 
end Thiele;</langsyntaxhighlight>
 
example:
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
with Ada.Numerics.Generic_Elementary_Functions;
with Thiele;
Line 148 ⟶ 184:
Long_Float'Image (4.0 * Inverse (Tan, 1.0)));
end;
end Main;</langsyntaxhighlight>
 
output:
Line 161 ⟶ 197:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny] - Currying is supported.}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput'' - Also slicing a '''struct''' table and currying unimplemented.}}
<langsyntaxhighlight lang="algol68">PROC raise exception = ([]STRING msg)VOID: ( putf(stand error,("Exception:", $" "g$, msg, $l$)); stop );
 
# The MODE of lx and ly here should really be a UNION of "something REAL",
Line 213 ⟶ 249:
"tan", 4*inv tan(1)
))
)</langsyntaxhighlight>
Output:
<pre>
Line 241 ⟶ 277:
 
Note that each <math>\rho_n</math> needs to look up <math>\rho_{n-1}</math> twice, so the total look ups go up as <math>O(2^N)</math> while there are only <math>O(N^2)</math> values. This is a text book situation for memoization.
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
#include <math.h>
Line 304 ⟶ 340:
printf("%16.14f\n", 4 * i_tan(1.));
return 0;
}</syntaxhighlight>
}</lang>output<lang>3.14159265358979
{{out}}
<pre>3.14159265358979
3.14159265358979
3.14159265358979</langpre>
 
=={{header|C++}}==
{{trans|C}}
{{works with|C++14}}
<syntaxhighlight lang="cpp">#include <cmath>
#include <iostream>
#include <iomanip>
#include <string.h>
 
constexpr unsigned int N = 32u;
double xval[N], t_sin[N], t_cos[N], t_tan[N];
 
constexpr unsigned int N2 = N * (N - 1u) / 2u;
double r_sin[N2], r_cos[N2], r_tan[N2];
 
double ρ(double *x, double *y, double *r, int i, int n) {
if (n < 0)
return 0;
if (!n)
return y[i];
 
unsigned int idx = (N - 1 - n) * (N - n) / 2 + i;
if (r[idx] != r[idx])
r[idx] = (x[i] - x[i + n]) / (ρ(x, y, r, i, n - 1) - ρ(x, y, r, i + 1, n - 1)) + ρ(x, y, r, i + 1, n - 2);
return r[idx];
}
 
double thiele(double *x, double *y, double *r, double xin, unsigned int n) {
return n > N - 1 ? 1. : ρ(x, y, r, 0, n) - ρ(x, y, r, 0, n - 2) + (xin - x[n]) / thiele(x, y, r, xin, n + 1);
}
 
inline auto i_sin(double x) { return thiele(t_sin, xval, r_sin, x, 0); }
inline auto i_cos(double x) { return thiele(t_cos, xval, r_cos, x, 0); }
inline auto i_tan(double x) { return thiele(t_tan, xval, r_tan, x, 0); }
 
int main() {
constexpr double step = .05;
for (auto i = 0u; i < N; i++) {
xval[i] = i * step;
t_sin[i] = sin(xval[i]);
t_cos[i] = cos(xval[i]);
t_tan[i] = t_sin[i] / t_cos[i];
}
for (auto i = 0u; i < N2; i++)
r_sin[i] = r_cos[i] = r_tan[i] = NAN;
 
std::cout << std::setw(16) << std::setprecision(25)
<< 6 * i_sin(.5) << std::endl
<< 3 * i_cos(.5) << std::endl
<< 4 * i_tan(1.) << std::endl;
 
return 0;
}</syntaxhighlight>
{{out}}
<pre>3.141592653589793115997963
3.141592653589793560087173
3.141592653589794892354803</pre>
 
=={{header|Common Lisp}}==
Using the notations from above the C code instead of task desc.
<langsyntaxhighlight lang="lisp">;; 256 is heavy overkill, but hey, we memoized
(defparameter *thiele-length* 256)
(defparameter *rho-cache* (make-hash-table :test #'equal))
Line 353 ⟶ 448:
(format t "~f~%" (* 6 (inv-sin .5)))
(format t "~f~%" (* 3 (inv-cos .5)))
(format t "~f~%" (* 4 (inv-tan 1)))</langsyntaxhighlight>output (SBCL):<syntaxhighlight lang="text">3.141592653589793
3.1415926535885172
3.141592653589819</langsyntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.range, std.array, std.algorithm, std.math;
 
struct Domain {
Line 429 ⟶ 524:
writefln(" %20.19f 3 * inv_cos(0.5)", tcos.inverse(0.5L) * 3.0L);
writefln(" %20.19f 4 * inv_tan(1.0)", ttan.inverse(1.0L) * 4.0L);
}</langsyntaxhighlight>
{{out}}
<pre> 32 interpolating points
Line 440 ⟶ 535:
3.1415926535897932382 3 * inv_cos(0.5)
3.1415926535897932382 4 * inv_tan(1.0)</pre>
 
=={{header|FreeBASIC}}==
{{trans|Phix}}
<syntaxhighlight lang="vbnet">Const As Integer n1 = 32
Const As Integer n2 = (n1 * (n1 - 1) / 2)
Const As Double paso = 0.05
Const As Double INF = 1e308
Const As Double NaN = -(INF/INF)
 
Dim As Double xVal(n1), tSin(n1), tCos(n1), tTan(n1)
For i As Integer = 1 To n1
xVal(i) = (i-1) * paso
tSin(i) = Sin(xVal(i))
tCos(i) = Cos(xVal(i))
tTan(i) = Tan(xVal(i))
Next i
 
Dim As Integer rSin, rCos, rTan, rTrig
 
Dim Shared rhot(rTrig, n2) As Double
For i As Integer = 0 To rTrig
For j As Integer = 0 To n2
rhot(i, j) = NaN
Next j
Next i
 
Function rho(x() As Double, y() As Double, Byval rdx As Integer, _
Byval i As Integer, Byval n As Integer) As Double
If n < 0 Then Return 0
If n = 0 Then Return y(i+1)
Dim As Integer idx = (n1 - 1 - n) * (n1 - n) / 2 + i + 1
If rhot(rdx, idx) = NaN Then 'valor aún no calculado
rhot(rdx, idx) = (x(i+1) - x(i+1 + n)) _
/ (rho(x(), y(), rdx, i, n-1) - rho(x(), y(), rdx, i+1, n-1)) _
+ rho(x(), y(), rdx, i+1, n-2)
End If
Return rhot(rdx, idx)
End Function
 
Function thieleInterpolator(x() As Double, y() As Double, _
Byval rdx As Integer, Byval xin As Double, Byval n As Integer) As Double
If n > n1-1 Then Return 1
Return rho(x(), y(), rdx, 0, n) - rho(x(), y(), rdx, 0, n-2) _
+ (xin-x(n+1)) / thieleInterpolator(x(), y(), rdx, xin, n+1)
End Function
 
Print " PI : 3.141592653589793"
Print " 6*arcsin(0.5) : "; 6 * Asin(0.5)
Print " 3*arccos(0.5) : "; 3 * Acos(0.5)
Print " 4*arctan(1.0) : "; 4 * Atn(1.0)
 
Print "6*thiele(tSin,xVal,rSin,0.5,0) : "; 6 * thieleInterpolator(tSin(), xVal(), rSin, 0.5, 0)
Print "3*thiele(tCos,xVal,rCos,0.5,0) : "; 3 * thieleInterpolator(tCos(), xVal(), rCos, 0.5, 0)
Print "4*thiele(tTan,xVal,rTan,1.0,0) : "; 4 * thieleInterpolator(tTan(), xVal(), rTan, 1.0, 0)
 
Sleep</syntaxhighlight>
 
=={{header|Go}}==
{{trans|ALGOL 68}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 497 ⟶ 649:
return y[0] + (xin-x[0])/(ρ0[1]+a)
}
}</langsyntaxhighlight>
Output:
<pre>
Line 507 ⟶ 659:
=={{header|Haskell}}==
Caching of rho is automatic due to lazy lists.
<langsyntaxhighlight lang="haskell">thiele :: [Double] -> [Double] -> Double -> Double
thiele xs ys = f rho1 (tail xs)
where
f _ [] _ = 1
f r@(r0:r1:r2:rs) (x:xs) v = r2 - r0 + (v - x) / f (tail r) xs v
rho1 = ((!! 1) . (++ [0])) <$> rho
rho = [0,repeat 0 ..] : [repeat 0,0 ..] : ys : rnext (tail rho) xs (tail xs)
where
rnext _ _ [] = []
Line 523 ⟶ 675:
-- Inverted interpolation function of f
invInterp :: (Double -> Double) -> [Double] -> Double -> Double
invInterp f xs = thiele (fmap <$>f xs) xs
 
main :: IO ()
Line 536 ⟶ 688:
[inv_sin, inv_cos, inv_tan] =
uncurry ((. div_pi) . invInterp) <$>
[(sin, (2, 31)), (cos, (2, 100)), (tan, (4, 1001000))]
-- N points taken uniformly from 0 to Pi/d
div_pi (d, n) = (* (pi / (d * n))) <$> [0 .. n]</langsyntaxhighlight>
{{out}}
<pre>3.141592653589795
3.141592653589791
3.1415926535897905141592653587783</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j">
<lang j>
span =: {. - {: NB. head - tail
spans =: span\ NB. apply span to successive infixes
</syntaxhighlight>
</lang>
 
<pre>
Line 557 ⟶ 709:
</pre>
 
<syntaxhighlight lang="j">
<lang j>
NB. abscissae_of_knots coef ordinates_of_knots
NB. returns the interpolation coefficients for eval
Line 578 ⟶ 730:
(p{~>:i)+(x-i{xx)%(p{~i+2)+a
)
</syntaxhighlight>
</lang>
 
<pre>
Line 607 ⟶ 759:
</pre>
 
<syntaxhighlight lang="j">
<lang j>
thiele =: 2 : 0
p =. _2 _{.,:n
Line 621 ⟶ 773:
(p{~>:i)+(y-i{m)%a+p{~i+2
)
</syntaxhighlight>
</lang>
 
<pre>
Line 638 ⟶ 790:
=={{header|Java}}==
{{trans|C}}
<langsyntaxhighlight lang="java">import static java.lang.Math.*;
 
public class Test {
Line 692 ⟶ 844:
System.out.printf("%16.14f%n", 4 * thiele(t_tan, xval, r_tan, 1.0, 0));
}
}</langsyntaxhighlight>
<pre>3.14159265358979
3.14159265358979
3.14159265358980</pre>
 
=={{header|Julia}}==
Accuracy improves with a larger table and smaller step size.
{{trans|C}}
<syntaxhighlight lang="julia">const N = 256
const N2 = N * div(N - 1, 2)
const step = 0.01
const xval_table = zeros(Float64, N)
const tsin_table = zeros(Float64, N)
const tcos_table = zeros(Float64, N)
const ttan_table = zeros(Float64, N)
const rsin_cache = Dict{Float64, Float64}()
const rcos_cache = Dict{Float64, Float64}()
const rtan_cache = Dict{Float64, Float64}()
 
function rho(x, y, rhocache, i, n)
if n < 0
return 0.0
elseif n == 0
return y[i+1]
end
idx = (N - 1 - n) * div(N - n, 2) + i
if !haskey(rhocache, idx)
rhocache[idx] = (x[i+1] - x[i + n+1]) / (rho(x, y, rhocache, i, n - 1) -
rho(x, y, rhocache, i + 1, n - 1)) + rho(x, y, rhocache, i + 1, n - 2)
end
rhocache[idx]
end
 
function thiele(x, y, r, xin, n)
if n > N - 1
return 1.0
end
rho(x, y, r, 0, n) - rho(x, y, r, 0, n - 2) + (xin - x[n + 1]) / thiele(x, y, r, xin, n + 1)
end
 
function thiele_tables()
for i in 1:N
xval_table[i] = (i-1) * step
tsin_table[i] = sin(xval_table[i])
tcos_table[i] = cos(xval_table[i])
ttan_table[i] = tsin_table[i] / tcos_table[i]
end
println(6 * thiele(tsin_table, xval_table, rsin_cache, 0.5, 0))
println(3 * thiele(tcos_table, xval_table, rcos_cache, 0.5, 0))
println(4 * thiele(ttan_table, xval_table, rtan_cache, 1.0, 0))
end
 
thiele_tables()
</syntaxhighlight>{{output}}<pre>
3.1415926535898335
3.141592653589818
3.141592653589824
</pre>
 
=={{header|Kotlin}}==
{{trans|C}}
<langsyntaxhighlight lang="scala">// version 1.1.2
 
const val N = 32
Line 741 ⟶ 947:
println("%16.14f".format(3 * thiele(tcos, xval, rcos, 0.5, 0)))
println("%16.14f".format(4 * thiele(ttan, xval, rtan, 1.0, 0)))
}</langsyntaxhighlight>
 
{{out}}
Line 748 ⟶ 954:
3.14159265358979
3.14159265358980
</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">num = 32;
num2 = num (num - 1)/2;
step = 0.05;
ClearAll[\[Rho], Thiele]
\[Rho][x_List, y_List, i_Integer, n_Integer] := Module[{idx},
If[n < 0,
0
,
If[n == 0,
y[[i + 1]]
,
idx = (num - 1 - n) (num - n)/2 + i + 1;
If[r[[idx]] === Null,
r[[idx]] = (x[[1 + i]] -
x[[1 + i + n]])/(\[Rho][x, y, i, n - 1] - \[Rho][x, y,
i + 1, n - 1]) + \[Rho][x, y, i + 1, n - 2];
];
r[[idx]]
]
]
]
Thiele[x_List, y_List, xin_, n_Integer] := Module[{},
If[n > num - 1,
1
,
\[Rho][x, y, 0, n] - \[Rho][x, y, 0, n - 2] + (xin - x[[n + 1]])/
Thiele[x, y, xin, n + 1]
]
]
xval = Range[0, num - 1] step;
funcvals = Sin[xval];
r = ConstantArray[Null, num2];
6 Thiele[funcvals, xval, 0.5, 0]
funcvals = Cos[xval];
r = ConstantArray[Null, num2];
3 Thiele[funcvals, xval, 0.5, 0]
funcvals = Tan[xval];
r = ConstantArray[Null, num2];
4 Thiele[funcvals, xval, 1.0, 0]</syntaxhighlight>
{{out}}
<pre>3.14159
3.14159
3.14159</pre>
 
=={{header|Nim}}==
{{trans|Java}}
<syntaxhighlight lang="nim">import strformat
import math
 
const N = 32
const N2 = N * (N - 1) div 2
const STEP = 0.05
 
var xval = newSeq[float](N)
var tsin = newSeq[float](N)
var tcos = newSeq[float](N)
var ttan = newSeq[float](N)
var rsin = newSeq[float](N2)
var rcos = newSeq[float](N2)
var rtan = newSeq[float](N2)
 
proc rho(x, y: openArray[float], r: var openArray[float], i, n: int): float =
if n < 0:
return 0
if n == 0:
return y[i]
let idx = (N - 1 - n) * (N - n) div 2 + i
if r[idx] != r[idx]:
r[idx] = (x[i] - x[i + n]) /
(rho(x, y, r, i, n - 1) - rho(x, y, r, i + 1, n - 1)) +
rho(x, y, r, i + 1, n - 2)
return r[idx]
 
proc thiele(x, y: openArray[float], r: var openArray[float], xin: float, n: int): float =
if n > N - 1:
return 1
return rho(x, y, r, 0, n) - rho(x, y, r, 0, n - 2) +
(xin - x[n]) / thiele(x, y, r, xin, n + 1)
 
for i in 0..<N:
xval[i] = float(i) * STEP
tsin[i] = sin(xval[i])
tcos[i] = cos(xval[i])
ttan[i] = tsin[i] / tcos[i]
 
for i in 0..<N2:
rsin[i] = NaN
rcos[i] = NaN
rtan[i] = NaN
echo fmt"{6 * thiele(tsin, xval, rsin, 0.5, 0):16.14f}"
echo fmt"{3 * thiele(tcos, xval, rcos, 0.5, 0):16.14f}"
echo fmt"{4 * thiele(ttan, xval, rtan, 1.0, 0):16.14f}"</syntaxhighlight>
{{out}}
<pre>
3.14159265358979
3.14159265358979
3.14159265358979
</pre>
 
Line 753 ⟶ 1,061:
This example shows how the accuracy changes with the degree of interpolation. The table 'columns' are only constructed implicitly during the recursive calculation of <em>rdiff</em> and <em>thiele</em>, but (as mentioned in the C code example) using memoization or explicit tabulation would speed up the calculation. The interpolation uses the nearest points around <em>x</em> for accuracy.
 
<langsyntaxhighlight OCamllang="ocaml">let xv, fv = fst, snd
 
let rec rdiff a l r =
Line 788 ⟶ 1,096:
Printf.printf "4*arctan(1.0) = %.15f\n" (4.0*.(interpolate 1.0 tan_tab n));;
 
List.iter test [8; 12; 16]</langsyntaxhighlight>
Output:
<pre>Degree 8 interpolation:
Line 805 ⟶ 1,113:
4*arctan(1.0) = 3.141592653589793</pre>
 
=={{header|Perl 6}}==
{{trans|Sidef}}
{{Works with|rakudo|2016.07}}<br>
<syntaxhighlight lang="perl">use strict;
Implemented to parallel the (generalized) formula. (i.e. clearer, but naive and very slow.)
use warnings;
<lang perl6>use v6;
use feature 'say';
use Math::Trig;
# reciprocal difference:
use utf8;
multi sub ρ(&f, @x where * < 1) { 0 } # Identity
 
multi sub ρ(&f, @x where * == 1) { &f(@x[0]) }
sub thiele {
multi sub ρ(&f, @x where * > 1) {
my($x, $y) = @_;
( @x[0] - @x[* - 1] ) # ( x - x[n] )
 
/ (ρ(&f, @x[^(@x - 1)]) # / ( ρ[n-1](x[0], ..., x[n-1])
my @ρ;
- ρ(&f, @x[1..^@x]) ) # - ρ[n-1](x[1], ..., x[n]) )
+push @ρ(&f, @x[1..^(@$$y[$_]) x (@$y- 1$_)]); for # + ρ[n-2](x[1],0 ..., x[n@$y-1]);
for my $i (0 .. @ρ - 2) {
$ρ[$i][1] = (($$x[$i] - $$x[$i+1]) / ($ρ[$i][0] - $ρ[$i+1][0]))
}
for my $i (2 .. @ρ - 2) {
for my $j (0 .. (@ρ - 2) - $i) {
$ρ[$j][$i] = ((($$x[$j]-$$x[$j+$i]) / ($ρ[$j][$i-1]-$ρ[$j+1][$i-1])) + $ρ[$j+1][$i-2])
}
}
my @ρ0 = @{$ρ[0]};
 
return sub {
my($xin) = @_;
 
my $a = 0;
for my $i (reverse 2 .. @ρ0 - 2) {
$a = (($xin - $$x[$i-1]) / ($ρ0[$i] - $ρ0[$i-2] + $a))
}
$$y[0] + (($xin - $$x[0]) / ($ρ0[1] + $a))
}
}
# Thiele:
multi sub thiele($x, %f, $ord where { $ord == +%f }) { 1 } # Identity
multi sub thiele($x, %f, $ord) {
my &f = {%f{$^a}}; # f(x) as a table lookup
# Caveat: depends on the fact that Rakudo maintains key order within hashes
my $a = ρ(&f, %f.keys[^($ord +1)]);
my $b = ρ(&f, %f.keys[^($ord -1)]);
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(&fn, $d, $lim) {
my %h;
for 0..$lim { %h{ &fn($_ * $d) } = $_ * $d }
return %h;
}
sub MAIN($tblsz = 12) {
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:";
say "sin interpolation: $sin_pi";
say "cos interpolation: $cos_pi";
say "tan interpolation: $tan_pi";
}</lang>
 
my(@x,@sin_table,@cos_table,@tan_table);
Output:
push @x, .05 * $_ for 0..31;
push @sin_table, sin($_) for @x;
push @cos_table, cos($_) for @x;
push @tan_table, tan($_) for @x;
 
my $sin_inverse = thiele(\@sin_table, \@x);
<pre>pi = 3.14159265358979
my $cos_inverse = thiele(\@cos_table, \@x);
estimations using a table of 12 elements:
my $tan_inverse = thiele(\@tan_table, \@x);
sin interpolation: 3.14159265358961
 
cos interpolation: 3.1387286696692
say 6 * &$sin_inverse(0.5);
tan interpolation: 3.14159090545243</pre>
say 3 * &$cos_inverse(0.5);
say 4 * &$tan_inverse(1.0);</syntaxhighlight>
{{out}}
<pre>3.14159265358979
3.14159265358979
3.1415926535898</pre>
 
=={{header|Phix}}==
{{trans|C}}
To be honest I was slightly wary of this, what with tables being passed by reference and fairly heavy use of closures in other languages, but in the end all it took was a simple enum (R_SIN..R_TRIG).
<!--<syntaxhighlight lang="phix">-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">N</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">32</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">N2</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">N</span> <span style="color: #0000FF;">*</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">N</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">STEP</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0.05</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">inf</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1e300</span><span style="color: #0000FF;">*</span><span style="color: #000000;">1e300</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">nan</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-(</span><span style="color: #000000;">inf</span><span style="color: #0000FF;">/</span><span style="color: #000000;">inf</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">xval</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">t_sin</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">t_cos</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">t_tan</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">N</span><span style="color: #0000FF;">),</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">N</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">xval</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">STEP</span>
<span style="color: #000000;">t_sin</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xval</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
<span style="color: #000000;">t_cos</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cos</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xval</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
<span style="color: #000000;">t_tan</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">t_sin</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">t_cos</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">enum</span> <span style="color: #000000;">R_SIN</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">R_COS</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">R_TAN</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">R_TRIG</span><span style="color: #0000FF;">=$</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">rhot</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">nan</span><span style="color: #0000FF;">,</span><span style="color: #000000;">N2</span><span style="color: #0000FF;">),</span><span style="color: #000000;">R_TRIG</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">rho</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">rdx</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">int</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">int</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">idx</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">N</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">1</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">*</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">N</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">2</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">;</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">rhot</span><span style="color: #0000FF;">[</span><span style="color: #000000;">rdx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">nan</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- value not computed yet</span>
<span style="color: #000000;">rhot</span><span style="color: #0000FF;">[</span><span style="color: #000000;">rdx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">])</span>
<span style="color: #0000FF;">/</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">rho</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">rdx</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">rho</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">rdx</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))</span>
<span style="color: #0000FF;">+</span> <span style="color: #000000;">rho</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">rdx</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">rhot</span><span style="color: #0000FF;">[</span><span style="color: #000000;">rdx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">thiele</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">rdx</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">xin</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">></span><span style="color: #000000;">N</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">rho</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">rdx</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">rho</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">rdx</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">+</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">xin</span><span style="color: #0000FF;">-</span><span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">thiele</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">rdx</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">xin</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">fmt</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">machine_bits</span><span style="color: #0000FF;">()=</span><span style="color: #000000;">32</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"%32s : %.14f\n"</span>
<span style="color: #0000FF;">:</span><span style="color: #008000;">"%32s : %.17f\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"PI"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">PI</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"6*arcsin(0.5)"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arcsin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0.5</span><span style="color: #0000FF;">)})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"3*arccos(0.5)"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arccos</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0.5</span><span style="color: #0000FF;">)})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"4*arctan(1)"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"6*thiele(t_sin,xval,R_SIN,0.5,0)"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">*</span><span style="color: #000000;">thiele</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t_sin</span><span style="color: #0000FF;">,</span><span style="color: #000000;">xval</span><span style="color: #0000FF;">,</span><span style="color: #000000;">R_SIN</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0.5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"3*thiele(t_cos,xval,R_COS,0.5,0)"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">*</span><span style="color: #000000;">thiele</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t_cos</span><span style="color: #0000FF;">,</span><span style="color: #000000;">xval</span><span style="color: #0000FF;">,</span><span style="color: #000000;">R_COS</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0.5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"4*thiele(t_tan,xval,R_TAN,1,0)"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">*</span><span style="color: #000000;">thiele</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t_tan</span><span style="color: #0000FF;">,</span><span style="color: #000000;">xval</span><span style="color: #0000FF;">,</span><span style="color: #000000;">R_TAN</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)})</span>
<!--</syntaxhighlight>-->
{{out}}
(64 bit, obviously 3 fewer digits on 32 bit)
<pre>
PI : 3.14159265358979324
6*arcsin(0.5) : 3.14159265358979324
3*arccos(0.5) : 3.14159265358979324
4*arctan(1) : 3.14159265358979324
6*thiele(t_sin,xval,R_SIN,0.5,0) : 3.14159265358979324
3*thiele(t_cos,xval,R_COS,0.5,0) : 3.14159265358979324
4*thiele(t_tan,xval,R_TAN,1,0) : 3.14159265358979324
</pre>
 
=={{header|PicoLisp}}==
{{trans|C}}
<langsyntaxhighlight PicoLisplang="picolisp">(scl 17)
(load "@lib/math.l")
 
Line 922 ⟶ 1,286:
 
(de iTan (X)
(thiele *TanTable *InvTanTable 1.0 1) )</langsyntaxhighlight>
Test:
<langsyntaxhighlight PicoLisplang="picolisp">(prinl (round (* 6 (iSin 0.5)) 15))
(prinl (round (* 3 (iCos 0.5)) 15))
(prinl (round (* 4 (iTan 1.0)) 15))</langsyntaxhighlight>
Output:
<pre>3.141592653589793
Line 933 ⟶ 1,297:
 
=={{header|PowerShell}}==
<langsyntaxhighlight PowerShelllang="powershell">Function Reciprocal-Difference( [Double[][]] $function )
{
$rho=@()
Line 1,001 ⟶ 1,365:
#uncomment to see the function
#"{$atan}"
4*$atan.InvokeReturnAsIs(1)</langsyntaxhighlight>
 
=={{header|Python}}==
{{trans|Go}}
<langsyntaxhighlight lang="python">#!/usr/bin/env python3
 
import math
Line 1,036 ⟶ 1,400:
print('{:16.14f}'.format(6*iSin(.5)))
print('{:16.14f}'.format(3*iCos(.5)))
print('{:16.14f}'.format(4*iTan(1)))</langsyntaxhighlight>
{{out}}
<pre>
Line 1,045 ⟶ 1,409:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
(define xs (for/vector ([x (in-range 0.0 1.6 0.05)]) x))
Line 1,085 ⟶ 1,449:
(* 3 (icos 0.5))
(* 4 (itan 1.))
</syntaxhighlight>
</lang>
Output:
<langsyntaxhighlight lang="racket">
3.141592653589793
3.1415926535897936
3.1415926535897953
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{Works with|rakudo|2018.09}}<br>
Implemented to parallel the generalized formula, making for clearer, but slower, code. Offsetting that, the use of <code>Promise</code> allows concurrent calculations, so running all three types of interpolation should not take any longer than running just one (presuming available cores).
 
<syntaxhighlight lang="raku" line># reciprocal difference:
multi sub ρ(&f, @x where * < 1) { 0 } # Identity
multi sub ρ(&f, @x where * == 1) { &f(@x[0]) }
multi sub ρ(&f, @x where * > 1) {
( @x[0] - @x[* - 1] ) # ( x - x[n] )
/ (ρ(&f, @x[^(@x - 1)]) # / ( ρ[n-1](x[0], ..., x[n-1])
- ρ(&f, @x[1..^@x]) ) # - ρ[n-1](x[1], ..., x[n]) )
+ ρ(&f, @x[1..^(@x - 1)]); # + ρ[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 &f = {%f{$^a}}; # f(x) as a table lookup
# must sort hash keys to maintain order between invocations
my $a = ρ(&f, %f.keys.sort[^($ord +1)]);
my $b = ρ(&f, %f.keys.sort[^($ord -1)]);
my $num = $x - %f.keys.sort[$ord];
my $cont = thiele($x, %f, $ord +1);
# Thiele always takes this form:
return $a - $b + ( $num / $cont );
}
## Demo
sub mk-inv(&fn, $d, $lim) {
my %h;
for 0..$lim { %h{ &fn($_ * $d) } = $_ * $d }
return %h;
}
sub MAIN($tblsz = 12) {
 
my ($sin_pi, $cos_pi, $tan_pi);
my $p1 = Promise.start( { my %invsin = mk-inv(&sin, 0.05, $tblsz); $sin_pi = 6 * thiele(0.5, %invsin, 0) } );
my $p2 = Promise.start( { my %invcos = mk-inv(&cos, 0.05, $tblsz); $cos_pi = 3 * thiele(0.5, %invcos, 0) } );
my $p3 = Promise.start( { my %invtan = mk-inv(&tan, 0.05, $tblsz); $tan_pi = 4 * thiele(1.0, %invtan, 0) } );
await $p1, $p2, $p3;
say "pi = {pi}";
say "estimations using a table of $tblsz elements:";
say "sin interpolation: $sin_pi";
say "cos interpolation: $cos_pi";
say "tan interpolation: $tan_pi";
}</syntaxhighlight>
 
Output:
 
<pre>pi = 3.14159265358979
estimations using a table of 12 elements:
sin interpolation: 3.14159265358961
cos interpolation: 3.1387286696692
tan interpolation: 3.14159090545243</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">
const N: usize = 32;
const STEP: f64 = 0.05;
 
fn main() {
let x: Vec<f64> = (0..N).map(|i| i as f64 * STEP).collect();
let sin = x.iter().map(|x| x.sin()).collect::<Vec<_>>();
let cos = x.iter().map(|x| x.cos()).collect::<Vec<_>>();
let tan = x.iter().map(|x| x.tan()).collect::<Vec<_>>();
 
println!(
"{}\n{}\n{}",
6. * thiele(&sin, &x, 0.5),
3. * thiele(&cos, &x, 0.5),
4. * thiele(&tan, &x, 1.)
);
}
 
fn thiele(x: &[f64], y: &[f64], xin: f64) -> f64 {
let mut p: Vec<Vec<f64>> = (0..N).map(|i| (i..N).map(|_| 0.0).collect()).collect();
 
(0..N).for_each(|i| p[i][0] = y[i]);
 
(0..N - 1).for_each(|i| p[i][1] = (x[i] - x[i + 1]) / (p[i][0] - p[i + 1][0]));
 
(2..N).for_each(|i| {
(0..N - i).for_each(|j| {
p[j][i] = (x[j] - x[j + i]) / (p[j][i - 1] - p[j + 1][i - 1]) + p[j + 1][i - 2];
})
});
 
let mut a = 0.;
(2..N).rev().for_each(|i| {
a = (xin - x[i - 1]) / (p[0][i] - p[0][i - 2] + a);
});
y[0] + (xin - x[0]) / (p[0][1] + a)
}
 
</syntaxhighlight>
{{out}}
<pre>
3.141592653589793
3.1415926535897936
3.1415926535897953
</pre>
 
=={{header|Sidef}}==
{{trans|Python}}
<langsyntaxhighlight lang="ruby">func thiele(x, y) {
var ρ = {|i| [y[i]]*(y.len-i) }.map(^y)
 
Line 1,133 ⟶ 1,605:
say 6*iSin(0.5)
say 3*iCos(0.5)
say 4*iTan(1)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,140 ⟶ 1,612:
3.14159265358979323846264318595256260456200366896
</pre>
 
=={{header|Swift}}==
 
{{trans|Kotlin}}
 
<syntaxhighlight lang="swift">let N = 32
let N2 = N * (N - 1) / 2
let step = 0.05
 
var xval = [Double](repeating: 0, count: N)
var tsin = [Double](repeating: 0, count: N)
var tcos = [Double](repeating: 0, count: N)
var ttan = [Double](repeating: 0, count: N)
var rsin = [Double](repeating: .nan, count: N2)
var rcos = [Double](repeating: .nan, count: N2)
var rtan = [Double](repeating: .nan, count: N2)
 
func rho(_ x: [Double], _ y: [Double], _ r: inout [Double], _ i: Int, _ n: Int) -> Double {
guard n >= 0 else {
return 0
}
 
guard n != 0 else {
return y[i]
}
 
let idx = (N - 1 - n) * (N - n) / 2 + i
 
if r[idx] != r[idx] {
r[idx] = (x[i] - x[i + n]) /
(rho(x, y, &r, i, n - 1) - rho(x, y, &r, i + 1, n - 1)) + rho(x, y, &r, i + 1, n - 2)
}
 
return r[idx]
}
 
func thiele(_ x: [Double], _ y: [Double], _ r: inout [Double], _ xin: Double, _ n: Int) -> Double {
guard n <= N - 1 else {
return 1
}
 
return rho(x, y, &r, 0, n) - rho(x, y, &r, 0, n - 2) + (xin - x[n]) / thiele(x, y, &r, xin, n + 1)
}
 
for i in 0..<N {
xval[i] = Double(i) * step
tsin[i] = sin(xval[i])
tcos[i] = cos(xval[i])
ttan[i] = tsin[i] / tcos[i]
}
 
print(String(format: "%16.14f", 6 * thiele(tsin, xval, &rsin, 0.5, 0)))
print(String(format: "%16.14f", 3 * thiele(tcos, xval, &rcos, 0.5, 0)))
print(String(format: "%16.14f", 4 * thiele(ttan, xval, &rtan, 1.0, 0)))
</syntaxhighlight>
 
{{out}}
 
<pre>3.14159265358979
3.14159265358979
3.14159265358980</pre>
 
=={{header|Tcl}}==
{{works with|Tcl|8.5}}
{{trans|D}}
<langsyntaxhighlight lang="tcl">#
### Create a thiele-interpretation function with the given name that interpolates
### off the given table.
Line 1,187 ⟶ 1,720:
expr {$f1 + ($x - [lindex $X 1]) / ([lindex $rho 1] + $a)}
}} $X [lindex $p 1] [lindex $F 1]
}</langsyntaxhighlight>
Demonstration code:
<langsyntaxhighlight lang="tcl">proc initThieleTest {} {
for {set i 0} {$i < 32} {incr i} {
lappend trigTable(x) [set x [expr {0.05 * $i}]]
Line 1,204 ⟶ 1,737:
puts "pi estimate using sin interpolation: [expr {6 * [invSin 0.5]}]"
puts "pi estimate using cos interpolation: [expr {3 * [invCos 0.5]}]"
puts "pi estimate using tan interpolation: [expr {4 * [invTan 1.0]}]"</langsyntaxhighlight>
Output:
<pre>
Line 1,210 ⟶ 1,743:
pi estimate using cos interpolation: 3.141592653589793
pi estimate using tan interpolation: 3.141592653589794
</pre>
 
=={{header|Wren}}==
{{trans|C}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./fmt" for Fmt
 
var N = 32
var N2 = N * (N - 1) / 2
var STEP = 0.05
 
var xval = List.filled(N, 0.0)
var tsin = List.filled(N, 0.0)
var tcos = List.filled(N, 0.0)
var ttan = List.filled(N, 0.0)
var rsin = List.filled(N2, 0/0)
var rcos = List.filled(N2, 0/0)
var rtan = List.filled(N2, 0/0)
 
var rho
rho = Fn.new { |x, y, r, i, n|
if (n < 0) return 0
if (n == 0) return y[i]
var idx = (N - 1 - n) * (N - n) / 2 + i
if (r[idx].isNan) {
r[idx] = (x[i] - x[i + n]) /
(rho.call(x, y, r, i, n - 1) - rho.call(x, y, r, i + 1, n - 1)) +
rho.call(x, y, r, i + 1, n - 2)
}
return r[idx]
}
 
var thiele
thiele = Fn.new { |x, y, r, xin, n|
if (n > N - 1) return 1
return rho.call(x, y, r, 0, n) - rho.call(x, y, r, 0, n -2) +
(xin - x[n]) / thiele.call(x, y, r, xin, n + 1)
}
 
for (i in 0...N) {
xval[i] = i * STEP
tsin[i] = xval[i].sin
tcos[i] = xval[i].cos
ttan[i] = tsin[i] / tcos[i]
}
Fmt.print("$16.14f", 6 * thiele.call(tsin, xval, rsin, 0.5, 0))
Fmt.print("$16.14f", 3 * thiele.call(tcos, xval, rcos, 0.5, 0))
Fmt.print("$16.14f", 4 * thiele.call(ttan, xval, rtan, 1.0, 0))</syntaxhighlight>
 
{{out}}
<pre>
3.14159265358979
3.14159265358979
3.14159265358979
</pre>
 
Line 1,215 ⟶ 1,802:
{{trans|C}}
Please see the C example for the comments I've removed (this is an as pure-as-I-make-it translation).
<langsyntaxhighlight lang="zkl">const N=32, N2=(N * (N - 1) / 2), STEP=0.05;
 
fcn rho(xs,ys,rs, i,n){
Line 1,254 ⟶ 1,841:
print("%16.14f\n".fmt( 6.0 * i_sin(0.5)));
print("%16.14f\n".fmt( 3.0 * i_cos(0.5)));
print("%16.14f\n".fmt( 4.0 * i_tan(1.0)));</langsyntaxhighlight>
{{out}}
<pre>
2,122

edits