(* Curvature Calculations *)

Norm[v_] := Sqrt[v . v] (* Norm of a vector *)

(* Let x[u,v] be the coordinate patch *)

xu[x_,u_,v_] := Simplify[Derivative[1,0][x][u,v]]
xv[x_,u_,v_] := Simplify[Derivative[0,1][x][u,v]]
xuu[x_,u_,v_] := Simplify[Derivative[2,0][x][u,v]]
xuv[x_,u_,v_] := Simplify[Derivative[1,1][x][u,v]]
xvu[x_,u_,v_] := Simplify[Derivative[1,1][x][u,v]]
xvv[x_,u_,v_] := Simplify[Derivative[0,2][x][u,v]]

EE[x_,u_,v_] := Simplify[xu[x,u,v] . xu[x,u,v]]
FF[x_,u_,v_] := Simplify[xu[x,u,v] . xv[x,u,v]]
GG[x_,u_,v_] := Simplify[xv[x,u,v] . xv[x,u,v]]

preUU[x_,u_,v_] := Simplify[Cross[xu[x,u,v],xv[x,u,v]]]
(* UU[x_,u_,v_] := Simplify[preUU[x,u,v]]/PowerExpand[Simplify[Norm[preUU[x,u,v]]]]  *)
UU[x_,u_,v_] := Simplify[preUU[x,u,v]]/Simplify[Norm[preUU[x,u,v]]]

ll[x_,u_,v_] := Simplify[UU[x,u,v] . xuu[x,u,v]]
mm[x_,u_,v_] := Simplify[UU[x,u,v] . xuv[x,u,v]]
nn[x_,u_,v_] := Simplify[UU[x,u,v] . xvv[x,u,v]]

(* Gaussian Curvature *)
KK[x_,u_,v_] := Simplify[(ll[x,u,v] nn[x,u,v] - mm[x,u,v]^2)/(EE[x,u,v] GG[x,u,v] - FF[x,u,v]^2)]

(* Mean Curvature *)
HH[x_,u_,v_] := Simplify[(GG[x,u,v] ll[x,u,v] + EE[x,u,v] nn[x,u,v] - 2 FF[x,u,v] mm[x,u,v])/(2 (EE[x,u,v] GG[x,u,v] - FF[x,u,v]^2))]

