1 (load(pytranslate), 'done);
6 (temp_vars_map : ?\*maxima\-variables\-dictionary\-name\*,
7 ?\*maxima\-variables\-dictionary\-name\* : "m_vars",
8 temp_funcs_map : ?\*maxima\-function\-dictionary\-name\*,
9 ?\*maxima\-function\-dictionary\-name\* : "m_funcs",
13 (preprocess(form) := block([ret],
14 ret : regex_subst("m_vars",
15 ?\*maxima\-variables\-dictionary\-name\*,
17 ret: regex_subst("m_funcs",
18 ?\*maxima\-function\-dictionary\-name\*, ret),
19 ret: regex_subst("block_random(",
22 ret: regex_subst("\"lambda_random\"",
25 ret: regex_subst("lambda_random(",
28 ret: regex_subst(" lambda_random ",
31 ret: regex_subst(" lambda_random",
34 ret: regex_subst("block_random(",
64 /* Arithmetic operators */
65 preprocess(pytranslate(a + b));
67 (m_vars[\"b\"] + m_vars[\"a\"])"$
68 preprocess(pytranslate(a*b));
70 (m_vars[\"a\"] * m_vars[\"b\"])"$
71 preprocess(pytranslate(a/b));
73 (m_vars[\"a\"] / m_vars[\"b\"])"$
74 preprocess(pytranslate(a-b));
76 (m_vars[\"a\"] + (-m_vars[\"b\"]))"$
77 preprocess(pytranslate(a^b));
79 m_funcs[\"pow\"](m_vars[\"a\"], m_vars[\"b\"])"$
80 preprocess(pytranslate(a!));
82 m_funcs[\"factorial\"](m_vars[\"a\"])"$
84 /* Boolean Operators */
85 preprocess(pytranslate(a or b));
87 (m_vars[\"a\"] or m_vars[\"b\"])"$
88 preprocess(pytranslate(a and b));
90 (m_vars[\"a\"] and m_vars[\"b\"])"$
91 preprocess(pytranslate(not(a)));
95 /* Relational Operators */
96 preprocess(pytranslate(a >= b));
98 (m_vars[\"a\"] >= m_vars[\"b\"])"$
99 preprocess(pytranslate(a <= b));
101 (m_vars[\"a\"] <= m_vars[\"b\"])"$
102 preprocess(pytranslate(a > b));
104 (m_vars[\"a\"] > m_vars[\"b\"])"$
105 preprocess(pytranslate(a < b));
107 (m_vars[\"a\"] < m_vars[\"b\"])"$
108 preprocess(pytranslate(a # b));
110 (m_vars[\"a\"] != m_vars[\"b\"])"$
111 preprocess(pytranslate(a = b));
113 (m_vars[\"a\"] == m_vars[\"b\"])"$
116 preprocess(pytranslate([a,b,c]));
118 [m_vars[\"a\"], m_vars[\"b\"], m_vars[\"c\"]]"$
121 preprocess(pytranslate('(a:b)));
123 m_vars[\"a\"] = m_vars[\"b\"]"$
124 preprocess(pytranslate('([a,b,c]:[1,2,3])));
126 [m_vars[\"a\"], m_vars[\"b\"], m_vars[\"c\"]] = [1, 2, 3]"$
128 (kill(allbut(preprocess, temp_vars_map, temp_funcs_map)), 'done);
131 /* Function Definition */
132 preprocess(pytranslate(f(x):=x^2));
135 def f(x, m_vars = m_vars):
136 m_vars = Stack({}, m_vars)
137 m_vars.ins({\"x\" : x})
138 return(m_funcs[\"pow\"](m_vars[\"x\"], 2))
140 preprocess(pytranslate(f(x, [y]):=[x, y]));
143 def f(x, *y, m_vars = m_vars):
144 m_vars = Stack({}, m_vars)
145 m_vars.ins({\"x\" : x, \"y\" : list(y)})
146 return([m_vars[\"x\"], m_vars[\"y\"]])
149 (kill(allbut(preprocess, temp_vars_map, temp_funcs_map)), 'done);
152 /* Array-definition */
153 preprocess(pytranslate('array(abc, 1, 2, 3)));
155 m_vars[\"abc\"] = ([([([None] * 3)] * 2)] * 1)"$
156 preprocess(pytranslate('array(abc, fixnum, 1, 2, 3)));
158 m_vars[\"abc\"] = ([([([None] * 3)] * 2)] * 1)"$
159 preprocess(pytranslate('array([abc,def,tre], 1, 2, 3)));
162 m_vars[\"abc\"] = ([([([None] * 3)] * 2)] * 1)
163 m_vars[\"def\"] = ([([([None] * 3)] * 2)] * 1)
164 m_vars[\"tre\"] = ([([([None] * 3)] * 2)] * 1)"$
166 /* Array reference */
167 preprocess(pytranslate('(abc[1,2,3,4])));
169 m_vars[\"abc\"][0][1][2][3]"$
171 (kill(allbut(preprocess, temp_vars_map, temp_funcs_map)), 'done);
174 /* Block statements */
175 /* Regex substitution is used to catch the random names generated by gensym and replace them with func_random */
176 preprocess(pytranslate(f(x):=block(a:10, b:x*20, x+a+b)));
179 def f(x, m_vars = m_vars):
180 m_vars = Stack({}, m_vars)
181 m_vars.ins({\"x\" : x})
184 m_vars[\"b\"] = (m_vars[\"x\"] * 20)
185 return((m_vars[\"x\"] + m_vars[\"a\"] + m_vars[\"b\"]))
188 (?\*symbols\-directly\-convert\*:[], 'done);
190 preprocess(pytranslate('(block(d : block(c : block(e : 20, f : e + 10, f), c : c + 1 + 2 + 3, c), d))));
192 def block_random(m_vars):
193 m_vars = Stack({}, m_vars)
195 m_vars[\"d\"] = block_random(Stack({}, m_vars))
196 return(m_vars[\"d\"])
197 def block_random(m_vars):
198 m_vars = Stack({}, m_vars)
200 m_vars[\"c\"] = block_random(Stack({}, m_vars))
201 m_vars[\"c\"] = (6 + m_vars[\"c\"])
202 return(m_vars[\"c\"])
203 def block_random(m_vars):
204 m_vars = Stack({}, m_vars)
207 m_vars[\"f\"] = (10 + m_vars[\"e\"])
208 return(m_vars[\"f\"])
209 block_random(Stack({}, m_vars))"$
213 preprocess(pytranslate('(if 1>2 then z:10)));
218 preprocess(pytranslate('(if 1>2 then z:10 else z:30)));
225 preprocess(pytranslate('(if 1>2 then z:10 elseif 1<2 then z:30 else z:40)));
234 pytranslate('(f(x):=if x>2 then 10 elseif x<2 then 30 else 40));
237 def f(x, m_vars = m_vars):
238 m_vars = Stack({}, m_vars)
239 m_vars.ins({\"x\" : x})
240 return((10 if (m_vars[\"x\"] > 2) else (30 if (m_vars[\"x\"] < 2) else 40)))
243 (?\*symbols\-directly\-convert\*:[], 'done);
245 (kill(allbut(preprocess, temp_vars_map, temp_funcs_map)), 'done);
249 pytranslate(lambda([a,b], a+b));
251 lambda a, b, m_vars = Stack({}, m_vars): (a + b)"$
252 pytranslate('(xx:lambda([a,b], a+b)));
254 m_vars[\"xx\"] = lambda a, b, m_vars = Stack({}, m_vars): (a + b)"$
255 preprocess(pytranslate('(lmb:lambda([a,b,c], d:a+b, e:b^c, d+e))));
258 def lambda_random(a, b, c, m_vars = m_vars):
259 m_vars = Stack({}, m_vars)
260 m_vars.ins({\"a\" : a, \"b\" : b, \"c\" : c})
261 m_vars[\"d\"] = (a + b)
262 m_vars[\"e\"] = m_funcs[\"pow\"](b, c)
263 return((m_vars[\"d\"] + m_vars[\"e\"]))
264 m_funcs[\"lambda_random\"] = lambda_random
265 m_vars[\"lmb\"] = lambda a, b, c, m_vars = Stack({}, m_vars): lambda_random(a, b, c, Stack({}, m_vars))"$
267 (kill(allbut(preprocess, temp_vars_map, temp_funcs_map)), 'done);
272 pytranslate('(for i in [1,2,3] do print(i)));
274 for m_vars[\"i\"] in [1, 2, 3]:
275 m_funcs[\"print\"](m_vars[\"i\"])"$
277 pytranslate('(for i in [1,2,3] do (print(i), print(i*2))));
279 for m_vars[\"i\"] in [1, 2, 3]:
280 m_funcs[\"print\"](m_vars[\"i\"])
281 m_funcs[\"print\"]((2 * m_vars[\"i\"]))"$
283 /* Initial, increment and limit values specified */
284 pytranslate('(for i from 1 step 3 thru 5 do print(i)));
286 for m_vars[\"i\"] in range(1, 6, 3):
287 m_funcs[\"print\"](m_vars[\"i\"])"$
288 pytranslate('(for i from 1 step 3 thru 5 do (print(i), a:20+i, print(i*a))));
290 for m_vars[\"i\"] in range(1, 6, 3):
291 m_funcs[\"print\"](m_vars[\"i\"])
292 m_vars[\"a\"] = (20 + m_vars[\"i\"])
293 m_funcs[\"print\"]((m_vars[\"a\"] * m_vars[\"i\"]))"$
295 /* Initial, increment and condition specified */
296 /* while condition */
297 pytranslate('(for i:10 step 5 while i*5<70 do (print(i), print(5*i))));
301 while not(((5 * m_vars[\"i\"]) >= 70)):
302 m_funcs[\"print\"](m_vars[\"i\"])
303 m_funcs[\"print\"]((5 * m_vars[\"i\"]))
304 m_vars[\"i\"] = (m_vars[\"i\"] + 5)
306 /* unless condition */
307 pytranslate('(for i:8 step -1 unless i<3 do (print(i))));
311 while not((m_vars[\"i\"] < 3)):
312 m_funcs[\"print\"](m_vars[\"i\"])
313 m_vars[\"i\"] = (m_vars[\"i\"] + -1)
315 pytranslate('(for i:8 step -1 unless i<3 do print(i)));
319 while not((m_vars[\"i\"] < 3)):
320 m_funcs[\"print\"](m_vars[\"i\"])
321 m_vars[\"i\"] = (m_vars[\"i\"] + -1)
327 math.floor(m_vars[\"a\"])"$
328 pytranslate('fix(a));
330 math.floor(m_vars[\"a\"])"$
331 pytranslate(sqrt(a));
333 math.sqrt(m_vars[\"a\"])"$
339 preprocess(pytranslate('endcons(a,b)));
341 [*m_vars[\"b\"], m_vars[\"a\"]]"$
342 pytranslate(endcons(a, f(b,c)));
344 m_funcs[\"f\"](m_vars[\"b\"], m_vars[\"c\"], m_vars[\"a\"])"$
345 pytranslate(endcons(a, [d,b,c]));
347 [m_vars[\"d\"], m_vars[\"b\"], m_vars[\"c\"], m_vars[\"a\"]]"$
349 /* Multiple Indentations */
350 preprocess( pytranslate('(for i:5 step 0 while i>=1 do
351 ((for j from i step 0 while j>=1 do
352 ((for k from j step 0 while k>=1 do
360 while not((m_vars[\"i\"] < 1)):
362 m_vars[\"j\"] = m_vars[\"i\"]
363 while not((m_vars[\"j\"] < 1)):
365 m_vars[\"k\"] = m_vars[\"j\"]
366 while not((m_vars[\"k\"] < 1)):
367 m_funcs[\"print\"](m_vars[\"k\"], m_vars[\"j\"], m_vars[\"i\"])
368 m_vars[\"k\"] = (-1 + m_vars[\"k\"])
369 m_vars[\"k\"] = (m_vars[\"k\"] + 0)
371 m_vars[\"j\"] = (-1 + m_vars[\"j\"])
372 m_vars[\"j\"] = (m_vars[\"j\"] + 0)
374 m_vars[\"i\"] = (-1 + m_vars[\"i\"])
375 m_vars[\"i\"] = (m_vars[\"i\"] + 0)
380 /* Continued Fractions */
381 /* cf1 produces continued fraction as a list for any number */
382 preprocess(pytranslate(cf1(x):=block ([ q:[], m: fix((x)), _rem:0, _num, _den, _prec: sqrt(ratepsilon) ],
386 while k>0 and _rem>ratepsilon do (
387 print(float(_rem/_prec)),
392 _rem: (_den/_num - m),
399 def cf1(x, m_vars = m_vars):
400 m_vars = Stack({}, m_vars)
401 m_vars.ins({\"x\" : x})
402 m_vars.ins({\"q\" : [], \"m\" : math.floor(m_vars[\"x\"]), \"_rem\" : 0, \"_num\" : None, \"_den\" : None, \"_prec\" : math.sqrt(m_vars[\"ratepsilon\"])})
403 m_vars[\"_rem\"] = (m_vars[\"x\"] + (-m_vars[\"m\"]))
404 m_vars[\"k\"] = m_vars[\"fpprec\"]
405 m_vars[\"q\"] = [m_vars[\"m\"]]
407 while ((m_vars[\"k\"] > 0) and (m_vars[\"_rem\"] > m_vars[\"ratepsilon\"])):
408 m_funcs[\"print\"](m_funcs[\"float\"]((m_vars[\"_rem\"] / m_vars[\"_prec\"])))
409 m_vars[\"_num\"] = m_funcs[\"num\"](m_vars[\"_rem\"])
410 m_vars[\"_den\"] = m_funcs[\"denom\"](m_vars[\"_rem\"])
411 m_vars[\"m\"] = math.floor((m_vars[\"_den\"] / m_vars[\"_num\"]))
412 m_vars[\"q\"] = [*m_vars[\"q\"], m_vars[\"m\"]]
413 m_vars[\"_rem\"] = ((m_vars[\"_den\"] / m_vars[\"_num\"]) + (-m_vars[\"m\"]))
414 m_vars[\"k\"] = (m_vars[\"k\"] + (-1))
415 return(m_vars[\"q\"])
416 m_funcs[\"cf1\"] = cf1"$
418 /* cfeval works on converting a continuous fraction list back to a number */
419 preprocess(pytranslate(
420 cfeval(_a, [len]):=block ([ z:0, m:0, n:0, p1:1, p2:0, q1:0, q2:1 ] ,
425 if not numberp(len) then error("illegal argument: ", len, " nan ")
427 if _a[1]#0 then z:_a[1],
428 for i:2 thru len do (
437 def block_random(m_vars):
438 m_vars = Stack({}, m_vars)
439 m_vars[\"len\"] = m_funcs[\"first\"](m_vars[\"len\"])
440 return((m_funcs[\"error\"](\"illegal argument: \", m_vars[\"len\"], \" nan \") if not(m_funcs[\"numberp\"](m_vars[\"len\"])) else None))
442 def cfeval(_a, *len, m_vars = m_vars):
443 m_vars = Stack({}, m_vars)
444 m_vars.ins({\"_a\" : _a, \"len\" : list(len)})
445 m_vars.ins({\"z\" : 0, \"m\" : 0, \"n\" : 0, \"p1\" : 1, \"p2\" : 0, \"q1\" : 0, \"q2\" : 1})
447 if m_funcs[\"listp\"](m_vars[\"_a\"]):
448 m_vars[\"len\"] = m_funcs[\"length\"](m_vars[\"_a\"])
452 if (m_vars[\"_a\"][0] != 0):
453 m_vars[\"z\"] = m_vars[\"_a\"][0]
454 for m_vars[\"i\"] in range(2, (m_vars[\"len\"] + 1)):
455 m_vars[\"m\"] = m_vars[\"p2\"]
456 m_vars[\"n\"] = m_vars[\"q2\"]
457 m_vars[\"p2\"] = ((m_vars[\"_a\"][(m_vars[\"i\"] + -1)] * m_vars[\"p2\"]) + m_vars[\"p1\"])
458 m_vars[\"q2\"] = ((m_vars[\"_a\"][(m_vars[\"i\"] + -1)] * m_vars[\"q2\"]) + m_vars[\"q1\"])
459 m_vars[\"p1\"] = m_vars[\"m\"]
460 m_vars[\"q1\"] = m_vars[\"n\"]
461 return((m_vars[\"z\"] + (m_vars[\"p2\"] / m_vars[\"q2\"])))
462 m_funcs[\"cfeval\"] = cfeval"$
464 /* Cantor's Function - Calculates the value of Cantor's function at x in n iterations */
465 preprocess(pytranslate(cantorr2(x, n):=block([ret:0, k:0],
466 if not numberp(x) then
467 return('cantorr2(x, n)),
468 if x =0 then return(0),
469 if x =1 then return(1),
472 if x>0 and x<= 1/3 then
473 ret: cantorr2(3*x, n-1),
474 if 1/3<x and x<2/3 then
476 if x>=2/3 and x< 1 then
477 ret:1 + cantorr2(3*x-2, n-1),
482 def block_random(m_vars):
483 m_vars = Stack({}, m_vars)
485 if ((m_vars[\"x\"] > 0) and (m_vars[\"x\"] <= (1 / 3))):
486 m_vars[\"ret\"] = cantorr2((3 * m_vars[\"x\"]), (m_vars[\"n\"] + (-1)))
488 if (((1 / 3) < m_vars[\"x\"]) and (m_vars[\"x\"] < (2 / 3))):
491 if ((m_vars[\"x\"] >= (2 / 3)) and (m_vars[\"x\"] < 1)):
492 m_vars[\"ret\"] = (1 + cantorr2(((3 * m_vars[\"x\"]) + (-2)), (m_vars[\"n\"] + (-1))))
493 return((m_vars[\"ret\"] / 2))
495 def cantorr2(x, n, m_vars = m_vars):
496 m_vars = Stack({}, m_vars)
497 m_vars.ins({\"x\" : x, \"n\" : n})
498 m_vars.ins({\"ret\" : 0, \"k\" : 0})
500 if not(m_funcs[\"numberp\"](m_vars[\"x\"])):
501 return(m_funcs[\"cantorr2\"](m_vars[\"x\"], m_vars[\"n\"]))
503 if (m_vars[\"x\"] == 0):
506 if (m_vars[\"x\"] == 1):
508 m_funcs[\"print\"](m_vars[\"x\"], m_vars[\"n\"])
509 return((block_random(m_vars) if (m_vars[\"n\"] > 0) else m_vars[\"x\"]))
510 m_funcs[\"cantorr2\"] = cantorr2"$
512 /* Tests for Plotting code */
513 preprocess(pytranslate('(plot3d(lambda([x, y], x^2 + y^(-1)), [x, 1, 10], [y, 1, 10]))));
515 m_funcs[\"plot3d\"](lambda x, y, m_vars = Stack({}, m_vars): (m_funcs[\"pow\"](x, 2) + m_funcs[\"pow\"](y, (-1))), [\"x\", 1, 10], [\"y\", 1, 10])"$
516 preprocess(pytranslate('(plot2d(lambda([x], x^2), [x,0,2]))));
518 m_funcs[\"plot2d\"](lambda x, m_vars = Stack({}, m_vars): m_funcs[\"pow\"](x, 2), [\"x\", 0, 2])"$
520 (?\*symbols\-directly\-convert\*:[], 'done);
522 (kill(allbut(preprocess, temp_vars_map, temp_funcs_map)), 'done);
525 (?\*maxima\-variables\-dictionary\-name\* : temp_vars_map,
526 ?\*maxima\-function\-dictionary\-name\* : temp_funcs_map,