Debian Codegraph @

texmf-dist/tex/lualatex/luacas/test/expressions/luacas-autosimplify.lua

raw
1 local a = BinaryOperation.ADDEXP
2 ({Integer(3),
3 Integer(5)})
4
5 local b = BinaryOperation.MULEXP
6 ({BinaryOperation.ADDEXP
7 ({Integer(13),
8 Integer(12)}),
9 Integer(-4)})
10
11 local c = BinaryOperation.DIVEXP
12 ({SymbolExpression("x"),
13 SymbolExpression("y")})
14
15 local d = BinaryOperation.DIVEXP
16 ({BinaryOperation.ADDEXP
17 ({Integer(4),
18 Integer(-3)}),
19 SymbolExpression("y")})
20
21 local e = BinaryOperation.ADDEXP
22 ({Integer(3),
23 Integer(4),
24 Integer(5),
25 Integer(6)})
26
27 starttest("expression construction")
28 testeq(a, "3 + 5")
29 testeq(b, "(13 + 12) * -4")
30 testeq(c, "x / y")
31 testeq(d, "(4 + -3) / y")
32 testeq(e, "3 + 4 + 5 + 6")
33 endtest()
34
35 starttest("expression evaluation...")
36 testeq(a:evaluate(), dparse("8"))
37 testeq(b:evaluate(), dparse("-100"))
38 testeq(c:evaluate(), dparse("(x / y)"))
39 testeq(d:evaluate(), dparse("(1 / y)"))
40 testeq(e:evaluate(), dparse("18"))
41 endtest()
42
43 local g = BinaryOperation.POWEXP
44 ({Integer(0),
45 SymbolExpression("x")})
46
47 local h = BinaryOperation.POWEXP
48 ({Integer(1),
49 SymbolExpression("x")})
50
51 local i = BinaryOperation.POWEXP
52 ({SymbolExpression("x"),
53 Integer(0)})
54
55 local j = BinaryOperation.POWEXP
56 ({SymbolExpression("x"),
57 Integer(1)})
58
59 local k = BinaryOperation.POWEXP
60 ({SymbolExpression("x"),
61 SymbolExpression("y")})
62
63 local l = BinaryOperation.POWEXP
64 ({BinaryOperation.POWEXP
65 ({BinaryOperation.POWEXP
66 ({SymbolExpression("x"),
67 Integer(3)}),
68 Integer(4)}),
69 Integer(5)})
70
71 local m = BinaryOperation.POWEXP
72 ({BinaryOperation.MULEXP
73 ({SymbolExpression("x"),
74 SymbolExpression("y")}),
75 SymbolExpression("a")})
76
77 local n = BinaryOperation.MULEXP
78 ({SymbolExpression("x"),
79 SymbolExpression("y"),
80 Integer(0),
81 Integer(-2)
82 })
83
84 local o = BinaryOperation.MULEXP
85 ({SymbolExpression("x"),
86 BinaryOperation.MULEXP
87 ({SymbolExpression("y"),
88 SymbolExpression("z")})})
89
90 local p = BinaryOperation.MULEXP
91 ({SymbolExpression("x")})
92
93 local q = BinaryOperation.MULEXP
94 ({SymbolExpression("x"), SymbolExpression("x"), SymbolExpression("x"), SymbolExpression("x")})
95
96 local r = BinaryOperation.MULEXP
97 ({SymbolExpression("x"), Integer(3), SymbolExpression("a")})
98
99 local s = BinaryOperation.ADDEXP
100 ({SymbolExpression("x")})
101
102 local t = BinaryOperation.ADDEXP
103 ({SymbolExpression("x"),
104 BinaryOperation.ADDEXP
105 ({Integer(3),
106 SymbolExpression("y")})})
107
108 local u = BinaryOperation.ADDEXP
109 ({BinaryOperation.MULEXP
110 ({SymbolExpression("x"),
111 SymbolExpression("y")}),
112 BinaryOperation.MULEXP
113 ({SymbolExpression("y"),
114 SymbolExpression("x")})})
115
116 local v = BinaryOperation.ADDEXP
117 ({Integer(3),
118 BinaryOperation.ADDEXP
119 ({BinaryOperation.MULEXP
120 ({Integer(2),
121 BinaryOperation.POWEXP
122 ({SymbolExpression("x"),
123 Integer(2)})}),
124 BinaryOperation.ADDEXP
125 ({BinaryOperation.MULEXP
126 ({Integer(1),
127 SymbolExpression("y")}),
128 BinaryOperation.MULEXP
129 ({Integer(0),
130 SymbolExpression("x")})})}),
131 Integer(6)})
132
133 local w = BinaryOperation.MULEXP
134 ({BinaryOperation.DIVEXP
135 ({Integer(1),
136 SymbolExpression("x")}),
137 SymbolExpression("x")})
138
139 local x = BinaryOperation.MULEXP
140 ({BinaryOperation.DIVEXP
141 ({SymbolExpression("y"),
142 SymbolExpression("x")}),
143 BinaryOperation.DIVEXP
144 ({SymbolExpression("x"),
145 SymbolExpression("y")})})
146
147 local y = BinaryOperation.MULEXP
148 ({BinaryOperation.DIVEXP
149 ({Integer(1),
150 Integer(3)}),
151 SymbolExpression("x")})
152
153 local z = BinaryOperation.ADDEXP
154 ({SymbolExpression("x"),
155 SymbolExpression("y"),
156 BinaryOperation.SUBEXP
157 ({SymbolExpression("x"),
158 SymbolExpression("y")})})
159
160 local A = dparse("(-aa-x)+(x+aa)")
161
162 starttest("expression autosimplification")
163 testeq(g:autosimplify(), parse("0"), g)
164 testeq(h:autosimplify(), parse("1"), h)
165 testeq(i:autosimplify(), parse("1"), i)
166 testeq(j:autosimplify(), parse("x"), j)
167 testeq(k:autosimplify(), parse("x ^ y"), k)
168 testeq(l:autosimplify(), parse("(x ^ 60)"), l)
169 testeq(m:autosimplify(), parse("((x * y) ^ a)"), m)
170 testeq(n:autosimplify(), parse("0"), n)
171 testeq(o:autosimplify(), parse("(x * y * z)"), o)
172 testeq(p:autosimplify(), parse("x"), p)
173 testeq(q:autosimplify(), parse("(x ^ 4)"), q)
174 testeq(r:autosimplify(), parse("(3 * a * x)"), r)
175 testeq(s:autosimplify(), parse("x"), s)
176 testeq(t:autosimplify(), parse("(3 + x + y)"), t)
177 testeq(u:autosimplify(), parse("(2 * x * y)"), u)
178 testeq(v:autosimplify(), parse("(9 + (2 * (x ^ 2)) + y)"), v)
179 testeq(w:autosimplify(), parse("1"), w)
180 testeq(x:autosimplify(), parse("1"), x)
181 testeq(y:autosimplify(), parse("(1/3 * x)"), y)
182 testeq(z:autosimplify(), parse("(2 * x)"), z)
183 testeq(A:autosimplify(), parse("0"), A)
184 endtest()
185
186
187 local aa = SymbolExpression("x") + SymbolExpression("y") + SymbolExpression("z")
188 local ab = -(SymbolExpression("x") / SymbolExpression("y"))
189 local ac = Integer(2)*SymbolExpression("x")*SymbolExpression("y") - Integer(3)*SymbolExpression("x")*SymbolExpression("z")
190
191 starttest("metamethod expressions")
192
193 testeq(aa, dparse("(x + y) + z"))
194 testeq(aa:autosimplify(), parse("(x + y + z)"), aa)
195 testeq(ab, dparse("- (x / y)"))
196 testeq(ab:autosimplify(), parse("(-1 * x * (y ^ -1))"), ab)
197 testeq(ac:autosimplify(), parse("((2 * x * y) + (-3 * x * z))"), ac)
198
199 endtest()