Wednesday, 6 November 2019

Tensor Calculus with Word VBA macros

Presentation of Word VBA macros for helping with tensor equations!
One of the things you often to do when working in with tensor equations is to expand things like Riemann tensors, covariant derivatives and Christoffel symbols. The expansions are shown below at (1) to (6). They all involve shuffling indices and introducing dummy indices which are used in 'contractions'. These are summations over the dummy index so more like an expansion than a contraction.

The expansions are fiddly and after a while my eyes start to pop out. You can see that they pretty much all turn into Christoffel symbols, the ##\Gamma_{\mu\nu}^\sigma## thing. That in turn can be expressed in terms of the metric and inverse metric: ##g_{\mu\nu}\ ,\ g^{\mu\nu}##. So if you know the metrics you can write out all the Christoffel symbols in terms of coordinates - proper equations. There are only slightly less than ##n^3## of these in ## n## dimensions. There's a reduction to only ##n^2\left(n+1\right)/2## coefficients because ##\Gamma_{\mu\nu}^\sigma=\Gamma_{\mu\nu}^\sigma## (that's called being 'torsion free', more like drudgery free). Even when it's six on a the surface of a sphere, that process finally pops my eyes right out of their sockets and it's all too easy to make a mistake at any stage of the process. And it's  40 Christoffel symbols in the four dimensions of General Relativity.

When I came to Exercise 3.12 on 14 October I had to expand a Riemann tensor again. So I wrote some code to do it. That was quite hard and I decided to do covariant derivatives and Christoffel symbols while I was at it. Then I suffered from mission creep and decided to fully expand those ##n^2\left(n+1\right)/2## coefficients of the Christoffel symbol. There are lots of terms like ##g^{\sigma\lambda}\partial_\mu g_{\lambda\nu}## which very often vanish, it was quite hard to work out which they were and use the information. I had to learn about the structure of MS equations from scratch. That may be the subject of another post. Three weeks slipped by and now I now have this all-singing toolbox:

Button

LxL: Insert inline equation
x: Insert equation on new line
x|(n): Insert numbered equation in new table row
x|x|(n): Inserted two part numbered equation row

Grid: Operations for enhancing tabular equations

Expand: Expand equation containing Riemann, Christoffel and/or covariant derivative
Renumber: Renumber all equations and references
To Web: Prepare document for Web/MathJax in new window

Pick up: Pick up definitions from equations



Write Metrics: Write out (& calculate) picked up metrics
Write ##\Gamma##s: Write out full expansion of Riemann symbols using metrics and coordinates.

The box at the bottom is for information and error messages, when it might beep.



Riemann tensor

\begin{align}
R_{\ \ \ \sigma\mu\nu}^\rho=\partial_\mu\Gamma_{\nu\sigma}^\rho-\partial_\nu\Gamma_{\mu\sigma}^\rho+\Gamma_{\mu\lambda}^\rho\Gamma_{\nu\sigma}^\lambda-\Gamma_{\nu\lambda}^\rho\Gamma_{\mu\sigma}^\lambda&\phantom {10000}(1)\nonumber
\end{align}Covariant derivative
(0,2) tensor
\begin{align}
\nabla_\mu V^{\nu\rho}=\partial_\mu V^{\nu\rho}+\Gamma_{\mu\lambda}^\nu V^{\lambda\rho}+\Gamma_{\mu\lambda}^\rho V^{\nu\lambda}&\phantom {10000}(2)\nonumber
\end{align}vector
\begin{align}
\nabla_\mu V^\nu=\partial_\mu V^\nu+\Gamma_{\mu\lambda}^\nu V^\lambda&\phantom {10000}(3)\nonumber
\end{align}one form
\begin{align}
\nabla_\mu\omega_\nu=\partial_\mu\omega_\nu-\Gamma_{\mu\nu}^\lambda\omega_\lambda&\phantom {10000}(4)\nonumber
\end{align}Other tensor
\begin{align}
\nabla_\mu U_{\ \ \lambda\ \ \kappa}^{\nu\ \rho}=\partial_\mu U_{\ \ \lambda\ \ \kappa}^{\nu\ \rho}+\Gamma_{\mu\sigma}^\nu U_{\ \ \lambda\ \ \kappa}^{\sigma\ \rho}+\Gamma_{\mu\sigma}^\rho U_{\ \ \lambda\ \ \kappa}^{\nu\ \sigma}-\Gamma_{\mu\lambda}^\sigma U_{\ \ \sigma\ \ \kappa}^{\nu\ \rho}-\Gamma_{\mu\kappa}^\sigma U_{\ \ \lambda\ \ \sigma}^{\nu\ \rho}&\phantom {10000}(5)\nonumber
\end{align}Christoffel symbol
\begin{align}
\Gamma_{\mu\nu}^\sigma=\frac{1}{2}g^{\sigma\lambda}\left(\partial_\mu g_{\lambda\nu}+\partial_\nu g_{\mu\lambda}-\partial_\lambda g_{\mu\nu}\right)&\phantom {10000}(6)\nonumber
\end{align} To read more about the macros click Read More.
Macros available at Archive2019-11-06.
Pdf file here: Tensor Calculus.pdf.


