# Introduction

This workbook provides some simple examples of how to use Maxima, first
as a calculator, and then as a way to specify and analyze functional 
relationships.

Try to enter and evaluate commands before opening the cells marked
/*Input. Execute (ctrl-enter) for results */.

## Some Basics

Clear Maxima's memory, using kill(all)$, and set the floating point print 
precision (fpprintprec) to 5.

Enter these two commands: 5*4; and 5*4$. Why do they yield different result?
Which of the two yields the output? How can you tell, other than by knowing
that ; and $ punctuation marks yield different results?

/*Input. Execute (ctrl-enter) for results */
ratprint:5$ kill(all)$
/* 
The ratprint and kill(all) commands are not required.
The ratprint command reduces the number of decimal places, and 
the kill(all) command clears Maxima's memory, just in case some
extraneous values have been stored from previous computations.
*/
5*4; 5*4$
/*The results differ because ; ends the command and 
prints its results, but $ ends the command and
stores the results. The first command is %i1,
so the resulting output is %o1. This is the name
under which the result of 5*4$ is stored. Output
%o2 is created and stored but not printed.

This comment is followed by end; because a 
wxMaxima cell cannot end with a comment*/ end;

Simplify this computation: (16/(8-6)+9-4)/(2^5+3^2). Calculate
the answer by hand and compare the order of your operations
with those of Maxima.

(16/(8-6)+9-4)/(2^5+3^2);
/*
The material below is not required. It provides a couple 
of decompositions of the computation
*/
Numerator:(16/(8-6)+9-4);
Denominator: (2^5+3^2); 
16/(8-6); %+9; %-4;

Remove the parentheses and have Maxima calculate the result. 
Calculate the result by hand, as before.

16/8-6 +9-4/2^5+3^2;
/*
Not required: A decomposition of the computation
*/
16/8; %-6; %+9; %-4/2^5; %+3^2;

Maxima can work with radicals, including fractional roots. Recall that 
sqrt(x) = x^(1/2), and that other roots can be expressed this way.
Evaluate these: sqrt(49), -sqrt(49), sqrt(-49), (32/243)^(5), (32/243)^(-5), 
(32/243)^(1/5), (32/243)^(0.2).

Note that the third value is a complex number, where %i is Maxima's
representation of sqrt(-1). Why are the third and fourth values reciprocals 
of each other? Why are the last two items expressed differently?

/*Input. Execute (ctrl-enter) for results */
[sqrt(49), -sqrt(49), sqrt(-49)];
[(32/243)^(5), (32/243)^(-5), 
(32/243)^(1/5), (32/243)^(0.2)];

Assign (bind) the name b to 5*4. Enter the commands b:5*4$, b;, 
b^2;, and b!;.

/*Input. Execute (ctrl-enter) for results */
b:5*4$ b; b^2; b!;

Enter the commands log(b);, log(1.0*b);, and float(log(b));. Why doesn't 
Maxima provide a floating-point decimal representation of log(b)? 
Why does Maxima yield a floating-point decimal representation of 
log(1.0*b)?

/*Input. Execute (ctrl-enter) for results */
log(b); log(1.0*b); float(log(b));
/*
When possible Maxima returns an exact value, not
a floating-point representation of that value. 
The number 20 is an exact value, but the value of
1.0*20 is treated as an approximation (confirm that
1.0*20 is 20.0 and not 20). Given that the log of
the floating-point value 20.0 is being determined, 
Maxima returns a floating-point value.  

Note the end; command that follows this comment. 
A wxMaxima cell cannot end with a comment. Any string
followed by a semicolon can be entered.
*/ end;

Confirm that Maxima can nest commands by comparing the following results:
1. sqrt(144); log(%);,
2. sqrt(log(144));, and
3. log(sqrt(144));.

/*Input. Execute (ctrl-enter) for results */
sqrt(144.0); log(%); sqrt(log(144.0)); log(sqrt(144.0));

