Chebyshev coefficients: Difference between revisions

no edit summary
m (add a ;Task: section header, added whitespace.)
No edit summary
 
(27 intermediate revisions by 11 users not shown)
Line 9:
Calculate coefficients:   cosine function,   '''10'''   coefficients,   interval   '''0   1'''
<br><br>
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F test_func(Float x)
R cos(x)
 
F mapper(x, min_x, max_x, min_to, max_to)
R (x - min_x) / (max_x - min_x) * (max_to - min_to) + min_to
 
F cheb_coef(func, n, min, max)
V coef = [0.0] * n
L(i) 0 .< n
V f = func(mapper(cos(math:pi * (i + 0.5) / n), -1, 1, min, max)) * 2 / n
L(j) 0 .< n
coef[j] += f * cos(math:pi * j * (i + 0.5) / n)
R coef
 
F cheb_approx(=x, n, min, max, coef)
V a = 1.0
V b = mapper(x, min, max, -1, 1)
V res = coef[0] / 2 + coef[1] * b
 
x = 2 * b
V i = 2
L i < n
V c = x * b - a
res = res + coef[i] * c
(a, b) = (b, c)
i++
 
R res
 
V n = 10
V minv = 0
V maxv = 1
V c = cheb_coef(test_func, n, minv, maxv)
 
print(‘Coefficients:’)
L(i) 0 .< n
print(c[i])
 
