Jump to content

Chebyshev coefficients: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Perl}}: formatted output)
m (syntax highlighting fixup automation)
Line 13:
{{trans|Python}}
 
<langsyntaxhighlight lang=11l>F test_func(Float x)
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)))</langsyntaxhighlight>
 
{{out}}
Line 102:
{{trans|FreeBASIC}}
Given the limitations of the language, only 8 coefficients are calculated
<langsyntaxhighlight lang=BASIC256>a = 0: b = 1: n = 8
dim cheby(n)
dim coef(n)
Line 118:
print i; " : "; cheby[i]
next i
end</langsyntaxhighlight>
{{out}}
<pre>0 : 1.64716947539
Line 133:
{{works with|QuickBasic|4.5}}
{{trans|FreeBASIC}}
<langsyntaxhighlight lang=qbasic>pi = 4 * ATN(1)
a = 0: b = 1: n = 10
DIM cheby!(n)
Line 150:
PRINT USING " # : ##.#####################"; i; cheby(i)
NEXT i
END</langsyntaxhighlight>
{{out}}
<pre>
Line 166:
 
==={{header|FreeBASIC}}===
<langsyntaxhighlight lang=freebasic>Const pi As Double = 4 * Atn(1)
Dim As Double i, w, j
Dim As Double a = 0, b = 1, n = 10
Line 183:
Print i; " : "; cheby(i)
Next i
Sleep</langsyntaxhighlight>
{{out}}
<pre> 0 : 1.647169475390314
Line 198:
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight lang=yabasic>a = 0: b = 1: n = 10
dim cheby(n)
dim coef(n)
Line 214:
print i, " : ", cheby(i)
next i
end</langsyntaxhighlight>
{{out}}
<pre>0 : 1.64717
Line 230:
=={{header|C}}==
C99.
<langsyntaxhighlight lang=C>#include <stdio.h>
#include <string.h>
#include <math.h>
Line 296:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
{{trans|C++}}
<langsyntaxhighlight lang=csharp>using System;
using System.Collections.Generic;
using System.Linq;
Line 408:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Coefficients:
Line 452:
The wrapper class ChebyshevApprox_ supports very terse user code.
 
<langsyntaxhighlight lang=CPP>
#include <iostream>
#include <iomanip>
Line 554:
}
}
</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 620:
writefln("%1.3f % 10.10f % 10.10f % 4.2e", x, f, approx, approx - f);
}
}</langsyntaxhighlight>
{{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]
.</langsyntaxhighlight>
 
=={{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.)
<langsyntaxhighlight lang=go>package main
 
import (
Line 787:
}
return x1*t - s + .5*c.c[0]
}</langsyntaxhighlight>
{{out}}
<pre>
Line 818:
=={{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 846:
}
}
}</langsyntaxhighlight>
{{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>
<lang J>
chebft =: adverb define
:
Line 868:
(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 914:
System.out.println(d);
}
}</langsyntaxhighlight>
 
<pre>Coefficients:
Line 934:
 
'''Preliminaries'''
<langsyntaxhighlight lang=jq>def lpad($len): tostring | ($len - length) as $l | (" " * $l) + .;
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;</langsyntaxhighlight>
'''Chebyshev Coefficients'''
<langsyntaxhighlight lang=jq>def mapRange($x; $min; $max; $minTo; $maxTo):
(($x - $min)/($max - $min))*($maxTo - $minTo) + $minTo;
Line 993:
| join(" ") );
 
task</langsyntaxhighlight>
{{out}}
<pre>
Line 1,037:
{{trans|Go}}
 
<langsyntaxhighlight lang=julia>mutable struct Cheb
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</langsyntaxhighlight>
 
{{out}}
Line 1,116:
=={{header|Kotlin}}==
{{trans|C}}
<langsyntaxhighlight lang=scala>// version 1.1.2
 
typealias DFunc = (Double) -> Double
Line 1,164:
System.out.printf("%1.3f %1.8f %1.8f % 4.1e\n", x, f, approx, approx - f)
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,207:
=={{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 1,244:
 
main()
</syntaxhighlight>
</lang>
{{out}}
<pre>Coefficients:
Line 1,260:
=={{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 1,279:
EndIf
TextWindow.WriteLine(i+" : "+t+cheby[i])
EndFor</langsyntaxhighlight>
{{out}}
<pre>
Line 1,296:
=={{header|Nim}}==
{{trans|Go}}
<langsyntaxhighlight lang=Nim>import lenientops, math, strformat, sugar
 
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}"</langsyntaxhighlight>
 
{{out}}
Line 1,375:
{{trans|C}}
 
<langsyntaxhighlight lang=perl>use constant PI => 2 * atan2(1, 0);
 
sub chebft {
Line 1,391:
}
 
printf "%+13.7e\n", $_ for chebft(sub{cos($_[0])}, 0, 1, 10);</langsyntaxhighlight>
{{out}}
<pre>+1.6471695e+00
Line 1,406:
=={{header|Phix}}==
{{trans|Go}}
<!--<langsyntaxhighlight 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: #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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,481:
=={{header|Python}}==
{{trans|C++}}
<langsyntaxhighlight lang=python>import math
 
def test_func(x):
Line 1,532:
return None
 
main()</langsyntaxhighlight>
{{out}}
<pre>Coefficients:
Line 1,574:
{{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,597:
(module+ test
(chebft 0 1 10 cos))
;; Tim Brown 2015</langsyntaxhighlight>
 
{{out}}
Line 1,615:
 
{{trans|C}}
<syntaxhighlight lang=raku perl6line>sub chebft ( Code $func, Real \a, Real \b, Int \n ) {
 
my \bma = ½ × (b - a);
Line 1,627:
}
 
say chebft(&cos, 0, 1, 10)».fmt: '%+13.7e';</langsyntaxhighlight>
 
{{out}}
Line 1,656:
 
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,685:
/*──────────────────────────────────────────────────────────────────────────────────────*/
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,724:
 
=={{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,752:
end
 
main()</langsyntaxhighlight>
{{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)].
<langsyntaxhighlight lang=Scala>import scala.math.{Pi, cos}
 
object ChebyshevCoefficients extends App {
Line 1,798:
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);
Line 1,816:
chebft(func(v){v.cos}, 0, 1, 10).each { |v|
say ("%+.10e" % v);
}</langsyntaxhighlight>
 
{{out}}
Line 1,836:
{{trans|Kotlin}}
 
<langsyntaxhighlight lang=swift>import Foundation
 
typealias DFunc = (Double) -> Double
Line 1,893:
 
print(String(format: "%1.3f %1.8f %1.8f % 4.1e", x, f, approx, approx - f))
}</langsyntaxhighlight>
 
{{out}}
Line 1,936:
{{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,951:
If cheby(i)<=0 Then t="" Else t=" "
WScript.StdOut.WriteLine i&" : "&t&cheby(i)
Next</langsyntaxhighlight>
{{out}}
<pre>
Line 1,968:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang=vbnet>Module Module1
 
Structure ChebyshevApprox
Line 2,074:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>Coefficients:
Line 2,114:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang=ecmascript>import "/fmt" for 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)
}</langsyntaxhighlight>
 
{{out}}
Line 2,204:
=={{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 2,212:
})
}
chebft(0.0,1.0,10,fcn(x){ x.cos() }).enumerate().concat("\n").println();</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits

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