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
has the solutions for every whole number . The principal one takes in . It is continuous everywhere except across the negative real axis, where its imaginary part jumps from (just above) to (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 there is a convention, and it is reported. The real trouble is an uncertain input. Suppose a calculation gives with an error of in each part. Is near or near ? The enclosure of lies across the cut, the true could be on either side, and the two answers differ by . 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 -th root is with the principal logarithm, so it has the same cut and its angle lies in . { branch: k } multiplies by . 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 is not . 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 .
inherits the logarithm's cut and its branches. A whole-number exponent has no cut at all, and a rational exponent is computed as the -th root to the power , 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:
| Function | Formula | Cut | Sides |
|---|---|---|---|
asin | the real axis outside | above, below | |
acos | the same | above, below | |
atan | the imaginary axis outside | right, left | |
asinh | the imaginary axis outside | right, left | |
acosh | the real axis left of 1 | above, below | |
atanh | the real axis outside | above, below |
So asin("2") is on a cut. Its two candidates are , 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 to the logarithm and multiplies an -th root by . 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 instead of 0.
@ratmath/complex and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.