Chebyshev coefficients: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
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)
 
Line 96 ⟶ 95:
0.950 0.5816830895 0.5816830895 -8.98e-14
</pre>
 
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
Given the limitations of the language, only 8 coefficients are calculated
<syntaxhighlight lang=BASIC256"basic256">a = 0: b = 1: n = 8
dim cheby(n)
dim coef(n)
Line 133 ⟶ 130:
{{works with|QuickBasic|4.5}}
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">pi = 4 * ATN(1)
a = 0: b = 1: n = 10
DIM cheby!(n)
Line 166 ⟶ 163:
 
==={{header|FreeBASIC}}===
<syntaxhighlight 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 198 ⟶ 195:
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="yabasic">a = 0: b = 1: n = 10
dim cheby(n)
dim coef(n)
Line 226 ⟶ 223:
8 : 6.5963e-10
9 : -1.0022e-11</pre>
 
 
=={{header|C}}==
C99.
<syntaxhighlight lang=C"c">#include <stdio.h>
#include <string.h>
#include <math.h>
Line 297 ⟶ 292:
return 0;
}</syntaxhighlight>
 
=={{header|C sharp|C#}}==
{{trans|C++}}
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 444 ⟶ 438:
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 452 ⟶ 445:
The wrapper class ChebyshevApprox_ supports very terse user code.
 
<syntaxhighlight lang=CPP"cpp">
#include <iostream>
#include <iomanip>
Line 555 ⟶ 548:
}
</syntaxhighlight>
 
=={{header|D}}==
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].
Line 698 ⟶ 690:
0.900 0.62160996827066445648 0.62160996827066445674 2.71e-19
0.950 0.58168308946388349416 0.58168308946388349403 -1.63e-19</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="text">numfmt 0 5
a = 0
b = 1
Line 717 ⟶ 708:
print cheby[i]
.</syntaxhighlight>
 
=={{header|Go}}==
Wikipedia gives a formula for coefficients in a section [https://en.wikipedia.org/wiki/Chebyshev_polynomials#Example_1 "Example 1"]. Read past the bit about the inner product to where it gives the technique based on the discrete orthogonality condition. The N of the WP formulas is the parameter nNodes in the code here. It is not necessarily the same as n, the number of polynomial coefficients, the parameter nCoeff here.
Line 724 ⟶ 714:
 
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 (
Line 815 ⟶ 805:
1.0 0.54030231 0.54030231 -4.476e-13
</pre>
 
=={{header|Groovy}}==
{{trans|Java}}
<syntaxhighlight 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 859 ⟶ 848:
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"j">
chebft =: adverb define
:
Line 870 ⟶ 858:
</syntaxhighlight>
Calculate coefficients:
<syntaxhighlight lang=J"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}}
<syntaxhighlight lang="java">import static java.lang.Math.*;
import java.util.function.Function;
 
Line 927 ⟶ 914:
6.59630183807991E-10
-1.0021913854352249E-11</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
Line 934 ⟶ 920:
 
'''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];
 
Line 952 ⟶ 938:
end;</syntaxhighlight>
'''Chebyshev Coefficients'''
<syntaxhighlight lang="jq">def mapRange($x; $min; $max; $minTo; $maxTo):
(($x - $min)/($max - $min))*($maxTo - $minTo) + $minTo;
Line 1,032 ⟶ 1,018:
1.00 0.54030230 0.54030230 4.47e-13
</pre>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
{{trans|Go}}
 
<syntaxhighlight lang="julia">mutable struct Cheb
c::Vector{Float64}
min::Float64
Line 1,113 ⟶ 1,098:
0.9 0.62160997 0.62160997 -4.449e-13
1.0 0.54030231 0.54030231 -4.476e-13</pre>
 
=={{header|Kotlin}}==
{{trans|C}}
<syntaxhighlight lang="scala">// version 1.1.2
 
typealias DFunc = (Double) -> Double
Line 1,204 ⟶ 1,188:
1.000 0.54030231 0.54030231 4.5e-13
</pre>
 
=={{header|Lua}}==
{{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
end
Line 1,257 ⟶ 1,240:
6.5963018380799e-010
-1.0021913854352e-011</pre>
 
=={{header|Microsoft Small Basic}}==
{{trans|Perl}}
<syntaxhighlight lang="smallbasic">' N Chebyshev coefficients for the range 0 to 1 - 18/07/2018
pi=Math.pi
a=0
Line 1,293 ⟶ 1,275:
9 : -0,0000000000100189955816952521
</pre>
 
=={{header|Nim}}==
{{trans|Go}}
<syntaxhighlight lang=Nim"nim">import lenientops, math, strformat, sugar
 
type Cheb = object
Line 1,371 ⟶ 1,352:
0.9 0.62160997 0.62160997 -4.450e-13
1.0 0.54030231 0.54030231 -4.476e-13</pre>
 
=={{header|Perl}}==
{{trans|C}}
 
<syntaxhighlight lang="perl">use constant PI => 2 * atan2(1, 0);
 
sub chebft {
Line 1,403 ⟶ 1,383:
+6.5962992e-10
-1.0021994e-11</pre>
 
=={{header|Phix}}==
{{trans|Go}}
<!--<syntaxhighlight lang=Phix"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,478 ⟶ 1,457:
1.0 0.54030231 0.54030231 -4.477e-13
</pre>
 
=={{header|Python}}==
{{trans|C++}}
<syntaxhighlight lang="python">import math
 
def test_func(x):
Line 1,569 ⟶ 1,547:
0.900 0.6216099683 0.6216099683 4.46e-13
0.950 0.5816830895 0.5816830895 -8.99e-14</pre>
 
=={{header|Racket}}==
 
{{trans|C}}
 
<syntaxhighlight lang="racket">#lang typed/racket
(: chebft (Real Real Nonnegative-Integer (Real -> Real) -> (Vectorof Real)))
(define (chebft a b n func)
Line 1,610 ⟶ 1,587:
6.596299173544651e-010
-1.0022016549982027e-011)</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
 
{{trans|C}}
<syntaxhighlight lang="raku" line>sub chebft ( Code $func, Real \a, Real \b, Int \n ) {
 
my \bma = ½ × (b - a);
Line 1,640 ⟶ 1,616:
+6.5962992e-10
-1.0021994e-11</pre>
 
=={{header|REXX}}==
{{trans|C}}
Line 1,656 ⟶ 1,631:
 
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. */
parse arg a b N . /*obtain optional arguments from the CL*/
Line 1,722 ⟶ 1,697:
19 Chebyshev coefficient is: 2.859065292763079576513213370136E-29
</pre>
 
=={{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
end
Line 1,765 ⟶ 1,739:
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)].
<syntaxhighlight lang=Scala"scala">import scala.math.{Pi, cos}
 
object ChebyshevCoefficients extends App {
Line 1,799 ⟶ 1,772:
 
}</syntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Raku}}
<syntaxhighlight lang="ruby">func chebft (callback, a, b, n) {
 
var bma = (0.5 * b-a);
Line 1,831 ⟶ 1,803:
-1.0022591709e-11
</pre>
 
=={{header|Swift}}==
 
{{trans|Kotlin}}
 
<syntaxhighlight lang="swift">import Foundation
 
typealias DFunc = (Double) -> Double
Line 1,932 ⟶ 1,903:
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.
<syntaxhighlight lang="vb">' N Chebyshev coefficients for the range 0 to 1
Dim coef(10),cheby(10)
pi=4*Atn(1)
Line 1,965 ⟶ 1,935:
9 : -1,0022016549982E-11
</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<syntaxhighlight lang="vbnet">Module Module1
 
Structure ChebyshevApprox
Line 2,110 ⟶ 2,079:
0.950 0.58168308946388 0.58168308946379 -8.992806E-014
1.000 0.54030230586814 0.54030230586859 4.468648E-013</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{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 }
Line 2,201 ⟶ 2,169:
1.000 0.54030231 0.54030231 4.47e-13
</pre>
 
=={{header|zkl}}==
{{trans|C}}{{trans|Perl}}
<syntaxhighlight 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;
10,327

edits