Maxima - free sophisticated computer algebra system (CAS)

"Maxima is a computer program for doing mathematics calculations, symbolic manipulations, numerical computations and graphics. Procedures can be programmed and then run by Maxima to do complex tasks. Much of the syntax for other languages such as Maple was copied from Maxima" (text from Maxima Primer);


Maxima symbolic-math package written in Common Lisp
See also Fermat - computer algebra system
GUI New version of Maxima contains both types of GUI
Documentation Packages Gallery of my images


Defining a Function:
F(z):=z*z+c;
or
define(f(z),z*z+c);
1-st derivative of the function with respect to z:
diff(f(z),z,1);

(%o2) 2 z

Defining derivative functions
(%i1) f(x):=q*x^2+r;
(%o1) f(x) := q x^2 + r
(%i3) define(df(x),diff(f(x),x));
(%o2) df(x) := 2 q x
(%i3) df(a);
(%o3) 2 a q


c:0.5;
F(z):=z*z+c;
fixed(c):=float(rectform(solve([F(z)=z],[z])));
a:fixed(c);
a[1];
a[2];
(%i20) d(z):=diff(F(z),z,1);
(%o20) d(z):=diff(F(z),z,1)
(%i21) d(z);
(%o21) 2*z
rectform(2*a[1]+2*a[2]);
4.0*z=2.0

see also discussion about diff
The roots of Z3 = 0 were evaluated in Maxima by the commands:
(From the Mandelbrot Set Glossary and Encyclopedia  © 1987-2004 Robert P. Munafo)

roots and how to extract numerical values from list :
definition of function :
(%i1) P(n):=if n=0 then 0 else P(n-1)^2+c;
(%o1) P(n):=if n=0 then 0 else P(n-1)^2+c
list of roots:
(%i3) a:float(float(allroots(%i*P(3))));
(%o3) [c=0.0,
c=0.74486176661974*%i-0.12256116687665,
c=-0.74486176661974*%i-0.12256116687665,
c=-1.754877666246693]
fist root :
(%i4) a[1];
(%o4) c=0.0
value of second root :
(%i6) b:rhs(a[2]);
(%o6) 0.74486176661974*%i-0.12256116687665

Ploting logistic function:

