Wednesday, March 23, 2016

Keeping Track of Complicated Algebra

Lately, I have been doing a lot or independent work in classical electromagnetism.  I have found, as anyone who has ever derived the Poynting Vector can attest, that the equations can be hairy.  An equation like $\epsilon_0\frac{\partial}{\partial t} \left (E\times B \right)$ can quickly blow up into several terms, if you are not careful, especially if you don't recognize, at first, that certain collections of terms can be simplified into smaller quantities.  No matter how brilliant you are, if your algebra explodes enough, there will come a point where you will need some way of keeping track of all your expressions.  Not properly doing so risks the possibility of making an obscure algebraic error somewhere, which can be the source of much hair pulling.  This elicits the question of if there is a good way of notating your algebra so that you don't get lost.  Unfortunately, an internet search does not yield very useful results, especially since Google loves to substitute "complex" for "complicated," and we know where that leads!  Fortunately, I have worked out my own method that seems to work pretty well.  So, for this post, I will take a detour from discussing derivations to discussing derivation technique.  If anyone else has different methods to share, I would definitely encourage it in comments.

Issue 1: Introducing New Variables

Suppose that you have just worked out an expression, and it results in a long list of terms.  For example,

$(A + B + C + D)^2 = A^2 + B^2 + C^2 + D^2 + 2AB + 2AC + 2AD + 2BC + 2BD + 2CD$

Yes, I know that can be rewritten in concise form, using summation notation.  Shut up.

Now, suppose that you can expand each term further, but you are particularly interested in $2AB$, and wish to evaluate that first.

Well, one obvious solution is to next write,
$X = 2AB = ...$
And then proceed with the expansion.  Eventually, you substitute X back in.  That's not so bad, obviously.

What if, however, you want to eventually expand some terms, but combine others.  For example, you recognize that $A^2 + 2AC + 2CD$ means something.  Now, you have a problem, because your terms appear in different places in your sum.  In the example I gave above, it isn't so difficult if you write,

$Y = A^2 + 2AC + 2CD = ...$

and proceed as before.

Of course, this changes if your expression is much worse, say if you are raising to the 5th power instead of the 2nd.  Now, you have a problem, since your terms might be scattered all over the place.

You might solve this by reorganizing the terms in the equation to be close to each other, but that means writing your unfortunately long equation again.  There is also the issue that you are doing redundant work by writing, $A^2 + 2AC + 2CD$ again, just to remind yourself what Y is.

Additionally, there is another problem, which I have read others gripe about, and that is that we are about to run out of variable names.  We started at X.

The natural procession would then usually be Y, Z, then W, and then...what?  R, S, T, U, V?

You don't want to have variables that look like preceeding variables, and you don't want to just arbitrarily start assigning random letters, because the relationship between variable name and assignment would be...well...random.  Maybe you start using greek letters like $\eta$.  Now, you need to know the greek alpabet, and, on top of that, mathematicians and physicists love to use greek letters, so you will get collisions.

Fortunately, there is better shorthand to use.  For lack of a more imaginitive name, I call this "curly brace indexed assignment."  Now, before we get into this, keep in mind that this won't apply to certain situations in which there are collisions between notations and variable names, but I believe that in those cases, the modification of the notation is so straightforward as to not be worth mentioning.  It is also important to point out that this notation is a work in progress.  It is probably not complete and likely doesn't cover certain cases, even if you have solved a collision problem.  It is simply something that makes keeping track of your algebra in many cases easy.

Let's start with assigning X, since that's the simplest case.  First, X sucks because it is at the end of the alphabet.  So, let's start with A.  Of course, we see that A already exists as a variable, so let's find some way to easily distinguish it.  The method to do this should really depend on the situation.  In this case, I have simply chosen to index it with a subscript: $A_0$

That's where the "indexed" comes from in my name.  Now for the curly brace.

Under (or over) the same expression, I simply write this:
$(A + B + C + D)^2 = A^2 + B^2 + C^2 + D^2 + \underset{A_0}{\underbrace{2AB}}  + 2AC + 2AD + 2BC + 2BD + 2CD$

