www.delorie.com/gnu/docs/maxima/maxima_119.html   search  
 
Buy GNU books!


Maxima Manual

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

37.3.2 SIMPLIFICATION

The arguments to BUILDQ need to be protected from simplification until the substitutions have been carried out. This code should affect that by using '.

buildq can be useful for building functions on the fly. One of the powerful things about MAXIMA is that you can have your functions define other functions to help solve the problem. Further below we discuss building a recursive function, for a series solution. This defining of functions inside functions usually uses define, which evaluates its arguments. A number of examples are included under splice.

Function: SPLICE (atom)
This is used with buildq to construct a list. This is handy for making argument lists, in conjunction with BUILDQ

 
MPRINT([X]) ::= BUILDQ([U : x],
  if (debuglevel > 3) print(splice(u)));

Including a call like
 
MPRINT("matrix is ",MAT,"with length",LENGTH(MAT))
is equivalent to putting in the line

 
IF DEBUGLEVEL > 3
  THEN PRINT("matrix is ",MAT,"with length",
              LENGTH(MAT))

A more non trivial example would try to display the variable values AND their names.
 
MSHOW(A,B,C) 

should become

 
PRINT('A,"=",A,",",'B,"=",B,", and",'C,"=",C)

so that if it occurs as a line in a program we can print values.

 
(C101) foo(x,y,z):=mshow(x,y,z);
(C102) foo(1,2,3);
X = 1 , Y = 2 , and Z = 3

The actual definition of mshow is the following. Note how buildq lets you build 'QUOTED' structure, so that the 'u lets you get the variable name. Note that in macros, the RESULT is a piece of code which will then be substituted for the macro and evaluated.

 
MSHOW([lis])::=BLOCK([ans:[],N:LENGTH(lis)],
	   FOR i THRU N DO
	       (ans:APPEND(ans,
			   BUILDQ([u:lis[i]],
				  ['u,"=",u])),
		IF i < N
		    THEN ans
		    :APPEND(ans,
			    IF i < N-1 THEN [","]
			        ELSE [", and"])),
	   BUILDQ([U:ans],PRINT(SPLICE(u))))

The splice also works to put arguments into algebraic operations:

 
(C108) BUILDQ([A:'[B,C,D]],+SPLICE(A));
(D108) D+C+B
Note how the simplification only occurs AFTER the substitution, The operation applying to the splice in the first cae is the + while in the second it is the *, yet logically you might thing splice(a)+splice(A) could be replaced by 2*splice(A). No simplification takes place with the buildq To understand what SPLICE is doing with the algebra you must understand that for MAXIMA, a formula an operation like A+B+C is really internally similar to +(A,B,C), and similarly for multiplication. Thus *(2,B,C,D) is 2*B*C*D

 
(C114) BUILDQ([A:'[B,C,D]],+SPLICE(A));
(D114) D+C+B
 
(C111) BUILDQ([A:'[B,C,D]],SPLICE(A)+SPLICE(A));
(D111) 2*D+2*C+2*B
  but
(C112) BUILDQ([A:'[B,C,D]],2*SPLICE(A));
(D112) 2*B*C*D

Finally the buildq can be invaluable for building recursive functions. Suppose your program is solving a differential equation using the series method, and has determined that it needs to build a recursion relation
 
F[N]:=(-((N^2-2*N+1)*F[N-1]+F[N-2]+F[N-3])/(N^2-N))
and it must do this on the fly inside your function. Now you would really like to add expand.
 
F[N]:=EXPAND((-((N^2-2*N+1)*F[N-1]+F[N-2]+F[N-3])
  /(N^2-N)));
but how do you build this code. You want the expand to happen each time the function runs, NOT before it.
 
kill(f),
val:(-((N^2-2*N+1)*F[N-1]+F[N-2]+F[N-3])/(N^2-N)),
define(f[n],buildq([u:val],expand(u))),
does the job. This might be useful, since when you do
 
With the Expand
(C28) f[6];
(D28) -AA1/8-13*AA0/180
where as without it is kept unsimplified, and even after 6 terms it becomes:
 
(C25) f[6];
(D25) (5*(-4*(-3*(-2*(AA1+AA0)+AA1+AA0)/2
	    -(AA1+AA0)/2+AA1)
	/3
	-(-2*(AA1+AA0)+AA1+AA0)/6+(-AA1-AA0)/2)
      /4
      +(-3*(-2*(AA1+AA0)+AA1+AA0)/2
       -(AA1+AA0)/2+AA1)
       /12-(2*(AA1+AA0)-AA1-AA0)/6)
      /30
The expression quickly becomes complicated if not simplified at each stage, so the simplification must be part of the definition. Hence the buildq is useful for building the form.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright © 2003   by The Free Software Foundation     Updated Jun 2003