In the next cell, examine the factorial, and the related gamma command.
First, use the simple command 4!; to determine that 4! = 24. Then use the
command gamma(5); to confirm that gamma(n) = (n-1)!. Then enter the
command (9/2)!; and then enter a command that renders this result as a
floating-point representation. (Obviously Maxima does not compute 
n! as n*(n-1)*...*2*1, as one would do with integers. Rather it uses the 
gamma function.)

Use the commands 5!;, factorial(5);, and gamma(6); as ways to confirm
that 5! = 120.  

/*Input. Execute (ctrl-enter) for results */
 4!; gamma(5);  (9/2)!; float(%);  
/*
The last three, equivalent commands are 
entered as a list. This is not necessary. Note,
however, that when the commands are entered by
a list, they are separated by commas, and the
entire list is followed by a semicolon 
*/
[5!, factorial(5), gamma(6)];

## Expressions

Create a command y:log(x)$ and another z:sqrt(x)$ to bind the names
y and z to the associated expressions.

/*Input. Execute (ctrl-enter) for results */
y : log(x)$ z : sqrt(x)$

Now use  ev and subst commands to evaluate the expressions.
First enter the command ev(y,x=25.0) to evaluate the first expression.
Note that the expression x=25.0$ temporarily assigns the value 25.0 to x,
but does not replace x with 25.0 in Maxima's memory.  Similarly, evaluate
expression z when x = 25.

/*Input. Execute (ctrl-enter) for results */
[ev(y,x=25.0), ev(z,x=25.0)];

In the book, we mostly use subst rather than ev.  Use the 
command subst(2.5,x,[x,y]; to evaluate the two expressions.
Also, use subst(x=25.0, [y,z]; to achieve the same result.

/*Input. Execute (ctrl-enter) for results */
subst(25.0,x,[y,z]);
subst(x=25.0,[y,z]);

Create a command to bind the name w to this expression:
50*sqrt(a,b). Use the command subst([a=36,b=4,x=25],[w,y,z]);
to evaluate this expression and the two above. Finally, instruct
Maxima to express these results as floating-point numbers.

/*Input. Execute (ctrl-enter) for results */
kill(a,b)$    /*To ensure that values from 
earlier cells do not contaminate the expressions
to be analyzed*/

w:50*sqrt(a*b); subst([a=36,b=4,x=25],[w,y,z]);
/*
Note that the % in the command below refers
to the entire list above, not to the last item
in that list.
*/
float(%);

Creating explicit functional expressions often results in more flexibility 
that just binding an expression to a name. Enter the command 
f(x):=log(x);. Also, create a function that defines g(x) as the square root of x.
Now enter the command [f(25.0), g(25.0)];.  Evaluate the two functions
when x = 5 and when x = 5.0. 

/*Input. Execute (ctrl-enter) for results */
f(x):=log(x)$  g(x):=sqrt(x)$
[f(25.0),g(25.0)];
[f(5),g(5)]; [f(5.0),g(5.0)];

Extend the use of functional forms to include two variables and a 
parameter. Use the function f(x,y,a) = a*sqrt(x*y).  Evaluate these:
f(x,y,50), f(16,y,50), f(16,36,50).

/*Input. Execute (ctrl-enter) for results */
kill(f)$ /*Removes the specification used above*/

f(x,y,a):=a*sqrt(x*y);

/*
Note that the values must be entered in the 
order in which they occur in f(x,y,a), not in 
the order in which they occur in the right-hand
side of the equation
*/

[f(x,y,50), f(16,y,50), f(16,36,50)];

Enter the assignment expr:5*x+4*y-20;.  Now use subst([x=5,y=3),expr);
to evaluate this expression. Repeat this process, evaluating expr for
x=0 and y=5 (what goes wrong here?) and again for x=10 and y=-15/2. 

Note that the name to which an expression is bound can be a string of
characters, not necessarily a single character. If we anticipate further
expressions, we could use a name like expr1. Using a descriptive
expression name can serve as a memory aid.

/*Input. Execute (ctrl-enter) for results */
expr: 5*x + 4*y - 20;
subst([x=5,y=3],expr);
subst([x=0,y=5],expr); /*Maxima has bound log(x) to the name y */
subst([x=10,y=-15/2],expr);