Debian Codegraph @

texmf-dist/tex/lualatex/luacas/test/luacas-helper.lua

raw
1 -- helper functions
2
3
4 function whatis(a)
5 if a == nil then
6 return nil
7 end
8 if a:type() == SymbolExpression then
9 return "Sym"
10 end
11 if a:type() == BinaryOperation then
12 return "BinOp"
13 end
14 if a:type() == FunctionExpression then
15 return "FncExp"
16 end
17 if a:type() == TrigExpression then
18 return "TrigExp"
19 end
20 if a:type() == Integer then
21 return "Int"
22 end
23 if a:type() == Rational then
24 return "Ratl"
25 end
26 if a:type() == DerivativeExpression then
27 return "DervExp"
28 end
29 if a:type() == DiffExpression then
30 return "DiffExp"
31 end
32 if a:type() == IntegralExpression then
33 return "Intgrl"
34 end
35 if a:type() == SqrtExpression then
36 return "Sqrt"
37 end
38 if a:type() == PolynomialRing then
39 return "Poly"
40 end
41 if a:type() == AbsExpression then
42 return "ABS"
43 end
44 if a:type() == Logarithm then
45 return "LOG"
46 end
47 if a:type() == RootExpression then
48 return "RootOf"
49 end
50 if a:type() == Equation then
51 return "="
52 end
53 return "No Clue"
54 end
55
56 function longwhatis(a)
57 if a == nil then
58 return nil
59 end
60 if a:type() == SymbolExpression then
61 return "SymbolExpression"
62 end
63 if a:type() == BinaryOperation then
64 return "BinaryOperation"
65 end
66 if a:type() == FunctionExpression then
67 return "FunctionExpression"
68 end
69 if a:type() == TrigExpression then
70 return "TrigExpression"
71 end
72 if a:type() == Integer then
73 return "Integer"
74 end
75 if a:type() == Rational then
76 return "Rational"
77 end
78 if a:type() == DerivativeExpression then
79 return "DerivativeExpression"
80 end
81 if a:type() == DiffExpression then
82 return "DiffExpression"
83 end
84 if a:type() == IntegralExpression then
85 return "IntegralExpression"
86 end
87 if a:type() == SqrtExpression then
88 return "SqrtExpression"
89 end
90 if a:type() == PolynomialRing then
91 return "PolynomialRing"
92 end
93 if a:type() == AbsExpression then
94 return "AbsExpression"
95 end
96 if a:type() == Logarithm then
97 return "Logarithm"
98 end
99 if a:type() == RootExpression then
100 return "RootExpression"
101 end
102 if a:type() == Equation then
103 return "Equation"
104 end
105 return "No Clue"
106 end
107
108 function whatring(a)
109 if a:getring() == Rational.makering() then
110 return "Rational"
111 end
112 if a:getring() == PolynomialRing.makering() then
113 return "PolynomialRing"
114 end
115 if a:getring() == Integer.makering() then
116 return "Integer"
117 end
118 if a:getring() == IntegerModN.makering() then
119 return "IntegerModN"
120 end
121 return "No Clue"
122 end
123
124 function nameof(sym)
125 if sym == nil then
126 return nil
127 end
128 if sym:type() == BinaryOperation then
129 local binops = {BinaryOperation.ADD,
130 BinaryOperation.MUL,
131 BinaryOperation.SUB,
132 BinaryOperation.DIV,
133 BinaryOperation.POW,
134 BinaryOperation.IDIV,
135 BinaryOperation.MOD}
136 local obslab = {"ADD",
137 "MUL",
138 "SUB",
139 "DIV",
140 "POW",
141 "IDIV",
142 "MOD"}
143 for i,j in pairs(binops) do
144 if sym.operation == j then
145 return obslab[i]
146 end
147 end
148 end
149 if sym:type() == FunctionExpression or sym:type() == TrigExpression then
150 return tostring(sym.name)
151 end
152 if sym:type() == SymbolExpression or sym:type() == Integer then
153 return tostring(sym)
154 end
155 if sym:type() == Rational then
156 return tostring(sym.numerator).."/"..tostring(sym.denominator)
157 end
158 if sym:type() == DerivativeExpression then
159 return "DD"
160 end
161 if sym:type() == DiffExpression then
162 return "diff"
163 end
164 if sym:type() == IntegralExpression then
165 return "$\\mathtt{\\int}$"
166 end
167 if sym:type() == SqrtExpression then
168 return "$\\mathtt{\\sqrt{\\phantom{x}}}$"
169 end
170 if sym:type() == PolynomialRing then
171 return "Poly"
172 end
173 if sym:type() == AbsExpression then
174 return "abs"
175 end
176 if sym:type() == Logarithm then
177 return "log"
178 end
179 if sym:type() == RootExpression then
180 return "RootOf"
181 end
182 if sym:type() == Equation then
183 return "$\\mathtt{=}$"
184 end
185 return "No Clue"
186 end
187
188 function Expression:getfullsubexpressionsrec()
189 local result = {}
190 for _, expression in ipairs(self:subexpressions()) do
191 result[#result+1] = expression
192 result = JoinArrays(result, expression:getfullsubexpressionsrec())
193 end
194 return result
195 end
196
197 function Expression:gettheshrub()
198 local string = ""
199 for index, expression in ipairs(self:subexpressions()) do
200 string = string.."child {node [label=-90:{expr["..tostring(index).."]}] {$\\mathtt{"..expression:tolatex().."}$}}"
201 end
202 return string
203 end
204
205 function Expression:getthetree()
206 local string = ""
207 for _, expression in ipairs(self:subexpressions()) do
208 if expression:isatomic() then
209 string = string.."child {node{"..nameof(expression).."}}"
210 else
211 string = string.."child {node{"..nameof(expression).."}"..expression:getthetree().."}"
212 end
213 end
214 return string
215 end
216
217 function Expression:gettheforest()
218 local string = ""
219 for _, expression in ipairs(self:subexpressions()) do
220 if expression:isatomic() then
221 string = string.." [ "..nameof(expression).." ] "
222 else
223 string = string.." [ "..nameof(expression)..expression:gettheforest().." ] "
224 end
225 end
226 return string
227 end
228
229 function Expression:getthefancyshrub()
230 local string = ""
231 if self:type() == DiffExpression then
232 for _, expression in ipairs(self:subexpressions()) do
233 string = string.." [ $\\mathtt{"..expression:tolatex().."}$, tikz+={\\node[anchor=north,font=\\ttfamily\\footnotesize,gray] at (.south) {.expression};} ] "
234 end
235 string = string.." [ $\\mathtt{\\{"
236 for _,symbol in ipairs(self.symbols) do
237 if next(self.symbols,_) == nil then
238 string = string .. symbol:tolatex().."\\}}$, tikz+={\\node[anchor=north,font=\\ttfamily\\footnotesize,gray] at (.south) {.symbols};} ] "
239 else
240 string = string .. symbol:tolatex() .. ","
241 end
242 end
243 return string
244 end
245 if self:type() == IntegralExpression then
246 string = string .. " [ $\\mathtt{"..self.expression:tolatex().."}$, tikz+={\\node[anchor=north,font=\\ttfamily\\footnotesize,gray] at (.south) {.expression};} ] "
247 string = string .. "[ $\\mathtt{"..self.symbol:tolatex().."}$, tikz+={\\node[anchor=north,font=\\ttfamily\\footnotesize,gray] at (.south) {.symbol};} ]"
248 if self:isdefinite() then
249 string = string .. "[ $\\mathtt{"..self.lower:tolatex().."}$, tikz+={\\node[anchor=north,font=\\ttfamily\\footnotesize,gray] at (.south) {.lower};} ] "
250 string = string .. "[ $\\mathtt{"..self.upper:tolatex().."}$, tikz+={\\node[anchor=north,font=\\ttfamily\\footnotesize,gray] at (.south) {.upper};} ] "
251 return string
252 end
253 return string
254 end
255 if self:type() == PolynomialRing then
256 string = string .. " [ $\\mathtt{\\{"
257 for index=0, self.degree:asnumber() do
258 string = string .. tostring(self.coefficients[index])
259 if index < self.degree:asnumber() then
260 string = string .. ","
261 end
262 end
263 string = string .. "\\} }$, tikz+={\\node[anchor=north,font=\\ttfamily\\footnotesize,gray] at (.south) {.coefficients}; \\node[anchor=south west, font=\\ttfamily\\footnotesize,gray] at (.north west) {.ring "..whatring(self).."};} ]"
264 string = string .. " [ $\\mathtt{"..self.symbol.. "}$, tikz+={\\node[anchor=north, font=\\ttfamily\\footnotesize,gray] at (.south) {.symbol};} ]"
265 return string
266 end
267 if self:type() == SqrtExpression then
268 string = string .. " [ $\\mathtt{"..self.expression:tolatex().."}$, tikz+={\\node[anchor=north,font=\\ttfamily\\footnotesize,gray] at (.south) {.expression};} ]"
269 string = string .. "[ $\\mathtt{"..self.root:tolatex().."}$, tikz+={\\node[anchor=north,font=\\ttfamily\\footnotesize,gray] at (.south) {.root};} ]"
270 return string
271 end
272 if self:type() == TrigExpression then
273 string = string .. " [ $\\mathtt{"..self.expression:tolatex().."}$, tikz+={\\node[anchor=north,font=\\ttfamily\\footnotesize,gray] at (.south) {.expression};} ]"
274 return string
275 end
276 if self:type() == AbsExpression then
277 string = string .. " [ $\\mathtt{"..self.expression:tolatex().."}$, tikz+={\\node[anchor=north,font=\\ttfamily\\footnotesize,gray] at (.south) {.expression};} ] "
278 return string
279 end
280 if self:type() == Logarithm then
281 string = string .. " [$\\mathtt{" ..self.expression:tolatex().."}$, tikz+={\\node[anchor=north,font=\\ttfamily\\footnotesize,gray] at (.south) {.expression};} ]"
282 string = string .. " [$\\mathtt{" ..self.base:tolatex().."}$, tikz+={\\node[anchor=north,font=\\ttfamily\\footnotesize,gray] at (.south) {.base};} ]"
283 return string
284 end
285 if self:type() == RootExpression then
286 string = string .. "[$\\mathtt{" ..self.expression:tolatex().."}$, tikz+={\\node[anchor=north,font=\\ttfamily\\footnotesize,gray] at (.south) {.expression};} ]"
287 return string
288 end
289 if self:type() == FunctionExpression then
290 local string1 = ''
291 local string2 = ''
292 local string3 = ''
293 for index=1, #self.variables do
294 string1 = string1 .. tostring(self.expressions[index])
295 if index < #self.variables then
296 string1 = string1 .. ","
297 end
298 string2 = string2 .. tostring(self.variables[index])
299 if index < #self.variables then
300 string2 = string2 .. ","
301 end
302 string3 = string3 .. tostring(self.derivatives[index])
303 if index < #self.variables then
304 string3 = string3 .. ","
305 end
306 end
307 string = string .. "[$\\mathtt{ \\{" .. string1 .. "\\}}$, tikz+={\\node[anchor=north,font=\\ttfamily\\footnotesize,gray] at (.south) {.expressions};} ]"
308 string = string .. "[$\\mathtt{ \\{" .. string2 .. "\\}}$, tikz+={\\node[anchor=north,font=\\ttfamily\\footnotesize,gray] at (.south) {.variables};} ]"
309 string = string .. "[$\\mathtt{ \\{" .. string3 .. "\\}}$, tikz+={\\node[anchor=north,font=\\ttfamily\\footnotesize,gray] at (.south) {.derivatives};} ]"
310 return string
311 end
312 if self:type() == Equation then
313 string = string .. " [$\\mathtt{" ..self.lhs:tolatex().."}$, tikz+={\\node[anchor=north,font=\\ttfamily\\footnotesize,gray] at (.south) {.lhs};} ]"
314 string = string .. " [$\\mathtt{" ..self.rhs:tolatex().."}$, tikz+={\\node[anchor=north,font=\\ttfamily\\footnotesize,gray] at (.south) {.rhs};} ]"
315 return string
316 end
317 for index, expression in ipairs(self:subexpressions()) do
318 string = string.." [ $\\mathtt{"..expression:tolatex().."}$, tikz+={\\node[anchor=north,font=\\ttfamily\\footnotesize,gray] at (.south) {.expression["..index.."]};} ] "
319 end
320 return string
321 end
322