Skip to content

Compiled special functions give their complex value in auto and complex mode; Arcsec and Arccsc are typed as complex out of domain - #436

Closed
enumeratio wants to merge 2 commits into
cortex-js:mainfrom
enumeratio:compiled-complex-special-functions
Closed

enumeratio wants to merge 2 commits into
cortex-js:mainfrom
enumeratio:compiled-complex-special-functions

Conversation

@enumeratio

Copy link
Copy Markdown
Contributor

Fix #434.

The JavaScript target now follows the compile mode for these heads, as Sqrt, Ln, Arcsin and Power already do:

  • In auto and complex mode: where the facts about the operands don't prove the value real, the head takes the complex lane. It calls the interpreter's complex kernel through _SYS, the way Gamma uses _SYS.cgamma.
  • In strict mode: the real kernel stays, and a complex value gives NaN.
  • A complex operand takes the complex kernel in every mode.

The fact that decides "real, complex or unknown" is in one place, complexContinuation (compilation/complex-continuation.ts). Both isComplexValued and the emitters read it.

head real kernel kept when complex kernel
HurwitzZeta(s, a) a ≥ 0 or integer s _SYS.churwitzZeta
LerchPhi(z, s, a) z ≤ 1 (or integer s ≤ 0), and a ≥ 0 or integer s _SYS.clerchPhi
PolyLog(s, z) z ≤ 1 or integer s ≤ 0 _SYS.cpolyLog
EllipticK(m), EllipticE(m) m ≤ 1 _SYS.cellipticK, _SYS.cellipticE
Hypergeometric2F1 z ≤ 1, or the series terminates _SYS.chypergeometric2F1
LambertW(x, k) x ≥ −1/e on branch 0; [−1/e, 0) on branch −1 _SYS.clambertW

GPU: HurwitzZeta, LerchPhi and PolyLog decline to compile when the value is provably complex (e.g. LerchPhi(3, 2.5, 1)). When it is only unknown, the real helper stays, so a shader over plain symbols still compiles. The other heads have no GPU lowering.

Arcsec and Arccsc: over a real operand of unknown magnitude they are now typed complex | infinity | nan, where before they were typed number. They then take the complex lane, as Arcosh does. Compiled Arcsec(0.5) was NaN and is now 1.3170i.

Tests:

  • The NaN pins in zeta-values, polylog-order, lerch-phi-values and item-374-hurwitz-negative-order now expect the interpreter's value.
  • A new table-driven compile-complex-continuation.test.ts checks each head:
    • compiled in auto and complex mode against .N();
    • strict gives NaN;
    • the real region is unchanged in every mode;
    • a complex operand takes the complex kernel;
    • the GPU targets decline a provably complex value.

Left out: incomplete EllipticE and StieltjesGamma keep their real-only lowerings.

…F1 and LambertW take the interpreter complex kernel in auto and complex mode, strict stays NaN, GPU declines a provably complex value; Arcsec/Arccsc type their complex part. Fix cortex-js#434
arnog added a commit that referenced this pull request Oct 15, 2026
… precision, integration gaps

PR #436 (enumeratio, fix #434): the JavaScript target lowers HurwitzZeta,
LerchPhi, PolyLog, EllipticK, the complete EllipticE, Hypergeometric2F1 and
LambertW to the interpreter's complex kernel in `auto` and `complex` mode
where the operand facts do not prove the value real; `strict` keeps the real
kernel and its NaN; a complex operand takes the complex kernel in every mode.
The verdict lives in one place (`complexContinuation`), read by both
`isComplexValued` and the emitters. The GLSL and WGSL targets decline a
provably complex value, now including a positive integer order on the cut
`z > 1` (`PolyLog(2, 3)`, `LerchPhi(3, 2, 1)`). `Arcsec` and `Arccsc` of an
operand of unknown magnitude type `complex | infinity | nan` instead of
`number`, so the compiled lanes read the complex value.

PR #440 (enumeratio, fix #439): `Argument` of a complex value passes the
bignum parts to `Arctan2`, so `N(Argument(e + iπ), 25)` carries 25 digits.
Also from #439: `Transpose` of an operand with no known shape is typed
`unknown` (a union such as `matrix | vector` is transposed member by
member), so `MatrixMultiply(q, Transpose(q))` with a free `q` stays
unevaluated instead of an `incompatible-type` error.

PR #441 (KingArth0r): definite integrals of more families evaluate in
closed form — an absolute value or sign of a non-affine argument is split at
its exact roots; integer-cell sums (Floor, Ceil, Fract with an affine
weight) and `|sin(kx + m)|` over many periods no longer grow with the cell
count; `∫₀¹ {1/x} dx = 1 − γ` and `∫₀¹ ⌊1/x⌋ dx = +∞`; primitives for Erf,
Erfc, BesselJ(1, ·), BesselI(1, ·), Digamma, Trigamma and PolyGamma of an
affine argument (and `d/dx LogGamma = Digamma`); the nested exponential
`∫ e^{x + e^x} dx`; the Bose moments `∫₀^∞ xⁿ/(e^{ax} − 1) dx`. With the Rubi
rules loaded, a provider antiderivative with no value at a bound falls back
to the built-in one. Loading the rules compiles each distinct pattern once,
and an invalid `stepBudget` is rejected before compilation. The PR's
profiling script and JSON record are not included.

Review fixes on top of the PRs: a user definition with its own `compile`
handler keeps its own lane; compiled `HurwitzZeta(1, z)` for a non-real `z`
is the pole; the period shortcut declines outside radian mode; exact
coefficients in the cell sums stay exact; a polygamma integral between two
consecutive poles evaluates; the infinite-bound inert decision reads the
heads that contain the variable.

ROADMAP: the antiderivative of Sin and Cos ignores the degree angular unit
(found by the review; pre-existing).

Fix #434
Fix #439

Co-authored-by: enumeratio <314378874+enumeratio@users.noreply.github.com>
Co-authored-by: KingArthur <118565558+KingArth0r@users.noreply.github.com>
@arnog

arnog commented Oct 15, 2026

Copy link
Copy Markdown
Member

Merged by hand onto main as 403a216, with the CHANGELOG/ROADMAP conflict resolved. Two additions: a positive integer order on the cut z > 1 is also provably complex, so PolyLog(2, 3) and LerchPhi(3, 2, 1) decline on the shader targets too; and a user definition with its own compile handler keeps its own lane. Thank you.

@arnog arnog closed this Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Compiled code: a complex value from real operands takes the complex lane for Sqrt/Ln/Arcsin/Power, but is NaN for the special functions

2 participants