/* set the variable "a" to 2
a:2$
/* defines a function f1
f1(x):=a*x*(1-x)$
/* plot the function y:=f1(x) in the range 0 <= x <=1
plot2d(f1(x),[x,0,1]);
/* define function y:=f2(x)
f2(x):=x;
/* plot f1 and f2*/
plot2d([f1(x),f2(x)],[x,0,1]);
/* Save plot to eps file in c:\
plot2d([f1(x)f2(x)],[x,0,1],[gnuplot_term,ps], [gnuplot_out_file,"log.eps"]);

How to compute gradient of 2D scalar field
From: David and Michele Holmgren shaw.ca>
Subject: Re: Possibility/ease of doing some vector ops in Maxima
Newsgroups: gmane.comp.mathematics.maxima.general
Date: 2002-09-19 01:00:07 GMT

Hi,
(...) At the Maxima prompt, type:
f:x*y*(x-y)^2;
v:[x,y];
g:makelist(diff(f,v[i]),i,1,2);

The first line is your function (...),
the second is a list of variables,
and the third line is the gradient (a list (vector) with elements being those of grad(f)).

I hope this helps, and gives you a few ideas...

Dave Holmgren
------------------
definition of gradient g of function f in pseudocode:
f(x,y):=x*y*(x-y)^2;
g:=diff(f,x)*i + diff(f,y)*j;

Complex number


Function: atan (x)
- Arc Tangent.

Function: atan2 (y, x)
- yields the value of atan(y/x) in the interval -%pi to %pi.
theta= carg(z)
The complex argument of z is an angle theta in (-%pi, %pi]
r exp (theta %i) = z
r = abs(z) is the complex magnitude of z

(%i1) c:0;
(%o1) 0
(%i2) atan(c);
(%o2) 0
(%i5) atan2(imagpart(c),realpart(c));
atan2(0,0) has been generated.
-- an error. To debug this try debugmode(true);
(%i6) carg(c);
(%o6) 0

Polar form :
(%i2) m(r,a):=r * exp(2*%i*%pi*a);
(%o2) c:m(1,0.5);
(%o2) -1
(%i62) carg(b)/(2*%pi);
(%o62) 1/2
(%i63) abs(b);
(%o63) 1

(%i9) z:zx+zy*%i;
(%o9) %i zy + zx

(%i10) expand(z^2);
(%o10) - zy^2 + 2 %i zx zy + zx^2

(%i11) c:cx+cy*%i;
(%o11) %i cy + cx

(%i12) expand(c+z^2);
(%o12) - zy^2 + 2 %i zx zy + zx^2 + %i cy + cx

(%i13) realpart(c+z^2);
(%o13) - zy^2 + zx^2 + cx

(%i14) imagpart(c+z^2);
(%o14) 2 zx zy + cy

(%i15) abs(z+c);
(%o15) sqrt((zy + cy)^2 + (zx + cx)^2 )

(%i16) carg(z);
(%o16) atan2(zy, zx)

(%i17) f(z):=z*z+c;
(%o17) f(z) := z z + c

(%i18) realpart(f(z));
(%o18) - zy2 + zx2 + cx
rectform(z*z+c);

(%i9) c:-.75 +.0001*%i;
(%o9) 1.0*10^-4*%i-0.75
(%i10) carg(c);
(%o10) 3.14145932025725

z:0+0*%i;
c:-2+0*%i;
for m:1 thru 20 step 1 do
block(
display(z, carg(z))
z:z*z+c
);

(%i1) solve([(c^2+c)^2+c=c^2+c], [c]);
(%o1) [c=-2,c=0]

Modulus of complex number :

plot3d(sqrt(u*u+v*v),[u,-5,5],[v,-7,7]);

the same in different form :

plot3d(cabs(u+v*%i),[u,-5,5],[v,-7,7]);
complex number in maxima byZdzislaw Meglicki


Prime factors:
a:ifactors(8);
[[2,3]]
(%i23) b:a[1][1];
(%o23) 2
(%i24) length(a);
(%o24) 1

save data (result of your computations) to file ( in the form of Lisp expressions) :
save("iim20.lisp",xy);

load it :
load("iim20.lisp");


Polynomials:

here P is a polynomial in one variable x

fP:factor(P);
part(fP,1); /* first factor */
rat(P, x); /* Factoring polynomials */
hipow(a,x); /* degree of expanded polynomial */
hipow(expand(P),x); /* degree of polynomial if non expanded */
(explanation by Stavros Macrakis )
Note that hipow is purely syntactic -- it looks for the highest *explicit*
power of x in the expression:

    hipow((x^2-1)*(x-1),x) => 2 (not 3)
    hipow((x^2-1)/(x-1),x) => 2 (not 1)

You may want to expand your expression with 'rat' first:

    hipow(rat((x^2-1)*(x-1)),x) => 3
    hipow(rat((x^2-1)/(x-1)),x) => 1

If your expression is in fact not a polynomial, you might be surprised by
the results, though they are correct by the syntactic definition:

    hipow(1/x,x) => -1    (Maxima treats 1/x as x^(-1) )
    hipow(1/(x^2-1),x); => 2 (not -2)
    hipow(sin(x^2),x); => 2


/* gives a list of coefficients */
give_coefficients(P,x):=block
( P:expand(P), /* if not expanded */
degree:hipow(P,x),
a:makelist(coeff(P,x,degree-i),i,0,degree));

c_list:give_coefficients(P,x);
/* find max coeefficient */
c_max:lmax(c_list);

find its binary size :
log2(x) := log(x) / log(2);
log2(c_max);
If I'm not wrong fpprec should be greater then log2(c_max) ( for bfallroots)

Give_prec(P,x):= ceiling(log2(lmax(give_coefficients(P,x))));

find all roots:
fpprec:32; fpprintprec:8;
roots:bfallroots(%i*P);

(explanation be Leo Butler :)

(%i2) load(mnewton);

(%o2)
"/home/work/maxima/sandbox/maxima-current-release/share/contrib/mnewton.mac"
(%i3) g(x):=x^5-x+1;

(%o3) g(x):=x^5-x+1
(%i4) (fpprec:50, newtonepsilon:bfloat(10^(-fpprec+5)),
mnewton(g(x),x,-2));

