Surprises in Symbolic Computing - Complex Branch Cuts
Although numerical computing has a big reputation for creating unintuitive failures, symbolic computing is probably just as bad. Unfortunately, symbolic computing receives less attention and many people incorrectly seek it as a refuge from numerical computing’s convoluted nuances. I hope to cover several common mistakes in this blog.
By far, the most common mistake made is failing to understand functions with branch cuts. These are functions (often inverse functions) that could have multiple output values. As an example, consider the square root function. We could say that \(\sqrt{4}\) is either 2 or -2. There are two “branches” that we could choose here. We normally choose it to mean the positive branch (+2), but in some cases it may be helpful to interpret it as (-2). This is a serious ambiguity.
Inverse trigonometric functions are especially troublesome - they have many branchs to choose from. The Sine function is equal to zero for an infinitely many values.
\[0 = sin(0) = sin(\pi) = sin(2 \pi) ...\]So, \(arcsin(0)\) can have an infinite number of values. We choose one of these “branches” by convention. When doing algebra, it is important to choose the correct value based on what we intend (arcsin) to mean. Most often this is just clear from context and human mathematicians don’t give it a second thought.
The correct choice of which branch to take is often just not even encoded in the equation. It’s implicit - not explicit. Without this information, symbolic solvers have to choose a branch cut to take. Even more frustrating, sometimes different solvers or books don’t even agree on which branch to take.
A Basic Algebra Identity That Doesn’t Seem to Hold
Here is a common identity about how exponents and fractions are related:
\[(\frac{a}{b})^n = \frac{a^n}{b^n}\]But this identity can fail, if we accidentally choose the wrong branch cut. Consider:
\[(\frac{-1 + i}{ -1 - i}) ^{\frac{1}{3}}\]Which evaluates to:
\[0.866025 - 0.5i\]And compare it with
\[\frac{(-1 + i)^{\frac{1}{3}}}{( -1 - i)^{\frac{1}{3}}}\]Which just gives:
\[i\]The difference is that different branch cuts have been taken. This is not obvious for most engineers and even many mathematicians.
A Simple Differential Equation Gone Wrong
Consider this differential equation:
\[\sqrt{f'(x)} = -x\]We could solve it like this:
\[f'(x) = x^2\] \[f(x) = \frac{x^3}{3}+c\]This is exactly what a symbolic solver will do with this equation.
This solution doesn’t satisfy the original equation unless we note down that the square root is meant to take the negative branch. Unfortunately, any solver is going to take positive branch instead.
This is just one of the simplest examples of these problems. With larger equations, tracking down the incorrect branch can become a nightmare.
When the Fundamental Theorem of Calculus Fails
This example is taken from an excellent post in Wolfram Research’s Blog.
The fundamental theorem of calculus very roughly says that integration and differentiation are opposites and should undo each other. However, consider the example using a logarithm below. Logarithms, like a lot of inverse functions, have branch cuts.
\[\frac{\partial \log (-x)}{\partial x} = \frac{1}{x}\]So if we integrate \(1/x\), we should get the original function, \(\log(-x)\), right? Nope.
\[\int \frac{1}{x} \, dx = \log (x)\]The functions \(\log(x)\) and \(\log(-x)\) aren’t the same. Notice that we didn’t need to use complex numbers anywhere to see this issue. It’s commonly believed that branch cuts will never be an issue if you stay away from complex numbers. This isn’t the case.
When the fundamental theorem of calculus appears to fail in symbolic computing, branch cuts are almost always the cause. Fortunately, there is a good test to see if this is the case. Because branch cuts change the value of a function by adding a constant complex value, we should get a piecewise constant function when subtracting the expected result from the result we get back. This is a simple test to see if branch cuts are the culprit.
Another simpler example is the integral of \(\frac{1}{\sqrt{1-x^2}}\). This ideally should be ArcSin, but it can also be
\[\tan ^{-1}\left(\frac{x}{\sqrt{1-x^2}}\right)\]These are different functions, but if you subtract them, you’ll see they are piecewise different. They differ in their interpretation of the branchcut taken by ArcTan and ArcSin.