-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmomfit.t
190 lines (171 loc) · 4.94 KB
/
momfit.t
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
-- SPDX-FileCopyrightText: 2024 René Hiemstra <[email protected]>
-- SPDX-FileCopyrightText: 2024 Torsten Keßler <[email protected]>
--
-- SPDX-License-Identifier: MIT
local base = require("base")
local concepts = require("concepts")
local darray = require("darray")
local range = require("range")
local recdiff = require("recdiff")
local tmath = require("tmath")
import "terraform"
local Number = concepts.Number
local concept Interval(T) where {T: Number}
Self:addentry("left", T)
Self:addentry("right", T)
Self.traits.eltype = T
end
local Real = concepts.Real
local Integer = concepts.Integer
local RecDiff = recdiff.RecDiff
local terraform clenshawcurtis(alloc, n: N, rec: &R, dom: &I)
where {
N: Integer,
R: RecDiff(Real),
I: Interval(Real)
}
var x = [darray
.DynamicVector(I.traits.eltype)].new(alloc, n)
([range.Unitrange(int)].new(0, n)
>> range.transform(
[terra(i: int, n: int): I.traits.eltype
return tmath.cos(tmath.pi * (2 * i + 1) / (2 * n))
end],
{n = n})
):collect(&x)
var nmax = 20
if n > 10 then
nmax = 2 * n
end
var mom = [darray
.DynamicVector(R.traits.eltype)].zeros(alloc, nmax)
recdiff.olver(alloc, rec, &mom)
-- The quadrature weights on the reference domain (-1, 1) are given by
-- the inverse DCT-III transform, that is a scaled DCT-II transform of
-- the moments of the weight function in the Chebyshev basis.
var w = [darray
.DynamicVector(R.traits.eltype)].zeros(alloc, n)
for i = 0, n do
var res = mom(0) / 2
for j = 1, n do
var arg = tmath.pi / (2 * n) * (2 * i + 1) * j
res = res + tmath.cos(arg) * mom(j)
end
w(i) = res
end
w:scal([I.traits.eltype](2) / n)
var xq = [darray
.DynamicVector(I.traits.eltype)].new(alloc, n)
(x >> range.transform([
terra(
x: I.traits.eltype,
a: I.traits.eltype,
b: I.traits.eltype
)
return (b + a) / 2 + (b - a) / 2 * x
end],
{a = dom.left, b = dom.right})
):collect(&xq)
var wq = [darray
.DynamicVector(I.traits.eltype)].new(alloc, n)
(w >> range.transform([
terra(
w: I.traits.eltype,
a: I.traits.eltype,
b: I.traits.eltype
)
return (b - a) / 2 * w
end],
{a = dom.left, b = dom.right})
):collect(&wq)
return xq, wq
end
local function IntervalFactory(T)
local struct impl{
left: T
right: T
}
impl.metamethods.__typename = function(self)
return ("Interval(%s)"):format(tostring(T))
end
base.AbstractBase(impl)
impl.traits.eltype = T
impl.staticmethods.new = terra(left: T, right: T)
return impl {left, right}
end
return impl
end
local ExpMom = terralib.memoize(function(T)
local struct impl {
a: T
}
function impl.metamethods.__typename(self)
return ("ExpMom(%s)"):format(tostring(T))
end
base.AbstractBase(impl)
impl.traits.depth = 5
impl.traits.ninit = 2
impl.traits.eltype = T
local Integer = concepts.Integer
local Stack = concepts.Stack
terraform impl:getcoeff(n: I, y: &S) where {I: Integer, S: Stack(T)}
var a = self.a
y:set(0, -a * (n + 1))
y:set(1, -2 * a * (n + 1))
y:set(2, -2 * (a + n * n - 1))
y:set(3, 2 * a * (n - 1))
y:set(4, a * (n - 1))
y:set(5, 2 * (tmath.exp(-4 * a) + terralib.select(n % 2 == 0, 1, -1)))
end
local Stack = concepts.Stack
terraform impl:getinit(y: &S) where {S: Stack(T)}
var a = self.a
var arg = 2 * tmath.sqrt(a)
var y0 = tmath.sqrt(tmath.pi) * tmath.erf(arg) / arg
var y1 = -y0 - (tmath.exp(-4 * a) - 1) / (2 * a)
y:set(0, y0)
y:set(1, y1)
end
impl.staticmethods.new = terra(a: T)
return impl {a}
end
return impl
end)
local ConstMom = terralib.memoize(function(T)
local struct impl {}
base.AbstractBase(impl)
impl.traits.depth = 3
impl.traits.ninit = 1
impl.traits.eltype = T
local Integer = concepts.Integer
local Stack = concepts.Stack
terraform impl:getcoeff(n: I, y: &S) where {I: Integer, S: Stack(T)}
var val: T
if n == 0 then
val = 2
elseif n % 2 == 0 then
val = [T](-2) / (n * n - 1)
else
val = 0
end
y:set(0, 0)
y:set(1, 1)
y:set(2, 0)
y:set(3, val)
end
local Stack = concepts.Stack
terraform impl:getinit(y: &S) where {S: Stack(T)}
y:set(0, 2)
end
impl.staticmethods.new = terra()
return impl {}
end
return impl
end)
return {
Interval = Interval,
IntervalFactory = IntervalFactory,
clenshawcurtis = clenshawcurtis,
ExpMom = ExpMom,
ConstMom = ConstMom,
}