How to "vectorize" code
- To: mathgroup at smc.vnet.net
- Subject: [mg97066] How to "vectorize" code
- From: Aaron Fude <aaronfude at gmail.com>
- Date: Wed, 4 Mar 2009 07:13:25 -0500 (EST)
Hi,
The code below computes Christoffel symbols for 2D coordinate systems.
I know there are packages that do that, but that's not what I am
after. Computing Christoffels is just a model problem for me. The
actual class of problems that I am after is much different and
wouldn't be in any package, but will have some of the same elements.
I know that the attached code as atrocious and I am looking for hints
on how to "vectorize" it so, for example, in can work in any
dimension, with no loops, etc. So this is really an open ended
question: please help me fix everything that's silly. Many thanks in
advance,
Aaron
Clear["Global`*"];
Christoffel2D[mapping_] := (
e1 = Derivative[1, 0][mapping];
e2 = Derivative[0, 1][mapping];
gij = ({{ e1[##].e1[##], e1[##].e2[##]}, {e2[##].e1[##],
e2[##].e2[##]}} // FullSimplify) &;
gIJ = Inverse[gij[##]] &;
E1 = gIJ[##][[1, 1]]*e1[##] + gIJ[##][[1, 2]]*e2[##] &;
E2 = gIJ[##][[2, 1]]*e1[##] + gIJ[##][[2, 2]]*e2[##] &;
e11 = Derivative[1, 0][e1];
e12 = Derivative[0, 1][e1];
e21 = Derivative[1, 0][e2];
e22 = Derivative[0, 1][e2];
For [i = 1, i <= 2, i = i + 1,
For [j = 1, j <= 2, j = j + 1,
For [k = 1, k <= 2, k = k + 1,
ToExpression[
"G" <> ToString[i] <> ToString[j] <> ToString[k] <> "=Dot[e" <>
ToString[i] <> ToString[j] <> "[##],E" <> ToString[k] <>
"[##]]&;"];
]]];
({
{{G111[##], G112[##]}, {G121[##], G122[##]}},
{{G211[##], G212[##]}, {G221[##], G222[##]}}
} // FullSimplify) &
)
polar[r_, theta_] := {r Sin[theta], -r Cos[theta]}
Christoffel2D[polar][r, theta] // MatrixForm