Branches

Branches and cuts

The logarithm, roots, powers and the inverse trigonometric functions have many values. Choosing one everywhere forces a jump along a curve, the branch cut. This package tells you when an input is on a cut, gives the value on each side, and can follow a branch continuously along a path.

The logarithm

ew=ze^w = z has the solutions w=log⁡∣z∣+i(θ+2πk)w = \log|z| + i(\theta + 2\pi k) for every whole number kk. The principal one takes θ\theta in (−π,π](-\pi, \pi]. It is continuous everywhere except across the negative real axis, where its imaginary part jumps from π\pi (just above) to −π-\pi (just below).

import { log, rect } from "@ratmath/complex";

const a = log("1 + i");
String(a);            // "0.34657359028 + 0.7853981634i ± 4.2e-22"
a.value;              // the Rect
a.straddles;          // false

const b = log("-1");
b.straddles;          // true: the input is on the cut
b.value;              // null
b.candidates;         // [{ side: "above", value: Rect }, { side: "below", value: Rect }]
b.convention.side;    // "above": what the usual convention picks for a point exactly on the cut
String(b);
// on the cut (the negative real axis).  above: 0 + 3.1415926536i ± 1.7e-21;
// below: 0 - 3.1415926536i ± 1.7e-21.  Convention: above

String(log("-1", { side: "below" }));   // "0 - 3.1415926536i ± 1.7e-21"
String(log("i", { branch: 1 }));        // "0 + 7.853981634i ± 3.4e-21  (branch 1)": adds 2πi

Every function on this page returns a Branched result with these fields. .rect is the value when there is one and otherwise throws a ComplexError (code branch-cut) that names the sides to choose from, so code that never expects a cut fails loudly instead of quietly using the wrong value. .hull is a rectangle around every candidate.

Why report instead of choosing

For the exact point −1-1 there is a convention, and it is reported. The real trouble is an uncertain input. Suppose a calculation gives z=−2z = -2 with an error of 10−1010^{-10} in each part. Is log⁡z\log z near iπi\pi or near −iπ-i\pi? The enclosure of zz lies across the cut, the true zz could be on either side, and the two answers differ by 2πi2\pi i. Floating-point arithmetic picks whichever side the rounding error happened to land on. Here the result says straddles: true, gives both candidates, and has no convention, because none would be honest:

const c = log(rect("-2", "1/10"));
c.straddles;       // true
c.convention;      // null
c.candidates.map((x) => `${x.side}: ${x.value}`);
// ["above: 0.69246186711 + 3.11530112278i ± 5.1e-2", "below: 0.69246186711 - 3.11530112278i ± 5.1e-2"]

Each candidate is the function continued from that side over the whole input, so if you know from elsewhere which side the true value is on, { side } gives a correct enclosure.

Roots and powers

The principal nn-th root is e(log⁡z)/ne^{(\log z)/n} with the principal logarithm, so it has the same cut and its angle lies in (−π/n,π/n](-\pi/n, \pi/n]. { branch: k } multiplies by e2πik/ne^{2\pi i k/n}. A square root that is a complex rational is returned exactly.

import { sqrt, root, pow } from "@ratmath/complex";

String(sqrt("3 + 4i"));                 // "2 + i"
String(sqrt("-4"));                     // on the cut.  above: 2i;  below: -2i.  Convention: above
String(sqrt("-4", { side: "below" }));  // "-2i"
String(root("-8", 3));                  // on the cut: 1 + 1.732…i above, 1 − 1.732…i below
String(pow("i", "i"));                  // "0.20787957635 + 0i ± 1.1e-22": e^(−π/2)
String(pow("1 + i", 10).value);         // "32i": whole-number powers of exact numbers are exact

The principal cube root of −8-8 is not −2-2. The real cube root is branch 1 of the three; a real-number calculator picks it because it never leaves the real line. roots(z, n) returns all nn.

zw=ewlog⁡zz^w = e^{w \log z} inherits the logarithm's cut and its branches. A whole-number exponent has no cut at all, and a rational exponent p/qp/q is computed as the qq-th root to the power pp, so that exact roots stay exact.

Inverse functions

Each is defined by a formula in logarithms and square roots, and its cuts come from theirs:

FunctionFormulaCutSides
asin−ilog⁡ ⁣(iz+1−z1+z)-i\log\!\big(iz + \sqrt{1 - z}\sqrt{1 + z}\big)the real axis outside [−1,1][-1, 1]above, below
acosπ/2−arcsin⁡z\pi/2 - \arcsin zthe sameabove, below
atan−i atanh⁡(iz)-i\,\operatorname{atanh}(iz)the imaginary axis outside [−i,i][-i, i]right, left
asinh−iarcsin⁡(iz)-i\arcsin(iz)the imaginary axis outside [−i,i][-i, i]right, left
acoshlog⁡ ⁣(z+z−1z+1)\log\!\big(z + \sqrt{z - 1}\sqrt{z + 1}\big)the real axis left of 1above, below
atanh12(log⁡(1+z)−log⁡(1−z))\tfrac12\big(\log(1 + z) - \log(1 - z)\big)the real axis outside [−1,1][-1, 1]above, below

So asin("2") is on a cut. Its two candidates are π/2±1.3169…i\pi/2 \pm 1.3169\ldots i, and the convention reported for the exact point is the one the formula gives with principal values, with nothing else decided by hand. Conventions on cuts differ between systems (some use the sign of a floating-point zero to pick the side), which is one more reason the result names the side instead of leaving you to find out.

Following a branch along a path

A branch can be continued along any path that avoids the branch point: keep the value changing continuously, and when the path crosses the cut, move to the next branch instead of jumping. continueAlong does this for a polygonal path of exact points, and the crossings are decided exactly, with no numerical tolerance.

import { continueAlong, winding } from "@ratmath/complex";

const loop = ["1", "i", "-1", "-i", "1"];        // once round the origin
const r = continueAlong("log", loop);
String(r.value);     // "0 + 6.2831853072i ± 3.4e-21": log 1 has become 2πi
r.turns;             // 1
r.values;            // the value at every vertex

String(continueAlong({ root: 2 }, loop).value);   // "-1 + 0i ± 4.2e-22": √1 has become −1
winding(loop);       // 1: the winding number about 0, exact
winding(["2", "2i", "-2", "-2i"], "1/2");         // about another point

This is the monodromy of the function: going once round the branch point adds 2πi2\pi i to the logarithm and multiplies an nn-th root by e2πi/ne^{2\pi i/n}. The surface on which these functions become single-valued, the Riemann surface, is exactly the record of how many times you have been round. { about: c } puts the branch point at cc instead of 0.

Try itEdit the code and run it with @ratmath/complex and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs