-
Notifications
You must be signed in to change notification settings - Fork 1
/
lu.t
231 lines (200 loc) · 5.8 KB
/
lu.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
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
-- SPDX-FileCopyrightText: 2024 René Hiemstra <[email protected]>
-- SPDX-FileCopyrightText: 2024 Torsten Keßler <[email protected]>
--
-- SPDX-License-Identifier: MIT
import "terraform"
local base = require("base")
local err = require("assert")
local concepts = require("concepts")
local mathfun = require("mathfuns")
local lapack = require("lapack")
local Matrix = concepts.Matrix
local Vector = concepts.Vector
local Number = concepts.Number
local Integral = concepts.Integral
terraform factorize(a : &M, p : &P, tol : T)
where {M : Matrix(Number), P : Vector(Integral), T : Number}
var n = a:rows()
for i = 0, n do
p:set(i, i)
end
for i = 0, n do
var maxA = [tol.type](0)
var imax = i
for k = i, n do
var absA = mathfun.abs(a:get(k, i))
if absA > maxA then
maxA = absA
imax = k
end
end
err.assert(maxA > tol)
if imax ~= i then
var j = p:get(i)
p:set(i, p:get(imax))
p:set(imax, j)
for k = 0, n do
var tmp = a:get(i, k)
a:set(i, k, a:get(imax, k))
a:set(imax, k, tmp)
end
end
for j = i + 1, n do
a:set(j, i, a:get(j, i) / a:get(i, i))
for k = i + 1, n do
var tmp = a:get(j, k)
a:set(j, k, tmp - a:get(j, i) * a:get(i, k))
end
end
end
end
local BLASMatrix = concepts.BLASDenseMatrix
local ContiguousVector = concepts.ContiguousVector
local BLASNumber = concepts.BLASNumber
terraform factorize(a: &M, p: &P, tol: T)
where {M: BLASMatrix(BLASNumber), P: ContiguousVector(int32), T: BLASNumber}
var n, m, adata, lda = a:getblasdenseinfo()
err.assert(n == m)
var np, pdata = p:getbuffer()
err.assert(n == np)
var info = lapack.getrf(lapack.ROW_MAJOR, n, n, adata, lda, pdata)
return info
end
local Bool = concepts.Bool
local conj = mathfun.conj
terraform solve(trans: B, a: &M, p: &P, x: &V)
where {B: Bool, M: Matrix(Number), P: Vector(Integral), V: Vector(Number)}
var n = a:rows()
if not trans then
for i = 0, n do
var idx = p:get(i)
while idx < i do
idx = p:get(idx)
end
var tmp = x:get(i)
x:set(i, x:get(idx))
x:set(idx, tmp)
end
for i = 0, n do
for k = 0, i do
x:set(i, x:get(i) - a:get(i, k) * x:get(k))
end
end
for ii = 0, n do
var i = n - 1 - ii
for k = i + 1, n do
x:set(i, x:get(i) - a:get(i, k) * x:get(k))
end
x:set(i, x:get(i) / a:get(i, i))
end
else
for i = 0, n do
for k = 0, i do
x:set(i, x:get(i) - conj(a:get(k, i)) * x:get(k))
end
x:set(i, x:get(i) / conj(a:get(i, i)))
end
for ii = 0, n do
var i = n - 1 - ii
for k = i + 1, n do
x:set(i, x:get(i) - conj(a:get(k, i)) * x:get(k))
end
end
for ii = 0, n do
var i = n - 1 - ii
var idx = p:get(i)
while idx < i do
idx = p:get(idx)
end
var tmp = x:get(i)
x:set(i, x:get(idx))
x:set(idx, tmp)
end
end
end
local function get_trans(T)
if concepts.Complex(T) then
return "C"
else
return "T"
end
end
local BLASVector = concepts.BLASVector
terraform solve(trans: B, a: &M, p: &P, x: &V)
where {B: Bool, M: BLASMatrix(BLASNumber), P: ContiguousVector(int32), V: BLASVector(BLASNumber)}
var n, m, adata, lda = a:getblasdenseinfo()
err.assert(n == m)
var np, pdata = p:getbuffer()
err.assert(n == np)
var nx, xdata, incx = x:getblasinfo()
var lapack_trans: rawstring
if trans then
lapack_trans = [get_trans(a.type.type.eltype)]
else
lapack_trans = "N"
end
lapack.getrs(
lapack.ROW_MAJOR,
@lapack_trans,
n,
1,
adata,
lda,
pdata,
xdata,
incx
)
end
local LUFactory = terralib.memoize(function(M, P)
local T = M.eltype
local Vector = concepts.Vector(T)
local VectorIntegral = concepts.Vector(Integral)
local Matrix = concepts.Matrix(T)
local Factorization = concepts.Factorization(T)
assert(Matrix(M), "Type " .. tostring(M)
.. " does not implement the matrix interface")
assert(VectorIntegral(P), "Type " .. tostring(P)
.. " does not implement the vector interface")
local Ts = T
local Ts = concepts.Complex(T) and T.traits.eltype or T
local struct lu{
a: &M
p: &P
tol: Ts
}
function lu.metamethods.__typename(self)
return ("LUFactorization(%s)"):format(tostring(T))
end
base.AbstractBase(lu)
terra lu:rows()
return self.a:rows()
end
terra lu:cols()
return self.a:cols()
end
terra lu:factorize()
escape
local impl = factorize:dispatch(&M, &P, Ts)
emit quote return impl(self.a, self.p, self.tol) end
end
end
terraform lu:solve(trans: B, x: &V) where {B: Bool, V: Vector}
solve(trans, self.a, self.p, x)
end
terraform lu:apply(trans: B, a: T, x: &V1, b: T, y: &V2)
where {B: Bool, V1: Vector, V2: Vector}
self:solve(trans, x)
y:scal(b)
y:axpy(a, x)
end
assert(Factorization(lu))
lu.staticmethods.new = terra(a: &M, p: &P, tol: Ts)
err.assert(a:rows() == a:cols())
err.assert(p:size() == a:rows())
return lu {a, p, tol}
end
return lu
end)
return {
LUFactory = LUFactory,
}