print("\n\nApproximation:\n x func(x) approx diff")
L(i) 20
V x = mapper(i, 0.0, 20.0, minv, maxv)
V f = test_func(x)
V approx = cheb_approx(x, n, minv, maxv, c)
print(‘#.3 #.10 #.10 #.’.format(x, f, approx, format_float_exp(approx - f, 2, 9)))</syntaxhighlight>
 
{{out}}
<pre>
Coefficients:
1.64717
-0.232299
-0.0537151
0.00245824
0.000282119
-7.72223e-06
-5.89856e-07
1.15214e-08
6.5963e-10
-1.00219e-11
 
 
Approximation:
x func(x) approx diff
0.000 1.0000000000 1.0000000000 4.68e-13
0.050 0.9987502604 0.9987502604 -9.36e-14
0.100 0.9950041653 0.9950041653 4.62e-13
0.150 0.9887710779 0.9887710779 -4.73e-14
0.200 0.9800665778 0.9800665778 -4.60e-13
0.250 0.9689124217 0.9689124217 -2.32e-13
0.300 0.9553364891 0.9553364891 2.62e-13
0.350 0.9393727128 0.9393727128 4.61e-13
0.400 0.9210609940 0.9210609940 1.98e-13
0.450 0.9004471024 0.9004471024 -2.47e-13
0.500 0.8775825619 0.8775825619 -4.58e-13
0.550 0.8525245221 0.8525245221 -2.46e-13
0.600 0.8253356149 0.8253356149 1.96e-13
0.650 0.7960837985 0.7960837985 4.53e-13
0.700 0.7648421873 0.7648421873 2.54e-13
0.750 0.7316888689 0.7316888689 -2.28e-13
0.800 0.6967067093 0.6967067093 -4.47e-13
0.850 0.6599831459 0.6599831459 -4.37e-14
0.900 0.6216099683 0.6216099683 4.46e-13
0.950 0.5816830895 0.5816830895 -8.98e-14
</pre>
=={{header|ALGOL 60}}==
{{works with|GNU Marst|Any - tested with release 2.7}}
{{Trans|ALGOL W}}...which is{{Trans|Java}}
<syntaxhighlight lang="algol60">
begin comment Chebyshev coefficients ;
 
real PI;
 
procedure chebyshevCoef( func, min, max, coef, N )
; value min, max, N
; real procedure func
; real min, max
; real array coef
; integer N
;
begin
real procedure map( x, min x, max x, min to, max to )
; value x, min x, max x, min to, max to
; real x, min x, max x, min to, max to
;
begin
map := ( x - min x ) / ( max x - min x ) * ( max to - min to ) + min to
end map ;
 
integer i, j;
for i := 0 step 1 until N - 1 do begin
real m, f;
m := map( cos( PI * ( i + 0.5 ) / N ), -1, 1, min, max );
f := func( m ) * 2 / N;
for j := 0 step 1 until N - 1 do begin
coef[ j ] := coef[ j ] + f * cos( PI * j * ( i + 0.5 ) / N )
end j
end i
end chebyshevCoef ;
 
PI := arctan( 1 ) * 4;
begin
integer N;
N := 10;
begin
real array c [ 0 : N - 1 ];
integer i;
chebyshevCoef( cos, 0, 1, c, N );
outstring( 1, "Coefficients:\n" );
for i := 0 step 1 until N - 1 do begin
if c[ i ] >= 0 then outstring( 1, " " );
outstring( 1, " " );outreal( 1, c[ i ] );outstring( 1, "\n" )
end i
end
end
end
</syntaxhighlight>
{{out}}
<pre>
Coefficients:
1.64716947539
-0.232299371615
-0.053715114622
0.00245823526698
0.000282119057434
-7.72222915635e-006
-5.89855645675e-007
1.15214277563e-008
6.59630183808e-010
-1.00219138544e-011
</pre>
 
=={{header|ALGOL 68}}==
{{Trans|Java}}... using nested procedures and returning the coefficient array instead of using a reference parameter.
<syntaxhighlight lang="algol68">
BEGIN # Chebyshev Coefficients #
 
PROC chebyshev coef = ( PROC( REAL )REAL func, REAL min, max, INT n )[]REAL:
BEGIN
 
PROC map = ( REAL x, min x, max x, min to, max to )REAL:
( x - min x ) / ( max x - min x ) * ( max to - min to ) + min to;
 
[ 0 : n - 1 ]REAL coef; FOR i FROM LWB coef TO UPB coef DO coef[ i ] := 0 OD;
FOR i FROM 0 TO UPB coef DO
REAL m = map( cos( pi * ( i + 0.5 ) / n ), -1, 1, min, max );
REAL f = func( m ) * 2 / n;
FOR j FROM 0 TO UPB coef DO
coef[ j ] +:= f * cos( pi * j * ( i + 0.5 ) / n )
OD
OD;
coef
END # chebyshev coef # ;
 
BEGIN
INT n = 10;
REAL min := 0, max := 1;
[]REAL c = chebyshev coef( cos, min, max, n );
print( ( "Coefficients:", newline ) );
FOR i FROM LWB c TO UPB c DO
print( ( fixed( c[ i ], -18, 14 ), newline ) )
OD
END
 
END
</syntaxhighlight>
{{out}}
<pre>
Coefficients:
1.64716947539031
-0.23229937161517
-0.05371511462205
0.00245823526698
0.00028211905743
-0.00000772222916
-0.00000058985565
0.00000001152143
0.00000000065963
-0.00000000001002
</pre>
 
=={{header|ALGOL W}}==
{{Trans|Java}}... using nested procedures. In Algol W, procedures can't find the bounds of array parameters, so an extra parameter is reuired for the chebyshevCoef procedure.
<syntaxhighlight lang="algolw">
begin % Chebyshev coefficients %
 
procedure chebyshevCoef ( real procedure func
; real value min, max
; real array coef ( * )
; integer value N
) ;
begin
real procedure map ( real value x, min_x, max_x, min_to, max_to ) ;
( x - min_x ) / ( max_x - min_x ) * ( max_to - min_to ) + min_to;
 
for i := 0 until N - 1 do begin
real m, f;
m := map( cos( PI * ( i + 0.5 ) / N ), -1, 1, min, max );
f := func( m ) * 2 / N;
for j := 0 until N - 1 do begin
coef( j ) := coef( j ) + f * cos( PI * j * ( i + 0.5 ) / N )
end for_j
end for_i
end chebyshevCoef ;
 
begin
integer N;
N := 10;
begin
real array c ( 0 :: N - 1 );
chebyshevCoef( cos, 0, 1, c, N );
write( "Coefficients:" );
for i := 0 until N - 1 do write( r_format := "S", r_w := 14, c( i ) )
end
end
end.
</syntaxhighlight>
{{out}}
<pre>
Coefficients:
1.6471694'+00
-2.3229937'-01
-5.3715114'-02
2.4582352'-03
2.8211905'-04
-7.7222291'-06
-5.8985564'-07
1.1521427'-08
6.5963014'-10
-1.0021983'-11
</pre>
 
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
The [[#MSX-BASIC|MSX-BASIC]] solution works without any changes.
 
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
Given the limitations of the language, only 8 coefficients are calculated
<syntaxhighlight lang="basic256">a = 0: b = 1: n = 8
dim cheby(n)
dim coef(n)
 
for i = 0 to n-1
coef[i] = cos(cos(pi/n*(i+1/2))*(b-a)/2+(b+a)/2)
next i
 
for i = 0 to n-1
w = 0
for j = 0 to n-1
w += coef[j] * cos(pi/n*i*(j+1/2))
next j
cheby[i] = w*2/n
print i; " : "; cheby[i]
next i
end</syntaxhighlight>
{{out}}
<pre>0 : 1.64716947539
1 : -0.23229937162
2 : -0.05371511462
3 : 0.00245823527
4 : 0.00028211906
5 : -0.00000772223
6 : -5.89855645106e-07
7 : 1.15214275009e-08</pre>
 
==={{header|Chipmunk Basic}}===
{{trans|FreeBASIC}}
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="qbasic">100 cls
110 rem pi = 4 * atn(1)
120 a = 0
130 b = 1
140 n = 10
150 dim cheby(n)
160 dim coef(n)
170 for i = 0 to n-1
180 coef(i) = cos(cos(pi/n*(i+1/2))*(b-a)/2+(b+a)/2)
190 next i
200 for i = 0 to n-1
210 w = 0
220 for j = 0 to n-1
230 w = w+coef(j)*cos(pi/n*i*(j+1/2))
240 next j
250 cheby(i) = w*2/n
260 print i;" : ";cheby(i)
270 next i
280 end</syntaxhighlight>
{{out}}
<pre>0 : 1.647169
1 : -0.232299
2 : -0.053715
3 : 2.458235E-03
4 : 2.821191E-04
5 : -7.722229E-06
6 : -5.898556E-07
7 : 1.152143E-08
8 : 6.596304E-10
9 : -1.002234E-11</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">Const pi As Double = 4 * Atn(1)
Dim As Integer i, j
Dim As Double w, a = 0, b = 1, n = 10
Dim As Double cheby(n), coef(n)
 
For i = 0 To n-1
coef(i) = Cos(Cos(pi/n*(i+1/2))*(b-a)/2+(b+a)/2)
Next i
 
For i = 0 To n-1
w = 0
For j = 0 To n-1
w += coef(j) * Cos(pi/n*i*(j+1/2))
Next j
cheby(i) = w*2/n
Print i; " : "; cheby(i)
Next i
Sleep</syntaxhighlight>
{{out}}
<pre> 0 : 1.647169475390314
1 : -0.2322993716151719
2 : -0.05371511462204768
3 : 0.002458235266981634
4 : 0.0002821190574339161
5 : -7.7222291556156e-006
6 : -5.898556451056081e-007
7 : 1.152142750093788e-008
8 : 6.596299062522348e-010
9 : -1.002201654998203e-011</pre>
 
==={{header|Gambas}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">Public coef[10] As Float
 
Public Sub Main()
Dim i As Integer, j As Integer
Dim w As Float, a As Float = 0, b As Float = 1, n As Float = 10
For i = 0 To n - 1
coef[i] = Cos(Cos(Pi / n * (i + 1 / 2)) * (b - a) / 2 + (b + a) / 2)
Next
For i = 0 To n - 1
w = 0
For j = 0 To n - 1
w += coef[j] * Cos(Pi / n * i * (j + 1 / 2))
Next
cheby[i] = w * 2 / n
Print i; " : "; cheby[i]
Next
End</syntaxhighlight>
{{out}}
<pre>0 : 1,64716947539031
1 : -0,232299371615172
2 : -0,053715114622048
3 : 0,002458235266982
4 : 0,000282119057434
5 : -7,7222291556156E-6
6 : -5,89855645105608E-7
7 : 1,15214275009379E-8
8 : 6,59629906252235E-10
9 : -1,0022016549982E-11</pre>
 
==={{header|GW-BASIC}}===
{{trans|FreeBASIC}}
{{works with|PC-BASIC|any}}
<syntaxhighlight lang="qbasic">100 CLS
110 PI# = 4 * ATN(1)
120 A# = 0
130 B# = 1
140 N# = 10
150 DIM CHEBY(N#)
160 DIM COEF(N#)
170 FOR I = 0 TO N#-1
180 COEF(I) = COS(COS(PI#/N#*(I+1/2))*(B#-A#)/2+(B#+A#)/2)
190 NEXT I
200 FOR I = 0 TO N#-1
210 W# = 0
220 FOR J = 0 TO N#-1
230 W# = W# + COEF(J) * COS(PI#/N#*I*(J+1/2))
240 NEXT J
250 CHEBY(I) = W# * 2 / N#
260 PRINT I; " : "; CHEBY(I)
270 NEXT I
280 END</syntaxhighlight>
{{out}}
<pre>0 : 1.647169
1 : -.2322993
2 : -5.371515E-02
3 : 2.458321E-03
4 : 2.820671E-04
5 : -7.766486E-06
6 : -5.857175E-07
7 : 9.834766E-08
8 : -1.788139E-07
9 : -9.089708E-08</pre>
 
==={{header|Minimal BASIC}}===
{{trans|FreeBASIC}}
{{works with|BASICA}}
<syntaxhighlight lang="qbasic">110 LET P = 4 * ATN(1)
120 LET A = 0
130 LET B = 1
140 LET N = 10
170 FOR I = 0 TO N-1
180 LET K(I) = COS(COS(P/N*(I+1/2))*(B-A)/2+(B+A)/2)
190 NEXT I
200 FOR I = 0 TO N-1
210 LET W = 0
220 FOR J = 0 TO N-1
230 LET W = W + K(J) * COS(P/N*I*(J+1/2))
240 NEXT J
250 LET C(I) = W * 2 / N
260 PRINT I; " : "; C(I)
270 NEXT I
280 END</syntaxhighlight>
{{out}}
<pre> 0 : 1.6471695
1 : -.23229937
2 : -5.3715115E-2
3 : 2.4582353E-3
4 : 2.8211906E-4
5 : -7.7222291E-6
6 : -5.8985565E-7
7 : 1.1521437E-8
8 : 6.5962449E-10
9 : -1.0018986E-11</pre>
 
==={{header|MSX Basic}}===
{{trans|FreeBASIC}}
{{works with|MSX BASIC|any}}
<syntaxhighlight lang="qbasic">100 CLS : rem 10 HOME for Applesoft BASIC
110 PI = 4 * ATN(1)
120 A = 0
130 B = 1
140 N = 10
150 DIM CHEBY(N)
160 DIM COEF(N)
170 FOR I = 0 TO N-1
180 COEF(I) = COS(COS(PI/N*(I+1/2))*(B-A)/2+(B+A)/2)
190 NEXT I
200 FOR I = 0 TO N-1
210 W = 0
220 FOR J = 0 TO N-1
230 W = W + COEF(J) * COS(PI/N*I*(J+1/2))
240 NEXT J
250 CHEBY(I) = W * 2 / N
260 PRINT I; " : "; CHEBY(I)
270 NEXT I
280 END</syntaxhighlight>
 
==={{header|QBasic}}===
{{works with|QBasic}}
{{works with|QuickBasic|4.5}}
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">pi = 4 * ATN(1)
a = 0: b = 1: n = 10
DIM cheby!(n)
DIM coef!(n)
 
FOR i = 0 TO n - 1
coef(i) = COS(COS(pi / n * (i + 1 / 2)) * (b - a) / 2 + (b + a) / 2)
NEXT i
 
FOR i = 0 TO n - 1
w = 0
FOR j = 0 TO n - 1
w = w + coef(j) * COS(pi / n * i * (j + 1 / 2))
NEXT j
cheby(i) = w * 2 / n
PRINT USING " # : ##.#####################"; i; cheby(i)
NEXT i
END</syntaxhighlight>
{{out}}
<pre> 0 : 1.647169470787048000000
1 : -0.232299402356147800000
2 : -0.053715050220489500000
3 : 0.002458173315972090000
4 : 0.000282166845863685000
5 : -0.000007787576578266453
6 : -0.000000536595905487047
7 : 0.000000053614126471757
8 : 0.000000079823998078155
9 : -0.000000070922546058227</pre>
 
==={{header|Quite BASIC}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">100 cls
110 rem pi = 4 * atn(1)
120 let a = 0
130 let b = 1
140 let n = 10
150 array c
160 array k
170 for i = 0 to n-1
180 let k[i] = cos(cos(pi/n*(i+1/2))*(b-a)/2+(b+a)/2)
190 next i
200 for i = 0 to n-1
210 let w = 0
220 for j = 0 to n-1
230 let w = w + k[j] * cos(pi/n*i*(j+1/2))
240 next j
250 let c[i] = w * 2 / n
260 print i; " : "; c[i]
270 next i
280 end</syntaxhighlight>
{{out}}
<pre>0 : 1.6471694753903137
1 : -0.23229937161517186
2 : -0.05371511462204768
3 : 0.0024582352669816343
4 : 0.0002821190574339161
5 : -0.0000077222291556156
6 : -5.898556451056081e-7
7 : 1.1521427500937876e-8
8 : 6.59629917354465e-10
9 : -1.0022016549982027e-11</pre>
 
==={{header|Run BASIC}}===
{{trans|FreeBASIC}}
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
<syntaxhighlight lang="vb">pi = 4 * atn(1)
a = 0
b = 1
n = 10
dim cheby(n)
dim coef(n)
for i = 0 to n-1
coef(i) = cos(cos(pi/n*(i+1/2))*(b-a)/2+(b+a)/2)
next i
for i = 0 to n-1
w = 0
for j = 0 to n-1
w = w + coef(j)*cos(pi/n*i*(j+1/2))
next j
cheby(i) = w * 2 / n
print i; " : "; cheby(i)
next i
end</syntaxhighlight>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="yabasic">a = 0: b = 1: n = 10
dim cheby(n)
dim coef(n)
 
for i = 0 to n-1
coef(i) = cos(cos(pi/n*(i+1/2))*(b-a)/2+(b+a)/2)
next i
 
for i = 0 to n-1
w = 0
for j = 0 to n-1
w = w + coef(j) * cos(pi/n*i*(j+1/2))
next j
cheby(i) = w*2/n
print i, " : ", cheby(i)
next i
end</syntaxhighlight>
{{out}}
<pre>0 : 1.64717
1 : -0.232299
2 : -0.0537151
3 : 0.00245824
4 : 0.000282119
5 : -7.72223e-06
6 : -5.89856e-07
7 : 1.15214e-08
8 : 6.5963e-10
9 : -1.0022e-11</pre>
 
=={{header|C}}==
C99.
<langsyntaxhighlight Clang="c">#include <stdio.h>
#include <string.h>
#include <math.h>
Line 78 ⟶ 672:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
{{trans|C++}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 190 ⟶ 783:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Coefficients:
Line 226 ⟶ 819:
0.900 0.62160996827066 0.62160996827111 4.444223E-013
0.950 0.58168308946388 0.58168308946379 -8.992806E-014</pre>
 
=={{header|C++}}==
Based on the C99 implementation above. The main improvement is that, because C++ containers handle memory for us, we can use a more functional style.
Line 234 ⟶ 826:
The wrapper class ChebyshevApprox_ supports very terse user code.
 
<syntaxhighlight lang="cpp">
<lang CPP>
#include <iostream>
#include <iomanip>
Line 336 ⟶ 928:
}
}
</syntaxhighlight>
</lang>
 
=={{header|D}}==
This imperative code retains some of the style of the original C version.
<langsyntaxhighlight lang="d">import std.math: PI, cos;
 
/// Map x from range [min, max] to [min_to, max_to].
Line 402 ⟶ 993:
writefln("%1.3f % 10.10f % 10.10f % 4.2e", x, f, approx, approx - f);
}
}</langsyntaxhighlight>
{{out}}
<pre>Coefficients:
Line 480 ⟶ 1,071:
0.900 0.62160996827066445648 0.62160996827066445674 2.71e-19
0.950 0.58168308946388349416 0.58168308946388349403 -1.63e-19</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="text">
<lang>fscale 15
numfmt 12 0
a = 0
b = 1
Line 488 ⟶ 1,079:
len coef[] n
len cheby[] n
for i range= 0 to n - 1
coef[i + 1] = cos (180 / pi * (cos (180 / n * (i + 1 / 2)) * (b - a) / 2 + (b + a) / 2))
.
for i = 0 to n - 1
w = 0
for j = 0 to n - 1
w += coef[j + 1] * cos (180 / n * i * (j + 1 / 2))
.
cheby[i + 1] = w * 2 / n
print cheby[i + 1]
.
</syntaxhighlight>
for i range n
w = 0
for j range n
w += coef[j] * cos (180 / n * i * (j + 1 / 2))
.
cheby[i] = w * 2 / n
print cheby[i]
.</lang>
 
=={{header|Go}}==
Line 506 ⟶ 1,098:
 
Two variances here from the WP presentation and most mathematical presentations follow other examples on this page and so keep output directly comparable. One variance is that the Kronecker delta factor is dropped, which has the effect of doubling the first coefficient. This simplifies both coefficient generation and polynomial evaluation. A further variance is that there is no scaling for the range of function values. The result is that coefficients are not necessarily bounded by 1 (2 for the first coefficient) but by the maximum function value over the argument range from min to max (or twice that for the first coefficient.)
<langsyntaxhighlight lang="go">package main
 
import (
Line 569 ⟶ 1,161:
}
return x1*t - s + .5*c.c[0]
}</langsyntaxhighlight>
{{out}}
<pre>
Line 597 ⟶ 1,189:
1.0 0.54030231 0.54030231 -4.476e-13
</pre>
 
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang="groovy">class ChebyshevCoefficients {
static double map(double x, double min_x, double max_x, double min_to, double max_to) {
return (x - min_x) / (max_x - min_x) * (max_to - min_to) + min_to
Line 628 ⟶ 1,219:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Coefficients:
Line 641 ⟶ 1,232:
6.59630183807991E-10
-1.0021913854352249E-11</pre>
 
=={{header|J}}==
From 'J for C Programmers: Calculating Chebyshev Coefficients [[http://www.jsoftware.com/learning/a_first_look_at_j_programs.htm#_Toc191734318]]
<syntaxhighlight lang="j">
<lang J>
chebft =: adverb define
:
Line 650 ⟶ 1,240:
(2 % x) * +/ f * 2 o. o. (0.5 + i. x) *"0 1 (i. x) % x
)
</syntaxhighlight>
</lang>
Calculate coefficients:
<syntaxhighlight lang="j">
<lang J>
10 (2&o.) chebft 0 1
1.64717 _0.232299 _0.0537151 0.00245824 0.000282119 _7.72223e_6 _5.89856e_7 1.15214e_8 6.59629e_10 _1.00227e_11
</syntaxhighlight>
</lang>
 
=={{header|Java}}==
Partial translation of [[Chebyshev_coefficients#C|C]] via [[Chebyshev_coefficients#D|D]]
{{works with|Java|8}}
<langsyntaxhighlight lang="java">import static java.lang.Math.*;
import java.util.function.Function;
 
Line 696 ⟶ 1,285:
System.out.println(d);
}
}</langsyntaxhighlight>
 
<pre>Coefficients:
Line 709 ⟶ 1,298:
6.59630183807991E-10
-1.0021913854352249E-11</pre>
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
'''Preliminaries'''
<syntaxhighlight lang="jq">def lpad($len): tostring | ($len - length) as $l | (" " * $l) + .;
def rpad($len; $fill): tostring | ($len - length) as $l | . + ($fill * $l)[:$l];
 
# Format a decimal number so that there are at least `left` characters
# to the left of the decimal point, and at most `right` characters to its right.
# No left-truncation occurs, so `left` can be specified as 0 to prevent left-padding.
# If tostring has an "e" then eparse as defined below is used.
def pp(left; right):
def lpad: if (left > length) then ((left - length) * " ") + . else . end;
def eparse: index("e") as $ix | (.[:$ix]|pp(left;right)) + .[$ix:];
tostring as $s
| $s
| if test("e") then eparse
else index(".") as $ix
| ((if $ix then $s[0:$ix] else $s end) | lpad) + "." +
(if $ix then $s[$ix+1:] | .[0:right] else "" end)
end;</syntaxhighlight>
'''Chebyshev Coefficients'''
<syntaxhighlight lang="jq">def mapRange($x; $min; $max; $minTo; $maxTo):
(($x - $min)/($max - $min))*($maxTo - $minTo) + $minTo;
def chebCoeffs(func; n; min; max):
(1 | atan * 4) as $pi
| reduce range(0;n) as $i ([]; # coeffs
((mapRange( ($pi * ($i + 0.5) / n)|cos; -1; 1; min; max) | func) * 2 / n) as $f
| reduce range(0;n) as $j (.;
.[$j] += $f * ($pi * $j * (($i + 0.5) / n)|cos)) );
def chebApprox(x; n; min; max; coeffs):
if n < 2 or (coeffs|length) < 2 then "'n' can't be less than 2." | error
else { a: 1,
b: mapRange(x; min; max; -1; 1) }
| .res = coeffs[0]/2 + coeffs[1]*.b
| .xx = 2 * .b
| reduce range(2;n) as $i (.;
(.xx * .b - .a) as $c
| .res += coeffs[$i]*$c)
| .a = .b
| .b = $c)
| .res
end ;
 
def task:
[10, 0, 1] as [$n, $min, $max]
| chebCoeffs(cos; $n; $min; $max) as $coeffs
| "Coefficients:",
($coeffs[]|pp(2;14)),
"\nApproximations:\n x func(x) approx diff",
(range(0;21) as $i
| mapRange($i; 0; 20; $min; $max) as $x
| ($x|cos) as $f
| chebApprox($x; $n; $min; $max; $coeffs) as $approx
| ($approx - $f) as $diff
| [ ($x|pp(0;3)|rpad( 4;"0")),
($f|pp(0;8)|rpad(10;"0")),
($approx|pp(0;8)),
($diff |pp(2;2)) ]
| join(" ") );
 
task</syntaxhighlight>
{{out}}
<pre>
Coefficients:
1.64716947539031
-0.23229937161517
-0.05371511462204
0.00245823526698
0.00028211905743
-7.72222915562670e-06
-5.89855645688475e-07
1.15214280338449e-08
6.59629580124221e-10
-1.00220526322303e-11
 
Approximations:
x func(x) approx diff
0.00 1.00000000 1.00000000 4.66e-13
0.05 0.99875026 0.99875026 -9.21e-14
0.10 0.99500416 0.99500416 4.62e-13
0.15 0.98877107 0.98877107 -4.74e-14
0.20 0.98006657 0.98006657 -4.60e-13
0.25 0.96891242 0.96891242 -2.32e-13
0.30 0.95533648 0.95533648 2.61e-13
0.35 0.93937271 0.93937271 4.60e-13
0.40 0.92106099 0.92106099 1.98e-13
0.45 0.90044710 0.90044710 -2.47e-13
0.50 0.87758256 0.87758256 -4.59e-13
0.55 0.85252452 0.85252452 -2.46e-13
0.60 0.82533561 0.82533561 1.95e-13
0.65 0.79608379 0.79608379 4.53e-13
0.70 0.76484218 0.76484218 2.55e-13
0.75 0.73168886 0.73168886 -2.26e-13
0.80 0.69670670 0.69670670 -4.46e-13
0.85 0.65998314 0.65998314 -4.45e-14
0.90 0.62160996 0.62160996 4.44e-13
0.95 0.58168308 0.58168308 -9.01e-14
1.00 0.54030230 0.54030230 4.47e-13
</pre>
=={{header|Julia}}==
{{works with|Julia|0.6}}
{{trans|Go}}
 
<langsyntaxhighlight lang="julia">mutable struct Cheb
c::Vector{Float64}
min::Float64
Line 763 ⟶ 1,455:
approx = evaluate(c, x)
@printf("%.1f %12.8f %12.8f % .3e\n", x, computed, approx, computed - approx)
end</langsyntaxhighlight>
 
{{out}}
Line 790 ⟶ 1,482:
0.9 0.62160997 0.62160997 -4.449e-13
1.0 0.54030231 0.54030231 -4.476e-13</pre>
 
=={{header|Kotlin}}==
{{trans|C}}
<langsyntaxhighlight lang="scala">// version 1.1.2
 
typealias DFunc = (Double) -> Double
Line 841 ⟶ 1,532:
System.out.printf("%1.3f %1.8f %1.8f % 4.1e\n", x, f, approx, approx - f)
}
}</langsyntaxhighlight>
 
{{out}}
Line 881 ⟶ 1,572:
1.000 0.54030231 0.54030231 4.5e-13
</pre>
 
=={{header|Lua}}==
{{trans|Java}}
<langsyntaxhighlight lang="lua">function map(x, min_x, max_x, min_to, max_to)
return (x - min_x) / (max_x - min_x) * (max_to - min_to) + min_to
end
Line 921 ⟶ 1,611:
 
main()
</syntaxhighlight>
</lang>
{{out}}
<pre>Coefficients:
Line 934 ⟶ 1,624:
6.5963018380799e-010
-1.0021913854352e-011</pre>
 
=={{header|Microsoft Small Basic}}==
{{trans|Perl}}
<langsyntaxhighlight lang="smallbasic">' N Chebyshev coefficients for the range 0 to 1 - 18/07/2018
pi=Math.pi
a=0
Line 956 ⟶ 1,645:
EndIf
TextWindow.WriteLine(i+" : "+t+cheby[i])
EndFor</langsyntaxhighlight>
{{out}}
<pre>
Line 970 ⟶ 1,659:
9 : -0,0000000000100189955816952521
</pre>
=={{header|МК-61/52}}==
 
{{trans|BASIC}}
<syntaxhighlight lang="mk-61">0 ПA 1 ПB 8 ПC 0 ПD ИПC ИПD
- x#0 44 пи ИПC / ИПD 1 ^ 2
/ + * cos ИПB ИПA - 2 / *
ИПB ИПA + 2 / + cos KПD ИПD 1
+ ПD БП 08 0 ПD ИПC ИПD - x#0
95 0 ПB ПE ИПC ИПE - x#0 83 пи
ИПC / ИПD * ИПE 1 ^ 2 / +
* cos KИПE * ИПB + ПB ИПE 1 +
ПE БП 54 ИПB 2 * ИПC / С/П ИПD
1 + ПD БП 46 С/П</syntaxhighlight>
=={{header|Nim}}==
{{trans|Go}}
<langsyntaxhighlight Nimlang="nim">import lenientops, math, strformat, sugar
 
type Cheb = object
Line 1,021 ⟶ 1,721:
let computed = fn(x)
let approx = cheb.eval(x)
echo &"{x:.1f} {computed:12.8f} {approx:12.8f} {computed-approx: .3e}"</langsyntaxhighlight>
 
{{out}}
Line 1,048 ⟶ 1,748:
0.9 0.62160997 0.62160997 -4.450e-13
1.0 0.54030231 0.54030231 -4.476e-13</pre>
 
=={{header|Perl}}==
{{trans|C}}
 
<langsyntaxhighlight lang="perl">use constant PI => 3.1415926535897932 * atan2(1, 0);
 
sub chebft {
Line 1,059 ⟶ 1,758:
 
my @pin = map { ($_ + 0.5) * (PI/$n) } 0..$n-1;
my @f = map { $func->( cos($_) * $bma + $bpa ) } @pin;
my @c = (0) x $n;
for my $j (0 .. $n-1) {
$c[$j] += $f[$_] * cos($j * $pin[$_]) for 0..$n-1;
$c[$j] *= (2.0/$n);
}
@c;
}
 
printprintf "$_%+13.7e\n", $_ for chebft(sub{cos($_[0])}, 0, 1, 10);</langsyntaxhighlight>
 
{{out}}
<pre>+1.647169475390316471695e+00
-2.3229937e-01
-0.232299371615172
-5.3715115e-02
-0.0537151146220477
+2.4582353e-03
0.00245823526698163
+2.8211906e-04
0.000282119057433938
-7.72222915566001e7222292e-06
-5.89855645105608e8985565e-07
+1.15214274787334e1521427e-08
+6.59629917354465e5962992e-10
-1.00219943455215e0021994e-11</pre>
 
=={{header|Phix}}==
{{trans|Go}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function Cheb(atom cmin, cmax, integer ncoeff, nnodes)
<span style="color: #008080;">function</span> <span style="color: #000000;">Cheb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">cmin</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cmax</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">ncoeff</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">nnodes</span><span style="color: #0000FF;">)</span>
sequence c = repeat(0,ncoeff),
<span style="color: #004080;">sequence</span> <span style="color: #000000;">c</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;">ncoeff</span><span style="color: #0000FF;">),</span>
f = repeat(0,nnodes),
<span style="color: #000000;">f</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;">nnodes</span><span style="color: #0000FF;">),</span>
p = repeat(0,nnodes)
<span style="color: #000000;">p</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;">nnodes</span><span style="color: #0000FF;">)</span>
atom z = (cmax + cmin) / 2,
<span style="color: #004080;">atom</span> <span style="color: #000000;">z</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">cmax</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">cmin</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span>
r = (cmax - cmin) / 2
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">cmax</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">cmin</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">2</span>
for k=1 to nnodes do
<span style="color: #008080;">for</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">nnodes</span> <span style="color: #008080;">do</span>
p[k] = PI * ((k-1) + 0.5) / nnodes
<span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">PI</span> <span style="color: #0000FF;">*</span> <span style="color: #0000FF;">((</span><span style="color: #000000;">k</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;">0.5</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">nnodes</span>
f[k] = cos(z + cos(p[k]) * r)
<span style="color: #000000;">f</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</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;">z</span> <span style="color: #0000FF;">+</span> <span style="color: #7060A8;">cos</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">])</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
atom n2 = 2 / nnodes
<span style="color: #004080;">atom</span> <span style="color: #000000;">n2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">nnodes</span>
for j=1 to nnodes do
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">nnodes</span> <span style="color: #008080;">do</span>
atom s := 0
<span style="color: #004080;">atom</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">:=</span> <span style="color: #000000;">0</span>
for k=1 to nnodes do
<span style="color: #008080;">for</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">nnodes</span> <span style="color: #008080;">do</span>
s += f[k] * cos((j-1)*p[k])
<span style="color: #000000;">s</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</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;">j</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">])</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
c[j] = s * n2
<span style="color: #000000;">c</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">n2</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
return c
<span style="color: #008080;">return</span> <span style="color: #000000;">c</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function evaluate(sequence c, atom cmin, cmax, x)
<span style="color: #008080;">function</span> <span style="color: #000000;">evaluate</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">cmin</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cmax</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">)</span>
atom x1 = (2*x - cmax - cmin) / (cmax - cmin),
<span style="color: #004080;">atom</span> <span style="color: #000000;">x1</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;">x</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">cmax</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">cmin</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">/</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">cmax</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">cmin</span><span style="color: #0000FF;">),</span>
x2 = 2*x1,
<span style="color: #000000;">x2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">x1</span><span style="color: #0000FF;">,</span>
t = 0, s = 0
<span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
for j=length(c) to 2 by -1 do
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #000000;">2</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
{t, s} = {x2 * t - s + c[j], t}
<span style="color: #0000FF;">{</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">x2</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">}</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
return x1 * t - s + c[1] / 2
<span style="color: #008080;">return</span> <span style="color: #000000;">x1</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">c</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>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
atom cmin = 0.0, cmax = 1.0
<span style="color: #004080;">atom</span> <span style="color: #000000;">cmin</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0.0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cmax</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1.0</span>
sequence c = Cheb(cmin, cmax, 10, 10)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">Cheb</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cmin</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cmax</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
printf(1, "Coefficients:\n")
<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: #008000;">"Coefficients:\n"</span><span style="color: #0000FF;">)</span>
pp(c,{pp_Nest,1,pp_FltFmt,"%18.15f"})
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,{</span><span style="color: #004600;">pp_Nest</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #004600;">pp_FltFmt</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%18.15f"</span><span style="color: #0000FF;">})</span>
printf(1,"\nx computed approximated computed-approx\n")
<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: #008000;">"\nx computed approximated computed-approx\n"</span><span style="color: #0000FF;">)</span>
constant n = 10
<span style="color: #008080;">constant</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">10</span>
for i=0 to 10 do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">10</span> <span style="color: #008080;">do</span>
atom x = (cmin * (n - i) + cmax * i) / n,
<span style="color: #004080;">atom</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">cmin</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;">i</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">cmax</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;">n</span><span style="color: #0000FF;">,</span>
calc = cos(x),
<span style="color: #000000;">calc</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cos</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">),</span>
est = evaluate(c, cmin, cmax, x)
<span style="color: #000000;">est</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">evaluate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cmin</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cmax</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">)</span>
printf(1,"%.1f %12.8f %12.8f %10.3e\n", {x, calc, est, calc-est})
<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: #008000;">"%.1f %12.8f %12.8f %10.3e\n"</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;">calc</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">est</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">calc</span><span style="color: #0000FF;">-</span><span style="color: #000000;">est</span><span style="color: #0000FF;">})</span>
end for</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,154 ⟶ 1,853:
1.0 0.54030231 0.54030231 -4.477e-13
</pre>
 
