\( \DeclareMathOperator{\abs}{abs} \newcommand{\ensuremath}[1]{\mbox{$#1$}} \)

Arithmetic in Maxima

 1 Introduction

When you begin typing outside of a text cell wxMaxima thinks
that you want to issue commands.  To do simple calculations
we type them in just like in a calculator.  So, if for instance,
I want to find 3+5, then I would go outside of the text cell
and type:

--> 3+5

If I hit Enter, then I notice that a new line opens up under 3+5,
but nothing happens.  Why not?  Maxima treats each math cell as
a LIST of instructions.  Pressing Enter just lets you enter in the
next command in your list.  But what if I don't have any?  What if
I just want 3+5?!

To EVALUATE a command (or a list of commands in a cell) we press:

Shift+Enter.

So let's try 3+5 again, this time with Shift+Enter:

--> 3+5;
\[\tag{%o1} 8\]

So Shift+Enter evaluates a cell.  You may also notice a very pale
grayed-out semi-colon (;) after 3+5.  The semi-colon tells Maxima
that this command is done.  wxMaxima has put it in to help remind us
that every command in Maxima must end with a semi-colon.  You
can type in the ; yourself, or you can let wxMaxima handle it.  Here is
how the ; can be useful:

--> 3+5; 2-25/5;sqrt(125);
\[\tag{%o4} 8\] \[\tag{%o5} -3\] \[\tag{%o6} {{5}^{\frac{3}{2}}}\]

Note how we seperated each calculation from the next using ;  This
way, we can do three calculations at once!  But also notice that input given
this way is hard to read.  The solution to this formatting dilemma is to add linebreaks in
the cell using plain old Enter:

--> 3+5;
2-25/2;
sqrt(125);
\[\tag{%o7} 8\] \[\tag{%o8} -\frac{21}{2}\] \[\tag{%o9} {{5}^{\frac{3}{2}}}\]

So after 3+5; I hit Enter to go to a new line, then after 2-25/2
another Enter and so on.  After I've typed everything in, I hit
Shift+Enter and all three calculations are run.

 2 Reusing Previous Calculations

Notice the (%o7), (%o8), and (%o9) in the previous example?  These
are labels that allow you to reuse these answers in future calculations.
For example:

--> %o7;%o8;
\[\tag{%o11} 8\] \[\tag{%o12} -\frac{21}{2}\]

or:

--> %o7*%o8 + sqrt(5)*%o9;
\[\tag{%o13} -59\]

You can use these labels in place of the outputs in ANY calculation!
This is *very* convenient for quick calcluations.  This is a bad strategy for
longer work, or work that is closed and reopened frequently.  If you close and reopen
a file, or go back in the document and recalculate cells, then the input output numbers
will shift around!  This is because they are tied to the Maxima computation engine
that is running in the background.  If you close the file, the engine shuts off.
If you open the file again, then it turns on fresh.  If you reference %o5 then that will reference the last %o5 that
was generated, even if there are two or three of them!

 2.1 I Do Not Want to See the Output

Sometimes, you'll want to calculuate something, but do not really care to see
the answer.  To supress the output of a calculation, instead of a (;) semi-colon, use
a dollar sign:

--> 5+24/16$

Note, that calculation was run, it just didn't display!  To see that it really did run,
we can ask Maxima to print out the output to the last calculation using % or %o1:

--> %;
\[\tag{%o2} \frac{13}{2}\]

So notice that I did the calculation 5+24/16$ which computes the result but doesn't
show it.  I can still refer back to that if I want by using the %o tag that matches it!

 2.2 I Want to See The Math that I Typed

Sometimes it is helpful to see the Math that we typed in a way that is similar to how we would
write it on a page.  wxMaxima has the ability to print out Mathematics in a "pretty" format
using the single quote (').  If you type a single quote in front of any wxMaxima command, then
Maxima will print out a pretty version of that command.  For example:

--> '3/sqrt(5) - %pi^7;
\[\tag{%o1} \frac{3}{\sqrt{5}}-{{\ensuremath{\pi} }^{7}}\]
--> 'integrate(x*sin(5*x),x); 'limit(x*sin(5*x),x,5);
\[\tag{%o3} \int {\left. x \sin{\left( 5 x\right) }dx\right.}\] \[\tag{%o4} \lim_{x\to 5}{x \sin{\left( 5 x\right) }}\]

Notice that wxMaxima does not evaluate any of these expressions, it merely prints them out
in a way that is easy to read.  This is convenient for presenting/interpreting answers
that come out of calculations.

 3 Arithmetic Operations

 3.1 Careful With Parentheses!

The arithmetic operations in Maxima are the same that you are used to
on a normal scientific calculator with one exception: you must be careful
when using parentheses for multipliction.  As an example, let's try
to multiply 3*4:

--> 3*4;
\[\tag{%o1} 12\]
--> 3(4);
\[\mbox{incorrect syntax: Syntax error}3(\textasciicircum\]

That's interesting!  The first multiplication is fine, but the second throws an error!
So does:

--> (3)4;
\[\mbox{incorrect syntax: 4 is not an infix operator}\mbox{(3)4;}\mbox{ \textasciicircum}\]

What's going on here?!  What we need to remember here is that Maxima is a CAS - an algebra system, and in Algebra
parentheses are also used for functions -- like when we write f(x).  If you put down parentheses without an operation
between them, Maxima assumes that you mean FUNCTION.  So by writing 3(4) you are telling Maxima: "the function 3 evaluated
at 4." Which is absurd.  By writing (3)4, Maxima interprets this (roughly) as "the empty function with no name evaluated at
3 and then affected by four."

O.k., but we need parentheses to help with grouping, so how do we do that.  Simple: just make sure to spell out the multiplication:

--> 3*(4); (3)*4; (3)*(4);
\[\tag{%o3} 12\] \[\tag{%o4} 12\] \[\tag{%o5} 12\]

 3.2 The Operations

The arithmetic operators that we can use are:

Addition: +

Subtraction: -

Multiplication: *

Division: /

Exponents: ^

Square Root: sqrt()

Nth Root: ^(1/N)

 3.3 Exact Answers Only - Unless You Explicitly Ask for Approximations

Maple is an algebra system, which means that its default mode is to give exact numerical quanities rather than decimal approximations.
Hence:

--> 62/8;
\[\tag{%o6} \frac{31}{4}\]

Note that instead of doing the long division to produce a decimal answer, Maxima
reduced the fraction - which is "the Algebra" thing to do.  Roots are handled in a similar way:

--> sqrt(50);
\[\tag{%o1} 5 \sqrt{2}\]

 3.4 How to Explicitly Ask for a Decimal

If you do actually want a decimal number, you can ask for one
using the float() function, which converts an "algebraic" type answer
into a floating point number:

--> float(31/4); float(sqrt(50));
\[\tag{%o13} 7.75\] \[\tag{%o14} 7.071067811865475\]

If you do not want so many digits, you can change the number
of digits displayed by altering the value of fpprintprec:

--> fpprintprec:6$ float(sqrt(3));
\[\tag{%o7} 1.73205\]
--> fpprintprec:3$ float(sqrt(3));
\[\tag{%o9} 1.73\]
--> fpprintprec:5$ float(sqrt(3));
\[\tag{%o11} 1.732\]

Note that fpprintprec stands for "floating point print precision"
and is set using the (:) colon, not =.  The reason for this is
due to the fact that fpprintprec is a variable.  More on this later!

Another thing that needs to be noted here is that fpprintprec
is not rounding anything (see the last example).  Finally, the
value that you set will remain in effect until you either
change it, or quit.

 4 Special Constants e and Pi

We often need to use Euler's constant or pi in our calculations.
Maxima has the constants built in. To use them, simply:

--> fpprintprec:16$
--> %e;float(%e);
\[\tag{%o4} \% e\] \[\tag{%o5} 2.718281828459045\]
--> %pi;float(%pi);
\[\tag{%o45} \ensuremath{\pi} \] \[\tag{%o46} 3.141592653589793\]

So if you wanted to find the area of a circle with radius 6:

--> %pi*6^2;
\[\tag{%o82} 36 \ensuremath{\pi} \]

or if we wanted the balance on a continuously compounded account
with an initial balance of $500 and interest rate of 3% after 5
years:

--> 500*%e^(.03*5);
\[\tag{%o48} 580.9171213641415\]

Wait!  What gives?!  You might be wondering why the last answer
was a decimal and not exact.  The reason is because we use a decimal
to represent 3% - Maxima thought that's what we wanted.  If we
wanted an exact answer, we need to be explicit about our exact intentions:

--> 500*%e^(5*3/100);
\[\tag{%o49} 500 {{\% e}^{\frac{3}{20}}}\]

 5 The Exponential and Logarithmic Functions

 5.1 The Exponential Functions

Exponential functions are easy in Maxima.  e^x is accessed by two means: either using
%e^x or using the built-in function exp(x).

--> exp(2);%e^2;
\[\tag{%o51} {{\% e}^{2}}\] \[\tag{%o52} {{\% e}^{2}}\]

Exponential functions with bases other than %e are accessed by (^)

--> 3^3; 3^(2/5);
\[\tag{%o53} 27\] \[\tag{%o54} {{3}^{\frac{2}{5}}}\]

Recall: Everything is exact until you ask for a decimal with float()!

--> float(3^(2/5));
\[\tag{%o55} 1.551845573915359\]

 5.2 The Logarithm

You read that title correctly: there is one logarithm in Maxima.  The only logarithm that Maxima
knows about has base e.  Unfortunately, since this is the only logarithm that Maxima knows about
it uses the symbol log() to represent it.  I repeat:

log(x) is the NATURAL LOGARITHM

While this is not a common notation in undergraduate Calculus classes, it is a widely used convention
in Academic Mathematics and Physics.  Sorry Engineers, you're going to have a little bit of an adjustment here!

--> log(%e);log(%e^5);log(10);
\[\tag{%o59} 1\] \[\tag{%o60} 5\] \[\tag{%o61} \log{(10)}\]

If you want a logarithm with another base then you must use the change of base formula.
For instance, if you wanted the base-2 log of 10 then you would use:

--> log(10)/log(2); float(log(10)/log(2));
\[\tag{%o62} \frac{\log{(10)}}{\log{(2)}}\] \[\tag{%o63} 3.321928094887362\]

While this is an inconvenience, it is a relatively minor one that is pretty easy to
get used to.

 5.3 Expanding and Contracting Logs

We can tell Maxima that we want it to expand all of the logarithms it comes across.  To do
this, we set a variable called, unsurprisingly logexpand.

Before setting logexpand:

--> log(a*b^2);
\[\tag{%o6} \log{\left( a\, {{b}^{2}}\right) }\]

After setting logexpand to 'super:'

--> logexpand:super; log(a*b^2);
\[\tag{%o9} \mathit{super}\] \[\tag{%o10} 2 \log{(b)}+\log{(a)}\]

There are settings less than super - but why would you want to be less than super?!  Seriously,
if you are interested in less expansion, you can easily look them up online in the Maxima documentation.
If you want to turn the expansion back off:

--> logexpand:false; log(a*b^2);
\[\tag{%o11} \mbox{false}\] \[\tag{%o12} \log{\left( a\, {{b}^{2}}\right) }\]

To contract logarithmic expressions, we use the command - get ready - it's pretty crazy - logcontract():

--> logcontract(log(a) + 2*log(b));
\[\tag{%o15} \log{\left( a\, {{b}^{2}}\right) }\]
--> logcontract(2*log(x) - 3*log(y));
\[\tag{%o19} \log{\left( \frac{{{x}^{2}}}{{{y}^{3}}}\right) }\]

logcontract() does a pretty good job, but it has some limitations.  It only works for integer coefficients,
not fractional ones:

--> logcontract((1/2)*log(x)+3*log(y));
\[\tag{%o20} \log{\left( {{y}^{3}}\right) }+\frac{\log{(x)}}{2}\]

Notice how it works on 3*log(y), but it punts on (1/2)*log(x).

 6 Expanding and Simplifying Rational Expressions

 6.1 Expanding

Computers only do what we tell them to do and sometimes that means being EXTRA precise.  Here
is an example:

--> (x+2)^2;
\[\tag{%o21} {{\left( 2+x\right) }^{2}}\]

Yes... that is true... but not really what I wanted.  I wanted to see what (x+2)^2 looked like all multiplied out!
Let's be more precise:

--> expand((x+2)^2);
\[\tag{%o22} {{x}^{2}}+4 x+4\]

That's better.  Again:

--> expand((x+2)*(3*x-5)*(x-4)^3);
\[\tag{%o23} 3 {{x}^{5}}-35 {{x}^{4}}+122 {{x}^{3}}-24 {{x}^{2}}-544 x+640\]

Without the expand() function, the above returns:

--> (x+2)*(3*x-5)*(x-4)^3;
\[\tag{%o24} {{\left( x-4\right) }^{3}}\, \left( x+2\right) \, \left( 3 x-5\right) \]

Which in some ways is much more efficient. But to see polynomials multiplied out, we need to explicitly ask
Maxima to expand().

 6.2 Contracting

What if we have a nasty polynomial and we want to factor it?  Maxima has a factor() command that does just this:

--> factor(3*x^5-35*x^4+122*x^3-24*x^2-544*x+640);
\[\tag{%o25} {{\left( x-4\right) }^{3}}\, \left( x+2\right) \, \left( 3 x-5\right) \]
--> factor(6*x^2 - 7*x + 1);
\[\tag{%o26} \left( x-1\right) \, \left( 6 x-1\right) \]

Maxima has another simplifying command that makes Algebra life much easier.  Imagine we want:

--> 1/(x+2) - (3*x+5)/(2-x)^3;
\[\tag{%o27} \frac{1}{x+2}-\frac{5+3 x}{{{\left( 2-x\right) }^{3}}}\]

But we want to see the fractions *actually* subtracted.  To do this, we can use the command:

ratsimp()

Which stands for rational simplify.

--> ratsimp(1/(x+2) - (3*x+5)/(2-x)^3);
\[\tag{%o28} \frac{2+23 x-3 {{x}^{2}}+{{x}^{3}}}{{{x}^{4}}-4 {{x}^{3}}+16 x-16}\]

Want to mess with just the numerator or the denominator seperately?

--> denom(%o28);num(%o28);
\[\tag{%o32} {{x}^{4}}-4 {{x}^{3}}+16 x-16\] \[\tag{%o33} {{x}^{3}}-3 {{x}^{2}}+23 x+2\]

Want the answer to look like it does in the answer key of your Algebra book?

--> factor(num(%o28))/factor(denom(%o28));
\[\tag{%o35} \frac{2+23 x-3 {{x}^{2}}+{{x}^{3}}}{{{\left( x-2\right) }^{3}}\, \left( x+2\right) }\]

 6.3 Parting Thoughts on Expanding and Contracting

While expanding and contracting expressions are important for presenting our
work to other people, Maxima does not care what form a particular expression is
given in.  While it is true that Maxima does see the two expressions as being
different:

--> is((x+2)^2 = x^2 + 4*x + 4);
\[\tag{%o40} \mbox{false}\]

It is smart enough to know that one is the expanded form of the other:

--> is(expand((x+2)^2)=x^2 + 4*x + 4);
\[\tag{%o41} \mbox{true}\]

If you think about it, if Maxima can do Algebra, it has to see the difference
between expanded and contracted representations just like we do - and it does.


Created with wxMaxima.