Chebyshev coefficients: Difference between revisions

Content added Content deleted
m (→‎{{header|Perl}}: formatted output)
m (syntax highlighting fixup automation)
Line 13: Line 13:
{{trans|Python}}
{{trans|Python}}


<lang 11l>F test_func(Float x)
<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)))</lang>
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
<lang BASIC256>a = 0: b = 1: n = 8
<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</lang>
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}}
<lang qbasic>pi = 4 * ATN(1)
<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</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 166: Line 166:


==={{header|FreeBASIC}}===
==={{header|FreeBASIC}}===
<lang freebasic>Const pi As Double = 4 * Atn(1)
<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</lang>
Sleep</syntaxhighlight>
{{out}}
{{out}}
<pre> 0 : 1.647169475390314
<pre> 0 : 1.647169475390314
Line 198: Line 198:
==={{header|Yabasic}}===
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang yabasic>a = 0: b = 1: n = 10
<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</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>0 : 1.64717
<pre>0 : 1.64717
Line 230: Line 230:
=={{header|C}}==
=={{header|C}}==
C99.
C99.
<lang C>#include <stdio.h>
<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;
}</lang>
}</syntaxhighlight>


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
{{trans|C++}}
{{trans|C++}}
<lang csharp>using System;
<syntaxhighlight lang=csharp>using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Linq;
Line 408: Line 408:
}
}
}
}
}</lang>
}</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.


<lang CPP>
<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.
<lang d>import std.math: PI, cos;
<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);
}
}
}</lang>
}</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]
.</lang>
.</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.)
<lang go>package main
<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]
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 818: Line 818:
=={{header|Groovy}}==
=={{header|Groovy}}==
{{trans|Java}}
{{trans|Java}}
<lang groovy>class ChebyshevCoefficients {
<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:
}
}
}
}
}</lang>
}</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}}
<lang java>import static java.lang.Math.*;
<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);
}
}
}</lang>
}</syntaxhighlight>


<pre>Coefficients:
<pre>Coefficients:
Line 934: Line 934:


'''Preliminaries'''
'''Preliminaries'''
<lang jq>def lpad($len): tostring | ($len - length) as $l | (" " * $l) + .;
<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;</lang>
end;</syntaxhighlight>
'''Chebyshev Coefficients'''
'''Chebyshev Coefficients'''
<lang jq>def mapRange($x; $min; $max; $minTo; $maxTo):
<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</lang>
task</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,037: Line 1,037:
{{trans|Go}}
{{trans|Go}}


<lang julia>mutable struct Cheb
<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</lang>
end</syntaxhighlight>


{{out}}
{{out}}
Line 1,116: Line 1,116:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|C}}
{{trans|C}}
<lang scala>// version 1.1.2
<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)
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,207: Line 1,207:
=={{header|Lua}}==
=={{header|Lua}}==
{{trans|Java}}
{{trans|Java}}
<lang lua>function map(x, min_x, max_x, min_to, max_to)
<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}}
<lang smallbasic>' N Chebyshev coefficients for the range 0 to 1 - 18/07/2018
<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</lang>
EndFor</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,296: Line 1,296:
=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Go}}
{{trans|Go}}
<lang Nim>import lenientops, math, strformat, sugar
<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}"</lang>
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}}


<lang perl>use constant PI => 2 * atan2(1, 0);
<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);</lang>
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}}
<!--<lang Phix>(phixonline)-->
<!--<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>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,481: Line 1,481:
=={{header|Python}}==
=={{header|Python}}==
{{trans|C++}}
{{trans|C++}}
<lang python>import math
<syntaxhighlight lang=python>import math


def test_func(x):
def test_func(x):
Line 1,532: Line 1,532:
return None
return None


main()</lang>
main()</syntaxhighlight>
{{out}}
{{out}}
<pre>Coefficients:
<pre>Coefficients:
Line 1,574: Line 1,574:
{{trans|C}}
{{trans|C}}


<lang racket>#lang typed/racket
<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</lang>
;; Tim Brown 2015</syntaxhighlight>


{{out}}
{{out}}
Line 1,615: Line 1,615:


{{trans|C}}
{{trans|C}}
<lang perl6>sub chebft ( Code $func, Real \a, Real \b, Int \n ) {
<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';</lang>
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'''.
<lang rexx>/*REXX program calculates N Chebyshev coefficients for the range 0 ──► 1 (inclusive)*/
<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.*/</lang>
r2r: return arg(1) // (pi() * 2) /*normalize radians ───► a unit circle.*/</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
Line 1,724: Line 1,724:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>def mapp(x, min_x, max_x, min_to, max_to)
<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()</lang>
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)].
<lang Scala>import scala.math.{Pi, cos}
<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"))


}</lang>
}</syntaxhighlight>


=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Raku}}
{{trans|Raku}}
<lang ruby>func chebft (callback, a, b, n) {
<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);
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,836: Line 1,836:
{{trans|Kotlin}}
{{trans|Kotlin}}


<lang swift>import Foundation
<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))
}</lang>
}</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.
<lang vb>' N Chebyshev coefficients for the range 0 to 1
<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</lang>
Next</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,968: Line 1,968:
=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
{{trans|C#}}
{{trans|C#}}
<lang vbnet>Module Module1
<syntaxhighlight lang=vbnet>Module Module1


Structure ChebyshevApprox
Structure ChebyshevApprox
Line 2,074: Line 2,074:
End Sub
End Sub


End Module</lang>
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}}
<lang ecmascript>import "/fmt" for 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)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,204: Line 2,204:
=={{header|zkl}}==
=={{header|zkl}}==
{{trans|C}}{{trans|Perl}}
{{trans|C}}{{trans|Perl}}
<lang zkl>var [const] PI=(1.0).pi;
<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();</lang>
chebft(0.0,1.0,10,fcn(x){ x.cos() }).enumerate().concat("\n").println();</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>