M-GSET chapter=1M-HM-GSET section=0M-HM-GSET name=Learning MacsymaM-HM-Ginclude chapter-headM-H
M-RLearning MacsymaM-S shows you how to type commands into the macsyma interactive algebraic manipulation system. You will learn a number of the basic commands, how to correct simple errors, and enough details for you to understand the kinds of operations which can be done by an algebraic manipulation ystem.
macsyma (pronounced M-RmaximaM-S^E) is a large computer program designed for the manipulation of algebraic expressions involving indeterminates, constants, and functions. macsyma can differentiate, integrate, take limits, solve equations, factor polynomials, expand functions in power series, and perform many other operations. You can use a programming language to extend macsyma's capabilities to new domains.
M-RLearning MacsymaM-S is divided into twelve short sections. A complete outline of the material they cover follows.
In chapter 1 you will learn
O How to type expressions into macsyma
O Some simple commands: expand, diff, ev, substitute
O Defining functions
O Fill this in
1.1 Typing Expressions
The section Getting started explained what to do to get to the first command prompt on your computer. We assume you are now faced with a display something like this:
% macsyma (this is what you typed)
M-Isome textM-I
(c1)
Suppose you wanted to work with the expression (x+1)3. You could type it in by using Fortran-style syntax as follows:
(c1) (x+1)**3;
The M-R;M-S and the (invisible) M-Rcarriage returnM-S terminates your command, and prompts macsyma to evaluate your expression and display the result. Your command can be several lines long, in which case you can format it any way you wish with extra spaces, tabs, or returns before the M-R;M-S. In this case of command c1 above, and every other command you type in, your expression is simplified and then evaluated. As it happens here, tthese processes the expression unchanged, and macsyma responds with the display below
3
(d1) (x + 1)
(c2)
Note that expressions are normally printed in a two-dimensional format with raised superscripts. This usually makes the expression easier to understand than a linear format, although as you will see in later displays, it sometimes requires some effort^E.
The result is assigned a label d1 which may be used in subsequent commands. You can also use a M-R$M-S to end a command, in which case the result will be computed but not displayed.
An important observation: the system is assigning values to the names c1, d1, etc. You should avoid using these same names for other purposes in your computations.
M-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QNoteM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-Q
If you choose to leave macsyma at this point, or any time a c-line prompt appears, type quit();. If you wish to stop in the midst of a computation and quit in a less orderly fashion, type -z (that's one character), to get back to the UNIX shell, and then type kill % .
M-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-QM-Q After the display, macsyma prompts you with the next line label, c2.
M-Gset name=Your Next CommandM-HM-Gset section=2M-HM-Ginclude chapter-headM-H
One of the many commands available in macsyma is expand, which converts an expression to a mathematically equivalent one, but with certain changes in the form.
Most macsyma commands have the form of a M-RfunctionM-S being applied to some arguments. In this case, the expression labelled d1 above is the single argument to expand.
(c2) expand(d1);
3 2
(d2) x + 3 x + 3 x + 1
1.2 Typing Errors
Before getting too involved, you should realize that if you make a mistake in typing, you can delete erroneous characters by using your standard UNIX erase, kill, word-kill, etc. characters. (Typically these are backspace, control-U, control-W, respectively). Some workstations provide various on-screen editing facilities, and these may also be used. macsyma provides one extra facility: If you wish to delete all lines of a multi-line command, type ?? to get a freshly re-displayed line label.
If you make a mistake but type the semi-colon and return key before you notice, the system will print a message:
(c2) expand((d1);
expand (( d1) ***$***
syntax error
please rephrase or edit.
(c2)
You should probably just retype the expression, if it is short. To use the editor, you would type the escape key and you would then enter the vi editor. This is described in further detail in M-RUsing Macsyma.M-S
Since only a small selection of commands will be covered in M-RLearning Mac^_syma,M-S you should be aware that additional information is available on almost any command in macsyma on-line. To see a blurb on, for example, the command expand, type
describe(expand);(return)
to the system. For some commands, an example is also available, and can be viewed by typing, for instance,
example(expand);
The on-line documentation may include local commands and other information that has not yet been incorporated into printed material.
More Commands
Let us consider a few additional commands and facilities.
To compute the derivative of an expression, you use the diff command. Diff(expr,var) differentiates an expression with respect to the variable var.
(c3) sin(x)*cos(x);
(d3) cos(x) sin(x)
(c4) diff(%,x);
2 2
(d4) cos (x) - sin (x)
In line c4 we used the special symbol %. This symbol is a shorthand for the previous expression computed, which in this case is labelled d3.
To differentiate an expression twice, use diff(expr,var,2).
(c5) diff(d3,x,2);
(d5) - 4 cos(x) sin(x)
The concept of differentiation in macsyma is actually a good deal more general than is illustrated by this example. Application of the chain rule is generally supported, and a
large repertoire of built-in functions and operations are provided.
1.3 Substitution and Evaluation
There are a number of ways of producing a new expression from an old one. One technique is to evaluate an expression with (temporary) asssociation of values with some of the indeterminates or variables in it. For example, to substitute x2 for every occurrence of z in the expression z exp(z) you could type
(c6) z*exp(z);
z
(d6) z %e
(c7) ev(d6,z=x**2);
2
2 x
(d7) x %e
In macsyma, the base of the natural logarithms is written %e, to distinguish it from a simple variable named e. M-V-1 is written as %i and p is written %pi.
An alternative syntax for exactly the same command as the explicit command ev (for evaluation in a context where z = x 2 ) on line c7 is to implicitly invoke the command.
(c8) d6,z=x**2;
2
2 x
(d8) x %e
The ev command has more elaborate options and uses. Ev M-RevaluatesM-S the first expression (leftmost argument) in an environment specified by the additional expressions which are either equations as shown above, or built-in M-RswitchesM-S which control macsyma.
An equivalent command in this particular case, is substitute. You can abbreviate the command as subst:
(c9) subst(x**2,z,d6);
2
2 x
(d9) x %e
Observe that if you substitute a for b in c you write subst(a,b,c);.
Note that the commands ev and subst do not change the value of z itself. The next section explains how you can assign values to variables.
1.5 Assignment
Although some very sophisticated linguistic facilities are available in macsyma, we will describe only the most basic features here.
Assignment is very familiar to programmers, and it is used to associate a M-RvalueM-S with a name. macsyma implicitly assigns values to the d-labels we have been using. You can, however, make up your own labels for using single-letter or multiple-letter names.
To assign a value to a variable a, used a colon (:) as follows:
(c10) a:%;
2
2 x
(d10) x %e
(c11) a+1;
2
2 x
(d11) x %e + 1
Sometimes users unintentionally re-use a variable such as a above as a symbols or an M-RindeterminateM-S: as though they had no value. Differentiating with respect to x is not very meaningful when x is (say) a number. If you are surprised when such values appear and wish to reuse the same name again, you can refer to the symbol a regardless of any value associated with it, you can use the expression 'a. This is pronounced M-Rquote aM-S. To permanently M-RunassignM-S a value from a, say:
(c12) a:'a;
(d12) a
Now if you ask for the value of a, you get:
(c13) a;
(d13) a
Another command which is sometimes used for the same purpose is kill(a); It also removes other possible information you might have about a, too.
1.6 Defining Functions
Function definition in macsyma is a way of storing computational routines to be used repeatedly. Functions in macsyma always return values and can therefore be used in expressions.
Sometimes it is convenient to think of functions in macsyma or other programming languages as similar to mathematical functions. Although we can also ask macsyma to compute a number approximating sin(0.5), that is not where it ends. When you use macsyma there is a real possibility that you might really mean a mathematical function rather than merely the M-Rcomputing rule associating a number with some input.M-S For example, in macsyma you can find solutions to equations involving the sine function (named sin), or you might expect sin in the answer to a differentiation problem, etc.
Nevertheless, we will abuse the term function to describe these computational routines. In the jargon of programming language descriptions, these function definiitions associate a name with some number of M-Rformal parametersM-S and a computational rule or expression or M-RbodyM-S. The value which is produced by M-RapplyingM-S the function is basically the result of substituting M-Ractual parametersM-S or values for the formal parameters in theM-RbodyM-S.
For example to define a function f (z) which will be, essentially, a short-hand for sin2z + 1 you use the following command. Note the use of M-R:=M-S.
(c14) f(z) := sin(z)**2+1;
2
(d14) f(z) := sin (z) + 1
(c15) f(x+1);
2
(d15) sin (x + 1) + 1
It is possible to define functions which use any of the built-in macsyma commands, or use other user-defined functions which may be defined with other function definition commands. macsyma allows you to define self-referential or recursive functions, and allows you to use M-RundefinedM-S functions as well. An undefined function merely returns M-RitselfM-S as its value.
macsyma has many more commands than we have illustrated here. Even so, it is unlikely that everything you might want to do has been anticipated, and therefore macsyma provides you with a complete programming language.
This may not be a comfort to you if you are totally unfamiliar with programming, but if you want to use macsyma for elaborate processing of mathematical equations or data, you may have to learn some of this. If you have some familiarity with some conventional programming languages, you may be pleasantly surprised by the additional generality afforded by M-RsymbolicM-S computation. Further on in M-RLearning MacsymaM-S we give some lessons in this language.
1.7 Equations, Part, and Solve
Sometimes it is convenient to compute with equations in macsyma . An equation is not the same as an assignment, a function definition, or a test for equality. Some programming languages overuse the symbol M-R=M-S for some of these, but macsyma does not. In macsyma, an equation is just an expression containing two subexpressions separated by an M-R=M-S. Certain simplification and evaluation operations are defined to work in (usually) conventional ways on equations, as illustrated below. Line c16 shows how to type in an equation.
(c16) x**2+2*x=y**2;
2 2
(d16) x + 2 x = y
(c17) d16+1;
2 2
(d17) x + 2 x + 1 = y + 1
The left-hand-side of an equation can be selected with the lhs command, or by the more general part command which can be used to pick apart expressions such as sums, products, etc.
(c18) lhs(%);
2
(d18) x + 2 x + 1
(c19) part(d17,2);
2
(d19) y + 1
(c20) part(%,1);
2
(d20) y
To save typing, the same result as d20 could have been obtained from part(part (d17,2),1) or even more simply, part(d17,2,1). Equations are used by some commands, such as solve. Solve returns a list of equations, that is, a sequence separated by commas and enclosed in square brackets:
(c21) x**2-1;
2
(d21) x - 1
(c22) solve(%,x);
(d22) [x = - 1, x = 1]
Notice the way the list of the two solutions is displayed. We can substitute the first of the two answers in d21, and evaluate:
(c23) d21,part(%,1);
(d23) 0
1.8 Sums, Programs, Ev Revisited
You can compute the sum of the first five positive integers, squaredby:
(c24) sum(i**2,i,1,5);
(d24) 55
Another way of providing a similar result by writing a small program, is to use a for statement.
(c25) for i:1 step 1 thru 5 do s:s+i**2 ;
(d25) done
The result is stored as the value of the variable s. If s had been 0 previously, the current value of s would be 55. Actually,
(c26) s;
(d26) s + 55
This reflects the fact that the value of s initially was just s. You can, of course, substitute 0 for the original value:
(c27) subst(0,'s,%);
(d27) 55
Note that this substitution does not affect the value of s. It just sets d27 to 55.
A more mysterious case occurs if you type
(c28) ev(s+55);
(d28) s + 165
explanation: if we had typed merely s+55, it would evaluate to (s+55)+55, or s+110. But ev evaluates M-Ronce moreM-S to get (s+55)+110. Ev is sometimes a puzzle if you do not separate those symbols that you use for programming purposes and therefore have values, and those symbols you use only for indeterminates.
In spite of the previous example, summations are not really the same as the programming languages' M-Rfor loopsM-S. Sometimes summations are merely placeholders for computations which for one reason or another cannot be done. This is an example:
(c29) 'sum(g(i),i,0,n);
n
====
\
(d29) > g(i)
/
====
i = 0
There are three reasons for this summation to be returned M-RunsummedM-S. We have not provided a M-Rfunction definitionM-S for g; the variable n is not some fixed integer value; and finally the M-RquoteM-S symbol or apostrophe (') to the left of the sum on line c29 inhibits evaluation. The effect of the quote is, in most circumstances the prevention of evaluation. Sometimes macsyma can deduce the value of a summation in closed form even though it cannot be evaluated, because of built-in simplification capabilities. Some summations can, for example, be solved by a fairly general procedure analogous to symbolic integration.
In macsyma a distinction is sometimes drawn between M-RnounM-S and M-RverbM-S forms of commands. Most functions are M-RverbsM-S in that they will be evaluated M-RawayM-S in an effort to eliminate them from expressions. To prevent such evaluation, the M-RquoteM-S illustrated above can generally be used. Some functions are nouns or verbs depending upon context such as the nature of their arguments. The trigonometric functions are this way, being nouns if they are given integer arguments, but verbs if they are given floating-point arguments, or ev'd with the numer option set:
(c30) sin(1);
(d30) sin(1)
(c31) sin(1),numer;
(d31) 0.8414709848078965
Floating-point numbers are carried to double precision. There is also an implementation of higher (arbitrarily higher!) precision M-RbigfloatsM-S available and described in M-RUsing Macsyma.M-S
More Expansion
Let us return to our very first example of a command, and provide some more details and techniques for expansion, or application of the distributive law of multiplication. In some computations where an indeterminate represents a quantity of small magnitude, you might wish to automatically drop higher-degree terms from an expansion. In some cases you might wish to drop terms in several variables, e.g.all terms in q with exponent greater than 7, and terms in p with exponent greater than 4, or terms of pn and qm combined so that their M-RweightM-S 2n + m exceeds 7. This sequence of commands does that:
(c32) ratwtlvl:7$
(c33) ratweight(q,1,p,2);
(d33) [q, 1, p, 2]
(c34) ratexpand((p+q+1)^8);
7 6 5 5 4 4 2 3 3
(d34) 8 q + 28 q + 168 p q + 56 q + 280 p q + 70 q + 560 p q + 280 p q
3 2 2 2 2 3 2
+ 56 q + 420 p q + 168 p q + 28 q + 280 p q + 168 p q + 56 p q + 8 q
3 2
+ 56 p + 28 p + 8 p + 1
The M-RratsM-S appearing in the command names here are an indication that the commands use something called M-Rcanonical RATional expression form,M-S or CRE form, an efficient data representation used for dealing with polynomials and ratios of polynomials. We have also used M-R^M-S instead of M-R**M-S (the two notations are interchangeable in macsyma.) We will return to representation issues shortly.
1.9 Integration
One of macsyma 's flashiest features is its ability to compute symbolic indefinite integrals:
(c35) x/(x^3+1);
x
(d35) ------
3
x + 1
(c36) integrate(%,x);
2 x - 1
2 atan(-------)
log(x - x + 1) sqrt(3) log(x + 1)
(d36) --------------- + ------------- - ----------
6 sqrt(3) 3
To check the solution, we can differentiate.
(c37) diff(%,x);
2 2 x - 1 1
(d37) ------------------ + -------------- - ---------
2 2 3 (x + 1)
(2 x - 1) 6 (x - x + 1)
3 (---------- + 1)
3
A more satisfactory form would result if the terms were expanded and the sum put over a common denominator. By converting this result to a CRE form (the same form used for the previous RAT commands) the expression is simplified. The ratsimp command does this:
(c38) ratsimp(%);
x
(d38) ------
3
x + 1
2 Representation and Simplification
^EThe acronym stands for Project MAC's SYmbolic MAnipulation System. MAC itself is an acronym, usually cited as meaning Man and Computer or Machine Aided Cognition. The Laboratory for Computer Science at the Massachusetts Institute of Technology was known as Project MAC during the initial development of macsyma .
^EThere are more readable (but slower) typesetting display facilities which requires more a elaborate computer terminal than the usual character-display type.