Chebyshev coefficients: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: formatted output) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 13: | Line 13: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang=11l>F test_func(Float x) |
||
R cos(x) |
R cos(x) |
||
Line 56: | Line 56: | ||
V f = test_func(x) |
V f = test_func(x) |
||
V approx = cheb_approx(x, n, minv, maxv, c) |
V approx = cheb_approx(x, n, minv, maxv, c) |
||
print(‘#.3 #.10 #.10 #.’.format(x, f, approx, format_float_exp(approx - f, 2, 9)))</ |
print(‘#.3 #.10 #.10 #.’.format(x, f, approx, format_float_exp(approx - f, 2, 9)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 102: | Line 102: | ||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
Given the limitations of the language, only 8 coefficients are calculated |
Given the limitations of the language, only 8 coefficients are calculated |
||
< |
<syntaxhighlight lang=BASIC256>a = 0: b = 1: n = 8 |
||
dim cheby(n) |
dim cheby(n) |
||
dim coef(n) |
dim coef(n) |
||
Line 118: | Line 118: | ||
print i; " : "; cheby[i] |
print i; " : "; cheby[i] |
||
next i |
next i |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 : 1.64716947539 |
<pre>0 : 1.64716947539 |
||
Line 133: | Line 133: | ||
{{works with|QuickBasic|4.5}} |
{{works with|QuickBasic|4.5}} |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
< |
<syntaxhighlight lang=qbasic>pi = 4 * ATN(1) |
||
a = 0: b = 1: n = 10 |
a = 0: b = 1: n = 10 |
||
DIM cheby!(n) |
DIM cheby!(n) |
||
Line 150: | Line 150: | ||
PRINT USING " # : ##.#####################"; i; cheby(i) |
PRINT USING " # : ##.#####################"; i; cheby(i) |
||
NEXT i |
NEXT i |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 166: | Line 166: | ||
==={{header|FreeBASIC}}=== |
==={{header|FreeBASIC}}=== |
||
< |
<syntaxhighlight lang=freebasic>Const pi As Double = 4 * Atn(1) |
||
Dim As Double i, w, j |
Dim As Double i, w, j |
||
Dim As Double a = 0, b = 1, n = 10 |
Dim As Double a = 0, b = 1, n = 10 |
||
Line 183: | Line 183: | ||
Print i; " : "; cheby(i) |
Print i; " : "; cheby(i) |
||
Next i |
Next i |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 0 : 1.647169475390314 |
<pre> 0 : 1.647169475390314 |
||
Line 198: | Line 198: | ||
==={{header|Yabasic}}=== |
==={{header|Yabasic}}=== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
< |
<syntaxhighlight lang=yabasic>a = 0: b = 1: n = 10 |
||
dim cheby(n) |
dim cheby(n) |
||
dim coef(n) |
dim coef(n) |
||
Line 214: | Line 214: | ||
print i, " : ", cheby(i) |
print i, " : ", cheby(i) |
||
next i |
next i |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 : 1.64717 |
<pre>0 : 1.64717 |
||
Line 230: | Line 230: | ||
=={{header|C}}== |
=={{header|C}}== |
||
C99. |
C99. |
||
< |
<syntaxhighlight lang=C>#include <stdio.h> |
||
#include <string.h> |
#include <string.h> |
||
#include <math.h> |
#include <math.h> |
||
Line 296: | Line 296: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<syntaxhighlight lang=csharp>using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using System.Linq; |
using System.Linq; |
||
Line 408: | Line 408: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Coefficients: |
<pre>Coefficients: |
||
Line 452: | Line 452: | ||
The wrapper class ChebyshevApprox_ supports very terse user code. |
The wrapper class ChebyshevApprox_ supports very terse user code. |
||
< |
<syntaxhighlight lang=CPP> |
||
#include <iostream> |
#include <iostream> |
||
#include <iomanip> |
#include <iomanip> |
||
Line 554: | Line 554: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|D}}== |
=={{header|D}}== |
||
This imperative code retains some of the style of the original C version. |
This imperative code retains some of the style of the original C version. |
||
< |
<syntaxhighlight lang=d>import std.math: PI, cos; |
||
/// Map x from range [min, max] to [min_to, max_to]. |
/// Map x from range [min, max] to [min_to, max_to]. |
||
Line 620: | Line 620: | ||
writefln("%1.3f % 10.10f % 10.10f % 4.2e", x, f, approx, approx - f); |
writefln("%1.3f % 10.10f % 10.10f % 4.2e", x, f, approx, approx - f); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Coefficients: |
<pre>Coefficients: |
||
Line 700: | Line 700: | ||
=={{header|EasyLang}}== |
=={{header|EasyLang}}== |
||
<lang>numfmt 0 5 |
<syntaxhighlight lang=text>numfmt 0 5 |
||
a = 0 |
a = 0 |
||
b = 1 |
b = 1 |
||
Line 716: | Line 716: | ||
cheby[i] = w * 2 / n |
cheby[i] = w * 2 / n |
||
print cheby[i] |
print cheby[i] |
||
.</ |
.</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
Line 724: | Line 724: | ||
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.) |
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.) |
||
< |
<syntaxhighlight lang=go>package main |
||
import ( |
import ( |
||
Line 787: | Line 787: | ||
} |
} |
||
return x1*t - s + .5*c.c[0] |
return x1*t - s + .5*c.c[0] |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 818: | Line 818: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang=groovy>class ChebyshevCoefficients { |
||
static double map(double x, double min_x, double max_x, double min_to, double max_to) { |
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 |
return (x - min_x) / (max_x - min_x) * (max_to - min_to) + min_to |
||
Line 846: | Line 846: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Coefficients: |
<pre>Coefficients: |
||
Line 862: | Line 862: | ||
=={{header|J}}== |
=={{header|J}}== |
||
From 'J for C Programmers: Calculating Chebyshev Coefficients [[http://www.jsoftware.com/learning/a_first_look_at_j_programs.htm#_Toc191734318]] |
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 |
chebft =: adverb define |
||
: |
: |
||
Line 868: | Line 868: | ||
(2 % x) * +/ f * 2 o. o. (0.5 + i. x) *"0 1 (i. x) % x |
(2 % x) * +/ f * 2 o. o. (0.5 + i. x) *"0 1 (i. x) % x |
||
) |
) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Calculate coefficients: |
Calculate coefficients: |
||
<syntaxhighlight lang=J> |
|||
<lang J> |
|||
10 (2&o.) chebft 0 1 |
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 |
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}}== |
=={{header|Java}}== |
||
Partial translation of [[Chebyshev_coefficients#C|C]] via [[Chebyshev_coefficients#D|D]] |
Partial translation of [[Chebyshev_coefficients#C|C]] via [[Chebyshev_coefficients#D|D]] |
||
{{works with|Java|8}} |
{{works with|Java|8}} |
||
< |
<syntaxhighlight lang=java>import static java.lang.Math.*; |
||
import java.util.function.Function; |
import java.util.function.Function; |
||
Line 914: | Line 914: | ||
System.out.println(d); |
System.out.println(d); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
<pre>Coefficients: |
<pre>Coefficients: |
||
Line 934: | Line 934: | ||
'''Preliminaries''' |
'''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]; |
def rpad($len; $fill): tostring | ($len - length) as $l | . + ($fill * $l)[:$l]; |
||
Line 950: | Line 950: | ||
| ((if $ix then $s[0:$ix] else $s end) | lpad) + "." + |
| ((if $ix then $s[0:$ix] else $s end) | lpad) + "." + |
||
(if $ix then $s[$ix+1:] | .[0:right] else "" end) |
(if $ix then $s[$ix+1:] | .[0:right] else "" end) |
||
end;</ |
end;</syntaxhighlight> |
||
'''Chebyshev Coefficients''' |
'''Chebyshev Coefficients''' |
||
< |
<syntaxhighlight lang=jq>def mapRange($x; $min; $max; $minTo; $maxTo): |
||
(($x - $min)/($max - $min))*($maxTo - $minTo) + $minTo; |
(($x - $min)/($max - $min))*($maxTo - $minTo) + $minTo; |
||
Line 993: | Line 993: | ||
| join(" ") ); |
| join(" ") ); |
||
task</ |
task</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,037: | Line 1,037: | ||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang=julia>mutable struct Cheb |
||
c::Vector{Float64} |
c::Vector{Float64} |
||
min::Float64 |
min::Float64 |
||
Line 1,086: | Line 1,086: | ||
approx = evaluate(c, x) |
approx = evaluate(c, x) |
||
@printf("%.1f %12.8f %12.8f % .3e\n", x, computed, approx, computed - approx) |
@printf("%.1f %12.8f %12.8f % .3e\n", x, computed, approx, computed - approx) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,116: | Line 1,116: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang=scala>// version 1.1.2 |
||
typealias DFunc = (Double) -> Double |
typealias DFunc = (Double) -> Double |
||
Line 1,164: | Line 1,164: | ||
System.out.printf("%1.3f %1.8f %1.8f % 4.1e\n", x, f, approx, approx - f) |
System.out.printf("%1.3f %1.8f %1.8f % 4.1e\n", x, f, approx, approx - f) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,207: | Line 1,207: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight 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 |
return (x - min_x) / (max_x - min_x) * (max_to - min_to) + min_to |
||
end |
end |
||
Line 1,244: | Line 1,244: | ||
main() |
main() |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>Coefficients: |
<pre>Coefficients: |
||
Line 1,260: | Line 1,260: | ||
=={{header|Microsoft Small Basic}}== |
=={{header|Microsoft Small Basic}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
< |
<syntaxhighlight lang=smallbasic>' N Chebyshev coefficients for the range 0 to 1 - 18/07/2018 |
||
pi=Math.pi |
pi=Math.pi |
||
a=0 |
a=0 |
||
Line 1,279: | Line 1,279: | ||
EndIf |
EndIf |
||
TextWindow.WriteLine(i+" : "+t+cheby[i]) |
TextWindow.WriteLine(i+" : "+t+cheby[i]) |
||
EndFor</ |
EndFor</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,296: | Line 1,296: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang=Nim>import lenientops, math, strformat, sugar |
||
type Cheb = object |
type Cheb = object |
||
Line 1,344: | Line 1,344: | ||
let computed = fn(x) |
let computed = fn(x) |
||
let approx = cheb.eval(x) |
let approx = cheb.eval(x) |
||
echo &"{x:.1f} {computed:12.8f} {approx:12.8f} {computed-approx: .3e}"</ |
echo &"{x:.1f} {computed:12.8f} {approx:12.8f} {computed-approx: .3e}"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,375: | Line 1,375: | ||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang=perl>use constant PI => 2 * atan2(1, 0); |
||
sub chebft { |
sub chebft { |
||
Line 1,391: | Line 1,391: | ||
} |
} |
||
printf "%+13.7e\n", $_ for chebft(sub{cos($_[0])}, 0, 1, 10);</ |
printf "%+13.7e\n", $_ for chebft(sub{cos($_[0])}, 0, 1, 10);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>+1.6471695e+00 |
<pre>+1.6471695e+00 |
||
Line 1,406: | Line 1,406: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
<!--< |
<!--<syntaxhighlight lang=Phix>(phixonline)--> |
||
<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> |
<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> |
||
<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> |
<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> |
||
Line 1,450: | Line 1,450: | ||
<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> |
<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> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,481: | Line 1,481: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<syntaxhighlight lang=python>import math |
||
def test_func(x): |
def test_func(x): |
||
Line 1,532: | Line 1,532: | ||
return None |
return None |
||
main()</ |
main()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Coefficients: |
<pre>Coefficients: |
||
Line 1,574: | Line 1,574: | ||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang=racket>#lang typed/racket |
||
(: chebft (Real Real Nonnegative-Integer (Real -> Real) -> (Vectorof Real))) |
(: chebft (Real Real Nonnegative-Integer (Real -> Real) -> (Vectorof Real))) |
||
(define (chebft a b n func) |
(define (chebft a b n func) |
||
Line 1,597: | Line 1,597: | ||
(module+ test |
(module+ test |
||
(chebft 0 1 10 cos)) |
(chebft 0 1 10 cos)) |
||
;; Tim Brown 2015</ |
;; Tim Brown 2015</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,615: | Line 1,615: | ||
{{trans|C}} |
{{trans|C}} |
||
<lang |
<syntaxhighlight lang=raku line>sub chebft ( Code $func, Real \a, Real \b, Int \n ) { |
||
my \bma = ½ × (b - a); |
my \bma = ½ × (b - a); |
||
Line 1,627: | Line 1,627: | ||
} |
} |
||
say chebft(&cos, 0, 1, 10)».fmt: '%+13.7e';</ |
say chebft(&cos, 0, 1, 10)».fmt: '%+13.7e';</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,656: | Line 1,656: | ||
The numeric precision is dependent on the number of decimal digits specified in the value of '''pi'''. |
The numeric precision is dependent on the number of decimal digits specified in the value of '''pi'''. |
||
< |
<syntaxhighlight 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. */ |
numeric digits length( pi() ) - length(.) /*DIGITS default is nine, but use 71. */ |
||
parse arg a b N . /*obtain optional arguments from the CL*/ |
parse arg a b N . /*obtain optional arguments from the CL*/ |
||
Line 1,685: | Line 1,685: | ||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
pi: pi=3.1415926535897932384626433832795028841971693993751058209749445923078164;return pi |
pi: pi=3.1415926535897932384626433832795028841971693993751058209749445923078164;return pi |
||
r2r: return arg(1) // (pi() * 2) /*normalize radians ───► a unit circle.*/</ |
r2r: return arg(1) // (pi() * 2) /*normalize radians ───► a unit circle.*/</syntaxhighlight> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
Line 1,724: | Line 1,724: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight 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 |
return (x - min_x) / (max_x - min_x) * (max_to - min_to) + min_to |
||
end |
end |
||
Line 1,752: | Line 1,752: | ||
end |
end |
||
main()</ |
main()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Coefficients: |
<pre>Coefficients: |
||
Line 1,768: | Line 1,768: | ||
=={{header|Scala}}== |
=={{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)]. |
{{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)]. |
||
< |
<syntaxhighlight lang=Scala>import scala.math.{Pi, cos} |
||
object ChebyshevCoefficients extends App { |
object ChebyshevCoefficients extends App { |
||
Line 1,798: | Line 1,798: | ||
c.foreach(d => println(f"$d%23.16e")) |
c.foreach(d => println(f"$d%23.16e")) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang=ruby>func chebft (callback, a, b, n) { |
||
var bma = (0.5 * b-a); |
var bma = (0.5 * b-a); |
||
Line 1,816: | Line 1,816: | ||
chebft(func(v){v.cos}, 0, 1, 10).each { |v| |
chebft(func(v){v.cos}, 0, 1, 10).each { |v| |
||
say ("%+.10e" % v); |
say ("%+.10e" % v); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,836: | Line 1,836: | ||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang=swift>import Foundation |
||
typealias DFunc = (Double) -> Double |
typealias DFunc = (Double) -> Double |
||
Line 1,893: | Line 1,893: | ||
print(String(format: "%1.3f %1.8f %1.8f % 4.1e", x, f, approx, approx - f)) |
print(String(format: "%1.3f %1.8f %1.8f % 4.1e", x, f, approx, approx - f)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,936: | Line 1,936: | ||
{{trans|Microsoft Small Basic}} |
{{trans|Microsoft Small Basic}} |
||
To run in console mode with cscript. |
To run in console mode with cscript. |
||
< |
<syntaxhighlight lang=vb>' N Chebyshev coefficients for the range 0 to 1 |
||
Dim coef(10),cheby(10) |
Dim coef(10),cheby(10) |
||
pi=4*Atn(1) |
pi=4*Atn(1) |
||
Line 1,951: | Line 1,951: | ||
If cheby(i)<=0 Then t="" Else t=" " |
If cheby(i)<=0 Then t="" Else t=" " |
||
WScript.StdOut.WriteLine i&" : "&t&cheby(i) |
WScript.StdOut.WriteLine i&" : "&t&cheby(i) |
||
Next</ |
Next</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,968: | Line 1,968: | ||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
{{trans|C#}} |
{{trans|C#}} |
||
< |
<syntaxhighlight lang=vbnet>Module Module1 |
||
Structure ChebyshevApprox |
Structure ChebyshevApprox |
||
Line 2,074: | Line 2,074: | ||
End Sub |
End Sub |
||
End Module</ |
End Module</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Coefficients: |
<pre>Coefficients: |
||
Line 2,114: | Line 2,114: | ||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang=ecmascript>import "/fmt" for Fmt |
||
var mapRange = Fn.new { |x, min, max, minTo, maxTo| (x - min)/(max - min)*(maxTo - minTo) + minTo } |
var mapRange = Fn.new { |x, min, max, minTo, maxTo| (x - min)/(max - min)*(maxTo - minTo) + minTo } |
||
Line 2,161: | Line 2,161: | ||
diffStr = (diff >= 0) ? " " + diffStr[0..3] : diffStr[0..4] |
diffStr = (diff >= 0) ? " " + diffStr[0..3] : diffStr[0..4] |
||
Fmt.print("$1.3f $1.8f $1.8f $s", x, f, approx, diffStr + e) |
Fmt.print("$1.3f $1.8f $1.8f $s", x, f, approx, diffStr + e) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,204: | Line 2,204: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
{{trans|C}}{{trans|Perl}} |
{{trans|C}}{{trans|Perl}} |
||
< |
<syntaxhighlight lang=zkl>var [const] PI=(1.0).pi; |
||
fcn chebft(a,b,n,func){ |
fcn chebft(a,b,n,func){ |
||
bma,bpa,fac := 0.5*(b - a), 0.5*(b + a), 2.0/n; |
bma,bpa,fac := 0.5*(b - a), 0.5*(b + a), 2.0/n; |
||
Line 2,212: | Line 2,212: | ||
}) |
}) |
||
} |
} |
||
chebft(0.0,1.0,10,fcn(x){ x.cos() }).enumerate().concat("\n").println();</ |
chebft(0.0,1.0,10,fcn(x){ x.cos() }).enumerate().concat("\n").println();</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |