Chebyshev coefficients: Difference between revisions

m (→‎{{header|Sidef}}: replaced "Num.pi" with "Number.pi")
(→‎{{header|Java}}: added Java)
Line 340:
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
</lang>
 
=={{header|Java}}==
Partial translation of [[Chebyshev_coefficients#C|C]] via [[Chebyshev_coefficients#D|D]]
{{works with|Java|8}}
<lang java>import static java.lang.Math.*;
import java.util.function.Function;
 
public 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;
}
 
static void chebyshevCoef(Function<Double, Double> func, double min,
double max, double[] coef) {
 
int N = coef.length;
 
for (int i = 0; i < N; i++) {
 
double m = map(cos(PI * (i + 0.5f) / N), -1, 1, min, max);
double f = func.apply(m) * 2 / N;
 
for (int j = 0; j < N; j++) {
coef[j] += f * cos(PI * j * (i + 0.5f) / N);
}
}
}
 
public static void main(String[] args) {
final int N = 10;
double[] c = new double[N];
double min = 0, max = 1;
chebyshevCoef(x -> cos(x), min, max, c);
 
System.out.println("Coefficients:");
for (double d : c)
System.out.println(d);
}
}</lang>
 
<pre>Coefficients:
1.6471694753903139
-0.23229937161517178
-0.0537151146220477
0.002458235266981773
2.8211905743405485E-4
-7.722229156320592E-6
-5.898556456745974E-7
1.1521427770166959E-8
6.59630183807991E-10
-1.0021913854352249E-11</pre>
 
=={{header|Perl}}==
Anonymous user