File: rducon.lsp

package info (click to toggle)
maxima 5.6-17
  • links: PTS
  • area: main
  • in suites: woody
  • size: 30,572 kB
  • ctags: 47,715
  • sloc: ansic: 154,079; lisp: 147,553; asm: 45,843; tcl: 16,744; sh: 11,057; makefile: 7,198; perl: 1,842; sed: 334; fortran: 24; awk: 5
file content (265 lines) | stat: -rw-r--r-- 14,883 bytes parent folder | download | duplicates (3)
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
;;; -*- Mode: Lisp; Package: Macsyma -*-                                 ;;;
;;;    (c) Copyright 1984 the Regents of the University of California.   ;;;
;;;        All Rights Reserved.                                          ;;;
;;;        This work was produced under the sponsorship of the           ;;;
;;;        U.S. Department of Energy.  The Government retains            ;;;
;;;        certain rights therein.                                       ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(macsyma-module rducon)

(eval-when (load eval) (or (get 'expens 'version)
			   (load "expense")))

(defmvar $const_eqns (list '(mlist simp))
         "List of equations of constant expressions found by REDUCE_CONSTS."
         modified-commands '$reduce_consts)

(defmvar $const_prefix '$xx
         "String used to prefix all symbols generated by REDUCE_CONSTS to
   represent constant expressions."
         modified-commands '$reduce_consts)

(defmvar $const_counter 1
         "Integer index used to uniquely identify all constant expressions
   found by calling REDUCE_CONSTS."
         fixnum
         modified-commands '$reduce_consts)

(defmacro minus-constantp (x) `(and (eq (caar ,x) 'mtimes)
                                    (= (length ,x) 3)
                                    (equal (cadr ,x) -1)))

(defun query-const-table (x)
   (do ((p (cdr $const_eqns) (cdr p)))
       ((null p))
     (and (alike1 (caddar p) x)
          (return (cadar p)))))

(defun new-name (default-name)
   (let ((name (or default-name
                   (prog1
                    (implode (nconc (exploden $const_prefix)
                                    (exploden $const_counter)))
                    (increment $const_counter)))))
     (MFUNCALL '$declare name '$constant)
     name))

(defun log-const (exp name)
   (let ((inconst (new-name name)))
     (setq $const_eqns `(,.$const_eqns ,`((mequal simp) ,inconst ,exp)))
     inconst))

(defun obtain-constant (key curr-const)
 (let ((inkey key))
   (or (query-const-table key)
       (do ((pursue (cdr $const_eqns) (cdr pursue))
            (pointer)
            (hold)
            (op)
            (expense ($expense key))
            (negative (mul -1 key))
            (sum? (mplusp key)))
           ((null pursue)
            (and pointer
                 (setq inkey
                       (cond ((eq op 'sum) (add (cadar pointer) hold))
                             ((eq op 'neg) (mul -1 (add (cadar pointer) hold)))
                             (t (mul (cadar pointer) hold))))
                 (do ((recheck (cdr $const_eqns) (cdr recheck))
                      (minkey (mul -1 inkey)))
                     ((null recheck))
                   (let ((exp (caddar recheck))
                         (lab (cadar recheck)))
                     (cond ((alike1 exp inkey) (return lab))
                           ((alike1 exp minkey)
                            (return (mul -1 lab))))))))
         (let ((rhs (caddar pursue)))
           (cond ((alike1 negative rhs) (return (mul -1 (cadar pursue))))
                 ((and sum?
                       (mplusp rhs)
                       (let ((trial (sub key rhs))
                             (trial-2 (sub negative rhs)))
                         (let ((estim (1+ ($expense trial)))
                               (estim-2 (1+ ($expense trial-2))))
                           (cond ((< estim estim-2)
                                  (and (< estim expense)
                                       (setq pointer pursue
                                             op 'sum
                                             expense estim
                                             hold trial)))
                                 (t
                                  (and (< estim-2 expense)
                                       (setq pointer pursue
                                             op 'neg
                                             expense estim-2
                                             hold trial-2))))))))
                 (t
                  (let* ((trial (div key rhs))
                         (estim (1+ ($expense trial))))
                    (and (< estim expense)
                         (setq pointer pursue
                               op 'prod
                               expense estim
                               hold trial)))))))
       (log-const inkey curr-const))))

(defun find-constant (x)
   (cond ((and (symbolp x) ($constantp x)) x)
         ((mtimesp x)
          (do ((fcon x (cdr fcon)))
              ((null (cdr fcon)))
            (let ((qcon (cadr fcon)))
              (and (symbolp qcon) ($constantp qcon) (return qcon)))))
         (t nil)))

(defun reduce-constants (x &optional newconst)
   (cond ((or ($mapatom x)
              (and (eq (caar x) 'mtimes)
                   (equal (cadr x) -1)
                   ($mapatom (caddr x))
                   (null (cdddr x))))
          x)
         ((query-const-table x))
         ((and (eq (caar x) 'mexpt)
               ($constantp x)
               (let ((xexpon (caddr x)) (xbase (cadr x)))
                 (do ((p (cdr $const_eqns) (cdr p))
                      (nstate (fixp xexpon))
                      (follow $const_eqns p))
                     ((null p))
                   (let ((obj (caddar p)))
                     (and (mexptp obj)
                          (alike1 xbase (cadr obj))
                          (let ((inquir-expon (caddr obj)))
                            (let ((both-fix (and nstate (fixp inquir-expon))))
                              (let ((dif (cond (both-fix (difference xexpon inquir-expon))
                                               (t (sub xexpon inquir-expon))))
                                    (gcd (cond (both-fix (gcd xexpon inquir-expon))
                                               (t ($gcd xexpon inquir-expon)))))
                                (or (and (fixp dif)
                                         (cond ((equal 1 dif)
                                                (let ((new-exp (mul (cadar p) xbase)))
                                                  (return (or (query-const-table new-exp)
                                                              (log-const new-exp newconst)))))
                                               ((equal -1 dif)
                                                (let ((inc (new-name newconst)))
                                                  (rplaca (cddar p) (mul inc xbase))
                                                  (rplacd follow (append `(((mequal simp) ,inc ,x)) p))
                                                  (return inc)))))
                                    (or (and (fixp gcd) (equal gcd 1))
                                        (let ((pw1 (cond (both-fix (quotient xexpon gcd))
                                                         (t (div xexpon gcd))))
                                              (pw2 (cond (both-fix (quotient inquir-expon gcd))
                                                         (t (div inquir-expon gcd)))))
                                          (cond ((and (fixp pw2) (equal pw2 1))
                                                 (let ((new-exp (power (cadar p) pw1)))
                                                   (return (or (query-const-table new-exp)
                                                               (log-const new-exp newconst)))))
                                                ((and (fixp pw1) (equal pw1 1))
                                                 (let ((inc (new-name newconst)))
                                                   (rplaca (cddar p) (power inc pw2))
                                                   (rplacd follow (append `(((mequal simp) ,inc ,x)) p))
                                                   (return inc)))
                                                (t (let ((inc (new-name nil)))
                                                     (rplaca (cddar p) (power inc pw2))
                                                     (rplacd follow (append `(((mequal simp) ,inc ,(power xbase gcd))) p))
                                                     (return (log-const (power inc pw1) newconst)))))))))))))))))
         (($constantp x) (obtain-constant x newconst))
         (t
          (let ((opr (caar x)))
            (cond ((memq opr '(mtimes mplus))
                   (let* ((product (eq opr 'mtimes))
                          (negative (and product (equal (cadr x) -1))))
                     (or (and negative (null (cdddr x))
                              (let ((new? (query-const-table (caddr x))))
                                (and new? (mul -1 new?))))
                         (do ((next (cdr x) (cdr next))
                              (itot 0)
                              (new)
                              (non-constants))
                             ((null next)
                              (cond ((and product (= (length new) 2) (equal (car new) -1))
                                     (muln (nconc new non-constants) nil))
                                    ((> (length new) 1)
                                     (let ((nc (obtain-constant (cond (product (muln new nil))
                                                                      (t (addn new nil)))
                                                                newconst)))
                                       (cond ((not product) (addn `(,.non-constants ,nc) nil))
                                             ((atom nc) (muln `(,.non-constants ,nc) nil))
                                             (t (muln (nconc (cdr nc) non-constants) nil)))))
                                    ((or new non-constants)
                                     (let ((tot (nconc new non-constants)))
                                       (cond (product (muln tot nil))
                                             (t (addn tot nil)))))
                                    (t x)))
                           (declare (fixnum itot))
                           (let* ((exam (car next))
                                  (result (reduce-constants exam)))
                             (cond ((eq exam result)
                                    (cond (($constantp exam)
                                           (increment itot)
                                           (if (and (null new)
                                                    (cond (negative (> itot 2))
                                                          (t (> itot 1))))
                                               (do ((seplist (cdr x) (cdr seplist)))
                                                   ((eq seplist next))
                                                 (let ((element (car seplist)))
                                                   (cond (($constantp element)
                                                          (setq new `(,.new ,element)))
                                                         (t (setq non-constants `(,.non-constants ,element)))))))
                                           (and new (setq new `(,.new ,exam))))
                                          ((or new non-constants) (setq non-constants `(,.non-constants ,exam)))))
                                   (t
                                    (or new non-constants
                                        (do ((seplist (cdr x) (cdr seplist)))
                                            ((eq seplist next))
                                          (let ((element (car seplist)))
                                            (cond (($constantp element)
                                                   (setq new `(,.new ,element)))
                                                  (t (setq non-constants `(,.non-constants ,element)))))))
                                    (cond ((or (atom result) (minus-constantp result))
                                           (setq new
                                                 (cond ((or (atom result) (not product)) `(,.new ,result))
                                                       (t
                                                        (let ((number? (car new)))
                                                          (cond (($numberp number?)
                                                                 (let ((new-prod (mul number? result)))
                                                                   (cond ((mtimesp new-prod)
                                                                          (nconc (cdr new-prod) (ncons new-prod)))
                                                                         (t (nconc (cdr new) (ncons new-prod))))))
                                                                (t (nconc (cdr result) new))))))))
                                          (t (setq non-constants `(,.non-constants ,result)))))))))))
                  (t
                   (do ((next (cdr x) (cdr next))
                        (new))
                       ((null next)
                        (cond ((null new) x)
                              ((not (eq opr 'mquotient))
                               (nconc (ncons (car x)) new))
                              (t
                               (let ((cnum (find-constant (car new)))
                                     (cden (find-constant (cadr new))))
                                 (cond ((and cnum cden)
                                        (let* ((ratio (obtain-constant (div cnum cden) newconst))
                                               (numerator (cond ((mtimesp (car new))
                                                                 (mul ratio (remq cnum (car new))))
                                                                (t ratio))))
                                          (cond ((mtimesp (cadr new))
                                                 (div numerator (muln (remq cden (cdadr new)) nil)))
                                                (t numerator))))
                                       (t x))))))
                     (let* ((exam (car next))
                            (result (reduce-constants exam)))
                       (cond ((eq exam result)
                              (and new (setq new `(,.new ,exam))))
                             (t
                              (or new
                                  (do ((copy (cdr x) (cdr copy)))
                                      ((eq copy next))
                                    (setq new `(,.new ,(car copy)))))
                              (setq new `(,.new ,result))))))))))))

(defun $reduce_consts (x &optional newconstant)
   (cond ((atom x) x)
         (t (reduce-constants x newconstant))))