1 \documentclass[12pt
]{article
}
3 \usepackage[driverfallback=dvipdfm,colorlinks,linkcolor=red
]{hyperref
}
5 \addtolength{\topmargin}{-
0.5in
}
6 \addtolength{\textheight}{1in
}
7 \addtolength{\footskip}{0.5in
}
9 \setlength{\parskip}{8pt
}
10 \setlength{\parindent}{0pt
}
12 \title{Minimal Maxima\\
13 {\small Released under the terms of the GNU General Public License, Version
2}}
14 \author{Robert Dodier
}
20 \section{What is Maxima?
}
22 Maxima
\footnote{Home page:
\url{https://maxima.sourceforge.io
} \\
23 Documents:
\url{https://maxima.sourceforge.io/documentation.html
} \\
24 Reference manual:
\url{https://maxima.sourceforge.io/docs/manual/en/maxima.html
}}
25 is a system for working with expressions,
26 such as $x + y$, $
\sin (a + b
\pi)$, and $u
\cdot v - v
\cdot u$.
28 Maxima is not much worried about the meaning of an expression.
29 Whether an expression is meaningful is for the user to decide.
31 Sometimes you want to assign values to the unknowns
32 and evaluate the expression.
33 Maxima is happy to do that.
34 But Maxima is also happy to postpone assignment of specific values;
35 you might carry out several manipulations of an expression,
36 and only later (or never) assign values to unknowns.
38 Let's see some examples.
42 \item I want to calculate the volume of a sphere.
45 (
%i1) V: 4/3 * %pi * r^3;
52 \item The radius is
10.
59 \item $V$ is the same as before; Maxima won't change $V$ until I tell it to do so.
69 \item Please re-evaluate $V$, Maxima.
78 \item I'd like to see a numerical value instead of an expression.
82 (
%o5) 4188.79020478639
89 Everything in Maxima is an expression,
90 including mathematical expressions, objects, and programming constructs.
91 An expression is either an atom, or an operator together with its arguments.
93 An atom is a symbol (a name), a string enclosed in quotation marks,
94 or a number (integer or floating point).
96 All nonatomic expressions are represented as $
\mathit{op
}(a_1,
\ldots, a_n)$
97 where $
\mathit{op
}$ is the name of an operator
98 and $a_1,
\ldots, a_n$ are its arguments.
99 (The expression may be displayed differently,
100 but the internal representation is the same.)
101 The arguments of an expression can be atoms or nonatomic expressions.
103 Mathematical expressions have a mathematical operator,
104 such as $+ \; - \; * \; / \; < \; = \; >$
105 or a function evaluation such as $
\mathbf{sin
}(x),
\mathbf{bessel
\_j}(n, x)$.
106 In such cases, the operator is the function.
108 Objects in Maxima are expressions.
109 A list $
[a_1,
\ldots, a_n
]$ is an expression $
\mathbf{list
}(a_1,
\ldots, a_n)$.
110 A matrix is an expression
112 \mathbf{matrix
}(
\mathbf{list
}(a_
{1,
1},
\ldots, a_
{1,n
}),
\ldots,
\mathbf{list
}(a_
{m,
1},
\ldots, a_
{m,n
}))
115 Programming constructs are expressions.
116 A code block $
\mathbf{block
} (a_1,
\ldots, a_n)$ is an expression with operator $
\mathbf{block
}$
117 and arguments $a_1,
\ldots, a_n$.
118 A conditional statement $
\mathbf{if\
} a
\mathbf{\ then\
} b
\mathbf{\ elseif\
} c
\mathbf{\ then\
} d$
119 is an expression $
\mathbf{if
}(a, b, c, d)$.
120 A loop $
\mathbf{for\
} a
\mathbf{\ in\
} L
\mathbf{\ do\
} S$ is an expression similar to $
\mathbf{do
}(a, L, S)$.
122 The Maxima function $
\mathbf{op
}$ returns the operator of a nonatomic expression.
123 The function $
\mathbf{args
}$ returns the arguments of a nonatomic expression.
124 The function $
\mathbf{atom
}$ tells whether an expression is an atom.
126 Let's see some more examples.
130 \item Atoms are symbols, strings, and numbers.
131 I've grouped several examples into a list so we can see them all together.
132 % [a, foo, foo_bar, "Hello, world!", 42, 17.29];
135 (
%i2) [a, foo, foo_bar, "Hello, world!", 42, 17.29];
136 (
%o2) [a, foo, foo_bar, Hello, world!, 42, 17.29]
139 \item Mathematical expressions.
140 % [a + b + c, a * b * c, foo = bar, a*b < c*d];
143 (
%i1) [a + b + c, a * b * c, foo = bar, a*b < c*d];
144 (
%o1) [c + b + a, a b c, foo = bar, a b < c d]
147 \item Lists and matrices.
148 The elements of a list or matrix can be any kind of expression,
149 even another list or matrix.
150 % L: [a, b, c, %pi, %e, 1729, 1/(a*d - b*c)];
151 % L2: [a, b, [c, %pi, [%e, 1729], 1/(a*d - b*c)]];
154 % M: matrix ([%pi, 17], [29, %e]);
155 % M2: matrix ([[%pi, 17], a*d - b*c], [matrix ([1, a], [b, 7]), %e]);
160 (
%i1) L: [a, b, c, %pi, %e, 1729, 1/(a*d - b*c)];
162 (
%o1) [a, b, c, %pi, %e, 1729, ---------]
164 (
%i2) L2: [a, b, [c, %pi, [%e, 1729], 1/(a*d - b*c)]];
166 (
%o2) [a, b, [c, %pi, [%e, 1729], ---------]]
174 (
%o4) [c, %pi, [%e, 1729], ---------]
176 (
%i5) M: matrix ([%pi, 17], [29, %e]);
180 (
%i6) M2: matrix ([[%pi, 17], a*d - b*c], [matrix ([1, a], [b, 7]), %e]);
181 [ [%pi, 17] a d - b c ]
194 \item Programming constructs are expressions.
195 $x : y$ means assign $y$ to $x$; the value of the assignment expression is $y$.
196 $
\mathbf{block
}$ groups several expressions, and evaluates them one after another;
197 the value of the block is the value of the last expression.
200 % block ([a], a: 42, a^2 - 1600) + block ([b], b: 5, %pi^b);
201 % (if a > 1 then %pi else %e) + (if b < 0 then 1/2 else 1/7);
204 (
%i1) (a: 42) - (b: 17);
208 (
%i3) block ([a], a: 42, a^2 - 1600) + block ([b], b: 5, %pi^b);
211 (
%i4) (if a > 1 then %pi else %e) + (if b < 0 then 1/2 else 1/7);
217 \item $
\mathbf{op
}$ returns the operator, $
\mathbf{args
}$ returns the arguments,
218 $
\mathbf{atom
}$ tells whether an expression is an atom.
223 % op (foo (p, q) := p - q);
225 % args (p + q > p*q);
226 % args (sin (p + q));
228 % args (foo (p, q) := p - q);
231 % atom (sin (p + q));
236 (
%i2) op (p + q > p*q);
238 (
%i3) op (sin (p + q));
240 (
%i4) op (foo (p, q));
242 (
%i5) op (foo (p, q) := p - q);
246 (
%i7) args (p + q > p*q);
248 (
%i8) args (sin (p + q));
250 (
%i9) args (foo (p, q));
252 (
%i10) args (foo (p, q) := p - q);
253 (
%o10) [foo(p, q), p - q]
258 (
%i13) atom (sin (p + q));
262 \item Operators and arguments of programming constructs.
263 The single quote tells Maxima to construct the expression but postpone evaluation.
264 We'll come back to that later.
265 % op ('(block ([a], a: 42, a^2 - 1600)));
266 % op ('(if p > q then p else q));
267 % op ('(for x in L do print (x)));
268 % args ('(block ([a], a: 42, a^2 - 1600)));
269 % args ('(if p > q then p else q));
270 % args ('(for x in L do print (x)));
273 (
%i1) op ('(block ([a], a: 42, a^2 - 1600)));
275 (
%i2) op ('(if p > q then p else q));
277 (
%i3) op ('(for x in L do print (x)));
279 (
%i4) args ('(block ([a], a: 42, a^2 - 1600)));
281 (
%o4) [[a], a : 42, a - 1600]
282 (
%i5) args ('(if p > q then p else q));
283 (
%o5) [p > q, p, true, q]
284 (
%i6) args ('(for x in L do print (x)));
285 (
%o6) [x, L, false, false, false, false, print(x)]
292 The value of a symbol is an expression associated with the symbol.
293 Every symbol has a value;
294 if not otherwise assigned a value, a symbol evaluates to itself.
295 (E.g., $x$ evaluates to $x$ if not otherwise assigned a value.)
297 Numbers and strings evaluate to themselves.
299 A nonatomic expression is evaluated approximately as follows.
302 \item Each argument of the operator of the expression is evaluated.
303 \item If an operator is associated with a callable function, the function is called,
304 and the return value of the function is the value of the expression.
307 Evaluation is modified in several ways.
308 Some modifications cause less evaluation:
311 \item Some functions do not evaluate some or all of their arguments,
312 or otherwise modify the evaluation of their arguments.
313 % Examples: $\mathbf{kill}$, $\mathbf{save}$, $\mathbf{sum}$, $\mathbf{:=}$ (function definition).
314 \item A single quote $'$ prevents evaluation.
316 \item $'a$ evaluates to $a$. Any other value of $a$ is ignored.
317 \item $'f(a_1,
\ldots, a_n)$ evaluates to $f(
\mathbf{ev
}(a_1),
\ldots,
\mathbf{ev
}(a_n))$.
318 That is, the arguments are evaluated but $f$ is not called.
319 \item $'(
\ldots)$ prevents evaluation of any expressions inside $(...)$.
323 Some modifications cause more evaluation:
326 \item Two single quotes $''a$ causes an extra evaluation at the time the expression $a$ is parsed.
327 \item $
\mathbf{ev
}(a)$ causes an extra evaluation of $a$ every time $
\mathbf{ev
}(a)$ is evaluated.
328 \item The idiom $
\mathbf{apply
}(f,
[a_1,
\ldots, a_n
])$ causes the evaluation
329 of the arguments $a_1,
\ldots, a_n$ even if $f$ ordinarily quotes them.
330 \item $
\mathbf{define
}$ constructs a function definition like $
\mathbf{:=
}$,
331 but $
\mathbf{define
}$ evaluates the function body while $
\mathbf{:=
}$ quotes it.
334 Let's consider how some expressions are evaluated.
338 \item Symbols evaluate to themselves if not otherwise assigned a value.
339 % block (a: 1, b: 2, e: 5);
343 (
%i1) block (a: 1, b: 2, e: 5);
345 (
%i2) [a, b, c, d, e];
346 (
%o2) [1, 2, c, d, 5]
349 \item Arguments of operators are ordinarily evaluated (unless evaluation is prevented one way or another).
350 % block (x: %pi, y: %e);
356 (
%i1) block (x: %pi, y: %e);
366 \item If an operator corresponds to a callable function,
367 the function is called (unless prevented).
368 Otherwise evaluation yields another expression with the same operator.
369 % foo (p, q) := p - q;
375 (
%i1) foo (p, q) := p - q;
376 (
%o1) foo(p, q) := p - q
385 \item Some functions quote their arguments.
386 Examples: $
\mathbf{save
}$, $
\mathbf{:=
}$, $
\mathbf{kill
}$.
387 % block (a: 1, b: %pi, c: x + y);
389 % save ("tmp.save", a, b, c);
396 (
%i1) block (a: 1, b: %pi, c: x + y);
399 (
%o2) [1, %pi, y + x]
400 (
%i3) save ("tmp.save", a, b, c);
408 (
%i6) kill (a, b, c);
414 \item A single quote prevents evaluation even if it would ordinarily happen.
415 % foo (x, y) := y - x;
416 % block (a: %e, b: 17);
423 (
%i1) foo (x, y) := y - x;
424 (
%o1) foo(x, y) := y - x
425 (
%i2) block (a: %e, b: 17);
437 \item Two single quotes (quote-quote) causes an extra evaluation at the time the expression is parsed.
439 % foo (x) := diff (sin (x), x);
440 % foo (x) := ''(diff (sin (x), x));
443 (
%i1) diff (sin (x), x);
445 (
%i2) foo (x) := diff (sin (x), x);
446 (
%o2) foo(x) := diff(sin(x), x)
447 (
%i3) foo (x) := ''(diff (sin (x), x));
448 (
%o3) foo(x) := cos(x)
451 \item $
\mathbf{ev
}$ causes an extra evaluation every time it is evaluated.
452 Contrast this with the effect of quote-quote.
453 % block (xx: yy, yy: zz);
461 (
%i1) block (xx: yy, yy: zz);
465 (
%i3) foo (x) := ''x;
469 (
%i5) bar (x) := ev (x);
470 (
%o5) bar(x) := ev(x)
475 \item $
\mathbf{apply
}$ causes the evaluation of arguments even if they are ordinarily quoted.
476 % block (a: aa, b: bb, c: cc);
477 % block (aa: 11, bb: 22, cc: 33);
478 % [a, b, c, aa, bb, cc];
479 % apply (kill, [a, b, c]);
480 % [a, b, c, aa, bb, cc];
482 % [a, b, c, aa, bb, cc];
485 (
%i1) block (a: aa, b: bb, c: cc);
487 (
%i2) block (aa: 11, bb: 22, cc: 33);
489 (
%i3) [a, b, c, aa, bb, cc];
490 (
%o3) [aa, bb, cc, 11, 22, 33]
491 (
%i4) apply (kill, [a, b, c]);
493 (
%i5) [a, b, c, aa, bb, cc];
494 (
%o5) [aa, bb, cc, aa, bb, cc]
495 (
%i6) kill (a, b, c);
497 (
%i7) [a, b, c, aa, bb, cc];
498 (
%o7) [a, b, c, aa, bb, cc]
501 \item $
\mathbf{define
}$ evaluates the body of a function definition.
502 % integrate (sin (a*x), x, 0, %pi);
503 % foo (x) := integrate (sin (a*x), x, 0, %pi);
504 % define (foo (x), integrate (sin (a*x), x, 0, %pi));
507 (
%i1) integrate (sin (a*x), x, 0, %pi);
511 (
%i2) foo (x) := integrate (sin (a*x), x, 0, %pi);
512 (
%o2) foo(x) := integrate(sin(a x), x, 0, %pi)
513 (
%i3) define (foo (x), integrate (sin (a*x), x, 0, %pi));
515 (
%o3) foo(x) := - - ----------
521 \section{Simplification
}
523 After evaluating an expression,
524 Maxima attempts to find an equivalent expression which is ``simpler.''
525 Maxima applies several rules which embody conventional notions of simplicity.
527 $
1 +
1$ simplifies to $
2$,
528 $x + x$ simplifies to $
2 x$,
529 and $
\mathbf{sin
}(
\mathbf{\%pi
})$ simplifies to $
0$.
532 many well-known identities are not applied automatically.
534 double-angle formulas for trigonometric functions,
535 or rearrangements of ratios such as $a/b + c/b
\rightarrow (a + c)/b$.
536 There are several functions which can apply identities.
538 Simplification is always applied unless explicitly prevented.
539 Simplification is applied even if an expression is not evaluated.
541 $
\mathbf{tellsimpafter
}$ establishes user-defined simplification rules.
543 Let's see some examples of simplification.
547 \item Quote mark prevents evaluation but not simplification.
548 When the global flag $
\mathbf{simp
}$ is $
\mathbf{false
}$,
549 simplification is prevented but not evaluation.
551 % '[1 + 1, x + x, x * x, sin (%pi)];
553 % block ([x: 1], x + x);
557 (
%i1) '[1 + 1, x + x, x * x, sin (%pi)];
559 (
%o1) [2, 2 x, x , 0]
561 (
%i3) block ([x: 1], x + x);
565 \item Some identities are not applied automatically.
566 $
\mathbf{expand
}$, $
\mathbf{ratsimp
}$, $
\mathbf{trigexpand
}$, $
\mathbf{demoivre
}$
567 are some functions which apply identities.
595 (
%i6) trigexpand (%);
596 (
%o6) 2 cos(x) sin(x)
597 (
%i7) a * exp (b * %i);
601 (
%o8) a (%i sin(b) + cos(b))
606 \section{apply, map, and lambda
}
610 \item $
\mathbf{apply
}$ constructs and evaluates an expression.
611 The arguments of the expression are always evaluated (even if they wouldn't be otherwise).
612 % apply (sin, [x * %pi]);
613 % L: [a, b, c, x, y, z];
617 (
%i1) apply (sin, [x * %pi]);
619 (
%i2) L: [a, b, c, x, y, z];
620 (
%o2) [a, b, c, x, y, z]
621 (
%i3) apply ("+", L);
622 (
%o3) z + y + x + c + b + a
625 \item $
\mathbf{map
}$ constructs and evaluates an expression for each item in a list of arguments.
626 The arguments of the expression are always evaluated (even if they wouldn't be otherwise).
627 The result is a list.
628 % map (foo, [x, y, z]);
629 % map ("+", [1, 2, 3], [a, b, c]);
630 % map (atom, [a, b, c, a + b, a + b + c]);
633 (
%i1) map (foo, [x, y, z]);
634 (
%o1) [foo(x), foo(y), foo(z)]
635 (
%i2) map ("+", [1, 2, 3], [a, b, c]);
636 (
%o2) [a + 1, b + 2, c + 3]
637 (
%i3) map (atom, [a, b, c, a + b, a + b + c]);
638 (
%o3) [true, true, true, false, false]
641 \item $
\mathbf{lambda
}$ constructs a lambda expression (i.e., an unnamed function).
642 The lambda expression can be used in some contexts like an ordinary named function.
643 $
\mathbf{lambda
}$ does not evaluate the function body.
644 % f: lambda ([x, y], (x + y)*(x - y));
647 % map (f, [1, 2, 3], [a, b, c]);
648 %% apply (lambda ([x, y], (x + y)*(x - y)), [p, q]);
649 %% map (lambda ([x, y], (x + y)*(x - y)), [1, 2, 3], [a, b, c]);
652 (
%i1) f: lambda ([x, y], (x + y)*(x - y));
653 (
%o1) lambda([x, y], (x + y) (x - y))
655 (
%o2) (a - b) (b + a)
656 (
%i3) apply (f, [p, q]);
657 (
%o3) (p - q) (q + p)
658 (
%i4) map (f, [1, 2, 3], [a, b, c]);
659 (
%o4) [(1 - a) (a + 1), (2 - b) (b + 2), (3 - c) (c + 3)]
664 \section{Built-in object types
}
666 An object is represented as an expression.
667 Like other expressions, an object comprises an operator and its arguments.
669 The most important built-in object types are lists, matrices, and sets.
675 \item A list is indicated like this: $
[a, b, c
]$.
677 \item If $L$ is a list, $L
[i
]$ is its $i$'th element. $L
[1]$ is the first element.
679 \item $
\mathbf{map
}(
\mathit{f
}, L)$ applies $
\mathit{f
}$ to each element of $L$.
681 \item $
\mathbf{apply
}(
\mathbf{"+"
}, L)$ is the sum of the elements of $L$.
683 \item $
\mathbf{for\
} x
\mathbf{\ in \
} L
\mathbf{\ do \
} \mathit{expr
}$
684 evaluates $
\mathit{expr
}$ for each element of $L$.
686 \item $
\mathbf{length
}(L)$ is the number of elements in $L$.
690 \subsection{Matrices
}
694 \item A matrix is defined like this: $
\mathbf{matrix
}(L_1,
\ldots, L_n)$
695 where $L_1,
\ldots, L_n$ are lists which represent the rows of the matrix.
697 \item If $M$ is a matrix, $M
[i, j
]$ or $M
[i
][j
]$ is its $(i, j)$'th element.
698 $M
[1,
1]$ is the element at the upper left corner.
700 \item The operator $
\mathbf{.
}$ represents noncommutative multiplication.
701 $M . L$, $L . M$, and $M . N$ are noncommutative products,
702 where $L$ is a list and $M$ and $N$ are matrices.
704 % \item $M\mathbf{\hat{ }\hat{ }}n$ is the noncommutative exponent, i.e., $M . M . \ldots . M$.
706 \item $
\mathbf{transpose
}(M)$ is the transpose of $M$.
708 \item $
\mathbf{eigenvalues
}(M)$ returns the eigenvalues of $M$.
710 \item $
\mathbf{eigenvectors
}(M)$ returns the eigenvectors of $M$.
712 \item $
\mathbf{length
}(M)$ returns the number of rows of $M$.
714 \item $
\mathbf{length
}(
\mathbf{transpose
}(M))$ returns the number of columns of $M$.
722 \item Maxima understands explicitly-defined finite sets.
723 Sets are not the same as lists; an explicit conversion is needed to change one into the other.
725 \item A set is specified within curly braces $\
{ \
}$ like this:
726 $\
{a, b, c,
\ldots\
}$ where the set elements are $a, b, c,
\ldots$.
728 \item $
\mathbf{union
} (A, B)$ is the union of the sets $A$ and $B$.
730 \item $
\mathbf{intersection
} (A, B)$ is the intersection of the sets $A$ and $B$.
732 \item $
\mathbf{cardinality
} (A)$ is the number of elements in the set $A$.
738 \subsection{Define a function
}
742 \item The operator $
\mathbf{:=
}$ defines a function, quoting the function body.
744 In this example, $
\mathbf{diff
}$ is reevaluated every time the function is called.
745 The argument is substituted for $x$ and the resulting expression is evaluated.
746 When the argument is something other than a symbol, that causes an error:
747 for $
\mathbf{foo
} (
1)$ Maxima attempts to evaluate $
\mathbf{diff
} (
\mathbf{sin
}(
1)^
2,
1)$.
748 % foo (x) := diff (sin(x)^2, x);
753 (
%i1) foo (x) := diff (sin(x)^2, x);
755 (
%o1) foo(x) := diff(sin (x), x)
757 (
%o2) 2 cos(u) sin(u)
759 Non-variable
2nd argument to diff:
765 \item $
\mathbf{define
}$ defines a function, evaluating the function body.
767 In this example, $
\mathbf{diff
}$ is evaluated only once (when the function is defined).
768 $
\mathbf{foo
} (
1)$ is OK now.
769 % define (foo (x), diff (sin(x)^2, x));
774 (
%i1) define (foo (x), diff (sin(x)^2, x));
775 (
%o1) foo(x) := 2 cos(x) sin(x)
777 (
%o2) 2 cos(u) sin(u)
779 (
%o3) 2 cos(1) sin(1)
784 \subsection{Solve an equation
}
785 % eq_1: a * x + b * y + z = %pi;
786 % eq_2: z - 5*y + x = 0;
787 % s: solve ([eq_1, eq_2], [x, z]);
789 % [subst (s[1], eq_1), subst (s[1], eq_2)];
793 (
%i1) eq_1: a * x + b * y + z = %pi;
794 (
%o1) z + b y + a x = %pi
795 (
%i2) eq_2: z - 5*y + x = 0;
796 (
%o2) z - 5 y + x = 0
797 (
%i3) s: solve ([eq_1, eq_2], [x, z]);
798 (b +
5) y -
%pi (b + 5 a) y - %pi
799 (
%o3) [[x = - ---------------, z = -----------------]]
803 (
%i5) [subst (s[1], eq_1), subst (s[1], eq_2)];
804 (b +
5 a) y -
%pi a ((b + 5) y - %pi)
805 (
%o5) [----------------- - ------------------- + b y = %pi,
807 (b +
5 a) y -
%pi (b + 5) y - %pi
808 ----------------- - --------------- -
5 y =
0]
811 (
%o6) [%pi = %pi, 0 = 0]
814 \subsection{Integrate and differentiate
}
816 $
\mathbf{integrate
}$ computes definite and indefinite integrals.
817 % integrate (1/(1 + x), x, 0, 1);
818 % integrate (exp(-u) * sin(u), u, 0, inf);
820 % integrate (1/(1 + x), x, 0, a);
821 % integrate (exp(-a*u) * sin(a*u), u, 0, inf);
822 % integrate (exp (sin (t)), t, 0, %pi);
823 % 'integrate (exp(-u) * sin(u), u, 0, inf);
826 (
%i1) integrate (1/(1 + x), x, 0, 1);
828 (
%i2) integrate (exp(-u) * sin(u), u, 0, inf);
832 (
%i3) assume (a > 0);
834 (
%i4) integrate (1/(1 + x), x, 0, a);
836 (
%i5) integrate (exp(-a*u) * sin(a*u), u, 0, inf);
840 (
%i6) integrate (exp (sin (t)), t, 0, %pi);
848 (
%i7) 'integrate (exp(-u) * sin(u), u, 0, inf);
858 $
\mathbf{diff
}$ computes derivatives.
860 % diff (sin (y*x), x);
861 % diff (sin (y*x), y);
862 % diff (sin (y*x), x, 2);
863 % 'diff (sin (y*x), x, 2);
866 (
%i1) diff (sin (y*x));
867 (
%o1) x cos(x y) del(y) + y cos(x y) del(x)
868 (
%i2) diff (sin (y*x), x);
870 (
%i3) diff (sin (y*x), y);
872 (
%i4) diff (sin (y*x), x, 2);
875 (
%i5) 'diff (sin (y*x), x, 2);
883 \subsection{Make a plot
}
885 $
\mathbf{plot2d
}$ draws two-dimensional graphs.
886 % plot2d (exp(-u) * sin(u), [u, 0, 2*%pi]);
887 % plot2d ([exp(-u), exp(-u) * sin(u)], [u, 0, 2*%pi]);
888 % xx: makelist (i/2.5, i, 1, 10);
889 % yy: map (lambda ([x], exp(-x) * sin(x)), xx);
890 % plot2d ([discrete, xx, yy]);
891 % plot2d ([discrete, xx, yy], [gnuplot_curve_styles, ["with points"]]);
894 (
%i1) plot2d (exp(-u) * sin(u), [u, 0, 2*%pi]);
896 (
%i2) plot2d ([exp(-u), exp(-u) * sin(u)], [u, 0, 2*%pi]);
898 (
%i3) xx: makelist (i/2.5, i, 1, 10);
899 (
%o3) [0.4, 0.8, 1.2, 1.6, 2.0, 2.4, 2.8, 3.2, 3.6, 4.0]
900 (
%i4) yy: map (lambda ([x], exp(-x) * sin(x)), xx);
901 (
%o4) [0.261034921143457, 0.322328869227062, .2807247779692679,
902 .2018104299334517,
.1230600248057767,
.0612766372619573,
903 .0203706503896865, -
.0023794587414574, -
.0120913057698414,
905 (
%i5) plot2d ([discrete, xx, yy]);
907 (
%i6) plot2d ([discrete, xx, yy], [gnuplot_curve_styles, ["with points"]]);
911 See also $
\mathbf{plot3d
}$.
913 \subsection{Save and load a file
}
915 $
\mathbf{save
}$ writes expressions to a file.
918 % save ("my.session", a, b);
919 % save ("my.session", all);
924 (
%i2) b: foo^2 * bar;
927 (
%i3) save ("my.session", a, b);
929 (
%i4) save ("my.session", all);
933 $
\mathbf{load
}$ reads expressions from a file.
934 % load ("my.session");
939 (
%i1) load ("my.session");
948 See also $
\mathbf{stringout
}$ and $
\mathbf{batch
}$.
950 \section{Maxima programming
}
953 % argument-quoting and argument-evaluating functions
954 % directory organization: src, tests, share, doc
956 There is one namespace, which contains all Maxima symbols.
957 There is no way to create another namespace.
959 All variables are global unless they appear in a declaration of local variables.
960 Functions, lambda expressions, and blocks can have local variables.
962 The value of a variable is whatever was assigned most recently,
963 either by explicit assignment or by assignment of a value to a local variable
964 in a block, function, or lambda expression.
965 This policy is known as
{\it dynamic scope
}.
967 If a variable is a local variable in a function, lambda expression, or block,
968 its value is local but its other properties
969 (as established by $
\mathbf{declare
}$)
971 The function $
\mathbf{local
}$ makes a variable local with respect to all properties.
973 By default a function definition is global,
974 even if it appears inside a function, lambda expression, or block.
975 $
\mathbf{local
}(f), f(x)
\mathbf{\ :=\
} \ldots$ creates a local function definition.
977 $
\mathbf{trace
}(
\mathit{foo
})$ causes Maxima to print an message when the function $
\mathit{foo
}$
978 is entered and exited.
980 Let's see some examples of Maxima programming.
984 \item All variables are global unless they appear in a declaration of local variables.
985 Functions, lambda expressions, and blocks can have local variables.
987 % (x: 42, y: 1729, z: foo*bar);
990 % lambda ([x, z], (x - z)/y);
991 % apply (%, [uu, vv]);
992 % block ([y, z], y: 65536, [x, y, z]);
995 (
%i1) (x: 42, y: 1729, z: foo*bar);
997 (
%i2) f (x, y) := x*y*z;
998 (
%o2) f(x, y) := x y z
1001 (
%i4) lambda ([x, z], (x - z)/y);
1003 (
%o4) lambda([x, z], -----)
1005 (
%i5) apply (%, [uu, vv]);
1009 (
%i6) block ([y, z], y: 65536, [x, y, z]);
1010 (
%o6) [42, 65536, z]
1013 \item The value of a variable is whatever was assigned most recently,
1014 either by explicit assignment or by assignment of a value to a local variable.
1019 % bar (x) := foo (%e);
1024 (
%i1) foo (y) := x - y;
1025 (
%o1) foo(y) := x - y
1030 (
%i4) bar (x) := foo (%e);
1031 (
%o4) bar(x) := foo(%e)
1040 \section{Lisp and Maxima
}
1043 % defining an argument-evaluating function in lisp
1044 % defining an argument-quoting function in lisp
1045 % calling a function defined in maxima from lisp
1046 % useful lisp fcns: meval, simplifya, displa
1048 The construct
{\bf :lisp
} $
\mathit{expr
}$ tells the Lisp interpreter
1049 to evaluate $
\mathit{expr
}$.
1050 This construct is recognized at the input prompt and in files processed by $
\mathbf{batch
}$,
1051 but not by $
\mathbf{load
}$.
1053 The Maxima symbol $
\mathbf{foo
}$ corresponds to the Lisp symbol \$foo,
1054 and the Lisp symbol foo corresponds to the Maxima symbol $
\mathbf{?foo
}$.
1056 {\bf :lisp
} $
\mathrm{(
}\mathbf{defun\
} \mathrm{\$foo\ (a)\ (
\ldots))
}$
1057 defines a Lisp function $
\mathrm{foo
}$ which evaluates its arguments.
1058 From Maxima, the function is called as $
\mathbf{foo
}(a)$.
1060 {\bf :lisp
} $
\mathrm{(
}\mathbf{defmspec\
} \mathrm{\$foo\ (e)\ (
\ldots))
}$
1061 defines a Lisp function $
\mathbf{foo
}$ which quotes its arguments.
1062 From Maxima, the function is called as $
\mathbf{foo
}(a)$.
1063 The arguments of $
\mathrm{\$foo
}$ are $(
\mathbf{cdr\
} e),$
1064 and $(
\mathbf{caar\
} e)$ is always $
\mathrm{\$foo
}$ itself.
1066 From Lisp, the construct $(
\mathbf{mfuncall\ '\$
}\mathrm{foo\
}a_1
\ldots a_n)$
1067 calls the function $
\mathbf{foo
}$ defined in Maxima.
1069 Let's reach into Lisp from Maxima and vice versa.
1073 \item The construct
{\bf :lisp
} $
\mathit{expr
}$ tells the Lisp interpreter
1074 to evaluate $
\mathit{expr
}$.
1083 ((MEXPT SIMP) ((MPLUS SIMP) $AA $BB)
2)
1086 \item {\bf :lisp
} $
\mathrm{(
}\mathbf{defun\
} \mathrm{\$foo\ (a)\ (
\ldots))
}$
1087 defines a Lisp function $
\mathbf{foo
}$ which evaluates its arguments.
1089 % :lisp (defun $foo (a b) `((mplus) ((mtimes) ,a ,b) $%pi))
1090 % (p: x + y, q: x - y);
1093 (
%i1) :lisp (defun $foo (a b) `((mplus) ((mtimes) ,a ,b) $%pi))
1095 (
%i1) (p: x + y, q: x - y);
1098 (
%o2) (x - y) (y + x) + %pi
1101 \item {\bf :lisp
} $
\mathrm{(
}\mathbf{defmspec\
} \mathrm{\$foo\ (e)\ (
\ldots))
}$
1102 defines a Lisp function $
\mathbf{foo
}$ which quotes its arguments.
1104 % (defmspec $bar (e) (let ((a (cdr e))) `((mplus) ((mtimes) ,@a) $%pi)))
1106 % (p: x + y, q: x - y);
1112 Type (to-maxima) to restart, ($quit) to quit Maxima.
1114 MAXIMA> (defmspec $bar (e) (let ((a (cdr e)))
1115 `((mplus) ((mtimes) ,@a) $
%pi)))
1116 #<FUNCTION (LAMBDA (E))
{B59873D
}>
1120 (
%i2) (p: x + y, q: x - y);
1124 (
%i4) bar (''p, ''q);
1125 (
%o4) (x - y) (y + x) + %pi
1128 \item From Lisp, the construct $(
\mathbf{mfuncall\ '\$
}\mathrm{foo\
}a_1
\ldots a_n)$
1129 calls the function $
\mathbf{foo
}$ defined in Maxima.
1132 % :lisp (displa (mfuncall '$blurf '((mplus) $grotz $mumble)))
1134 (
%i1) blurf (x) := x^2;
1137 (
%i2) :lisp (displa (mfuncall '$blurf '((mplus) $grotz $mumble)))