=={{header|Python}}==
{{trans|C++}}
<langsyntaxhighlight lang="python">import math
 
def test_func(x):
Line 1,208 ⟶ 1,906:
return None
 
main()</langsyntaxhighlight>
{{out}}
<pre>Coefficients:
Line 1,245 ⟶ 1,943:
0.900 0.6216099683 0.6216099683 4.46e-13
0.950 0.5816830895 0.5816830895 -8.99e-14</pre>
 
=={{header|Racket}}==
 
{{trans|C}}
 
<langsyntaxhighlight lang="racket">#lang typed/racket
(: chebft (Real Real Nonnegative-Integer (Real -> Real) -> (Vectorof Real)))
(define (chebft a b n func)
Line 1,273 ⟶ 1,970:
(module+ test
(chebft 0 1 10 cos))
;; Tim Brown 2015</langsyntaxhighlight>
 
{{out}}
Line 1,286 ⟶ 1,983:
6.596299173544651e-010
-1.0022016549982027e-011)</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
 
{{works with|Rakudo|2015.12}}
{{trans|C}}
<syntaxhighlight lang="raku" line>sub chebft ( Code $func, Real \a, Real \b, Int \n ) {
 
my \bma = ½ × (b - a);
<lang perl6>sub chebft ( Code $func, Real $a, Real $b, Int $n ) {
my \bpa = ½ × (b + a);
 
my $bma@pi-n = 0.5( *^n (»+» $b½ -) $a»×» (π/n);
my $bpa@f = 0.5 * = ( $b@pi-n».cos »×» bma »+» $abpa )».&$func;
my @sums = (^n).map: { [+] @f »×« ( @pi-n »×» $_ )».cos };
 
my @pi_n = ( (^$n).listsums »+» 0.5 ) »*×» ( pi 2/ $n );
my @f = ( @pi_n».cos »*» $bma »+» $bpa )».$func;
my @sums = map { [+] @f »*« ( @pi_n »*» $_ )».cos }, ^$n;
 
return @sums »*» ( 2 / $n );
}
 
say .fmtchebft('%+13.7e') for chebft &cos, 0, 1, 10)».fmt: '%+13.7e';</langsyntaxhighlight>
 
{{out}}
<pre>+1.6471695e+00
+1.6471695e+00
-2.3229937e-01
-5.3715115e-02
Line 1,317 ⟶ 2,011:
+1.1521427e-08
+6.5962992e-10
-1.0021994e-11</pre>
</pre>
 
=={{header|REXX}}==
Line 1,335 ⟶ 2,028:
 
The numeric precision is dependent on the number of decimal digits specified in the value of '''pi'''.
<langsyntaxhighlight lang="rexx">/*REXX program calculates N Chebyshev coefficients for the range 0 ──► 1 (inclusive)*/
numeric digits length( pi() ) - length(.) /*DIGITS default is nine, but use 71. */
parse arg a b N . /*obtain optional arguments from the CL*/
Line 1,364 ⟶ 2,057:
/*──────────────────────────────────────────────────────────────────────────────────────*/
pi: pi=3.1415926535897932384626433832795028841971693993751058209749445923078164;return pi
r2r: return arg(1) // (pi() * 2) /*normalize radians ───► a unit circle.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,401 ⟶ 2,094:
19 Chebyshev coefficient is: 2.859065292763079576513213370136E-29
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">def mapp(x, min_x, max_x, min_to, max_to)
return (x - min_x) / (max_x - min_x) * (max_to - min_to) + min_to
end
Line 1,431 ⟶ 2,123:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>Coefficients:
Line 1,444 ⟶ 2,136:
6.59630183807991e-10
-1.0021913854352249e-11</pre>
 
=={{header|Scala}}==
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/DqRNe2A/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/M5Ye6h8ZRkmTCNzexUh3uw Scastie (remote JVM)].
<langsyntaxhighlight Scalalang="scala">import scala.math.{Pi, cos}
 
