# Victor T.Toth

## http://www.vttoth.com/CMS/projects/61-the-maxima-computer-algebra-system

Example of using Maxima for derivation of Schwartzschild metric from Hilbert Variational principle.
Below L is Lambda-term.
Slightly corrected by S.Blinnikov on 21st October 2012 (Novosibirsk): Friedmann lines are removed.

if get('ctensor,'version)=false then load(ctensor);

if get('itensor,'version)=false then load(itensor);

remsym(g,2,0);
remsym(g,0,2);
remsym(gg,2,0);
remsym(gg,0,2);
remcomps(gg);
imetric(gg);

icurvature([a,b,c],[e])*gg([d,e],[])$

contract(rename(expand(%)))$

%,ichr2$

contract(rename(expand(%)))$

canform(%)$

contract(rename(expand(%)))$

components(gg([a,b],[]),kdels([a,b],[u,v])*g([u,v],[])/2);

components(gg([],[a,b]),kdels([u,v],[a,b])*g([],[u,v])/2);

%th(4),gg$

contract(rename(expand(%)))$

contract(canform(%))$

imetric(g);

contract(rename(expand(%th(2))))$

remcomps(R);

 components(R([a,b,c,d],[]),%th(2));

g([],[a,b])*R([a,b,c,d])*g([],[c,d])$

contract(rename(canform(%)))$

contract(rename(canform(%)))$

components(R([],[]),%);

decsym(g,2,0,[sym(all)],[]);

decsym(g,0,2,[],[sym(all)]);

ishow(1/(16*%pi*G)*((2*L+'R([],[])))*sqrt(-determinant(g)))$

L0:%,R$

canform(contract(canform(rename(contract(expand(diff(L0,g([],[m,n]))-
    idiff(diff(L0,g([],[m,n],k)),k)+idiff(rename(idiff(contract(
    diff(L0,g([],[m,n],k,l))),k),1000),l)))))))$

ishow(e([m,n],[])=canform(%*16*%pi/sqrt(-determinant(g))))$

EQ:ic_convert(%)$

ct_coords:[t,r,u,v];

lg:ident(4);

lg[1,1]:B;
lg[2,2]:-A;
lg[3,3]:-r^2;
lg[4,4]:-r^2*sin(u)^2;

kill(dependencies);

dependencies(A(r),B(r));

cmetric();

christof(false);

e:zeromatrix(4,4);

ev(EQ);

E:expand(radcan(ug.e));

exp:findde(E,2);

solve(ode2(exp[1],A,r),A);

%,%c=-2*M;

a:%[1],%c=-2*M;

ode2(ev(exp[2],a),B,r);

b:ev(%,%c=rhs(solve(rhs(%)*rhs(a)=1,%c)[1]));

factor(ev(ev(exp[3],a,b),diff));

lg:ev(lg,a,b),L=0$

ug:invert(lg)$

block([title: "Schwarzschild Potential for Mass M=2",M:2.],
    wxplot3d([r*cos(th),r*sin(th),1-ug[1,1]],[r,5.,50.],[th,-%pi,%pi],
    ['grid,20,30],['z,-2,0],[ps_file,"embedSchw.eps"],['legend,title]));

wxplot3d([r*cos(th),r*sin(th),1-ev(ug[1,1],M:2)],[r,5,50],[th,-%pi,%pi],
['grid,20,30],['z,-2,0],[legend,false]);

wx prefix is just to display plot3d in wxmaxima window with its resolution,
so it is better to control everything with plot3d directly.
Not all options work in plot3d, e.g. legend is OK only with false, so use the option title:

wxplot3d([r*cos(th),r*sin(th),1-ev(ug[1,1],M:2)],[r,5,50],[th,-%pi,%pi],[title,"Schwarzschild Potential for Mass M=2"],[legend,false]);

Next examples show calls to plot3d or plot2d and the figure appears in a separate window (gnuplot must be installed on your system).

plot3d([r*cos(th),r*sin(th),1-ev(ug[1,1],M:2)],[r,5,50],[th,-%pi,%pi])$

plot3d([r*cos(th),r*sin(th),1-ev(ug[1,1],M:2)],[r,5.,50.],[th,-%pi,%pi],
    ['grid,20,30],['z,-2,0],[title,"Schwarzschild Potential for Mass M=2"],[legend,false])$

Next are examples of plots in Maxima.

 plot3d ([[-3*x - y, [x, -2, 2], [y, -2, 2]],
   4*sin(3*(x^2 + y^2))/(x^2 + y^2), [x, -3, 3], [y, -3, 3]],
   [x, -4, 4], [y, -4, 4])$

Plot of a Klein bottle, defined parametrically: 

expr_1: 5*cos(x)*(cos(x/2)*cos(y) + sin(x/2)*sin(2*y) + 3.0) - 10.0;

expr_2: -5*sin(x)*(cos(x/2)*cos(y) + sin(x/2)*sin(2*y) + 3.0);

expr_3: 5*(-sin(x/2)*cos(y) + cos(x/2)*sin(2*y));

plot3d ([expr_1, expr_2, expr_3], [x, -%pi, %pi],
        [y, -%pi, %pi], [grid, 50, 50])$

Plot of a "spherical harmonic" function, using the predefined transformation,
spherical_to_xyz to transform from spherical coordinates to rectangular coordinates. 
See the documentation for spherical_to_xyz. 

/* set_plot_option ([legend, "sph"]); -- does not work */;

plot3d (sin(2*theta)*cos(phi), [theta, 0, %pi],
        [phi, 0, 2*%pi],
        [transform_xy, spherical_to_xyz], [grid,30,60])$

Use of the pre-defined function polar_to_xy to transform from cylindrical 
to rectangular coordinates. See the documentation for polar_to_xy. 

plot3d (r^.33*cos(th/3), [r, 0, 1], [th, 0, 6*%pi], [box, false],
   [grid, 12, 80], [transform_xy, polar_to_xy], [legend, false])$

Plot of a sphere using the transformation from spherical to rectangular coordinates. 
Option same_xyz is used to get the three axes scaled in the same proportion. 
When transformations are used, it is not convenient to eliminate the mesh lines, 
because Gnuplot will not show the surface correctly. 

plot3d ( 5, [theta, 0, %pi], [phi, 0, 2*%pi], same_xyz,
  [transform_xy, spherical_to_xyz], [mesh_lines_color,blue],
  [palette,[gradient,"#1b1b4e", "#8c8cf8"]], [legend, false])$

legend option is OK with plot2d, as the nex example shows

 xy:[[10,.6], [20,.9], [30,1.1], [40,1.3], [50,1.4]]$

 plot2d([[discrete,xy], 2*%pi*sqrt(l/980)], [l,0,50],
        [style, [points,5,2,6], [lines,1,1]],
        [legend,"experiment","theory"],
        [xlabel,"pendulum's length (cm)"], [ylabel,"period (s)"])$

wxplot2d([[discrete,xy], 2*%pi*sqrt(l/980)], [l,0,50],
        [style, [points,5,2,6], [lines,1,1]],
        [legend,"experiment","theory"],
        [xlabel,"pendulum's length (cm)"], [ylabel,"period (s)"])$