(* This gives te wedge product of two 1 forms the 
entries in the order (1,2),(1,3),...,(1,n),(2,3),...,(2,n),(3,4),... 
To read the i<j component of the output, w: w[[Lex[i,j]],n].*)	

MyWedge[u_, v_, n_] := Module[{w = {}, i, j}, 
  For [i = 1, i < n, i++, For[j = i + 1, j <= n, 
    j++, w = Append[w, u[[i]]v[[j]] - u[[j]]v[[i]]]]]; w]
	
Lex[i_,j_,n_]:=If[i>=j,0,(n-(i/2))(i-1)+j-i];

(* This corresponds to dx_i  it is realized as (0,...,0,1,0,...) the 1
in the i-th position*)

dx[i_,n_]:= Block[{L=Table[0,{n}]},L[[i]]=1;L]

(* Thinking of a 1-form as a vector of functions then this outputs the
exterior derivative as an n(n-1)/2-tuple as above with the components 
given in lex order: i.e. coefficient of dx_1 \wedge dx_2 is the first
component  dx_1|wedge dx_3 the second if n=4 then dx_2\wedge dx_3 is the
fourth ...*)

Myd[w_, n_] := -Sum[MyWedge[D[w, x[j]], dx[j, n], n], {j, 1, n}]

(* This is the connection form for a conformally flat metric exp(2f)\Sum(dx_i)^2 
i is the subscript and j is the superscript*) 

MyOmegaC[i_,j_,f_,n_,e_]:= D[f,x[i]]dx[j,n]-e[[i]]e[[j]]D[f,x[j]]dx[i,n]

(* This is the connection form for a metric \sum exp(2f_i)\e_i (dx_i)^2 *)

MyOmegaG[i_,j_,f_,n_,e_]:=Exp[f[[j]]-f[[i]]] D[f[[j]],x[i]]dx[j,n]-
e[[i]]e[[j]]Exp[f[[i]]-f[[j]]] D[f[[i]],x[j]]dx[i,n]

(*This calculates the curvature form for a conformally flat metric *)

CurvC[i_,j_,f_,n_,e_] := Myd[MyOmegaC[i,j,f,n,e],n] + 
Sum[MyWedge[MyOmegaC[p,j,f,n,e], MyOmegaC[i,p,f,n,e], n], {p, 1, n}]

(*This calculates the curvature form for a "diagonal" metric as above *)

CurvG[i_,j_,f_,n_,e_] := Myd[MyOmegaG[i,j,f,n,e],n] + 
Sum[MyWedge[MyOmegaG[p,j,f,n,e], MyOmegaG[i,p,f,n,e], n], {p, 1, n}]

(* Ricci using the curvatures above, Lex is at the top of the file *)
RicciC[i_,j_,f_,n_,e_] := Block[{},If[i<=j,Return[Sum[CurvC[i,k,f,n,e][[Lex[k,j,n]]],{k,1,i-1}]
+Sum[CurvC[i,k,f,n,e][[Lex[k,j,n]]],{k,i+1,j-1}]-
Sum[CurvC[i,k,f,n,e][[Lex[j,k,n]]],{k,j+1,n}]]];RicciC[j,i,f,n,e]]

RicciG[i_,j_,f_,n_,e_] := Block[{},If[i<=j,Return[Sum[CurvG[i,k,f,n,e][[Lex[k,j,n]]],{k,1,i-1}]
+Sum[CurvG[i,k,f,n,e][[Lex[k,j,n]]],{k,i+1,j-1}]-
Sum[CurvG[i,k,f,n,e][[Lex[j,k,n]]],{k,j+1,n}]]];RicciG[j,i,f,n,e]]

(* Scalar curvature conformally flat metric *)
RC[f_,n_,e_] := Module[{i,j,R=0,k=0},For[i=1,i<n,i++,For[j=i+1,j<=n,j++,k++;v[i,j]=
CurvC[i,j,f,n,e];R=R-v[i,j][[k]]]];Exp[-2 f] R];

(* Scalar curvature diagonal metric *)
RG[f_,n_,e_] := Module[{i,j,R=0,k=0},For[i=1,i<n,i++,For[j=i+1,j<=n,j++,k++;R=R+
Exp[-f[[i]]-f[[j]]] CurvG[i,j,f,n,e][[k]]]];R];



   
