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