File: facexp.mc

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 (286 lines) | stat: -rw-r--r-- 9,269 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
/* -*- MACSYMA -*- */
EVAL_WHEN(BATCH,TTYOFF:TRUE)$
/*ASB;FACEXP 10
2:37pm  Wednesday, 4 November 1981
*/
/* commented out of DOE MACSYMA
EVAL_WHEN(TRANSLATE,
	  IF GRAPHLOAD#TRUE THEN LOAD(['GRAPH,'FASL,'DSK,'DGVAL]),
	  TRANSCOMPILE:TRUE,
	  DECLARE([ARGDUM,ARGDUM2,OP_FCN_LIST],SPECIAL),
	  MODEDECLARE(FUNCTION(NULLLISTP,FREEOFL),BOOLEAN))$

EVAL_WHEN(BATCH,IF DEBUGLOAD#TRUE THEN LOAD(['DEBUG,'FASL,'DSK,'DGVAL]))$
*/
/* Variable definitions */

DEFINE_VARIABLE(NEXTLAYERFACTOR,FALSE,BOOLEAN)$
DEFINE_VARIABLE(FACSUM_COMBINE,TRUE,BOOLEAN)$

/* Autoloads */
/* commented out of DOE MACSYMA
IF STATUS(FEATURE,ITS)=TRUE
THEN (SETUP_AUTOLOAD([GENUT,FASL,DSK,DGVAL],
		     OPMAP,INTERSECTION,LDELETE,IFLOPMAP,FREEOFL,ORPARTITION,
		     LISTOFOPS,ORPARTITIONL,RLOIEWL,IFNOTCONS,LISTTOSUM,
		     LISTOFOPS_NONRAT,NULLLISTP,SETLIST),
      SETUP_AUTOLOAD([INDEX,FASL,DSK,DGVAL],INDEX))$

IF STATUS(FEATURE,MULTICS)=TRUE
THEN (SETUP_AUTOLOAD(">udd>Mathlab>Brenner>genut",
		     OPMAP,INTERSECTION,LDELETE,IFLOPMAP,FREEOFL,ORPARTITION,
		     LISTOFOPS,ORPARTITIONL,RLOIEWL,IFNOTCONS,
		     LISTOFOPS_NONRAT,NULLLISTP,SETLIST),
      SETUP_AUTOLOAD(">udd>Mathlab>Brenner>index",INDEX))$
*/
/* Predicates */

LOPPLUSP(EXP):=IS(INPART(EXP,0)="+")$

OPERATOR0P(EXP):=BLOCK(
  [IP0DUM],
  IS((IP0DUM:INPART(EXP,0))='OPERATOR OR IP0DUM=NOUNIFY('OPERATOR)))$

ORDERLASTP(EXP1,EXP2):=ORDERLESSP(LAST(EXP1),LAST(EXP2))$

/* User accessible functions */

FACTENEXPAND(EXP,[ARGDUMLIST]):=BLOCK(
  [INDEXEXPAND_CANONICAL:FALSE,PARTSWITCH:TRUE,INFLAG:TRUE,PIECE],
  FACEXPTENL(CONS(INDEXEXPAND(EXP),ARGDUMLIST)))$