object ChebyshevCoefficients extends App {
Line 1,477 ⟶ 2,168:
c.foreach(d => println(f"$d%23.16e"))
 
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">func chebft (callback, a, b, n) {
 
var bma = (0.5 * b-a);
var bpa = (0.5 * b+a);
 
var pi_n = ((0..(^n-1)  »+» 0.5)  »*» (NumberNum.pi / n));
var f = (pi_n  »cos»() » »*» bma  »+» bpa «call«  callback);
var sums = (0..(^n-1) «run«  {|i| f  »*«  ((pi_n  »*» i)  »cos»()») «+» });
 
sums  »*» (2/n);
}
 
for v in (chebft(func(v){v.cos}, 0, 1, 10).each) { |v|
say ("%+.10e" % v);
}</langsyntaxhighlight>
 
{{out}}
Line 1,515 ⟶ 2,205:
{{trans|Kotlin}}
 
<langsyntaxhighlight lang="swift">import Foundation
 
typealias DFunc = (Double) -> Double
Line 1,572 ⟶ 2,262:
 
print(String(format: "%1.3f %1.8f %1.8f % 4.1e", x, f, approx, approx - f))
}</langsyntaxhighlight>
 
{{out}}
Line 1,611 ⟶ 2,301:
0.950 0.58168309 0.58168309 -9.0e-14
1.000 0.54030231 0.54030231 4.5e-13</pre>
 
