7 用 MAXIMA 编程
到现在为止, 我们已经在交互模式下用过了 MAXIMA,就象个计算器一样. 然而, 对于那些扯进了反复控制次序的计算, 还是运行一个程序来得方便. 这里我们展示了一个短且简单的程序,用来计算一个有着两个变量 x 和 y 的函数 f 的临界点. 这个程序提示用户输入函数 f, 然后它计算 fx 和 fy 的偏导数, 然后,它使用 MAXIMA 命令 SOLVE 去获得 fx = fy = 0 的解. 这个程序是在 MAXIMA 之外用一个文本编辑器写得, 然后用BATCH 命令装载进 MAXIMA. 下面是程序列表:
/* --------------------------------------------------------------------------
this is file critpts.max:
as you can see, comments in maxima are like comments in C
Nelson Luis Dias, nldias@simepar.br
created 20000707
updated 20000707
--------------------------------------------------------------------------- */
critpts():=(
print("program to find critical points"),
/* ---------------------------------------------------------------------------
asks for a function
--------------------------------------------------------------------------- */
f:read("enter f(x,y)"),
/* ---------------------------------------------------------------------------
echoes it, to make sure
--------------------------------------------------------------------------- */
print("f = ",f),
/* ---------------------------------------------------------------------------
produces a list with the two partial derivatives of f
--------------------------------------------------------------------------- */
eqs:[diff(f,x),diff(f,y)],
/* ---------------------------------------------------------------------------
produces a list of unknowns
--------------------------------------------------------------------------- */
unk:[x,y],
/* ---------------------------------------------------------------------------
solves the system
--------------------------------------------------------------------------- */
solve(eqs,unk)
)$
这个程序 (实际上是个没有参数的函数) 叫做 critpts. 每一行都是一个有效的, 可以由键盘上执行的 MAXIMA 命令, 它们由逗号隔开. 偏导数被贮存在一个叫做 eqs 的变量中, 未知数贮存在 unk 中. 这里是它运行的例子:
(C1) batch("critpts.max");
batching #/home/nldias/work/papers2000/intromax/critpts.max
(C2)
(C2) critpts() := (PRINT("program to find critical points"),
f : READ("enter f(x,y)"), PRINT("f = ", f), eqs : [DIFF(f, x), DIFF(f, y)],
unk : [x, y], SOLVE(eqs, unk))
(C3) critpts();
program to find critical points
enter f(x,y)
%e^(x^3+y^2)*(x+y);
2 3
y + x
f = (y + x) %E
(D3) [[x = 0.4588955685487 %I + 0.35897908710869,
y = 0.49420173682751 %I - 0.12257873677837],
[x = 0.35897908710869 - 0.4588955685487 %I,
y = - 0.49420173682751 %I - 0.12257873677837],
[x = 0.41875423272348 %I - 0.69231242044203,
y = 0.4559120701117 - 0.86972626928141 %I],
[x = - 0.41875423272348 %I - 0.69231242044203,
y = 0.86972626928141 %I + 0.4559120701117]]
8 MAXIMA 函数提要
看在 maxima-5.4/info/ 目录中 texinfo 或 html 格式的手册. 从 MAXIMA 本身, 你可以用 DESCRIBE(function name).
ALLROOTS(A)
Finds all the (generally complex) roots of the polynomial equation A,and lists them in NUMERical format (i.e. to 16 significant figures).
APPEND(A,B)
Appends the list B to the list A, resulting in a single list.
BATCH(A)
Loads and runs a BATCH program with filename A.
COEFF(A,B,C)
Gives the coefficient of B raised to the power C in expression A.
CONCAT(A,B)
Creates the symbol AB.
CONS(A,B)
Adds A to the list B as its first element.
DEMOIVRE(A)
Transforms all complex exponentials in A to their trigonometric equivalents.
DENOM(A)
Gives the denominator of A.
DEPENDS(A,B)
Declares A to be a function of B. This is useful for writing unevaluated derivatives, as in specifying differential equations.
DESOLVE(A,B)
Attempts to solve a linear system A of ODE's for unknowns B using Laplace transforms.
DETERMINANT(A)
Returns the determinant of the square matrix A.
DIFF(A,B1,C1,B2,C2,...,Bn,Cn)
Gives the mixed partial derivative of A with respect to each Bi, Citimes. For brevity, DIFF(A,B,1) may be represented by DIFF(A,B).'DIFF(...) represents the unevaluated derivative, useful in specifyinga differential equation.
EIGENVALUES(A)
Returns two lists,the first being the eigenvalues of the square matrix A, and the secondbeing their respective multiplicities.
EIGENVECTORS(A)
Does everything that EIGENVALUES does, and adds a list of the eigenvectors of A.
ENTERMATRIX(A,B)
Cues the user to enter an A × B matrix, element by element.
EV(A,B1,B2,...,Bn)
Evaluates A subject to the conditions Bi. In particular the Bi may beequations, lists of equations (such as that returned by SOLVE), orassignments, in which cases EV ``plugs'' the Bi into A. The Bi may alsobe words such as NUMER (in which case the result is returned innumerical format), DETOUT (in which case any matrix inverses in A areperformed with the determinant factored out), or DIFF (in which caseall differentiations in A are evaluated, i.e., 'DIFF in A is replacedby DIFF). For brevity in a manual command (i.e., not inside auser-defined function), the EV may be dropped, shortening the syntax toA,B1,B2,...,Bn.
EXPAND(A)
Algebraically expands A. In particular multiplication is distributed over addition.
EXPONENTIALIZE(A)
Transforms all trigonometric functions in A to their complex exponential equivalents.
FACTOR(A)
Factors A.
FREEOF(A,B)
Is true if the variable A is not part of the expression B.
GRIND(A)
Displays a variable or function A in a compact format. When used withWRITEFILE and an editor outside of MAXIMA, it offers a scheme forproducing BATCH files which include MAXIMA-generated expressions.
IDENT(A)
Returns an A × A identity matrix.
IMAGPART(A)
Returns the imaginary part of A.
INTEGRATE(A,B)
Attempts to find the indefinite integral of A with respect to B.
INTEGRATE(A,B,C,D)
Attempts to find the indefinite integral of A with respect to B. takenfrom B = C to B = D. The limits of integration C and D may be taken isINF (positive infinity) of MINF (negative infinity).
INVERT(A)
Computes the inverse of the square matrix A.
KILL(A)
Removes the variable A with all its assignments and properties from the current MAXIMA environment.
LIMIT(A,B,C)
Gives the limit of expression A as variable B approaches the value C. The latter may be taken as INF of MINF as in INTEGRATE.
LHS(A)
Gives the left-hand side of the equation A.
LOADFILE(A)
Loads a disk file with filename A from the current default directory.The disk file must be in the proper format (i.e. created by a SAVEcommand).
MAKELIST(A,B,C,D)
Creates a list of A's (each of which presumably depends on B), concatenated from B = C to B = D
MAP(A,B)
Maps the function A onto the subexpressions of B.
MATRIX(A1,A2,...,An)
Creates a matrix consisting of the rows Ai, where each row Ai is a list of m elements, [B1, B2, ..., Bm].
NUM(A)
Gives the numerator of A.
ODE2(A,B,C)
Attempts to solve the first- or second-order ordinary differential equation A for B as a function of C.
PART(A,B1,B2,...,Bn)
First takes the B1th part of A, then the B2th part of that, and so on.
PLAYBACK(A)
Displays the last A (an integer) labels and their associatedexpressions. If A is omitted, all lines are played back. See the Manualfor other options.
RATSIMP(A)
Simplifies A and returns a quotient of two polynomials.
REALPART(A)
Returns the real part of A.
RHS(A)
Gives the right-hand side of the equation A.
SAVE(A,B1,B2,..., Bn)
Creates a disk file with filename A in the current default directory,of variables, functions, or arrays Bi. The format of the file permitsit to be reloaded into MAXIMA using the LOADFILE command. Everything(including labels) may be SAVEd by taking B1 equal to ALL.
SOLVE(A,B)
Attempts to solve the algebraic equation A for the unknown B. A listof solution equations is returned. For brevity, if A is an equation ofthe form C = 0, it may be abbreviated simply by the expression C.
STRING(A)
Converts A to MACSYMA's linear notation (similar to FORTRAN's) just asif it had been typed in and puts A into the buffer for possibleediting. The STRING'ed expression should not be used in a computation.
STRINGOUT(A,B1,B2,...,Bn)
Creates a disk file with filename A in the current default directory,of variables (e.g. labels) Bi. The file is in a text format and is notreloadable into MAXIMA. However the strungout expressions can beincorporated into a FORTRAN, BASIC or C program with a minimum ofediting.
SUBST(A,B,C)
Substitutes A for B in C.
TAYLOR(A,B,C,D)
Expands A in a Taylor series in B about B = C, up to and including theterm (B-C)D. MAXIMA also supports Taylor expansions in more than oneindependent variable; see the Manual for details.
TRANSPOSE(A)
Gives the transpose of the matrix A.
TRIGEXPAND(A)
Is a trig simplification function which uses the sum-of-anglesformulas to simplify the arguments of individual SIN's or COS's. Forexample, trigexpand(sin(x+y)) gives COS(x) SIN(y) + SIN(x) COS(y).
TRIGREDUCE(A)
Is a trig simplification function which uses trig identities toconvert products and powers of SIN and COS into a sum of terms, each ofwhich contains only a single SIN or COS. For example,trigreduce(sin(x)^2) gives (1 - COS(2x))/2.
TRIGSIMP(A)
Is atrig simplification function which replaces TAN, SEC, etc., by theirSIN and COS equivalents. It also uses the identity SIN()2 + COS()2 = 1.
--------------------------------------------------------------------------------
Footnotes:
1 Adapted from ``Perturbation Methods, Bifurcation Theory and Computer Algebra'' by Rand and Armbruster, Springer, 1987
2 Adapted to LATEX and HTML by Nelson L. Dias (nldias@simepar.br),SIMEPAR Technological Institute and Federal University of Paran? Brazil |