FACTORFACSUM(EXP,[ARGDUM]):=BLOCK(
  [EXPDUM,IP0DUM,PARTSWITCH:TRUE,INFLAG:TRUE,PIECE],
  IF ATOM(EXP) THEN RETURN(EXP),
  IF NULLLISTP(ARGDUM) THEN RETURN(AUTOFORM(EXP)),
  IF MATRIXP(EXP) OR LISTP(EXP) OR INPART(EXP,0)="="
  THEN RETURN(MAP(LAMBDA([ELEMDUM],APPLY('FACTORFACSUM,CONS(ELEMDUM,ARGDUM))),
		  EXP)),
  EXPDUM:AUTOFORM(EXP),
  IF (IP0DUM:INPART(EXPDUM,0))="^" OR IP0DUM="*"
  THEN MAP(LAMBDA([FACTDUM],APPLY('FACTORFACSUM,CONS(FACTDUM,ARGDUM))),
	   EXPDUM)
  ELSE FACSUML(CONS(EXPDUM,ARGDUM)))$


FACSUM([ARGLIST]):=FACSUML(ARGLIST)$

FACSUML(ARGLIST):=BLOCK(
  [FACTORFLAG:FALSE,PARTSWITCH:TRUE,INFLAG:TRUE,PIECE,
   FARGLIST:FIRST(ARGLIST)],
  IF MATRIXP(FARGLIST)
  THEN MATRIXMAP(LAMBDA([DUM],FACSUML(CONS(DUM,REST(ARGLIST)))),FARGLIST)
  ELSE IF LISTP(FARGLIST) OR INPART(FARGLIST,0)="="
       THEN MAP(LAMBDA([DUM],FACSUML(CONS(DUM,REST(ARGLIST)))),FARGLIST)
       ELSE BLOCK(
  [ARGDUM:REST(ARGLIST),ARGDUM2:[],RATFAC:FALSE],
  DECLARE([ARGDUM,ARGDUM2],SPECIAL),
  SETLIST(ARGSPLIT(FARGLIST,ARGDUM),'ARGDUM,'ARGDUM2),
  FACEXPL(CONS(APPLY('RATSIMP,CONS(FARGLIST,ARGDUM:RATSIMP(ARGDUM))),
	       ARGDUM))))$

/* Functions mainly for internal use */

FACEXP([ARGLIST]):=FACEXPL(ARGLIST)$

NEXTLAYER(EXP):=BLOCK(
  IF NOT NULLLISTP(ARGDUM2)
  THEN IFLOPMAP("*",
		LAMBDA([DUM],FACSUML(CONS(DUM,ARGDUM2))),
		IF NEXTLAYERFACTOR
		THEN AUTOFORM(EXP)
		ELSE EXP)
  ELSE AUTOFORM(EXP))$

FACEXPL(ARGLIST):=BLOCK(
  [EXPDUM:FIRST(ARGLIST),PARTITIONDUM,
   ARGDUM:REST(ARGLIST),NEXTLAYERFACTORSAVE:NEXTLAYERFACTOR,
   NEXTLAYERFACTOR:FALSE,NUMEXPDUM,DENEXPDUM],
  MODEDECLARE(NEXTLAYERFACTORSAVE,BOOLEAN),
  DECLARE([NUMEXPDUM,DENEXPDUM],SPECIAL),
  IF MEMBER('NEXTLAYERFACTOR,ARGLIST)
  THEN (ARGLIST:DELETE('NEXTLAYERFACTOR,ARGLIST),
	NEXTLAYERFACTOR:TRUE)
  ELSE NEXTLAYERFACTOR:NEXTLAYERFACTORSAVE,
  IF NULLLISTP(ARGDUM) OR LENGTH(ARGLIST)<2 OR FREEOFL(REST(ARGLIST),EXPDUM)
  THEN RETURN(NEXTLAYER(EXPDUM)),
  NUMEXPDUM:FACEXPFORM(NUM(EXPDUM)),
  IF (DENEXPDUM:DENOM(EXPDUM))#1 THEN DENEXPDUM:FACEXPFORM(DENOM(EXPDUM)),
  IF INPART(NUMEXPDUM,0)="+"
     AND NOT FREEOFL(ARGDUM,NUMEXPDUM)
     AND NOT FACSUM_COMBINE
  THEN IF DENEXPDUM#1
       THEN (PARTITIONDUM:ORPARTITIONL(NUMEXPDUM,"+",ARGDUM),
	     MULTTHRU(DENEXPDUM^-1,LAST(PARTITIONDUM))+
		DENEXPDUM^-1*FIRST(PARTITIONDUM))
       ELSE NUMEXPDUM
  ELSE NUMEXPDUM*DENEXPDUM^-1)$

FACEXPFORM(EXP):=(
  EXP:OPMAP(EXP,["+",'VPLUS,"*",'VSTAR]),
  IF INPART(EXP,0)="+"
  THEN FACEXPFORM1(EXP)
  ELSE EXP)$

FACEXPFORM1(EXPDUM):=BLOCK(
  [SUBDUM:ZEROSUBST(ARGDUM,EXPDUM)],
  DECLARE(SUBDUM,SPECIAL),
  EXPDUM-SUBDUM+NEXTLAYER(SUBDUM))$

FACEXPTEN([ARGLIST]):=FACEXPTENL(ARGLIST)$

FACTORFACEXPTEN(EXP,[ARGLIST]):=
  IFLOPMAP("*",
	   LAMBDA([FACDUM],FACEXPTENL(CONS(FACDUM,ARGLIST))),
	   AUTOFORM(EXP))$

FACEXPTENL(ARGLIST):=BLOCK(
  [FACEXPTENFLAG:TRUE],
  DECLARE(FACEXPTENFLAG,SPECIAL),
  MODEDECLARE(FACEXPTENFLAG,BOOLEAN),
  FACSUML(APPEND(ARGLIST,LISTOFTENS(FIRST(ARGLIST)))))$

VPLUS(EXP):=BLOCK(
  [VPSDUM:MAP(LAMBDA([TERM],
		     IF NULLLISTP(INTERSECTION(SHOWRATVARS(TERM),ARGDUM))
		     THEN NEXTLAYER(TERM)
		     ELSE OPMAP(TERM,OP_FCN_LIST)),
	      EXP)],
  DECLARE([OP_FCN_LIST,VPSDUM],SPECIAL),
  IF INPART(VPSDUM,0)="+"
  THEN FACEXPFORM1(VPSDUM)
  ELSE VPSDUM)$

VSTAR(EXP):=BLOCK(
  [ARGSEXPDUM:ARGS(EXP),PARTITIONDUM,EXPIARGDUM],
  FOR IARGDUM IN ARGSEXPDUM DO
    IF INPART(IARGDUM,0)="+"
    THEN IF NOT NULLLISTP(INTERSECTION(ARGDUM,SHOWRATVARS(IARGDUM)))
	 THEN (PARTITIONDUM:ORPARTITIONL(FACEXPFORM(IARGDUM),"+",ARGDUM),
	       EXPIARGDUM:IFMULTTHRU(1/IARGDUM,EXP),
	       EXP:IFMULTTHRU(EXPIARGDUM,LAST(PARTITIONDUM))+
			EXPIARGDUM*NEXTLAYER(FIRST(PARTITIONDUM)))
	 ELSE EXP:EXP/IARGDUM*NEXTLAYER(IARGDUM),
  IF INPART(EXP,0)="+"
  THEN FACEXPFORM1(EXP)
  ELSE EXP)$

FPLUS(EXP):=BLOCK(
  DECLARE([LIST,OP_FCN_LIST],SPECIAL),
  IFLOPMAP("+",
	   LAMBDA([DUM],OPMAP(DUM,OP_FCN_LIST)),
	   LISTTOSUM(LDELETE(LIST,ARGS(EXP)))))$

FEXPT(EXP):=BLOCK(
  [IP1EXP:ZEROSUBST(LIST,INPART(EXP,1))],
  DECLARE([LIST,IP1EXP],SPECIAL),
  IF IP1EXP=0
  THEN 0
  ELSE IP1EXP^ZEROSUBST(LIST,INPART(EXP,2)))$

FSTAR(EXP):=BLOCK(
  DECLARE(LIST,SPECIAL),
  IF LDELETE(LIST,ARGS(EXP))=ARGS(EXP)
  THEN MAP(LAMBDA([DUM],OPMAP(DUM,OP_FCN_LIST)),EXP)
  ELSE 0)$

ZEROSUBST(LIST,EXP):=
  IF MEMBER(EXP,LIST)
  THEN 0
  ELSE OPMAP(EXP,["*",'FSTAR,"+",'FPLUS,"^",'FEXPT])$

IFMULTTHRU(EXP1,EXP2):=
  IF INPART(EXP2,0)="+"
  THEN MULTTHRU(EXP1,EXP2)
  ELSE EXP1*EXP2$

Collect&&

COLLECTTEN(EXP):=COLLECTTERMSL(EXP,LISTOFTENS(EXP))$

COLLECTTERMS(EXP,[VARLIST]):=COLLECTTERMSL(EXP,VARLIST)$

COLLECTTERMSL(EXP,VARLIST):=BLOCK(
  [PARTSWITCH:TRUE,INFLAG:TRUE,PIECE],
  APPLY('COLLECTTERMS0,CONS(EXP,ARGSPLIT(EXP,VARLIST))))$

COLLECTTERMS0(EXP,THISLEVELDUM,NEXTLEVELDUM):=BLOCK(
  [IFORP:TRUE,SPLITDUM1,SPLITDUM2,SPLITDUM3,ANSLIST:[],FDUM,
   PREVDUM,LSPLIT3,ANSDUM,LASTDUMSAVE,PREVLASTDUM,
   RTHISLEVELDUM,FTHISLEVELDUM],
  MODEDECLARE(LSPLIT3,FIXNUM),
  DECLARE([FDUM,SPLITDUM3,ANSDUM],SPECIAL),
  IF EXP=0 THEN RETURN(0),
  IF NULLLISTP(THISLEVELDUM) OR FREEOFL(THISLEVELDUM,EXP)
  THEN IF NULLLISTP(NEXTLEVELDUM)
       THEN RETURN(EXP)
       ELSE (SPLITDUM1:ORPARTITIONL(EXP,"+",NEXTLEVELDUM),
	     RETURN(APPLY('COLLECTTERMS,CONS(FIRST(SPLITDUM1),NEXTLEVELDUM))
		    +IFLOPMAP("+",
			      LAMBDA([TERMDUM],
				     APPLY('COLLECTTERMS,
					   CONS(TERMDUM,NEXTLEVELDUM))),
			      LAST(SPLITDUM1)))),
  RTHISLEVELDUM:REST(THISLEVELDUM),
  IF FREEOF(FTHISLEVELDUM:FIRST(THISLEVELDUM),EXP)
  THEN RETURN(COLLECTTERMS0(EXP,RTHISLEVELDUM,NEXTLEVELDUM)),
  SPLITDUM1:ORPARTITIONL(EXP,"+",THISLEVELDUM),
  SPLITDUM2:ORPARTITION(LAST(SPLITDUM1),"+",FTHISLEVELDUM),
  ANSDUM:APPLY('COLLECTTERMS,CONS(FIRST(SPLITDUM1),NEXTLEVELDUM))
	 +COLLECTTERMS0(FIRST(SPLITDUM2),RTHISLEVELDUM,NEXTLEVELDUM),
  IF NOT LOPPLUSP(SPLITDUM3:LAST(SPLITDUM2))
  THEN RETURN(ANSDUM+APPLY('COLLECTTERMS,CONS(SPLITDUM3,NEXTLEVELDUM))),
  SPLITDUM3:SORT(MAPLIST(LAMBDA([TERM],ORPARTITION(TERM,"*",FTHISLEVELDUM)),
		 SPLITDUM3),
		 'ORDERLASTP),
  LSPLIT3:LENGTH(SPLITDUM3)-1,
  PREVLASTDUM:INPART(SPLITDUM3,1,2),
  PREVDUM:INPART(SPLITDUM3,1,1),
  SPLITDUM3:REST(SPLITDUM3),
  FOR IDUM THRU LSPLIT3 DO
       (IF (LASTDUMSAVE:INPART(SPLITDUM3,IDUM,2))=PREVLASTDUM
	THEN PREVDUM:PREVDUM+INPART(SPLITDUM3,IDUM,1)
	ELSE (ANSLIST:ENDCONS([PREVDUM,PREVLASTDUM],ANSLIST),
	      PREVDUM:INPART(SPLITDUM3,IDUM,1),
	      PREVLASTDUM:LASTDUMSAVE),
	IF IDUM=LSPLIT3
	THEN ANSLIST:ENDCONS([PREVDUM,PREVLASTDUM],ANSLIST)),
  LISTTOSUM(MAPLIST(LAMBDA([DUM],
			   IF FREEOFL(RTHISLEVELDUM,FDUM:FIRST(DUM))
			   THEN APPLY('COLLECTTERMS,CONS(FDUM,NEXTLEVELDUM))
				    *LAST(DUM)
			   ELSE MULTTHRUSPLIT(LAST(DUM),
					      COLLECTTERMS0(FDUM,RTHISLEVELDUM,
							    NEXTLEVELDUM),
					      RTHISLEVELDUM)),
		    ANSLIST))+ANSDUM)$

ARGSPLIT(EXP,LIST):=BLOCK(
  [LISTARGSDUM:[],NEWLIST:[]],
  FOR ARG IN LIST DO
      IF LISTP(ARG)
      THEN LISTARGSDUM:APPEND(LISTARGSDUM,ARG)
      ELSE IF OPERATOR0P(ARG)
	   THEN NEWLIST:
		APPEND(NEWLIST,APPLY('LISTOFOPS_NONRAT,CONS(EXP,ARGS(ARG))))
	   ELSE NEWLIST:CONS(ARG,NEWLIST),
  [NEWLIST,LISTARGSDUM])$

MULTTHRUSPLIT(FACTORDUM,SUMDUM,RTHISLEVELDUM):=BLOCK(
  [SPLITDUM1:ORPARTITIONL(SUMDUM,"+",RTHISLEVELDUM)],
  MULTTHRU(FACTORDUM,LAST(SPLITDUM1))+FACTORDUM*FIRST(SPLITDUM1))$

AUTOFORM(EXP):=APPLY(GET('FACSUM,'AUTOMATIC),[EXP])$

IF GET('FACSUM,'AUTOMATIC)=FALSE
THEN PUT('FACSUM,'NONUMFACTOR,'AUTOMATIC)$

SQFRFACSUM([ARGLIST]):=BLOCK(
  [DUM,AUTODUM:GET('FACSUM,'AUTOMATIC)],
  DECLARE([AUTODUM,DUM],SPECIAL),
  PUT('FACSUM,'SQFR,'AUTOMATIC),
  DUM:FACSUML(ARGLIST),
  PUT('FACSUM,AUTODUM,'AUTOMATIC),
  DUM)$

EVAL_WHEN(BATCH,TTYOFF:FALSE)$