| www.delorie.com/gnu/docs/maxima/maxima_119.html | search |
![]() Buy GNU books! | |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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.
MPRINT([X]) ::= BUILDQ([U : x], if (debuglevel > 3) print(splice(u))); |
Including a call like
MPRINT("matrix is ",MAT,"with length",LENGTH(MAT))
|
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 |
+
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)) |
expand.
F[N]:=EXPAND((-((N^2-2*N+1)*F[N-1]+F[N-2]+F[N-3]) /(N^2-N))); |
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))), |
With the Expand (C28) f[6]; (D28) -AA1/8-13*AA0/180 |
(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
|
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 |