About the Macros

The code is implemented in three modules of Word VBA and one form. The macro to launch the Tensor Calculus form is Main.StartAll. The form is called TensorCalculus. Most buttons into another macro. Those can all be also activated from the Quick Access toolbar which might have advantages, except that there are so many. The following list describes the action of each button and macro in more detail.

There is one module called OldMacros which mostly contains code written some time ago and reflects my lack of knowledge about MS equations. It contains a few constants which may be changed. The first has an obvious meaning I have it set to 12, compared to my normal font of 10. The second two are used when producing MathJax / Latex.
EquationFontSize = 12
InlineDelimiter = "##"
DisplayDelimiter = "$$"

Insert in line equation 

Button: LxL, Macro: OldMacros.InsertEquationInLine
Inserts inline equation followed by space which is important not to change the font in the rest of the line. Uses EquationFontSize.

Insert equation on a new line

Button: x, Macro: OldMacros.InsertEquationNewLine
Inserts equation on a new line. Best used on an empty line. Uses EquationFontSize.

Insert numbered equation on a new line

Button: x|(n), Macro: OldMacros.EquationTable2
Inserts numbered equation on a new line. Best used on an empty line. Uses EquationFontSize. Example:
\begin{align}
=&\phantom {10000}(7)\nonumber
\end{align}The equation contains the = sign and the number is one higher tan the preceding equation number. Normally the table row is a bit higher (default TableHeight = 1.29 cm) and the table has not outline. If the insertion point is immediately below a similar line, the row is formed as another row of the table.

Insert numbered two part equation on a new line

Button: x|x|(n), Macro: OldMacros.EquationTable3
Inserts numbered two part equation on a new line. Best used on an empty line. Uses EquationFontSize. Example:
\begin{align}
x&=&\phantom {10000}(8)\nonumber
\end{align}In this case there are two equations in the row, the first containing ## x##. Useful when there are a few lines to expand an expression or see example for Christoffel symbol expansion (20)-(28) below.

Show / Hide borders (added for Office 365)

Buttons: Show / Hide, Macros: OldMacros.BordersAll / BordersNone
Shows or hides or all borders on a table. Latter useful when equations get in a mess.

Box round table (added for Office 365)

Button: Box, Macro: OldMacros.EquationTable3
Puts a big box round a table. Useful when you get to the answer. Example:
\begin{align}
\therefore x=42&\phantom {10000}(9)\nonumber
\end{align}

Pack table rows closer

Button: Pack, Macro: OldMacros.Point8cmTableRows
Reduces selected table row heights to 0.8 cm.

Expand an equation

Button: Expand, Macro: Expander.ExpandSymbols
Expands all Riemann tensors ##R_{\ \ \ bcd}^a##, covariant derivatives ##\nabla_a## and Christoffel symbols ##\Gamma_{bc}^a##  according to equations (1) to (6) and further generalizations in the case of covariant derivatives.

