-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclausenMinimaxApprox.m
134 lines (99 loc) · 4.81 KB
/
clausenMinimaxApprox.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
(*
Approximations of Cl2[2,x] by rational function approximations
on the intervals [0, Pi/2) and [Pi/2, Pi).
*)
Needs["FunctionApproximations`"];
Get["../test/math/clausenBernoulli.m"];
Get["../test/math/clausenWu.m"];
(* maximum number of terms in Cl2 series *)
nMax = 100;
(* output precision *)
outPrec = 17;
(* bring rational function to standard form *)
PolynomialStandardForm[expr_, x_] :=
Module[{n = Numerator[expr], d = Denominator[expr], c},
c = d /. x -> 0;
Expand[n/c] / Expand[d/c]
]
FormatCoeffs[expr_, x_, prec_] :=
ScientificForm @ CForm @ N[#, prec]& /@ CoefficientList[expr, x]
CalcPade[fn_, interval_, {numTerms_, denTerms_}] :=
Module[{half = interval[[1]] + (interval[[2]] - interval[[1]])/2,
approx, diff, maxErr, y, range},
approx = EconomizedRationalApproximation[fn[x], {x, interval, numTerms, denTerms}] /. x -> (y + half);
approx = PolynomialStandardForm[approx, y];
Print["Pade approximant for ", InputForm[fn], " on the interval ", InputForm[interval]];
Print["Numerator coefficients: ",
FormatCoeffs[#,y,outPrec]& @ Numerator[approx]];
Print["Denominator coefficients: ",
FormatCoeffs[#,y,outPrec]& @ Denominator[approx]];
(* calculate maximum deviation *)
Print["half interval: ", InputForm[half]];
diff = (approx - fn[x]) /. y -> x - half;
range = Range[interval[[1]] + 10^(-outPrec), interval[[2]], (interval[[2]] - interval[[1]])/100];
maxErr = Max @ Abs @ N[diff /. x -> #, outPrec]& @ range;
Print["max error: ", InputForm @ maxErr];
]
CalcMinimax[fn_, interval_, {numTerms_, denTerms_}] :=
Module[{approx, maxErr},
approx = MiniMaxApproximation[fn[x], {x, interval, numTerms, denTerms}, WorkingPrecision -> 1000];
maxErr = approx[[2,2]];
approx = PolynomialStandardForm[approx[[2,1]], x];
Print["Approximant for ", InputForm[fn], " on the interval ", InputForm[interval]];
Print["Numerator coefficients: ",
FormatCoeffs[#,x,outPrec]& @ Numerator[approx]];
Print["Denominator coefficients: ",
FormatCoeffs[#,x,outPrec]& @ Denominator[approx]];
(* calculate maximum deviation *)
Print["max error: ", InputForm @ N[maxErr,6]];
approx
]
Print["======================================================="];
Print[" Cl2 "];
Print["======================================================="];
(* interval = {0, Pi/2}; *)
fLo[x_] := Module[{y}, Expand[1/y^2 (Cl2Lo[y, nMax]/y - 1 + Log[y])] /. y -> x]
CalcMinimax[N[fLo[Sqrt[#]], 10*outPrec]&, {0, (Pi/2)^2}, {3, 3}];
(* interval = {Pi/2, Pi}; *)
fHi[x_] := Cl2Hi[x, nMax]/(Pi - x)
(* transformation *)
trans[x_] := Sqrt[x] + Pi
(* inverse transformation *)
itrans[x_] := (Pi - x)^2
CalcPade[N[fHi[trans[#]], 10*outPrec]&, itrans /@ {Pi/2, Pi}, {5,5}];
Print["======================================================="];
Print[" Cl3 "];
Print["======================================================="];
(* interval = {0, Pi/2}; *)
fLo[x_] := Module[{y}, Normal[Series[Expand[(Cl[3, y, nMax] - Zeta[3])/y^2 - Log[y]/2], {y,0,nMax}]] /. y -> x]
CalcMinimax[N[fLo[Sqrt[#]], 10*outPrec]&, {0, (Pi/2)^2}, {3,3}];
(* interval = {Pi/2, Pi}; *)
fHi[x_] := Cl[3, x, nMax]
CalcPade[N[fHi[trans[#]], 10*outPrec]&, itrans /@ {Pi/2, Pi}, {5,5}];
Print["======================================================="];
Print[" Cl4 "];
Print["======================================================="];
(* interval = {0, Pi/2}; *)
fLo[x_] := Module[{y}, Normal[Series[Expand[(Cl[4, y, nMax]/y - Zeta[3])/y^2 - Log[y]/6], {y,0,nMax}]] /. y -> x]
CalcMinimax[N[fLo[Sqrt[#]], 10*outPrec]&, {0, (Pi/2)^2}, {3,3}];
(* interval = {Pi/2, Pi}; *)
fHi[x_] := Cl[4, x, nMax]/(Pi - x)
CalcPade[N[fHi[trans[#]], 10*outPrec]&, itrans /@ {Pi/2, Pi}, {5,5}];
Print["======================================================="];
Print[" Cl5 "];
Print["======================================================="];
(* interval = {0, Pi/2}; *)
fLo[x_] := Module[{y}, Normal[Series[Expand[Cl[5, y, nMax] + y^4 Log[y]/24], {y,0,nMax}]] /. y -> x]
CalcMinimax[N[fLo[Sqrt[#]], 10*outPrec]&, {0, (Pi/2)^2}, {3,4}];
(* interval = {Pi/2, Pi}; *)
fHi[x_] := Cl[5, x, nMax]
CalcPade[N[fHi[trans[#]], 10*outPrec]&, itrans /@ {Pi/2, Pi}, {5,5}];
Print["======================================================="];
Print[" Cl6 "];
Print["======================================================="];
(* interval = {0, Pi/2}; *)
fLo[x_] := Module[{y}, Normal[Series[Expand[Cl[6, y, nMax]/y + y^4 Log[y]/120], {y,0,nMax}]] /. y -> x]
CalcMinimax[N[fLo[Sqrt[#]], 10*outPrec]&, {0, (Pi/2)^2}, {3,3}];
(* interval = {Pi/2, Pi}; *)
fHi[x_] := Cl[6, x, nMax]/(Pi - x)
CalcPade[N[fHi[trans[#]], 10*outPrec]&, itrans /@ {Pi/2, Pi}, {4,5}];