Thiele's interpolation formula: Difference between revisions

Content added Content deleted
No edit summary
m (syntax highlighting fixup automation)
Line 30: Line 30:
{{trans|Python}}
{{trans|Python}}


<lang 11l>F thieleInterpolator(x, y)
<syntaxhighlight lang="11l">F thieleInterpolator(x, y)
V ρ = enumerate(y).map((i, yi) -> [yi] * (@y.len - i))
V ρ = enumerate(y).map((i, yi) -> [yi] * (@y.len - i))
L(i) 0 .< ρ.len - 1
L(i) 0 .< ρ.len - 1
Line 54: Line 54:
print(‘#.14’.format(6 * iSin(0.5)))
print(‘#.14’.format(6 * iSin(0.5)))
print(‘#.14’.format(3 * iCos(0.5)))
print(‘#.14’.format(3 * iCos(0.5)))
print(‘#.14’.format(4 * iTan(1)))</lang>
print(‘#.14’.format(4 * iTan(1)))</syntaxhighlight>


{{out}}
{{out}}
Line 65: Line 65:
=={{header|Ada}}==
=={{header|Ada}}==
thiele.ads:
thiele.ads:
<lang Ada>with Ada.Numerics.Generic_Real_Arrays;
<syntaxhighlight lang="ada">with Ada.Numerics.Generic_Real_Arrays;


generic
generic
Line 81: Line 81:
X, Y, RhoX : Real_Array (1 .. Length);
X, Y, RhoX : Real_Array (1 .. Length);
end record;
end record;
end Thiele;</lang>
end Thiele;</syntaxhighlight>


thiele.adb:
thiele.adb:
<lang Ada>package body Thiele is
<syntaxhighlight lang="ada">package body Thiele is
use type Real_Array;
use type Real_Array;


Line 139: Line 139:
end Inverse;
end Inverse;


end Thiele;</lang>
end Thiele;</syntaxhighlight>


example:
example:
<lang Ada>with Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO;
with Ada.Numerics.Generic_Elementary_Functions;
with Ada.Numerics.Generic_Elementary_Functions;
with Thiele;
with Thiele;
Line 184: Line 184:
Long_Float'Image (4.0 * Inverse (Tan, 1.0)));
Long_Float'Image (4.0 * Inverse (Tan, 1.0)));
end;
end;
end Main;</lang>
end Main;</syntaxhighlight>


output:
output:
Line 197: Line 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.}}
{{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.}}
{{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.}}
<lang algol68>PROC raise exception = ([]STRING msg)VOID: ( putf(stand error,("Exception:", $" "g$, msg, $l$)); stop );
<syntaxhighlight 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",
# The MODE of lx and ly here should really be a UNION of "something REAL",
Line 249: Line 249:
"tan", 4*inv tan(1)
"tan", 4*inv tan(1)
))
))
)</lang>
)</syntaxhighlight>
Output:
Output:
<pre>
<pre>
Line 277: Line 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.
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.
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
#include <string.h>
#include <math.h>
#include <math.h>
Line 340: Line 340:
printf("%16.14f\n", 4 * i_tan(1.));
printf("%16.14f\n", 4 * i_tan(1.));
return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>3.14159265358979
<pre>3.14159265358979
Line 349: Line 349:
{{trans|C}}
{{trans|C}}
{{works with|C++14}}
{{works with|C++14}}
<lang cpp>#include <cmath>
<syntaxhighlight lang="cpp">#include <cmath>
#include <iostream>
#include <iostream>
#include <iomanip>
#include <iomanip>
Line 397: Line 397:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>3.141592653589793115997963
<pre>3.141592653589793115997963
Line 405: Line 405:
=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
Using the notations from above the C code instead of task desc.
Using the notations from above the C code instead of task desc.
<lang lisp>;; 256 is heavy overkill, but hey, we memoized
<syntaxhighlight lang="lisp">;; 256 is heavy overkill, but hey, we memoized
(defparameter *thiele-length* 256)
(defparameter *thiele-length* 256)
(defparameter *rho-cache* (make-hash-table :test #'equal))
(defparameter *rho-cache* (make-hash-table :test #'equal))
Line 448: Line 448:
(format t "~f~%" (* 6 (inv-sin .5)))
(format t "~f~%" (* 6 (inv-sin .5)))
(format t "~f~%" (* 3 (inv-cos .5)))
(format t "~f~%" (* 3 (inv-cos .5)))
(format t "~f~%" (* 4 (inv-tan 1)))</lang>output (SBCL):<lang>3.141592653589793
(format t "~f~%" (* 4 (inv-tan 1)))</syntaxhighlight>output (SBCL):<syntaxhighlight lang="text">3.141592653589793
3.1415926535885172
3.1415926535885172
3.141592653589819</lang>
3.141592653589819</syntaxhighlight>


=={{header|D}}==
=={{header|D}}==
<lang d>import std.stdio, std.range, std.array, std.algorithm, std.math;
<syntaxhighlight lang="d">import std.stdio, std.range, std.array, std.algorithm, std.math;


struct Domain {
struct Domain {
Line 524: Line 524:
writefln(" %20.19f 3 * inv_cos(0.5)", tcos.inverse(0.5L) * 3.0L);
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);
writefln(" %20.19f 4 * inv_tan(1.0)", ttan.inverse(1.0L) * 4.0L);
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre> 32 interpolating points
<pre> 32 interpolating points
Line 538: Line 538:
=={{header|Go}}==
=={{header|Go}}==
{{trans|ALGOL 68}}
{{trans|ALGOL 68}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 592: Line 592:
return y[0] + (xin-x[0])/(ρ0[1]+a)
return y[0] + (xin-x[0])/(ρ0[1]+a)
}
}
}</lang>
}</syntaxhighlight>
Output:
Output:
<pre>
<pre>
Line 602: Line 602:
=={{header|Haskell}}==
=={{header|Haskell}}==
Caching of rho is automatic due to lazy lists.
Caching of rho is automatic due to lazy lists.
<lang haskell>thiele :: [Double] -> [Double] -> Double -> Double
<syntaxhighlight lang="haskell">thiele :: [Double] -> [Double] -> Double -> Double
thiele xs ys = f rho1 (tail xs)
thiele xs ys = f rho1 (tail xs)
where
where
Line 633: Line 633:
[(sin, (2, 31)), (cos, (2, 100)), (tan, (4, 1000))]
[(sin, (2, 31)), (cos, (2, 100)), (tan, (4, 1000))]
-- N points taken uniformly from 0 to Pi/d
-- N points taken uniformly from 0 to Pi/d
div_pi (d, n) = (* (pi / (d * n))) <$> [0 .. n]</lang>
div_pi (d, n) = (* (pi / (d * n))) <$> [0 .. n]</syntaxhighlight>
{{out}}
{{out}}
<pre>3.141592653589795
<pre>3.141592653589795
Line 640: Line 640:


=={{header|J}}==
=={{header|J}}==
<syntaxhighlight lang="j">
<lang j>
span =: {. - {: NB. head - tail
span =: {. - {: NB. head - tail
spans =: span\ NB. apply span to successive infixes
spans =: span\ NB. apply span to successive infixes
</syntaxhighlight>
</lang>


<pre>
<pre>
Line 652: Line 652:
</pre>
</pre>


<syntaxhighlight lang="j">
<lang j>
NB. abscissae_of_knots coef ordinates_of_knots
NB. abscissae_of_knots coef ordinates_of_knots
NB. returns the interpolation coefficients for eval
NB. returns the interpolation coefficients for eval
Line 673: Line 673:
(p{~>:i)+(x-i{xx)%(p{~i+2)+a
(p{~>:i)+(x-i{xx)%(p{~i+2)+a
)
)
</syntaxhighlight>
</lang>


<pre>
<pre>
Line 702: Line 702:
</pre>
</pre>


<syntaxhighlight lang="j">
<lang j>
thiele =: 2 : 0
thiele =: 2 : 0
p =. _2 _{.,:n
p =. _2 _{.,:n
Line 716: Line 716:
(p{~>:i)+(y-i{m)%a+p{~i+2
(p{~>:i)+(y-i{m)%a+p{~i+2
)
)
</syntaxhighlight>
</lang>


<pre>
<pre>
Line 733: Line 733:
=={{header|Java}}==
=={{header|Java}}==
{{trans|C}}
{{trans|C}}
<lang java>import static java.lang.Math.*;
<syntaxhighlight lang="java">import static java.lang.Math.*;


public class Test {
public class Test {
Line 787: Line 787:
System.out.printf("%16.14f%n", 4 * thiele(t_tan, xval, r_tan, 1.0, 0));
System.out.printf("%16.14f%n", 4 * thiele(t_tan, xval, r_tan, 1.0, 0));
}
}
}</lang>
}</syntaxhighlight>
<pre>3.14159265358979
<pre>3.14159265358979
3.14159265358979
3.14159265358979
Line 795: Line 795:
Accuracy improves with a larger table and smaller step size.
Accuracy improves with a larger table and smaller step size.
{{trans|C}}
{{trans|C}}
<lang julia>const N = 256
<syntaxhighlight lang="julia">const N = 256
const N2 = N * div(N - 1, 2)
const N2 = N * div(N - 1, 2)
const step = 0.01
const step = 0.01
Line 840: Line 840:


thiele_tables()
thiele_tables()
</lang>{{output}}<pre>
</syntaxhighlight>{{output}}<pre>
3.1415926535898335
3.1415926535898335
3.141592653589818
3.141592653589818
Line 848: Line 848:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|C}}
{{trans|C}}
<lang scala>// version 1.1.2
<syntaxhighlight lang="scala">// version 1.1.2


const val N = 32
const val N = 32
Line 890: Line 890:
println("%16.14f".format(3 * thiele(tcos, xval, rcos, 0.5, 0)))
println("%16.14f".format(3 * thiele(tcos, xval, rcos, 0.5, 0)))
println("%16.14f".format(4 * thiele(ttan, xval, rtan, 1.0, 0)))
println("%16.14f".format(4 * thiele(ttan, xval, rtan, 1.0, 0)))
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 900: Line 900:


=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>num = 32;
<syntaxhighlight lang="mathematica">num = 32;
num2 = num (num - 1)/2;
num2 = num (num - 1)/2;
step = 0.05;
step = 0.05;
Line 938: Line 938:
funcvals = Tan[xval];
funcvals = Tan[xval];
r = ConstantArray[Null, num2];
r = ConstantArray[Null, num2];
4 Thiele[funcvals, xval, 1.0, 0]</lang>
4 Thiele[funcvals, xval, 1.0, 0]</syntaxhighlight>
{{out}}
{{out}}
<pre>3.14159
<pre>3.14159
Line 946: Line 946:
=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Java}}
{{trans|Java}}
<lang Nim>import strformat
<syntaxhighlight lang="nim">import strformat
import math
import math


Line 993: Line 993:
echo fmt"{6 * thiele(tsin, xval, rsin, 0.5, 0):16.14f}"
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"{3 * thiele(tcos, xval, rcos, 0.5, 0):16.14f}"
echo fmt"{4 * thiele(ttan, xval, rtan, 1.0, 0):16.14f}"</lang>
echo fmt"{4 * thiele(ttan, xval, rtan, 1.0, 0):16.14f}"</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,004: Line 1,004:
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.
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.


<lang OCaml>let xv, fv = fst, snd
<syntaxhighlight lang="ocaml">let xv, fv = fst, snd


let rec rdiff a l r =
let rec rdiff a l r =
Line 1,039: Line 1,039:
Printf.printf "4*arctan(1.0) = %.15f\n" (4.0*.(interpolate 1.0 tan_tab n));;
Printf.printf "4*arctan(1.0) = %.15f\n" (4.0*.(interpolate 1.0 tan_tab n));;


List.iter test [8; 12; 16]</lang>
List.iter test [8; 12; 16]</syntaxhighlight>
Output:
Output:
<pre>Degree 8 interpolation:
<pre>Degree 8 interpolation:
Line 1,058: Line 1,058:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Sidef}}
{{trans|Sidef}}
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use feature 'say';
use feature 'say';
Line 1,102: Line 1,102:
say 6 * &$sin_inverse(0.5);
say 6 * &$sin_inverse(0.5);
say 3 * &$cos_inverse(0.5);
say 3 * &$cos_inverse(0.5);
say 4 * &$tan_inverse(1.0);</lang>
say 4 * &$tan_inverse(1.0);</syntaxhighlight>
{{out}}
{{out}}
<pre>3.14159265358979
<pre>3.14159265358979
Line 1,111: Line 1,111:
{{trans|C}}
{{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).
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).
<!--<lang Phix>-->
<!--<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: #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;">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>
Line 1,161: Line 1,161:
<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;">"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>
<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>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
(64 bit, obviously 3 fewer digits on 32 bit)
(64 bit, obviously 3 fewer digits on 32 bit)
Line 1,176: Line 1,176:
=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
{{trans|C}}
{{trans|C}}
<lang PicoLisp>(scl 17)
<syntaxhighlight lang="picolisp">(scl 17)
(load "@lib/math.l")
(load "@lib/math.l")


Line 1,229: Line 1,229:


(de iTan (X)
(de iTan (X)
(thiele *TanTable *InvTanTable 1.0 1) )</lang>
(thiele *TanTable *InvTanTable 1.0 1) )</syntaxhighlight>
Test:
Test:
<lang PicoLisp>(prinl (round (* 6 (iSin 0.5)) 15))
<syntaxhighlight lang="picolisp">(prinl (round (* 6 (iSin 0.5)) 15))
(prinl (round (* 3 (iCos 0.5)) 15))
(prinl (round (* 3 (iCos 0.5)) 15))
(prinl (round (* 4 (iTan 1.0)) 15))</lang>
(prinl (round (* 4 (iTan 1.0)) 15))</syntaxhighlight>
Output:
Output:
<pre>3.141592653589793
<pre>3.141592653589793
Line 1,240: Line 1,240:


=={{header|PowerShell}}==
=={{header|PowerShell}}==
<lang PowerShell>Function Reciprocal-Difference( [Double[][]] $function )
<syntaxhighlight lang="powershell">Function Reciprocal-Difference( [Double[][]] $function )
{
{
$rho=@()
$rho=@()
Line 1,308: Line 1,308:
#uncomment to see the function
#uncomment to see the function
#"{$atan}"
#"{$atan}"
4*$atan.InvokeReturnAsIs(1)</lang>
4*$atan.InvokeReturnAsIs(1)</syntaxhighlight>


=={{header|Python}}==
=={{header|Python}}==
{{trans|Go}}
{{trans|Go}}
<lang python>#!/usr/bin/env python3
<syntaxhighlight lang="python">#!/usr/bin/env python3


import math
import math
Line 1,343: Line 1,343:
print('{:16.14f}'.format(6*iSin(.5)))
print('{:16.14f}'.format(6*iSin(.5)))
print('{:16.14f}'.format(3*iCos(.5)))
print('{:16.14f}'.format(3*iCos(.5)))
print('{:16.14f}'.format(4*iTan(1)))</lang>
print('{:16.14f}'.format(4*iTan(1)))</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,352: Line 1,352:


=={{header|Racket}}==
=={{header|Racket}}==
<lang racket>
<syntaxhighlight lang="racket">
#lang racket
#lang racket
(define xs (for/vector ([x (in-range 0.0 1.6 0.05)]) x))
(define xs (for/vector ([x (in-range 0.0 1.6 0.05)]) x))
Line 1,392: Line 1,392:
(* 3 (icos 0.5))
(* 3 (icos 0.5))
(* 4 (itan 1.))
(* 4 (itan 1.))
</syntaxhighlight>
</lang>
Output:
Output:
<lang racket>
<syntaxhighlight lang="racket">
3.141592653589793
3.141592653589793
3.1415926535897936
3.1415926535897936
3.1415926535897953
3.1415926535897953
</syntaxhighlight>
</lang>


=={{header|Raku}}==
=={{header|Raku}}==
Line 1,405: Line 1,405:
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).
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).


<lang perl6># reciprocal difference:
<syntaxhighlight lang="raku" line># reciprocal difference:
multi sub ρ(&f, @x where * < 1) { 0 } # Identity
multi sub ρ(&f, @x where * < 1) { 0 } # Identity
multi sub ρ(&f, @x where * == 1) { &f(@x[0]) }
multi sub ρ(&f, @x where * == 1) { &f(@x[0]) }
Line 1,451: Line 1,451:
say "cos interpolation: $cos_pi";
say "cos interpolation: $cos_pi";
say "tan interpolation: $tan_pi";
say "tan interpolation: $tan_pi";
}</lang>
}</syntaxhighlight>


Output:
Output:
Line 1,462: Line 1,462:


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>
<syntaxhighlight lang="rust">
const N: usize = 32;
const N: usize = 32;
const STEP: f64 = 0.05;
const STEP: f64 = 0.05;
Line 1,500: Line 1,500:
}
}


</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,510: Line 1,510:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Python}}
{{trans|Python}}
<lang ruby>func thiele(x, y) {
<syntaxhighlight lang="ruby">func thiele(x, y) {
var ρ = {|i| [y[i]]*(y.len-i) }.map(^y)
var ρ = {|i| [y[i]]*(y.len-i) }.map(^y)
 
 
Line 1,548: Line 1,548:
say 6*iSin(0.5)
say 6*iSin(0.5)
say 3*iCos(0.5)
say 3*iCos(0.5)
say 4*iTan(1)</lang>
say 4*iTan(1)</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,560: Line 1,560:
{{trans|Kotlin}}
{{trans|Kotlin}}


<lang swift>let N = 32
<syntaxhighlight lang="swift">let N = 32
let N2 = N * (N - 1) / 2
let N2 = N * (N - 1) / 2
let step = 0.05
let step = 0.05
Line 1,609: Line 1,609:
print(String(format: "%16.14f", 3 * thiele(tcos, xval, &rcos, 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)))
print(String(format: "%16.14f", 4 * thiele(ttan, xval, &rtan, 1.0, 0)))
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,620: Line 1,620:
{{works with|Tcl|8.5}}
{{works with|Tcl|8.5}}
{{trans|D}}
{{trans|D}}
<lang tcl>#
<syntaxhighlight lang="tcl">#
### Create a thiele-interpretation function with the given name that interpolates
### Create a thiele-interpretation function with the given name that interpolates
### off the given table.
### off the given table.
Line 1,663: Line 1,663:
expr {$f1 + ($x - [lindex $X 1]) / ([lindex $rho 1] + $a)}
expr {$f1 + ($x - [lindex $X 1]) / ([lindex $rho 1] + $a)}
}} $X [lindex $p 1] [lindex $F 1]
}} $X [lindex $p 1] [lindex $F 1]
}</lang>
}</syntaxhighlight>
Demonstration code:
Demonstration code:
<lang tcl>proc initThieleTest {} {
<syntaxhighlight lang="tcl">proc initThieleTest {} {
for {set i 0} {$i < 32} {incr i} {
for {set i 0} {$i < 32} {incr i} {
lappend trigTable(x) [set x [expr {0.05 * $i}]]
lappend trigTable(x) [set x [expr {0.05 * $i}]]
Line 1,680: Line 1,680:
puts "pi estimate using sin interpolation: [expr {6 * [invSin 0.5]}]"
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 cos interpolation: [expr {3 * [invCos 0.5]}]"
puts "pi estimate using tan interpolation: [expr {4 * [invTan 1.0]}]"</lang>
puts "pi estimate using tan interpolation: [expr {4 * [invTan 1.0]}]"</syntaxhighlight>
Output:
Output:
<pre>
<pre>
Line 1,691: Line 1,691:
{{trans|C}}
{{trans|C}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "/fmt" for Fmt
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt


var N = 32
var N = 32
Line 1,733: Line 1,733:
Fmt.print("$16.14f", 6 * thiele.call(tsin, xval, rsin, 0.5, 0))
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", 3 * thiele.call(tcos, xval, rcos, 0.5, 0))
Fmt.print("$16.14f", 4 * thiele.call(ttan, xval, rtan, 1.0, 0))</lang>
Fmt.print("$16.14f", 4 * thiele.call(ttan, xval, rtan, 1.0, 0))</syntaxhighlight>


{{out}}
{{out}}
Line 1,745: Line 1,745:
{{trans|C}}
{{trans|C}}
Please see the C example for the comments I've removed (this is an as pure-as-I-make-it translation).
Please see the C example for the comments I've removed (this is an as pure-as-I-make-it translation).
<lang zkl>const N=32, N2=(N * (N - 1) / 2), STEP=0.05;
<syntaxhighlight lang="zkl">const N=32, N2=(N * (N - 1) / 2), STEP=0.05;


fcn rho(xs,ys,rs, i,n){
fcn rho(xs,ys,rs, i,n){
Line 1,784: Line 1,784:
print("%16.14f\n".fmt( 6.0 * i_sin(0.5)));
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( 3.0 * i_cos(0.5)));
print("%16.14f\n".fmt( 4.0 * i_tan(1.0)));</lang>
print("%16.14f\n".fmt( 4.0 * i_tan(1.0)));</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>