/* Copyright (C) 2008-2026 Viktor T. Toth <https://www.vttoth.com/>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * Deriving the Einstein field equations from the Einstein-Hilbert action
 *
 */
("Deriving the Einstein field equations from the Einstein-Hilbert action")$
if get('itensor,'version)=false then load(itensor);
("We declare the standard metric but remove automatic contraction rules...")$
("... that may incorporate hidden assumptions about symmetry")$
imetric(g);
remcon(g);
defcon(g,g,kdelta);
("Let us define the Riemann tensor, the Ricci tensor and the Ricci scalar")$
components(R([a,b,c,d],[]),(canform(isimplify(ev(icurvature([a,b,c],[e])*g([d,e],[]),ichr2,eval)))))$
components(R([a,b],[]),(canform(isimplify(ev(icurvature([a,b,c],[c]),ichr2,eval)))))$
components(R([],[]),(canform(isimplify(ev(icurvature([a,b,c],[c])*g([],[a,b]),ichr2,eval)))))$
("This is the Einstein-Hilbert action in standard form, which we evaluate and simplify")$
L0:ishow(1/(16*%pi*G)*((2*L+'R([],[])))*sqrt(-determinant(g)))$
L0:(isimplify(ev(L0,R,eval)))$
("We now generate terms of the 2nd order Euler-Lagrange equation")$
EL0:isimplify(diff(L0,g([],[m,n])))$
EL1:isimplify(-idiff(diff(L0,g([],[m,n],k)),k))$
EL2:isimplify(idiff(idiff(diff(L0,g([],[m,n],k,l)),k),l))$
("Sum and simplify")$
EL:(isimplify(expand((EL0+EL1+EL2)*16*%pi*G/sqrt(-determinant(g)))))$
("Did we recover the Einstein field equation?")$
("Let us write it down and form the difference")$
EFE:ishow('R([m,n],[])-1/2*g([m,n],[])*'R([],[])-L*g([m,n],[]))$
EFE:(isimplify(ev(EFE,R,eval)))$
D:isimplify(contract(isimplify(ev(EL,ichr2,eval))-rename(EFE)))$
("Why is D nonzero?")$
length(D);
("We have not yet declared the symmetry properties of the metric.")$
("Now is the time!")$
decsym(g,2,0,[sym(all)],[]);
decsym(g,0,2,[],[sym(all)]);
isimplify(D);
("Now that we verified the EFE, let us look at specific cases")$
load(ctensor);
derivabbrev:true;
EQ:ic_convert(ishow(G([m,n],[])=isimplify(EFE)))$
("Now we set up the FLRW metric of cosmology:")$
ct_coords:[t,r,u,v];
lg:ident(4);
lg[2,2]:-a^2/(1-k*r^2);
lg[3,3]:-a^2*r^2;
lg[4,4]:-a^2*r^2*sin(u)^2;
dependencies(a(t));
cmetric();
christof(false);
("We can at last evaluate the Euler-Lagrange equation...")$
G:zeromatrix(4,4);
("This is definitely going to take a little time...")$
ev(EQ);
("...and obtain the Einstein tensor for cosmology.")$
expand(radcan(ug.G));
("What about the spherically symmetric case?")$
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);
G:zeromatrix(4,4);
("We re-evaluate the Euler-Lagrange equation. Be patient...")$
ev(EQ);
G:expand(radcan(ug.G));
("In a vacuum solution, G should be zero. Let's solve for it!")$
E:findde(G,2);
solve(ode2(E[1],A,r),A);
%,%c=-2*M;
a:%[1],%c=-2*M;
ode2(ev(E[2],a),B,r);
b:ev(%,%c=rhs(solve(rhs(%)*rhs(a)=1,%c)[1]));
("The solution must be consistent with the third equation")$
factor(ev(ev(E[3],a,b),diff));
("Finally, we obtain the Schwarzschild-de Sitter metric")$
expand(ev(lg,a,b));
/* End of demo -- comment line needed by MAXIMA to resume demo menu */