For the case of Y, obviously, it won't work to do this, because they expressions appear at different points in the sum.  But, that's ok.  You could simply do the following:
$(A + B + C + D)^2 = \underset{B_0}{\underbrace{A^2}} + B^2 + C^2 + D^2 + \underset{A_0}{\underbrace{2AB}}  + \underset{B_0}{\underbrace{2AC}} + 2AD + 2BC + 2BD + \underset{B_0}{\underbrace{2CD}}$

You might have expected me to increase the index, and choose $A_1$ for this assignment, but remember, you are trying to make this easy on yourself.  Indexes can quickly get out of hand as the number of your variables grows.  So, what you are actually doing is taking advantage of the fact that the alphabet has 26  letters.  That means that, assuming there are no collisions between existing indexed variables, and this notation, you get to assign 26 very different looking variables (or possible 52, if you also choose to use lower case), before you have to increase the index.

In this way, the index serves two purpose, as an endless source of variables, and a way to quickly distinguish new variables from preexisting ones.

Now, obviously, if you had an $X_0$ somewhere, you have a problem, but, this can easily be fixed, and again, it depends on the situation.  Perhaps you could place the subscript on the left side  ${}_0X$ instead, but that's just one solution.

Of course, the preceding examples assume that your sum can be partitioned into non-overlapping sets.  What if you recognize that $\frac{B^2}{2} + C^2$ and $\frac{B^2}{2} + D^2$  are interesting terms?  Here is one way to introduce variables:

$(A + B + C + D)^2 = \underset{B_0}{\underbrace{A^2}} + \underset{2C_0}{\underbrace{B^2}} + \underset{D_0 - C_0}{\underbrace{C^2}} + \underset{E_0 - C_0}{\underbrace{D^2}} + \underset{A_0}{\underbrace{2AB}}  + \underset{B_0}{\underbrace{2AC}} + 2AD + 2BC + 2BD + \underset{B_0}{\underbrace{2CD}}$

Then this implies
$D_0 = \frac{B^2}{2} + C^2$

Likewise,
$E_0 = \frac{B^2}{2} + D^2$

Alternatively, if you didn't want to deal with working out algebra in your head, you could write,
$(A + B + C + D)^2 = \underset{B_0}{\underbrace{A^2}} + \underset{C_0\left\{/2\right\}, D_0\left\{/2\right\}}{\underbrace{B^2}} + \underset{C_0}{\underbrace{C^2}} + \underset{D_0}{\underbrace{D^2}} + \underset{A_0}{\underbrace{2AB}}  + \underset{B_0}{\underbrace{2AC}} + 2AD + 2BC + 2BD + \underset{B_0}{\underbrace{2CD}}$

In this case, the commas in under the curly braces indicate two variable assignments, and the $\left\{/2\right\}$ signifies that you will be dividing by 2 before adding the term to the respective variable.
I haven't tried this yet, but for more complicated expressions, I would suggest using lambda calculus.  For example, if you wanted to take your summand and apply the polynomial, $x^2 - x + 4$ to it, you would write $\left\{\lambda x.(x^2 - x + 4)\right\}$

Now, this works for summation, but what about multiplication?  Consider the following example:
$E(A + B + C + D)$
Suppose, you are interested in A, but want to remember that it is multiplied by E.

Then, you would write
$\underset{A_0}{\underbrace{E}}(\underset{A_0}{\underbrace{A}} + B + C + D)$

Issue 2: Deferring stuff you don't care about.

Sometimes you are doing a horrifying expansion, but you are only interested in part of it.  You don't want to work out the rest of the expansion, at least, not yet.  You simply want to reason with part of it.

Then, you can use ellipses, in combination with curly brace indexed assignment.

Let's say that you have
$(A+B+C+D)^4$

This will have waaaay to many terms to write out if you are only interested in, say, ABCD.

The solution is to write the following:
$(A+B+C+D)^4 = \underset{A_0}{\underbrace{ABCD}} + \underset{B_0}{\underbrace{...}}$

In this case the ellipses stands for "everything else in this sum."

Any Other Ideas?

That concludes my PSA for the day.  Please let me know if you have any suggestions for improving this technique, or alternative techniques.