feq/interpolant.lua

37 lines
777 B
Lua

--Returns a function which takes a sequence of polynomial coefficients and
--returns two functions which evaluate the polynomial and its derivative, respectively
return function( coefs )
local fixedPoint = coefs[0] or error( "Must have constant coefficient!" )
--Interpolant, naive
return function(x)
x = x - fixedPoint
local y = fixedPoint
local pow = 1
local fact = 1
for i = 1, #coefs do
pow = pow * x
fact = fact / i
y = y + pow * fact * coefs[i]
end
return y
end,
--Derivative, naive
function(x)
x = x - fixedPoint
local y = 0
local pow = 1
local fact = 1
for i = 1, #coefs do
y = y + pow * fact * coefs[i]
pow = pow * x
fact = fact / i
end
return y
end
end