(%o4) [[x = -1.1673039782614186842560458998548421807205603715255b0]]

Setting newtonepsilon to a bfloat means the computations are done
in bfloat. Use

? mnewton

to see the documentation.



"if I have a polynomial in the variable x, for instance

p:(a*x-b)*(3*x-2)+(c*x+d)*(1-2*x);

I can get the coefficients of the powers of x in the following way:

p_cre:rat(p,x);
c0:coeff(p_cre,x,0);
c1:coeff(p_cre,x,1);
c2:coeff(p_cre,x,2);
" (Christan Stengg on gmane.comp.mathematics.maxima.general)



map(float,1,2,3,4);
float evaluates to false
Improper name or value in functional position.
-- an error. To debug this try debugmode(true);

map('float,1,2,3,4);
[1.0,2.0,3.0,4.0]



Checking precision of floats and bigfloats ( by Barton Willis) :

(%i1) load(floatproperties)$

(%i2) float_eps();
(%o2) 1.1107651257113995*10-16

(%i3) largest_float;
(%o3) 1.7976931348623157*10308

(%i4) least_positive_float;
(%o4) 4.9406564584124654*10-324

(%i5) float_bits();
(%o5) 53

(%i6) bigfloat_eps();
(%o6) 1.387778780781446b-17

(%i7) bigfloat_eps(),fpprec : 100;
(%o7) 1.4287342391028437277697294353[46 digits]3422744594310027325215037b-101



"The e indicator is the normal syntax for floating-point numbers.
's' indicates single-precision floating-point
'd' indicates double-precision floating-point.
I believe all Maxima implementations represent *all* machine floating-point numbers as double-precision, so all three are equivalent.

'b' is Maxima syntax for arbitrary-precision floating point numbers, called 'bigfloats' or 'bfloats'.
The variable fpprec determines how many decimal digits are carried along. Thus:

1/3b0, fpprec=20;
=> 3.3333333333333333333b-1

1/3b0, fpprec=30;
=> 3.33333333333333333333333333333b-1
" ( by Stavros Macrakis )


to draw gradient vectors that are orthogonal to contour lines :
plot2d(x^2,[x,0,3],[plot_format,openmath]);
pplot2d(x^2,[x,0,3],[plot_format,openmath]);

"You should install the latest version of xmaxima that is distributed with Maxima" Jaime Villate


Plotting the gradient with plotdf

(%i1) load("plotdf.lisp")$



Running Maxima
maxima(1) - Linux man page How to run maxima from console

quit();

"maxima-init.mac is a file which is loaded automatically when Maxima starts.
You can use maxima-init.mac to customize your Maxima environment.
maxima-init.mac, if it exists, is typically placed in the directory named by maxima_userdir, although it can be in any directory searched by the function file_search."
In my Maxima under win XP ther is no maxima-init.mac file.
See Introduction to Maxima by Edwin L. (Ted) Woollett for more informations



(%i2) maxima_userdir;
(%o2) C:/Documents and Settings/adam/maxima

(%i1) file_search_maxima;

in windows :
append(["C:/work2/###.{mac,max}", "C:/work1/temp/###.{mac,max}"], file_search_maxima)$

or in Linux :
append(["/home/adam/###.{mac,dat}"], file_search_maxima)$
then search for file in home dir :

(%i3) file_search_lisp;
file_search("cc1.dat");
printfile("cc1.dat");
cdata : read_list("cc1.dat");



Maxima installation :
Maxima Installation  by Mario Rodríguez Riotorto
Getting started in Common Lisp on Ubuntu
SLIME
Maxima installation/compilation Mini-Howto
gcl



Save text file in Maxima :

f : openw (f_name),
printf(f, "~a~%", comment),
printf(f, "~a~%", "dri"),
printf(f, "~d~%", 0),
printf(f, "~d~%", degree),
for x in a do printf (f, "~d~%", x),
close (f));



Save svg file in Maxima :

Under windows it makes an svg file ( for me in C:\Program Files\Maxima-5.21.1\wxMaxima directory)



See also :
Maxima and memory, allocate
Jungreis algorithm

IIM/J in maxima


Main page


Feel free to e-mail me!

Author: Adam Majewski adammaj1-at-o2-dot-pl

http://republika.pl/fraktal/

About