1 | ;;;
|
---|
2 | ;;; CLTL2 lisp script, prints out a Mathematica equivalent to a GP tree,
|
---|
3 | ;;; for testing purposes (Show[f[x],{x,-1,1} is useful)
|
---|
4 | ;;;
|
---|
5 |
|
---|
6 | (defun conv (exp)
|
---|
7 | (cond
|
---|
8 | ((not (listp exp)) "x")
|
---|
9 | ((equal (first exp) '+)
|
---|
10 | (concatenate 'string "Plus[" (conv (second exp)) "," (conv (third exp)) "]"))
|
---|
11 | ((equal (first exp) '-)
|
---|
12 | (concatenate 'string "Subtract[" (conv (second exp)) "," (conv (third exp)) "]"))
|
---|
13 | ((equal (first exp) '*)
|
---|
14 | (concatenate 'string "Times[" (conv (second exp)) "," (conv (third exp)) "]"))
|
---|
15 | ((equal (first exp) '%)
|
---|
16 | (concatenate 'string "PDivide[" (conv (second exp)) "," (conv (third exp)) "]"))
|
---|
17 | ((equal (first exp) 'exp)
|
---|
18 | (concatenate 'string "Exp[" (conv (second exp)) "]"))
|
---|
19 | ((equal (first exp) 'sin)
|
---|
20 | (concatenate 'string "Sin[" (conv (second exp))"]"))
|
---|
21 | ((equal (first exp) 'cos)
|
---|
22 | (concatenate 'string "Cos[" (conv (second exp))"]"))
|
---|
23 | ((equal (first exp) 'rlog)
|
---|
24 | (concatenate 'string "RLog[" (conv (second exp))"]"))))
|
---|
25 |
|
---|
26 | (print "PDivide[x_,y_] := If[y==0,1,x/y]")
|
---|
27 | (print "RLog[x_] := If[x==0,1,Log[x]]")
|
---|
28 |
|
---|
29 | (princ (conv '(+ (sin (sin (+ (+ x x) (+ x x)))) (% (rlog
|
---|
30 | (* (* x x) (exp x))) (% (* (exp x) (rlog
|
---|
31 | x)) (sin (+ x x)))))))
|
---|
32 |
|
---|
33 | (conv '(+ x x))
|
---|
34 | (conv '(sin x))
|
---|
35 |
|
---|
36 | (conv ' (+ (sin x) (* (+ (* (* (cos (rlog x)) x)
|
---|
37 | (+ (* (cos (rlog x)) x) (% x (cos (rlog x)))))
|
---|
38 | (% x (% x x))) x)))
|
---|