-
Notifications
You must be signed in to change notification settings - Fork 1
/
poly.t
57 lines (44 loc) · 1.32 KB
/
poly.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
-- SPDX-FileCopyrightText: 2024 René Hiemstra <[email protected]>
-- SPDX-FileCopyrightText: 2024 Torsten Keßler <[email protected]>
--
-- SPDX-License-Identifier: MIT
local vec = require("svector")
local mathfuns = require("mathfuns")
local Polynomial = terralib.memoize(function(T, N)
local svector = vec.StaticVector(T,N)
local struct poly(Base){
coeffs : svector
}
poly.staticmethods = {}
poly.metamethods.__getmethod = function(self, methodname)
return self.methods[methodname] or poly.staticmethods[methodname]
end
local _evalpoly = macro(function(self, x)
local eval = terralib.newlist()
local y = symbol(T)
for i=N-2,0,-1 do
eval:insert(quote [y] = mathfuns.fusedmuladd(x, [y], self.coeffs(i)) end)
end
local M = N-1
return quote
var [y] = self.coeffs.data[M]
[eval]
in
[y]
end
end)
poly.methods.eval = terra(self : &poly, x : T)
return _evalpoly(self, x)
end
poly.metamethods.__apply = macro(function(self, x)
return `self:eval(x)
end)
poly.staticmethods.from = macro(function(...)
local args = {...}
return `poly{svector.from(args)}
end)
return poly
end)
return {
Polynomial = Polynomial
}