Jump to content

Thiele's interpolation formula: Difference between revisions

m
syntax highlighting fixup automation
No edit summary
m (syntax highlighting fixup automation)
Line 30:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F thieleInterpolator(x, y)
V ρ = enumerate(y).map((i, yi) -> [yi] * (@y.len - i))
L(i) 0 .< ρ.len - 1
Line 54:
print(‘#.14’.format(6 * iSin(0.5)))
print(‘#.14’.format(3 * iCos(0.5)))
print(‘#.14’.format(4 * iTan(1)))</langsyntaxhighlight>
 
{{out}}
Line 65:
=={{header|Ada}}==
thiele.ads:
<langsyntaxhighlight Adalang="ada">with Ada.Numerics.Generic_Real_Arrays;
 
generic
Line 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 139:
end Inverse;
 
end Thiele;</langsyntaxhighlight>
 
example:
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
with Ada.Numerics.Generic_Elementary_Functions;
with Thiele;
Line 184:
Long_Float'Image (4.0 * Inverse (Tan, 1.0)));
end;
end Main;</langsyntaxhighlight>
 
output:
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.}}
{{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 249:
"tan", 4*inv tan(1)
))
)</langsyntaxhighlight>
Output:
<pre>
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.
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
#include <math.h>
Line 340:
printf("%16.14f\n", 4 * i_tan(1.));
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>3.14159265358979
Line 349:
{{trans|C}}
{{works with|C++14}}
<langsyntaxhighlight lang="cpp">#include <cmath>
#include <iostream>
#include <iomanip>
Line 397:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>3.141592653589793115997963
Line 405:
=={{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 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 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 538:
=={{header|Go}}==
{{trans|ALGOL 68}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 592:
return y[0] + (xin-x[0])/(ρ0[1]+a)
}
}</langsyntaxhighlight>
Output:
<pre>
Line 602:
=={{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
Line 633:
[(sin, (2, 31)), (cos, (2, 100)), (tan, (4, 1000))]
-- N points taken uniformly from 0 to Pi/d
div_pi (d, n) = (* (pi / (d * n))) <$> [0 .. n]</langsyntaxhighlight>
{{out}}
<pre>3.141592653589795
Line 640:
 
=={{header|J}}==
<syntaxhighlight lang="j">
<lang j>
span =: {. - {: NB. head - tail
spans =: span\ NB. apply span to successive infixes
</syntaxhighlight>
</lang>
 
<pre>
Line 652:
</pre>
 
<syntaxhighlight lang="j">
<lang j>
NB. abscissae_of_knots coef ordinates_of_knots
NB. returns the interpolation coefficients for eval
Line 673:
(p{~>:i)+(x-i{xx)%(p{~i+2)+a
)
</syntaxhighlight>
</lang>
 
<pre>
Line 702:
</pre>
 
<syntaxhighlight lang="j">
<lang j>
thiele =: 2 : 0
p =. _2 _{.,:n
Line 716:
(p{~>:i)+(y-i{m)%a+p{~i+2
)
</syntaxhighlight>
</lang>
 
<pre>
Line 733:
=={{header|Java}}==
{{trans|C}}
<langsyntaxhighlight lang="java">import static java.lang.Math.*;
 
public class Test {
Line 787:
System.out.printf("%16.14f%n", 4 * thiele(t_tan, xval, r_tan, 1.0, 0));
}
}</langsyntaxhighlight>
<pre>3.14159265358979
3.14159265358979
Line 795:
Accuracy improves with a larger table and smaller step size.
{{trans|C}}
<langsyntaxhighlight lang="julia">const N = 256
const N2 = N * div(N - 1, 2)
const step = 0.01
Line 840:
 
thiele_tables()
</langsyntaxhighlight>{{output}}<pre>
3.1415926535898335
3.141592653589818
Line 848:
=={{header|Kotlin}}==
{{trans|C}}
<langsyntaxhighlight lang="scala">// version 1.1.2
 
const val N = 32
Line 890:
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 900:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">num = 32;
num2 = num (num - 1)/2;
step = 0.05;
Line 938:
funcvals = Tan[xval];
r = ConstantArray[Null, num2];
4 Thiele[funcvals, xval, 1.0, 0]</langsyntaxhighlight>
{{out}}
<pre>3.14159
Line 946:
=={{header|Nim}}==
{{trans|Java}}
<langsyntaxhighlight Nimlang="nim">import strformat
import math
 
Line 993:
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}"</langsyntaxhighlight>
{{out}}
<pre>
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.
 
<langsyntaxhighlight OCamllang="ocaml">let xv, fv = fst, snd
 
let rec rdiff a l r =
Line 1,039:
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 1,058:
=={{header|Perl}}==
{{trans|Sidef}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,102:
say 6 * &$sin_inverse(0.5);
say 3 * &$cos_inverse(0.5);
say 4 * &$tan_inverse(1.0);</langsyntaxhighlight>
{{out}}
<pre>3.14159265358979
Line 1,111:
{{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).
<!--<langsyntaxhighlight Phixlang="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>
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;">"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>
<!--</langsyntaxhighlight>-->
{{out}}
(64 bit, obviously 3 fewer digits on 32 bit)
Line 1,176:
=={{header|PicoLisp}}==
{{trans|C}}
<langsyntaxhighlight PicoLisplang="picolisp">(scl 17)
(load "@lib/math.l")
 
Line 1,229:
 
(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 1,240:
 
=={{header|PowerShell}}==
<langsyntaxhighlight PowerShelllang="powershell">Function Reciprocal-Difference( [Double[][]] $function )
{
$rho=@()
Line 1,308:
#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,343:
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,352:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
(define xs (for/vector ([x (in-range 0.0 1.6 0.05)]) x))
Line 1,392:
(* 3 (icos 0.5))
(* 4 (itan 1.))
</syntaxhighlight>
</lang>
Output:
<langsyntaxhighlight lang="racket">
3.141592653589793
3.1415926535897936
3.1415926535897953
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
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).
 
<syntaxhighlight lang="raku" perl6line># reciprocal difference:
multi sub ρ(&f, @x where * < 1) { 0 } # Identity
multi sub ρ(&f, @x where * == 1) { &f(@x[0]) }
Line 1,451:
say "cos interpolation: $cos_pi";
say "tan interpolation: $tan_pi";
}</langsyntaxhighlight>
 
Output:
Line 1,462:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
const N: usize = 32;
const STEP: f64 = 0.05;
Line 1,500:
}
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,510:
=={{header|Sidef}}==
{{trans|Python}}
<langsyntaxhighlight lang="ruby">func thiele(x, y) {
var ρ = {|i| [y[i]]*(y.len-i) }.map(^y)
 
Line 1,548:
say 6*iSin(0.5)
say 3*iCos(0.5)
say 4*iTan(1)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,560:
{{trans|Kotlin}}
 
<langsyntaxhighlight lang="swift">let N = 32
let N2 = N * (N - 1) / 2
let step = 0.05
Line 1,609:
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>
</lang>
 
{{out}}
Line 1,620:
{{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,663:
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,680:
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,691:
{{trans|C}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
 
var N = 32
Line 1,733:
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))</langsyntaxhighlight>
 
{{out}}
Line 1,745:
{{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,784:
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>
10,327

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.