author | Francesco Abbate <francesco.bbt@gmail.com> | 2011年10月26日 11:22:04 +0200 |
---|---|---|
committer | Francesco Abbate <francesco.bbt@gmail.com> | 2011年10月26日 11:22:04 +0200 |
commit | 1295e713310a8c57f9b9d3a34e094f600d61fa75 (patch) | |
tree | 3ee0ece5c34c6813e1eb886a94e2fd66867eb956 /misc.lua | |
parent | 505c0c9226842d4989e90fba8fe56cd3c42040d3 (diff) | |
download | gsl-shell-1295e713310a8c57f9b9d3a34e094f600d61fa75.tar.gz |
-rw-r--r-- | misc.lua | 59 |
diff --git a/misc.lua b/misc.lua deleted file mode 100644 index c4b2d1cf..00000000 --- a/misc.lua +++ /dev/null @@ -1,59 +0,0 @@ - -local template = require 'template' - -function num.ode(spec) - local required = {N= 'number', eps_abs= 'number'} - local defaults = {eps_rel = 0, a_y = 1, a_dydt = 0} - local is_known = {rkf45= true, rk8pd= true} - - for k, tp in pairs(required) do - if type(spec[k]) ~= tp then - error(string.format('parameter %s should be a %s', k, tp)) - end - end - for k, v in pairs(defaults) do - if not spec[k] then spec[k] = v end - end - - local method = spec.method and spec.method or 'rkf45' - if not is_known[method] then error('unknown ode method: ' .. method) end - spec.method = nil - - local ode = template.load(string.format('num/%s.lua.in', method), spec) - - local mt = { - __index = {evolve = ode.evolve, init = ode.init} - } - - return setmetatable(ode.new(), mt) -end - -local NLINFIT = { - __index = function(t, k) - if k == 'chisq' then - return t.lm.chisq() - else - if t.lm[k] then return t.lm[k] end - end - end -} - -function num.nlinfit(spec) - if not spec.n then error 'number of points "n" not specified' end - if not spec.p then error 'number of parameters "p" not specified' end - - if spec.n <= 0 or spec.p <= 0 then - error '"n" and "p" shoud be positive integers' - end - - local n, p = spec.n, spec.p - local s = { lm = template.load('num/lmfit.lua.in', {N= n, P= p}) } - - s.set = function(ss, fdf, x0) return ss.lm.set(fdf, x0) end - s.iterate = function(ss) return ss.lm.iterate() end - s.test = function(ss, epsabs, epsrel) return ss.lm.test(epsabs, epsrel) end - - setmetatable(s, NLINFIT) - - return s -end |