This repository has been archived by the owner on Mar 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
high-level-interface.lisp
165 lines (154 loc) · 6.15 KB
/
high-level-interface.lisp
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
(in-package :maxima-interface)
;;; Some of the operators used by Maxima are listed in comm.lisp.
(defparameter *lisp-to-maxima*
'((= . (maxima::mequal))
(/= . (maxima::mnotequal))
(+ . (maxima::mplus))
(- . (maxima::mminus))
(* . (maxima::mtimes))
(sqrt . (maxima::%sqrt))
(expt . (maxima::mexpt))
(sin . (maxima::%sin))
(cos . (maxima::%cos))
(tan . (maxima::%tan))
(asin . (maxima::%asin))
(acos . (maxima::%acos))
(sinh . (maxima::%sinh))
(cosh . (maxima::%cosh))
(tanh . (maxima::%tanh))
(asinh . (maxima::%asinh))
(acosh . (maxima::%acosh))
(atanh . (maxima::%atanh))
(abs . (maxima::mabs))
(signum . (maxima::%signum))
(mod . (maxima::$mod))
(pi . maxima::$%pi)
(i . maxima::$%i)
(inf . maxima::$inf)
(plus . maxima::$plus)
(minus . maxima::$minus)
(diff . (maxima::$diff))
(limit . (maxima::$limit))
(integrate . (maxima::$integrate)))
"Association list for translating Lisp to Maxima")
(defparameter *maxima-to-lisp/operations*
'((maxima::mequal . =)
(maxima::mnotequal . /=)
(maxima::mplus . +)
(maxima::mminus . -)
(maxima::mtimes . *)
(maxima::mquotient . /)
(maxima::mrat . /)
(maxima::rat . /)
(maxima::%sqrt . sqrt)
(maxima::mexpt . expt)
(maxima::%log . log)
(maxima::%sin . sin)
(maxima::%cos . cos)
(maxima::%tan . tan)
(maxima::%asin . asin)
(maxima::%acos . acos)
(maxima::%atan . atan)
(maxima::$atan2 . atan)
(maxima::%sinh . sinh)
(maxima::%cosh . cosh)
(maxima::%tanh . tanh)
(maxima::%asinh . asinh)
(maxima::%acosh . acosh)
(maxima::%atanh . atanh)
(maxima::mabs . abs)
(maxima::%signum . signum)
(maxima::$mod . mod)
(maxima::$diff . diff)
(maxima::%derivative . diff)
(maxima::%integrate . integrate)
(maxima::%limit . limit))
"Association list for translating operations from Maxima to Lisp")
(defparameter *maxima-to-lisp/constants*
'((maxima::$%pi . pi)
(maxima::$%e . (exp 1))
(maxima::$%i . (complex 0 1))
(maxima::$inf . inf)
(maxima::$plus . plus)
(maxima::$minus . minus))
"Association list for translating constants from Maxima to Lisp")
(defun lisp-to-maxima (sexp)
"Translate s-expression to Maxima."
(cond
((atom sexp)
(or (rest (assoc sexp *lisp-to-maxima*)) sexp))
((consp sexp)
(destructuring-bind (x . xs) sexp
(let ((entry (assoc x *lisp-to-maxima*)))
(if entry
(cons (rest entry) (mapcar #'lisp-to-maxima xs))
(case x ; Deal with special cases.
(1+
`((maxima::mplus) ,(lisp-to-maxima (first xs)) 1))
(1-
`((maxima::mplus) ,(lisp-to-maxima (first xs)) ((maxima::mminus) 1)))
(exp
`((maxima::mexpt) maxima::$%e ,(lisp-to-maxima (first xs))))
(log
(if (= (length xs) 2)
`((maxima::mquotient)
((maxima::%log) ,(lisp-to-maxima (first xs)))
((maxima::%log) ,(lisp-to-maxima (second xs))))
`((maxima::%log) ,(lisp-to-maxima (first xs)))))
(atan
(if (= (length xs) 2)
`((maxima::$atan2) ,@(mapcar #'lisp-to-maxima xs))
`((maxima::%atan) ,(lisp-to-maxima (first xs)))))
(complex
(if (= (length xs) 1)
(lisp-to-maxima (first xs))
`((maxima::mplus) ,(lisp-to-maxima (first xs))
((maxima::mtimes) ,(lisp-to-maxima (second xs)) maxima::$%i))))
(/
(if (= (length xs) 1)
`((maxima::mquotient) 1 ,(lisp-to-maxima (first xs)))
`((maxima::mquotient) ,@(mapcar #'lisp-to-maxima xs))))
(otherwise
(cons (list (lisp-to-maxima x)) (mapcar #'lisp-to-maxima xs))))))))))
(defun maxima-to-lisp (sexp)
"Translate s-expression from Maxima."
(flet ((translate-op (x)
(rest (assoc x *maxima-to-lisp/operations*)))
(translate-const (x)
(rest (assoc x *maxima-to-lisp/constants*))))
(cond
((atom sexp)
(or (translate-const sexp) sexp))
((consp sexp)
(destructuring-bind (x . xs) sexp
(or (translate-op x)
(if (member 'maxima::simp xs :test #'eq)
(maxima-to-lisp x) ; Omit tags added by Maxima.
(cons (maxima-to-lisp x) (maxima-to-lisp xs)))))))))
(defun evaluate (sexp)
"Evaluate an expression in Maxima and return its translation to Lisp."
(maxima-to-lisp (maxima-eval (lisp-to-maxima sexp))))
(defun diff (sexp var &optional (n 1))
"Differentiate s-expression with respect to VAR, N times."
(evaluate `(maxima::$diff ,sexp ,var ,n)))
(defun integrate (sexp var &rest integration-limits)
"Integrate s-expression with regard to variable VAR, optionally using INTEGRATION-LIMITS."
(if integration-limits
(destructuring-bind (a b) integration-limits
(evaluate `(maxima::$integrate ,sexp ,var ,a ,b)))
(evaluate `(maxima::$integrate ,sexp ,var))))
(defun expand (sexp)
"Expand s-expression."
(evaluate `(maxima::$expand ,sexp)))
(defun simplify (sexp)
"Simplify s-expression."
(evaluate `(maxima::$ratsimp (maxima::$trigsimp ,sexp))))
(defun limit (sexp &optional var value dir)
"Compute the limit of an s-expression as the variable VAR approaches VALUE
from the direction specified by DIR, which is either one of the PLUS or MINUS
symbols."
(declare (type (or null (member plus minus))))
(evaluate `(maxima::$limit ,sexp ,@(mapcar #'lisp-to-maxima
(cond
(dir (list var value dir))
(value (list var value)))))))