summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/luacas/tex/test/expressions/autosimplify.lua
blob: 09ad9874cd592c7c511ff157dae57df45a97159b (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
local a = BinaryOperation.ADDEXP
                ({Integer(3),
                Integer(5)})

local b = BinaryOperation.MULEXP
                ({BinaryOperation.ADDEXP
                    ({Integer(13),
                    Integer(12)}),
                Integer(-4)})

local c = BinaryOperation.DIVEXP
                ({SymbolExpression("x"),
                SymbolExpression("y")})

local d = BinaryOperation.DIVEXP
            ({BinaryOperation.ADDEXP
                ({Integer(4),
                Integer(-3)}),
            SymbolExpression("y")})

local e = BinaryOperation.ADDEXP
                            ({Integer(3),
                            Integer(4),
                            Integer(5),
                            Integer(6)})

starttest("expression construction")
testeq(a, "3 + 5")
testeq(b, "(13 + 12) * -4")
testeq(c, "x / y")
testeq(d, "(4 + -3) / y")
testeq(e, "3 + 4 + 5 + 6")
endtest()

starttest("expression evaluation...")
testeq(a:evaluate(), dparse("8"))
testeq(b:evaluate(), dparse("-100"))
testeq(c:evaluate(), dparse("(x / y)"))
testeq(d:evaluate(), dparse("(1 / y)"))
testeq(e:evaluate(), dparse("18"))
endtest()

local g = BinaryOperation.POWEXP
            ({Integer(0),
            SymbolExpression("x")})

local h = BinaryOperation.POWEXP
            ({Integer(1),
            SymbolExpression("x")})

local i = BinaryOperation.POWEXP
            ({SymbolExpression("x"),
            Integer(0)})

local j = BinaryOperation.POWEXP
            ({SymbolExpression("x"),
            Integer(1)})

local k = BinaryOperation.POWEXP
            ({SymbolExpression("x"),
            SymbolExpression("y")})

local l = BinaryOperation.POWEXP
            ({BinaryOperation.POWEXP
                ({BinaryOperation.POWEXP
                    ({SymbolExpression("x"),
                    Integer(3)}),
                Integer(4)}),
            Integer(5)})

local m = BinaryOperation.POWEXP
            ({BinaryOperation.MULEXP
                ({SymbolExpression("x"),
                SymbolExpression("y")}),
            SymbolExpression("a")})

            local n = BinaryOperation.MULEXP
            ({SymbolExpression("x"),
            SymbolExpression("y"),
            Integer(0),
            Integer(-2)
            })

local o = BinaryOperation.MULEXP
            ({SymbolExpression("x"),
            BinaryOperation.MULEXP
                ({SymbolExpression("y"),
                SymbolExpression("z")})})

local p = BinaryOperation.MULEXP
            ({SymbolExpression("x")})

local q = BinaryOperation.MULEXP
            ({SymbolExpression("x"), SymbolExpression("x"), SymbolExpression("x"), SymbolExpression("x")})

local r = BinaryOperation.MULEXP
            ({SymbolExpression("x"), Integer(3), SymbolExpression("a")})

            local s = BinaryOperation.ADDEXP
            ({SymbolExpression("x")})

local t = BinaryOperation.ADDEXP
            ({SymbolExpression("x"),
                BinaryOperation.ADDEXP
                    ({Integer(3),
                    SymbolExpression("y")})})

local u = BinaryOperation.ADDEXP
        ({BinaryOperation.MULEXP
            ({SymbolExpression("x"),
            SymbolExpression("y")}),
        BinaryOperation.MULEXP
            ({SymbolExpression("y"),
                SymbolExpression("x")})})

local v = BinaryOperation.ADDEXP
            ({Integer(3),
            BinaryOperation.ADDEXP
                ({BinaryOperation.MULEXP
                    ({Integer(2),
                    BinaryOperation.POWEXP
                        ({SymbolExpression("x"),
                        Integer(2)})}),
                BinaryOperation.ADDEXP
                    ({BinaryOperation.MULEXP
                        ({Integer(1),
                        SymbolExpression("y")}),
                    BinaryOperation.MULEXP
                        ({Integer(0),
                        SymbolExpression("x")})})}),
            Integer(6)})

            local w = BinaryOperation.MULEXP
            ({BinaryOperation.DIVEXP
                ({Integer(1),
                SymbolExpression("x")}),
            SymbolExpression("x")})

local x = BinaryOperation.MULEXP
            ({BinaryOperation.DIVEXP
                ({SymbolExpression("y"),
                SymbolExpression("x")}),
            BinaryOperation.DIVEXP
                ({SymbolExpression("x"),
                SymbolExpression("y")})})

local y = BinaryOperation.MULEXP
    ({BinaryOperation.DIVEXP
        ({Integer(1),
        Integer(3)}),
    SymbolExpression("x")})

local z = BinaryOperation.ADDEXP
    ({SymbolExpression("x"),
    SymbolExpression("y"),
    BinaryOperation.SUBEXP
        ({SymbolExpression("x"),
        SymbolExpression("y")})})

local A = dparse("(-aa-x)+(x+aa)")

starttest("expression autosimplification")
testeq(g:autosimplify(), parse("0"), g)
testeq(h:autosimplify(), parse("1"), h)
testeq(i:autosimplify(), parse("1"), i)
testeq(j:autosimplify(), parse("x"), j)
testeq(k:autosimplify(), parse("x ^ y"), k)
testeq(l:autosimplify(), parse("(x ^ 60)"), l)
testeq(m:autosimplify(), parse("((x * y) ^ a)"), m)
testeq(n:autosimplify(), parse("0"), n)
testeq(o:autosimplify(), parse("(x * y * z)"), o)
testeq(p:autosimplify(), parse("x"), p)
testeq(q:autosimplify(), parse("(x ^ 4)"), q)
testeq(r:autosimplify(), parse("(3 * a * x)"), r)
testeq(s:autosimplify(), parse("x"), s)
testeq(t:autosimplify(), parse("(3 + x + y)"), t)
testeq(u:autosimplify(), parse("(2 * x * y)"), u)
testeq(v:autosimplify(), parse("(9 + (2 * (x ^ 2)) + y)"), v)
testeq(w:autosimplify(), parse("1"), w)
testeq(x:autosimplify(), parse("1"), x)
testeq(y:autosimplify(), parse("(1/3 * x)"), y)
testeq(z:autosimplify(), parse("(2 * x)"), z)
testeq(A:autosimplify(), parse("0"), A)
endtest()


local aa = SymbolExpression("x") + SymbolExpression("y") + SymbolExpression("z")
local ab = -(SymbolExpression("x") / SymbolExpression("y"))
local ac = Integer(2)*SymbolExpression("x")*SymbolExpression("y") - Integer(3)*SymbolExpression("x")*SymbolExpression("z")

starttest("metamethod expressions")

testeq(aa, dparse("(x + y) + z"))
testeq(aa:autosimplify(), parse("(x + y + z)"), aa)
testeq(ab, dparse("- (x / y)"))
testeq(ab:autosimplify(), parse("(-1 * x * (y ^ -1))"), ab)
testeq(ac:autosimplify(), parse("((2 * x * y) + (-3 * x * z))"), ac)

endtest()