=={{header|VBScript}}==
{{trans|Microsoft Small Basic}}
To run in console mode with cscript.
<langsyntaxhighlight lang="vb">' N Chebyshev coefficients for the range 0 to 1
Dim coef(10),cheby(10)
pi=4*Atn(1)
Line 1,630 ⟶ 2,319:
If cheby(i)<=0 Then t="" Else t=" "
WScript.StdOut.WriteLine i&" : "&t&cheby(i)
Next</langsyntaxhighlight>
{{out}}
<pre>
Line 1,644 ⟶ 2,333:
9 : -1,0022016549982E-11
</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Module Module1
 
Structure ChebyshevApprox
Line 1,753 ⟶ 2,441:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>Coefficients:
Line 1,793 ⟶ 2,481:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
var mapRange = Fn.new { |x, min, max, minTo, maxTo| (x - min)/(max - min)*(maxTo - minTo) + minTo }
Line 1,840 ⟶ 2,528:
diffStr = (diff >= 0) ? " " + diffStr[0..3] : diffStr[0..4]
Fmt.print("$1.3f $1.8f $1.8f $s", x, f, approx, diffStr + e)
}</langsyntaxhighlight>
 
{{out}}
Line 1,879 ⟶ 2,567:
0.950 0.58168309 0.58168309 -8.99e-14
1.000 0.54030231 0.54030231 4.47e-13
</pre>
 
