forked from cpmech/gosl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genorthopoly.go
284 lines (231 loc) · 7.6 KB
/
genorthopoly.go
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright 2016 The Gosl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package fun
import (
"math"
"github.com/cpmech/gosl/chk"
)
// GeneralOrthoPoly (main) structure ////////////////////////////////////////////////////////////////
// GeneralOrthoPoly implements general orthogonal polynomials. It uses a general format and is NOT
// very efficient for large degrees. For efficiency, use the OrthoPoly structure instead.
//
// Reference:
// [1] Abramowitz M, Stegun IA (1972) Handbook of Mathematical Functions with Formulas, Graphs,
// and Mathematical Tables. U.S. Department of Commerce, NIST
//
// NOTE: this structure should be not be used for high-performance computing;
// it's probably useful for verifications or learning purposes only
//
type GeneralOrthoPoly struct {
// input
Kind string // type of orthogonal polynomial
N int // (max) degree of polynomial. Lower order can be quickly obtained after this polynomial with max(N) is generated
// computed
c [][]float64 // all c coefficients [N+1][M+1]
// internal
poly oPoly // implementation
}
// NewGeneralOrthoPoly creates a new orthogonal polynomial
//
// kind -- is the type of orthognal polynomial:
// "J" or "jac" : Jacobi
// "L" or "leg" : Legendre
// "H" or "her" : Hermite
// "T" or "cheby1" : Chebyshev first kind
// "U" or "cheby2" : Chebyshev second kind
//
// N -- is the (max) degree of the polynomial.
// Lower order can later be quickly obtained after this
// polynomial with max(N) is created
//
// alpha -- Jacobi only: α coefficient
//
// beta -- Jacobi only: β coefficient
//
// NOTE: all coefficients for the 0...N polynomials will be generated
//
// NOTE: this structure should be not be used for high-performance computing;
// it's probably useful for verifications or learning purposes only
//
func NewGeneralOrthoPoly(kind string, N int, alpha, beta float64) (o *GeneralOrthoPoly) {
o = new(GeneralOrthoPoly)
o.Kind = kind
o.N = N
o.poly = newopoly(kind, alpha, beta)
o.c = make([][]float64, o.N+1)
for n := 1; n <= o.N; n++ {
o.c[n] = make([]float64, o.poly.M(o.N)+1)
M := o.poly.M(n)
for m := 0; m <= M; m++ {
o.c[n][m] = o.poly.c(n, m)
}
}
return
}
// F computes P(n,x) with n=N (max)
// Since GeneralOrthoPoly is a general form, the summations are directly implement; i.e. no
// advantages are taken w.r.t the structure of the polynomial. Thus, these functions are not
// highly efficient for large degrees N
func (o *GeneralOrthoPoly) F(x float64) (res float64) {
return o.P(o.N, x)
}
// P computes P(n,x) where n must be ≤ N
// Since GeneralOrthoPoly is a general form, the summations are directly implement; i.e. no
// advantages are taken w.r.t the structure of the polynomial. Thus, these functions are not
// highly efficient for large degrees N
func (o *GeneralOrthoPoly) P(n int, x float64) (res float64) {
if n > o.N {
chk.Panic("the degree n must not be greater than max N. %d > %d", n, o.N)
}
if n == 0 {
return 1
}
for m := 0; m <= o.poly.M(n); m++ {
res += o.c[n][m] * o.poly.g(n, m, x)
}
res *= o.poly.d(n)
return
}
// oPoly database //////////////////////////////////////////////////////////////////////////////////
// oPoly defines the functions that GeneralOrthoPolys must have
//
// The general expression is (Table 22.3 Page 775 of [1]: Explicit Expressions):
//
// M(n)
// ————
// f(n, x) = d(n) ⋅ \ c(n, m) ⋅ g(n, m, x)
// /
// ————
// m = 0
type oPoly interface {
M(n int) int
d(n int) float64
c(n, m int) float64
g(n, m int, x float64) float64
}
// oPolyMaker defines a function that makes new oPolys
type oPolyMaker func(alpha, beta float64) oPoly
// oPolyDB implements a database of oPoly makers
var oPolyDB = make(map[string]oPolyMaker)
// newopoly finds oPoly or panic
func newopoly(kind string, alpha, beta float64) oPoly {
if maker, ok := oPolyDB[kind]; ok {
return maker(alpha, beta)
}
chk.Panic("cannot find OrthoPolynomial named %q in database", kind)
return nil
}
// Jacobi //////////////////////////////////////////////////////////////////////////////////////////
type opJacobi struct {
alpha float64
beta float64
}
func (o *opJacobi) M(n int) int {
return n
}
func (o *opJacobi) d(n int) float64 {
var twoPown uint64 = 1 << uint64(n) // 1<<n = 2ⁿ
return 1.0 / float64(twoPown)
}
func (o *opJacobi) c(n, m int) float64 {
r := Rbinomial(float64(n)+o.alpha, float64(m))
s := Rbinomial(float64(n)+o.beta, float64(n-m))
return r * s
}
func (o *opJacobi) g(n, m int, x float64) float64 {
return math.Pow(x-1, float64(n-m)) * math.Pow(x+1, float64(m))
}
func newJacobi(alpha, beta float64) oPoly {
o := new(opJacobi)
o.alpha = alpha
o.beta = beta
return o
}
// Legendre //////////////////////////////////////////////////////////////////////////////////////////
type opLegendre struct{}
func (o *opLegendre) M(n int) int {
return int(math.Floor(float64(n) / 2.0))
}
func (o *opLegendre) d(n int) float64 {
var twoPown uint64 = 1 << uint64(n) // 1<<n = 2ⁿ
return 1.0 / float64(twoPown)
}
func (o *opLegendre) c(n, m int) float64 {
r := Rbinomial(float64(n), float64(m))
s := Rbinomial(float64(2*n-2*m), float64(n))
return math.Pow(-1, float64(m)) * r * s
}
func (o *opLegendre) g(n, m int, x float64) float64 {
return math.Pow(x, float64(n-2*m))
}
func newLegendre(alpha, beta float64) oPoly {
return new(opLegendre)
}
// Hermite //////////////////////////////////////////////////////////////////////////////////////////
type opHermite struct{}
func (o *opHermite) M(n int) int {
return int(math.Floor(float64(n) / 2.0))
}
func (o *opHermite) d(n int) float64 {
return Factorial22(n)
}
func (o *opHermite) c(n, m int) float64 {
r := Factorial22(m)
s := Factorial22(n - 2*m)
return math.Pow(-1, float64(m)) / (r * s)
}
func (o *opHermite) g(n, m int, x float64) float64 {
return math.Pow(2*x, float64(n-2*m))
}
func newHermite(alpha, beta float64) oPoly {
return new(opHermite)
}
// Chebyshev1 //////////////////////////////////////////////////////////////////////////////////////////
type opChebyshev1 struct{}
func (o *opChebyshev1) M(n int) int {
return int(math.Floor(float64(n) / 2.0))
}
func (o *opChebyshev1) d(n int) float64 {
return float64(n) / 2.0
}
func (o *opChebyshev1) c(n, m int) float64 {
r := Factorial22(n - m - 1)
s := Factorial22(m)
t := Factorial22(n - 2*m)
return math.Pow(-1, float64(m)) * r / (s * t)
}
func (o *opChebyshev1) g(n, m int, x float64) float64 {
return math.Pow(2*x, float64(n-2*m))
}
func newChebyshev1(alpha, beta float64) oPoly {
return new(opChebyshev1)
}
// Chebyshev2 //////////////////////////////////////////////////////////////////////////////////////////
type opChebyshev2 struct{}
func (o *opChebyshev2) M(n int) int {
return int(math.Floor(float64(n) / 2.0))
}
func (o *opChebyshev2) d(n int) float64 {
return 1.0
}
func (o *opChebyshev2) c(n, m int) float64 {
r := Factorial22(n - m)
s := Factorial22(m)
t := Factorial22(n - 2*m)
return math.Pow(-1, float64(m)) * r / (s * t)
}
func (o *opChebyshev2) g(n, m int, x float64) float64 {
return math.Pow(2*x, float64(n-2*m))
}
func newChebyshev2(alpha, beta float64) oPoly {
return new(opChebyshev2)
}
// add polynomials to database /////////////////////////////////////////////////////////////////////
func init() {
oPolyDB["J"] = newJacobi
oPolyDB["L"] = newLegendre
oPolyDB["H"] = newHermite
oPolyDB["T"] = newChebyshev1
oPolyDB["U"] = newChebyshev2
}