-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy patheval.lua
More file actions
25 lines (22 loc) · 541 Bytes
/
eval.lua
File metadata and controls
25 lines (22 loc) · 541 Bytes
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
--[[
numerically evaluate the expression
vars = {
{[variable/name] = [value/constant]},
...
}
--]]
return function(expr, vars)
local symmath = require 'symmath'
if vars then
for k,v in pairs(vars) do
if type(k) == 'string' then k = symmath.var(k) end
-- clone so we can handle numbers and variable constants
expr = expr:replace(k, symmath.clone(v))
end
end
-- final simplify in case a variable constant was substituted,
-- and we have a simplify() rule for it
expr = expr()
local f = expr:compile()
return f()
end