=={{header|XPL0}}==
{{trans|C}}
<syntaxhighlight lang "XPL0">include xpllib; \for Print and Pi
 
func real Map(X, MinX, MaxX, MinTo, MaxTo);
\Map X from range Min,Max to MinTo,MaxTo
real X, MinX, MaxX, MinTo, MaxTo;
return (X-MinX) / (MaxX-MinX) * (MaxTo-MinTo) + MinTo;
 
proc ChebCoef(N, Min, Max, Coef);
int N; real Min, Max, Coef;
int I, J;
real F;
[for I:= 0 to N-1 do Coef(I):= 0.0;
for I:= 0 to N-1 do
[F:= Cos(Map(Cos(Pi*(float(I)+0.5)/float(N)), -1.0, 1.0, Min, Max)) *
2.0/float(N);
for J:= 0 to N-1 do
Coef(J):= Coef(J) + F*Cos(Pi*float(J) * (float(I)+0.5) / float(N));
];
];
 
func real ChebApprox(X, N, Min, Max, Coef);
real X; int N; real Min, Max, Coef;
real A, B, C, Res;
int I;
[A:= 1.0;
B:= Map(X, Min, Max, -1.0, 1.0);
Res:= Coef(0)/2.0 + Coef(1)*B;
X:= 2.0*B;
for I:= 2 to N-1 do
[C:= X*B - A;
Res:= Res + Coef(I)*C;
A:= B;
B:= C;
];
return Res;
];
 
