Let $g\in\mathcal R^{3\times 3}$ be a given known rank 2 tensor function, $gInv$ its inverse, and $dg\in\mathcal R^{3\times 3\times 3}$ its gradiant:
g[x_,y_,z_] = {{g11[x,y,z],g12[x,y,z],g13[x,y,z]},
{g21[x,y,z],g22[x,y,z],g23[x,y,z]},
{g31[x,y,z],g32[x,y,z],g33[x,y,z]}};
dg[x_,y_,z_] = Grad[g[x, y, z], {x, y, z}];
I want to define a rank 3 tensor function componentwise by looping over each entry. Ideally like this:
G[x_, y_, z_][[k, i, j]] = 0;
For[i = 1, i <= 3, i++, For[j = 1, j <= 3, j++, For[k = 1, k <= 3, k++, For[d = 1, d <= 3, d++,
G[x_, y_, z_][[k, i, j]] += 0.5 gInv[x, y, z][[k, d]]
(dg[x, y, z][[i, d, j]] + dg[x, y, z][[d, j, i]] - dg[x, y, z][[i, j, d]]);
]]]]
or maybe like this if the += is not suitable for a function definition:
For[i = 1, i <= 3, i++, For[j = 1, j <= 3, j++, For[k = 1, k <= 3, k++,
G[x_, y_, z_][[k, i, j]] =
0.5 gInv[x, y, z][[k, 1]](dg[x, y, z][[i, 1, j]] + dg[x, y, z][[1, j, i]] - dg[x, y, z][[i, j, 1]])
+0.5 gInv[x, y, z][[k, 2]](dg[x, y, z][[i, 2, j]] + dg[x, y, z][[2, j, i]] - dg[x, y, z][[i, j, 2]])
+0.5 gInv[x, y, z][[k, 3]](dg[x, y, z][[i, 3, j]] + dg[x, y, z][[3, j, i]] - dg[x, y, z][[i, j, 3]]);
]]]
However this notation does not work and unrolling all 4 For loops will most centainly introduce typos.
I am rather new to mathematica. Maybe there is a completely different solution which is much better. Feel free correct my approach or advice me something completely different. I might learn something.
Physics Background:
G are the Christoffel symbols from general relativity/differential geometry, and g the spatial 3-metric of a spatial hypersurface.
g[1,2][x_, y_, z_]
andG[k_,i_,j_][x_,y_,z_] = ...
(note that the first set of brackets are not[[double]]
). $\endgroup$TensorProduct
-TensorContract
combo. $\endgroup$