File: gener.lisp

package info (click to toggle)
maxima 5.9.1-9
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 32,272 kB
  • ctags: 14,123
  • sloc: lisp: 145,126; fortran: 14,031; tcl: 10,052; sh: 3,313; perl: 1,766; makefile: 1,748; ansic: 471; awk: 7
file content (351 lines) | stat: -rw-r--r-- 11,630 bytes parent folder | download
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
;;; -*- Mode:LISP; Package:MACSYMA -*-

;	** (c) Copyright 1979 Massachusetts Institute of Technology **
(in-package "MAXIMA")


(declare-top (special $metric $metricconvert indlist empty))

;$METRICCONVERT if non-NIL will allow $GENERATE to rename the metric tensor
;   ($METRIC must be bound) with 2 covariant indices to LG and with 2
;   contravariant indices to UG.

(defun $GENERATE (e)
       (prog (free lhs rhs)
	     (cond ((or (atom e) (not (eq (caar e) 'MEQUAL)))
		    (merror "GENERATE requires an equation as an argument"))
		   ((equal (setq free ($indices e)) empty)
		    (return (cons '(MSETQ) (cdr e))))
		   ((or (eq (ml-typep (cadr e)) 'SYMBOL)           ;If a symbol or
			(and (rpobj (cadr e))  ;an indexed object with no dummy
			     (null (cdaddr ($indices2 (cadr e))))))    ;indices
		    (setq lhs (cadr e) rhs (caddr e)))
		   ((or (eq (ml-typep (caddr e)) 'SYMBOL)
			(and (rpobj (caddr e))
			     (null (cdaddr ($indices2 (caddr e))))))
		    (setq lhs (caddr e) rhs (cadr e)))
		   (t (merror "At least one side of the equation must be a~
			      ~%symbol or a single indexed object")))
	     (cond ((and (not (eq (ml-typep lhs) 'SYMBOL))
			 (not (null (cdddr lhs))))
		    (merror "Cannot assign to indexed objects with derivative ~
			    indices:~%~M"
			    (ishow lhs))))
	     (setq free (nreverse (itensor-sort (cdadr free)))  ;Set FREE to just the
		   indlist nil)                           ;free indices
	     (and $METRICCONVERT (boundp '$METRIC)
		  (setq lhs (changename $METRIC t 0 2 '$UG
					(changename $METRIC t 2 0 '$LG lhs))
			rhs (changename $METRIC t 0 2 '$UG
					(changename $METRIC t 2 0 '$LG rhs))))
	     (tabulate rhs)
	     (setq indlist (unique indlist))
	     (do ((q (mapcar 'car indlist) (cdr q)))
		 ((null q))
		 (cond ((memq (car q) (cdr q))
			(merror "~
GENERATE cannot currently handle indexed objects of the same name~
~%with different numbers of covariant and//or contravariant indices:~%~M"
				(car q)))))
	     (cond ((not (eq (ml-typep lhs) 'SYMBOL))
		    (do ((test) (flag) (name))
			(flag)
			(setq test (list (caar lhs) (length (cdadr lhs))
					 (length (cdaddr lhs))))
			(cond ((or (zl-member test indlist)
				   (not (memq (car test)
					      (mapcar 'car indlist))))
			       (setq flag t))
			      (t
			       (mtell "Assignment is to be made to ~M~
~%This name with a different number of covariant and//or contravariant~
~%indices appears on the other side of the equation. To avoid array name~
~%conflicts, choose a new name for this object:~%"
				      (ishow lhs))
                               (cond ((not (eq (ml-typep
						(setq name
						      (retrieve nil nil)))
					       'SYMBOL))
				      (merror "Name not an atom")))
			       (setq lhs (cons (ncons name) (cdr lhs))))))))
	     (return (do ((free free (cdr free))
			  (equ (cons '(MSETQ) (list (changeform lhs)
						    (t-convert
						     (summer1 rhs))))))
			 ((null free) equ)
			 (setq equ (append '((MDO)) (ncons (car free))
					   '(1 1 NIL $DIM NIL)
					   (ncons equ)))))))

(defun TABULATE (e)        ;For each indexed object in E, appends a list of the
       (cond ((atom e))    ;name of that object and the number of covariant and
	     ((rpobj e)    ;contravariant indices to the global list INDLIST
	      (setq indlist (cons (list (caar e) (length (cdadr e))
					(length (cdaddr e)))
				  indlist)))
	     ((or (eq (caar e) 'MPLUS) (eq (caar e) 'MTIMES))
	      (mapcar 'tabulate (cdr e)))))

(defun UNIQUE (l)                   ;Returns a list of the unique elements of L
       (do ((a l (cdr a)) (b))
	   ((null a) b)
	   (cond ((not (zl-member (car a) b))
		  (setq b (cons (car a) b))))))

(defun SUMMER1 (e)     ;Applies SUMMER to the products and indexed objects in E
       (cond ((atom e) e)
	     ((eq (caar e) 'MPLUS)
  	      (cons (car e) (mapcar 'summer1 (cdr e))))
	     ((or (eq (caar e) 'MTIMES) (rpobj e))
	      (summer e (cdaddr ($indices e))))
	     (t e)))

(defun SUMMER (p dummy) ;Makes implicit sums explicit in the product or indexed
                        ;object P where DUMMY is the list of dummy indices of P
       (prog (dummy2 scalars indexed s)                          ;at this level
	     (setq dummy2 (intersect (all ($indices2 p)) dummy))
	     (do ((p (cond ((eq (caar p) 'MTIMES) (cdr p))
			   (t (ncons p))) (cdr p))
		  (obj))
		 ((null p))
		 (setq obj (car p))
		 (cond ((atom obj)
			(setq scalars (cons obj scalars)))
		       ((rpobj obj)
			(cond ((null (intersect dummy2 (all ($indices2 obj))))
			       (setq scalars (cons obj scalars)))
			      (t (setq indexed (cons obj indexed)))))
		       ((eq (caar obj) 'MPLUS)
			(setq s t)
			(cond ((null (intersect dummy (all ($indices obj))))
			       (setq scalars
				     (cons (summer1 obj) scalars)))
			      (t (setq indexed
				       (cons (summer1 obj) indexed)))))
		       (t (setq scalars (cons obj scalars)))))
	     (cond ((and s
			 (not (samelists dummy2
					 (setq s
					       (cdaddr
						($indices
						 (append '((MTIMES))
							 scalars indexed)))))))
		    (setq dummy2 s
			  s scalars
			  scalars nil)
		    (do ((p s (cdr p)) (obj))
			((null p))
			(setq obj (car p))
			(cond ((null (intersect dummy2 (all ($indices obj))))
			       (setq scalars (cons obj scalars)))
			      (t (setq indexed (cons obj indexed)))))))
	     (return
	      (simptimes
	       (nconc (ncons '(MTIMES))
		      scalars
		      (cond ((not (null indexed))
		             (do ((indxd (simptimes (cons '(MTIMES) indexed)
						    1 nil))
			          (dummy (itensor-sort dummy2) (cdr dummy)))
			         ((null dummy) (ncons indxd))
			         (setq indxd (nconc (ncons '($SUM))
						    (ncons indxd)
						    (ncons (car dummy))
						    '(1 $DIM)))))
			    (t nil)))
	       1 nil))))

(defun ALL (l)                        ;Converts [[A, B], [C, D]] into (A B C D)
       (append (cdadr l) (cdaddr l)))

(defun T-CONVERT (e)        ;Applies CHANGEFORM to each individual object in an
       (cond ((atom e) e)   ;expression
	     ((or (eq (caar e) 'MPLUS) (eq (caar e) 'MTIMES))
	      (cons (car e) (mapcar 't-convert (cdr e))))
	     ((eq (caar e) '$SUM)
	      (append (ncons (car e)) (ncons (t-convert (cadr e))) (cddr e)))
	     (t (changeform e))))

(defun CHANGEFORM (e)           ;Converts a single object from ITENSR format to
       (cond ((atom e) e)       ;ETENSR format
	     ((rpobj e)
	      (do ((deriv (cdddr e) (cdr deriv))
		   (new (cond ((and (null (cdadr e)) (null (cdaddr e)))
			       (caar e))     ;If no covariant and contravariant
			                     ;indices then make into an atom
			      (t (cons (cons (equiv-table (caar e)) '(ARRAY))
				       (append (cdadr e) (cdaddr e)))))))
		  ((null deriv) new)
		  (setq new (append '(($DIFF)) (ncons new)
				    (ncons (cons '($OMEGA ARRAY)
						 (ncons (car deriv))))))))
	     (t e)))

(defun EQUIV-TABLE (a)                ;Makes appropiate name changes converting
       (cond ((memq a '($CHR1 %CHR1)) '$LCS)             ;from ITENSR to ETENSR
	     ((memq a '($CHR2 %CHR2)) '$MCS)
	     (t a)))

(declare-top (unspecial indlist))

(declare-top (special SMLIST $FUNCS))
(setq $funcs '((MLIST)))

(DEFUN $MAKEBOX (E NAME)
       (COND ((ATOM E) E)
	     ((MTIMESP E) (MAKEBOX E NAME))
	     ((MPLUSP E)
	      (MYSUBST0 (SIMPLIFYA (CONS '(MPLUS)
					 (MAPCAR
					  (FUNCTION
					   (LAMBDA (Q) ($MAKEBOX Q NAME)))
					  (CDR E)))
				   NIL)
			E))
	     ((MEXPTP E) (LIST (CAR E) ($MAKEBOX (CADR E) NAME) (CADDR E)))
	     (T E))) 

(DEFUN MAKEBOX (E NAME)
       (PROG (L1 L2 X L3 L) 
	     (SETQ L (CDR E))
	AGAIN(SETQ X (CAR L))
	     (COND ((RPOBJ X)
		    (COND ((AND (EQ (CAAR X) NAME) (NULL (CDDDR X))
				(NULL (CDADR X)) (= (LENGTH (CDADDR X)) 2))
			   (SETQ L1 (CONS X L1)))
			  ((CDDDR X) (SETQ L2 (CONS X L2)))
			  (T (SETQ L3 (CONS X L3)))))
		   (T (SETQ L3 (CONS X L3))))
	     (AND (SETQ L (CDR L)) (GO AGAIN))
	     (COND ((OR (NULL L1) (NULL L2)) (RETURN E)))
	     (DO ((L2 L2 (CDR L2)))
		 ((NULL L2) )
		 (SETQ L L1)
;	     (DO L2 L2 (CDR L2)
;	      (NULL L2)
;	      (SETQ L L1)..)

	     (tagbody
	      LOOP
	      (SETQ X (CAR L))
	      (COND
	       ((AND (MEMQ (CAR (CDADDR X)) (CDDDAR L2))
		     (MEMQ (CADR (CDADDR X))(CDDDAR L2)))
		(SETQ 
		 L3
		 (CONS (NCONC
		  (LIST
		   (NCONS
		    (IMPLODE (APPEND '([ ])
				    (CDR (EXPLODEC (CAAAR L2))))))
		   (CADAR L2)
		   (CADDAR L2)) (SETDIFF (CDDDAR L2)(CDADDR X)))
		  L3))
		(SETQ L1 (ZL-DELETE X L1 1.)))
	       ((SETQ L (CDR L)) (GO LOOP))
	       (T (SETQ L3 (CONS (CAR L2) L3))))))
	     (RETURN (SIMPTIMES (CONS '(MTIMES) (NCONC L1 L3))
				1.
				NIL)))) 

(DECLARE-TOP (SPECIAL TENSR))

(DEFmfUN $AVERAGE N ((LAMBDA (TENSR) (SIMPLIFYA (AVERAGE (ARG 1)) NIL))
		   (AND (= N 2) (ARG 2))))

(DEFUN AVERAGE (E)
       (COND ((ATOM E ) E)
	     ((RPOBJ E) (COND ((OR (NOT TENSR) (EQ (CAAR E) TENSR))
			       (AVERAGE1 E))
			      (T E)))
	     (T (CONS (NCONS (CAAR E)) (MAPCAR (FUNCTION AVERAGE) (CDR E))))))

(DEFUN AVERAGE1 (E)
       (COND ((= (LENGTH (CDADR E)) 2)
	      (SETQ E (LIST '(MTIMES) '((RAT SIMP) 1 2)
			    (LIST '(MPLUS)
				  (CONS (CAR E)
					(CONS (AREV (CADR E)) (CDDR E))) E))))
	     ((= (LENGTH (CDADDR E)) 2)
	      (SETQ E (LIST '(MTIMES) '((RAT SMP) 1 2)
			    (LIST '(MPLUS)
				  (CONS (CAR E)
					(CONS (CADR E)
					      (CONS (AREV (CADDR E))
						    (CDDDR E)))) E)))))
       E)

(DEFUN AREV (L) (LIST (CAR L) (CADDR L) (CADR L)))

(DECLARE-TOP (UNSPECIAL TENSR))
(add2lnc '(($AVERAGE) $TENSOR) $funcs)

(defun $CONMETDERIV (e g)
       (cond ((not (eq (ml-typep g) 'SYMBOL))
	      (merror "Invalid metric name: ~M" g))
	     (t (conmetderiv e g ((lambda (l) (append (cdadr l) (cdaddr l)))
				  ($indices e))))))

(defun CONMETDERIV (e g indexl)
       (cond ((atom e) e)
	     ((rpobj e)
	      (cond ((and (eq (caar e) g) (null (cdadr e))
			  (equal (length (cdaddr e)) 2)
			  (not (null (cdddr e))))
		     (do ((e (cmdexpand (car e) (car (cdaddr e))
					(cadr (cdaddr e)) (cadddr e) indexl))
			  (deriv (cddddr e) (cdr deriv)))
			 ((null deriv) e)
			 (setq e (conmetderiv ($diff e (car deriv))
					      g indexl))))
		    (t e)))
	     (t (mysubst0 (cons (car e)
				(mapcar
				 (function (lambda (q) 
						   (conmetderiv q g indexl)))
				 (cdr e))) e))))

(defun CMDEXPAND (g i j k indexl)
       (do ((dummy) (flag))
	   (flag (list '(MPLUS SIMP)
		       (list '(MTIMES SIMP) -1
			     (list g (ncons SMLIST) (list SMLIST dummy i))
			     (list '($CHR2 SIMP) (list SMLIST dummy k)
				   (list SMLIST j)))
		       (list '(MTIMES SIMP) -1
			     (list g (ncons SMLIST) (list SMLIST dummy j))
			     (list '($CHR2 SIMP) (list SMLIST dummy k)
				   (list SMLIST i)))))
	   (setq dummy ($dummy))
	   (and (not (memq dummy indexl)) (setq flag t))))

(add2lnc '(($CONMETDERIV) $EXP $NAME) $funcs)

(defun $FLUSH1DERIV (e g)
       (cond ((not (eq (ml-typep g) 'SYMBOL))
	      (merror "Invalid metric name: ~M" g))
	     (t (flush1deriv e g))))

(defun FLUSH1DERIV (e g)
       (cond ((atom e) e)
	     ((rpobj e)
	      (cond ((and (eq (caar e) g) (equal (length (cdddr e)) 1)
			  (or (and (equal (length (cdadr e)) 2)
				   (null (cdaddr e)))
			      (and (equal (length (cdaddr e)) 2)
				   (null (cdadr e)))))
		     0)
		    (t e)))
	     (t (subst0 (cons (ncons (caar e))
			      (mapcar
			       (function (lambda (q) (flush1deriv q g)))
			       (cdr e))) e))))

(add2lnc '(($FLUSH1DERIV) $EXP $NAME) $funcs)

(defun $GEODESIC (exp g)
       ($flush1deriv ($flush exp '$CHR2 '%CHR2) g))

(add2lnc '(($GEODESIC) $EXP $NAME) $funcs)