The operand of a covariant derivative must be straightforward tensor. Brackets are not tolerated. Metric compatibility (##\nabla_\sigma g_{\mu\nu}=0##) is not recognized.
Riemann tensors are recognized by the ## R##.

Dummy (summation) indices are found by GetNewDummyIndex. Using indices already in the equation is avoided by FindIndexesUsed. If existing indices are all roman then the new dummy indices will be roman. Otherwise greek. If all indexes are used then new dummy indices cannot be provided so some bizarre symbol is inserted. It cannot be shown by the To Web function! Here's an image
Christoffel symbols generated are not re-expanded. Apply Expand again.

Renumber all equations

Button: Expand, Macro: OldMacros.RenumberEquationsCode
Renumbers all equations in document. Useful after new equations have been added in the middle of the document. Tip: When adding in new equations in the middle of a document, start the numbering at, say, 100. Prompts and assists to save document first before starting.

To Web: Create plain text and Latex suitable for MathJax processor

Button: Expand, Macro: OldMacros.RenumberEquationsCode
Opens a new window and copies the whole document as plain text and latex into it. With more editing it is suitable for a web page with a Latex / MathJax processor (such as Physics Forums). (Here's how I did it. See end of the post.)

The equation editor must be in Latex mode. I have not found a way to check this or change it in code. Special processing occurs at tables and equations:
Inline equations are output as inline Latex typically with ## delimiters.
Standalone equations are output as stand alone Latex typically with $$ delimiters.
Tables are assumed to have been created as numbered equations and generate latex code using the \begin{align}, \end{align} structure with line delimiters and equations and numbers inside.
Other tables might be destroyed!
Delimiters can be altered with the InlineDelimiter, DisplayDelimiter constants.

Pick up Coordinates

Button: Coordinates, Macro: TensorCalculus.PickUpCoordinates_Click
Pick up coordinates from an equation like ##\left\{t,r,\theta,\phi\right\}## and display them. These are used by Write Γs (see below).

Pick up Metric / Inverse Metric

Button: Metric / Inv. Metric, Macros: TensorCalculus. PickUpMetric_Click, PickUpInvMetric_Click
Picks up metric or inverse metric from a selected matrix see (11) and displays the dimensions. These are used by Write Metrics, Write Γs (see below).

Write metrics

Button: Write Metrics, Macro: TensorCalculus.WriteMetrics_Click
Writes out metrics in an equation. If inverse metric is undefined and the metric is diagonal the inverse metric will be calculated.

Write Christoffel Symbols

Button: Write Γs, Macro: Expander.WriteChristoffelSymbols
Writes out all the Christoffel symbols. Coordinates and both metrics must be loaded and same dimension. Christoffel symbol are in form ##\Gamma_{r\theta}^\theta## where ## r,\theta,\phi## were coordinates inless the first or second coordinate is ## x## when they are in the form ##\Gamma_{13}^2##. Zero components in the metrics  are eliminated. Derivatives of expressions not containing what is being  differentiated with respect to are also eliminated. For example
\begin{align}
\frac{\partial}{\partial x}\left({tz}^2\right)\rightarrow0&\phantom {10000}(10)\nonumber
\end{align}Identical terms in ##g^{\sigma\lambda}\partial_\mu g_{\lambda\nu}+g^{\sigma\lambda}\partial_\nu g_{\mu\lambda}-g^{\sigma\lambda}\partial_\lambda g_{\mu\nu}## may be identified and added or subtracted. Further differentiation and simplification is not attempted. See (20) to (28) for examples.

Invert equation

Button: -, Macro: Main.InvertEquation
There is no button for this bit of code that inverts an equation. It is a stub for testing InvertOmath which is used to calculate inverse metrics. It illustrates some of the difficulties in expression manipulation.

Apple Mac

This code will probably not work on Apple Macs unless operating with normal Word in a windows shell. VBA help tells us that
Visual Basic for the Macintosh does not support Unicode strings. Therefore, ChrW(n) cannot return all Unicode characters for n values in the range of 128–65,535, as it does in the Windows environment. Instead, ChrW(n) attempts a "best guess" for Unicode values n greater than 127. Therefore, you should not use ChrW in the Macintosh environment.
Use of Unicode strings is unavoidable.

Possible Enhancements

Add Undo, see https://docs.microsoft.com/en-us/office/vba/word/concepts/working-with-word/working-with-the-undorecord-object
Discover how to control Latex on/off. Posted a question on msofficeforums
Return focus to document after button pressed in Tensor Calculus dialog.
Renumber all equations should use progress error window in dialog.
Avoid use of function ToUnicode now that I understand more!
Pick up metric from equation such as ##{ds}^2={d\theta}^2+\sin^2{\theta}{d\phi}^2##.
Extract and replace latex for use on compatible websites that do differentiation etc.

Examples and notes

Surface of a sphere (2-sphere)

Ex 3.05 2-sphere geodesics and parallel transport.docx
Metric
\begin{align}
g_{ij}=\left(\begin{matrix}\mathrm{1}&\mathrm{0}\\\mathrm{0}&\sin^2{\theta}\\\end{matrix}\right)\ \ ,\ \ g^{ij}=\left(\begin{matrix}\mathrm{1}&\mathrm{0}\\\mathrm{0}&\sin^{-2}{\theta}\\\end{matrix}\right)&\phantom {10000}(11)\nonumber
\end{align}coordinates ##\left(\theta,\phi\right)##
Alternate metric (with strict italicisation)
\begin{align}
{ds}^2={d\theta}^2+\sin^2{\theta}{d\phi}^2&\phantom {10000}(12)\nonumber
\end{align}The Christoffel symbols were all 0 except
\begin{align}
\Gamma_{\phi\phi}^\theta&=-\sin{\theta}\cos{\theta}&\phantom {10000}(13)\nonumber\\
\Gamma_{\theta\phi}^\phi=\Gamma_{\phi\theta}^\phi&=\frac{2\sin{\theta}\cos{\theta}}{2\sin^2{\theta}}=\cot{\theta}&\phantom {10000}(14)\nonumber
\end{align}

Metric near Earth  (Exercise 3.06)

\begin{align}
\left\{t,r,\theta,\phi\right\}&\phantom {10000}(15)\nonumber
\end{align}Metric$$
{ds}^2=-\left(1+2\Phi\right){dt}^2+\left(1-2\Phi\right){dr}^2+r^2\left({d\theta}^2+\sin^2{\theta}{d\phi}^2\right)
$$Expanding ## \Phi## that is$$
{ds}^2=-\left(\frac{2GM}{r}-1\right){dt}^2+\left(1+\frac{2GM}{r}\right){dr}^2+r^2{d\theta}^2+r^2\sin^2{\theta}{d\phi}^2
$$
\begin{align}
g_{\mu\nu}&=\left(\begin{matrix}-\left(1+2\Phi\right)&0&0&0\\0&\left(1-2\Phi\right)&0&0\\0&0&r^2&0\\0&0&0&r^2\sin^2{\theta}\\\end{matrix}\right)&\phantom {10000}(16)\nonumber\\
g^{\mu\nu}&=\left(\begin{matrix}-\frac{1}{\left(1+2\Phi\right)}&0&0&0\\0&\frac{1}{\left(1-2\Phi\right)}&0&0\\0&0&\frac{1}{r^2}&0\\0&0&0&\frac{1}{r^2\sin^2{\theta}}\\\end{matrix}\right)&\phantom {10000}(17)\nonumber
\end{align}where$$
\Phi=-\frac{GM}{r}
$$So

\begin{align}
g_{\mu\nu}&=\left(\begin{matrix}\left(\frac{2GM}{r}-1\right)&\mathrm{0}&\mathrm{0}&\mathrm{0}\\\mathrm{0}&\left(1+\frac{2GM}{r}\right)&\mathrm{0}&\mathrm{0}\\\mathrm{0}&\mathrm{0}&r^2&\mathrm{0}\\\mathrm{0}&\mathrm{0}&\mathrm{0}&r^2\sin^2{\theta}\\\end{matrix}\right)&\phantom {10000}(18)\nonumber\\
g^{\mu\nu}&=\left(\begin{matrix}\frac{r}{2GM-r}&\mathrm{0}&\mathrm{0}&\mathrm{0}\\\mathrm{0}&\frac{r}{2GM+r}&\mathrm{0}&\mathrm{0}\\\mathrm{0}&\mathrm{0}&\frac{1}{r^2}&\mathrm{0}\\\mathrm{0}&\mathrm{0}&\mathrm{0}&\frac{1}{r^2\sin^2{\theta}}\\\end{matrix}\right)&\phantom {10000}(19)\nonumber
\end{align}Eliminating 0's and duplicates we got the following. I simplified the expressions by hand after the ##\rightarrow## sign. The result is the same as I got so laboriously in Exercise 3.06. Hurrah.
\begin{align}
\Gamma_{tr}^t&=\Gamma_{rt}^t=\ \frac{1}{2}\left(\frac{r}{2GM-r}\frac{\partial}{\partial r}\left(\left(\frac{2GM}{r}-1\right)\right)\right)\rightarrow\frac{1}{2}\frac{r}{2GM-r}\frac{-2GM}{r^2}=\frac{GM}{r^2-2GMr}&
&\phantom {10000}(20)\nonumber\\
\Gamma_{tt}^r&=\ \frac{1}{2}\left(-\left(\frac{r}{2GM+r}\frac{\partial}{\partial r}\left(\left(\frac{2GM}{r}-1\right)\right)\right)\right)\rightarrow\frac{1}{2}\frac{-r}{2GM+r}\frac{-2GM}{r^2}=\frac{GM}{r^2+2GMr}&
&\phantom {10000}(21)\nonumber\\
\Gamma_{rr}^r&=\ \frac{1}{2}\left(\frac{r}{2GM+r}\frac{\partial}{\partial r}\left(\left(1+\frac{2GM}{r}\right)\right)\right)\rightarrow\frac{1}{2}\frac{r}{2GM+r}\frac{-2GM}{r^2}=\frac{-GM}{r^2+2GMr}&
&\phantom {10000}(22)\nonumber\\
\Gamma_{\theta\theta}^r&=\ \frac{1}{2}\left(-\left(\frac{r}{2GM+r}\frac{\partial}{\partial r}\left(r^2\right)\right)\right)\rightarrow\frac{1}{2}\frac{-2r^2}{2GM+r}=\frac{-r^2}{2GM+r}&
&\phantom {10000}(23)\nonumber\\
\Gamma_{\phi\phi}^r&=\ \frac{1}{2}\left(-\left(\frac{r}{2GM+r}\frac{\partial}{\partial r}\left(r^2\sin^2{\theta}\right)\right)\right)\rightarrow\frac{1}{2}\frac{-2r^2\sin^2{\theta}}{2GM+r}=\frac{-r^2\sin^2{\theta}}{2GM+r}&
&\phantom {10000}(24)\nonumber\\
\Gamma_{r\theta}^\theta&=\Gamma_{\theta r}^\theta=\ \frac{1}{2}\left(\frac{1}{r^2}\frac{\partial}{\partial r}\left(r^2\right)\right)\rightarrow\frac{1}{r}&
&\phantom {10000}(25)\nonumber\\
\Gamma_{\phi\phi}^\theta&=\ \frac{1}{2}\left(-\left(\frac{1}{r^2}\frac{\partial}{\partial\theta}\left(r^2\sin^2{\theta}\right)\right)\right)\rightarrow-\sin{\theta}\cos{\theta}&
&\phantom {10000}(26)\nonumber\\
\Gamma_{r\phi}^\phi&=\Gamma_{\phi r}^\phi=\ \frac{1}{2}\left(\frac{1}{r^2\sin^2{\theta}}\frac{\partial}{\partial r}\left(r^2\sin^2{\theta}\right)\right)\rightarrow\frac{1}{r}&
&\phantom {10000}(27)\nonumber\\
\Gamma_{\theta\phi}^\phi&=\Gamma_{\phi\theta}^\phi=\ \frac{1}{2}\left(\frac{1}{r^2\sin^2{\theta}}\frac{\partial}{\partial\theta}\left(r^2\sin^2{\theta}\right)\right)\rightarrow\cot{\theta}&
&\phantom {10000}(28)\nonumber
\end{align}Special tests for WriteChristoffelSymbols
These exercise nearly all paths in WriteChristoffel from
If (LinearTerm(1) = "0") And (LinearTerm(2) = "0") And (LinearTerm(3) = "0") Then
coordinates##\left(x,y\right)##
metric for all Christoffel symbols$$
g_{ij}=\left(\begin{matrix}xy11&xy12\\xy12&xy22\\\end{matrix}\right)\ ,\ g^{ij}=\left(\begin{matrix}ixy11&ixy12\\ixy12&ixy22\\\end{matrix}\right)
$$exercises ElseIf LinearTerm(1) = LinearTerm(3) & ElseIf LinearTerm(2) = LinearTerm(3)$$
g_{ij}=\left(\begin{matrix}\mathrm{1}&\mathrm{0}\\\mathrm{0}&xy\\\end{matrix}\right)\ ,\ g^{ij}=\left(\begin{matrix}\mathrm{1}&\mathrm{0}\\\mathrm{0}&\frac{1}{xy}\\\end{matrix}\right)
$$

No comments:

Post a comment