def N=10, MinV=0.0, MaxV=1.0;
real C(N);
int I;
real X, F, Approx;
[ChebCoef(N, MinV, MaxV, C);
Print("Coefficients:\n");
for I:= 0 to N-1 do
Print(" %2.15f\n", C(I));
Print("\nApproximation:\n X Cos(X) Approx Diff\n");
for I:= 0 to 20 do
[X:= Map(float(I), 0.0, 20.0, MinV, MaxV);
F:= Cos(X);
Approx:= ChebApprox(X, N, MinV, MaxV, C);
Print("%2.2f %2.14f %2.14f %0.1f\n", X, F, Approx, Approx-F);
];
]</syntaxhighlight>
{{out}}
<pre>
Coefficients:
1.647169475390310
-0.232299371615172
-0.053715114622048
0.002458235266982
0.000282119057434
-0.000007722229156
-0.000000589855646
0.000000011521428
0.000000000659630
-0.000000000010022
 
Approximation:
X Cos(X) Approx Diff
0.00 1.00000000000000 1.00000000000047 4.7E-013
0.05 0.99875026039497 0.99875026039487 -9.4E-014
0.10 0.99500416527803 0.99500416527849 4.6E-013
0.15 0.98877107793604 0.98877107793599 -4.7E-014
0.20 0.98006657784124 0.98006657784078 -4.6E-013
0.25 0.96891242171064 0.96891242171041 -2.3E-013
0.30 0.95533648912561 0.95533648912587 2.6E-013
0.35 0.93937271284738 0.93937271284784 4.6E-013
0.40 0.92106099400289 0.92106099400308 2.0E-013
0.45 0.90044710235268 0.90044710235243 -2.5E-013
0.50 0.87758256189037 0.87758256188991 -4.6E-013
0.55 0.85252452205951 0.85252452205926 -2.5E-013
0.60 0.82533561490968 0.82533561490987 2.0E-013
0.65 0.79608379854906 0.79608379854951 4.5E-013
0.70 0.76484218728449 0.76484218728474 2.5E-013
0.75 0.73168886887382 0.73168886887359 -2.3E-013
0.80 0.69670670934717 0.69670670934672 -4.5E-013
0.85 0.65998314588498 0.65998314588494 -4.4E-014
0.90 0.62160996827066 0.62160996827111 4.5E-013
0.95 0.58168308946388 0.58168308946379 -9.0E-014
1.00 0.54030230586814 0.54030230586859 4.5E-013
</pre>
 
=={{header|zkl}}==
{{trans|C}}{{trans|Perl}}
<langsyntaxhighlight lang="zkl">var [const] PI=(1.0).pi;
fcn chebft(a,b,n,func){
bma,bpa,fac := 0.5*(b - a), 0.5*(b + a), 2.0/n;
Line 1,891 ⟶ 2,673:
})
}
chebft(0.0,1.0,10,fcn(x){ x.cos() }).enumerate().concat("\n").println();</langsyntaxhighlight>
{{out}}
<pre>