# How do you implement metric tensor in Mathematica?

I'm trying to follow several tutorials on metric tensors. One of the common exercises is a polar metric. I'm stuck trying to implement this in Mathematica. According to the derivation, the polar metric is this:$$g_{\mu\nu}= \begin{bmatrix}1 & 0 & 0\\0 & r^2 & 0\\0 & 0 & r^2 sin^2\theta\end{bmatrix}$$So if I want to implement this in Mathematica, I have: $$g_{\mu\nu}[r_,\phi,\theta]:=\{\{1.,0.,0.\},\{0.,r^2,0.\},\{0.,0.,r^2 Sin^2[\theta]\}$$Now, my goal is to find $$ds^2$$, the distance between two points on this manifold using the formula $$ds^2=\sum_{i,j=1}^3 g_{\mu\nu}dx^idx^j$$ So I construct two random points. I know from another exercise that the distance between these two points is $$\sqrt {17}$$:

p1 = CoordinateTransformData["Cartesian" -> "Spherical",
"Mapping", {4., 2., -1.}];
p2 = CoordinateTransformData["Cartesian" -> "Spherical",
"Mapping", {7., 4., -3.}];


And this is where I'm stuck. How do I use $$g_{\mu\nu}$$ to calculation the distance between the two points? That is, I have an $$r$$ parameter in the metric, but I can only extract a $$dr, d\theta, d\phi$$ from the two points. Where do I find the values that go into the matrix ($$r$$ and $$\theta$$)?

• The Riemannian metric defines the geodesic distance in a very indirect way: The geodesic distance between two points is the length of the shortest curve connecting the two points. By variational methods, one finds out that the shortest curve has to satisfy the geodesic equation. The latter is a nonlinear second order ordinatory differential equation with boundary conditions. That can be solved, e.g., with NDSolve and the shooting method. For special metrics, there is also a chance that one can derive a closed symbolic expression for the geodesic distance... Sep 22, 2019 at 16:26
• @HenrikSchumacher - Thanks for the attempt, but at this point I'm pretty friggin tired of theoretical discussions. I've parsed three textbooks and half a dozen papers. I want to see an example and understand step-by-step how the parameters are passed around. The summation looks like a simple formula, so, practically, how does one use it when it has parameters like $r$ and $\theta$ when the only parameters available are differentials? Sep 22, 2019 at 16:41
• What I tried to say: It is just not straight-forward to compute the geodesic distance between two points with respect to an arbitrary Riemannian metric. In general, one has to determine the shortest curve connecting these two points first. An in general, this curve is not a straight line (in the Euclidean sense of straightness; it is of course geodesically straight). Sep 22, 2019 at 16:53

Assuming we solved Euler's equations in a Cartesian system in a previous problem, we can parameterize the path through our points $$q_1$$ and $$q_2$$ as

q1 = {4, 2, -1};
q2 = {7, 4, -3};

x[t] = q1[[1]] + (q2[[1]] - q1[[1]]) t;
y[t] = q1[[2]] + (q2[[2]] - q1[[2]]) t;
z[t] = q1[[3]] + (q2[[3]] - q1[[3]]) t;


where we are at $$q_1$$ at time $$t=0$$ and at $$q_2$$ at $$t=1$$. Now we transform the path to spherical coordinates $$(r, \theta, \phi)$$ with this

{r[t], θ[t], ϕ[t]} =
CoordinateTransformData["Cartesian" -> "Spherical",
"Mapping", {x[t], y[t], z[t]}]


Then the differentials are

g = {{1, 0, 0}, {0, r[t]^2, 0}, {0, 0, r[t]^2 Sin[θ[t]]^2}};

dx = D[{r[t], θ[t], ϕ[t]}, t]

ds2 = Dot[dx, g, dx]


Finally, integrate along the path to get the distance

dist = Integrate[Sqrt[ds2], {t, 0, 1}];
dist^2        (*  17  *)


Alternate Parameterization

Another way to parameterize the straight line path is

{x[t], y[t], z[t]} = q1 + (q2 - q1) λ[t];


Here we can think of $$\lambda$$ or $$t$$ as the parameter. One important thing is we are at $$q_1$$ when $$\lambda(t_A)=0$$ and we are at $$q_2$$ when $$\lambda(t_B)=1$$. So $$\lambda$$ is a parameter that goes from 0 to 1 along the path while $$t$$ goes from $$t_A$$ to $$t_B$$.

Another important thing is to use the same $$\lambda(t)$$ for x[t], y[t] and z[t] in order to stay on the straight line.

Following the same procedure as above we obtain

ds2 = Dot[dx, g, dx] // Simplify       (*  17 [λ]'[t]^2  *)

dist = Integrate[Sqrt[ds2] // PowerExpand,
{t, Subscript[t, A], Subscript[t, B]}];
dist // TeXForm


$$\int_{t_A}^{t_B} \sqrt{17} \lambda '(t) \, dt$$

The integral evaluates to $$\sqrt{17}$$ for any suitable $$\lambda(t)$$ that takes us to $$q_1$$ at $$t=t_A$$ and to $$q_2$$ at $$t=t_B$$.

We could also show that any $$\lambda(t)$$ is suitable by (1) evaluating

Needs["VariationalMethods"]
EulerEquations[Sqrt[ds2], {λ[t]}, t]

(*  { True }  *)


and (2) stating the boundary conditions on $$\lambda(t)$$.

• And the answer would be much more difficult if one does not use the embedding in a Cartesian space with a flat metric. Sep 23, 2019 at 6:38
• Ha-ha. I just evaluated now Simplify[ ds2 ]` and got 17. I suppose that comes from using a constant velocity. Sep 23, 2019 at 8:10
• Your calculations are absolutely fine. They simply rely on the additional facts: 1) In flat space straight lines are the shortest lines, and 2) there is a known transformation from the spherical to cartesian coordinate system. In fact, the metric tensor allows to determine the same without resorting to these facts. But then, everything gets complicated as @HenrikSchumacher points out. Sep 23, 2019 at 8:39
• This is the right answer and explains why the section on Geodesics followed the section on metric tensors. One observation though: the distance between two points is a static value, so the use of time, $t$, was confusing for me. Wouldn't it make more sense to use the parameterized curve $P(\lambda)$ and then use $\lambda$ instead of $t$? Sep 28, 2019 at 12:52
• @Quarkly Yes, it would make more sense to use $\lambda$ and not refer to "time". I considered changing it, but decided to add the alternate parameterization instead. Note that the parameter does not describe the path or the endpoints. We need the parameter to take (directional) derivatives along the (given) path. The whole answer is so hand-wavy, I'm glad you were able to make some use of it. Sep 28, 2019 at 21:15

Not an answer, but an extended comment with formulas.

A possible misconception here is your assumption that $$\mathrm{d} s^2$$ were the distance between two points. That is just not true. The distance of two points $$p_0$$ and $$p_1$$ is

$$d(p_0,p_1) = \inf_\gamma L(\gamma),$$

where the infimum runs over all smooth curves $$\gamma \colon [0,1] \to \mathbb{R}^n$$ with $$\gamma(0) =p_0$$ and $$\gamma(1) = p_1$$ and where the curve length $$L(\gamma)$$ is given by

$$L(\gamma) = \int_\gamma \mathrm{d}s := \int_0^1 \sqrt{g(\gamma(t)) (\gamma'(t) , \gamma'(t))} \, \mathrm{d} t.$$

So, $$\mathrm{d}s^2$$ does appear in the expression for curve length, but for computing $$d(p_0,p_1)$$ one has also to minimize the expression $$L(\gamma)$$. That is in general quite a nontrivial task.