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
|
local LM = {}
local var_name = require 'lm-expr'
local factor_mt
local function mul_factor(a, b)
local c = {value= a.value .. ":" .. b.value}
return setmetatable(c, factor_mt)
end
factor_mt = {
__mul = mul_factor,
}
function LM.factor(name)
local t = {name= name, value = ""}
return setmetatable(t, factor_mt)
end
function LM.eval_test(...)
local inf = {np = select('#', ...)}
inf.class = {}
for k= 1, inf.np do
local v = select(k, ...)
inf.class[k] = (type(v) == 'number' and 1 or 0)
end
return inf
end
function LM.eval_func(inf, pt, i, ...)
for k = 1, inf.np do
local x = select(k, ...)
local value = (inf.class[k] == 1 and x or x.value)
gdt.set(pt, i, k, value)
end
end
LM.var_name = var_name
local function expr_to_name(expr)
if type(expr) == 'table' then
return expr.name
else
local base = '(average)'
local minus = expr < 0 and '- ' or ''
if expr == 1 or expr == -1 then
return string.format("%s%s", minus, base)
else
return string.format("%s%s / %g", minus, base, math.abs(expr))
end
end
end
function LM.find_names(...)
local n = select("#", ...)
local names = {}
for k = 1, n do
local expr = select(k, ...)
names[k] = expr_to_name(expr)
end
return names
end
return LM
|