Skip to content

feat: add symbolic definite integration for piecewise functions - #413

Merged
arnog merged 2 commits into
cortex-js:mainfrom
KingArth0r:feat/piecewise-integration
Oct 6, 2026
Merged

arnog merged 2 commits into
cortex-js:mainfrom
KingArth0r:feat/piecewise-integration

Conversation

@KingArth0r

Copy link
Copy Markdown
Contributor

Summary

Add symbolic definite integration of piecewise expressions over finite real intervals. Partition the interval at established switch points, select each expression on the resulting open subintervals, and delegate the smooth pieces to the existing Integrate machinery.

  • Support If/Which with first-true branch ordering, Min/Max, Floor, Ceil, and Fract.
  • Keep the affine fast path; reuse bounded-domain Solve for supported nonlinear crossings and periodic root families.
  • Reuse CE's exact-constant sign resolver for irrational/transcendental boundaries instead of replacing them with floating-point roots or tolerance-based ties.
  • Include poles and real-domain boundaries when classifying conditions. Preserve existing one-sided pole checks when integrating the selected pieces.
  • Use derivatives and established critical points to bound nonlinear integer-level partitions.
  • Preserve variable binding, reversed bounds, isolated exceptional point values, optional integration-provider dispatch, angular units, and user-defined library overrides.

Scope and safeguards

This is finite, real, symbolic definite integration, not a general inequality solver or an indefinite piecewise antiderivative implementation. Unsupported conditions, incomplete root sets, undecidable ordering/domain checks, and unresolved cell integrals remain unevaluated. Work is bounded by 128 interior cuts, 128 nonlinear solve calls, and a traversal budget. Infinite jump sets and general periodic/infinite-series reductions are not included. Nested combinations that cannot be classified completely may remain unevaluated.

Files

  • src/compute-engine/symbolic/piecewise-integrate.ts: partitioning, classification, and integration.
  • src/compute-engine/library/calculus.ts: Integrate dispatch (plus one formatter-required line wrap).
  • test/compute-engine/integrate-piecewise.test.ts: 60 regression tests.
  • test/compute-engine/compile-integrate.test.ts: expect exact compilation for the newly solvable example; retain numerical-integration coverage with an unsupported nonlinear trig condition.

Validation

  • 1,044 tests across 13 integration, compiler, parser, contour, and bounded-solver suites passed; 221 snapshots passed.
  • The final piecewise suite was rerun after the root-cache adjustment: all 60 tests passed.
  • TypeScript no-emit check, targeted source ESLint, formatting checks, and git diff checks passed.
  • Dependency check found no circular imports.
  • The full repository test suite and corpus pipeline were not run.

Built on upstream main at 5130eec. Integral Benchmark changes are intentionally excluded.

@arnog
arnog merged commit bd798dc into cortex-js:main Oct 6, 2026
4 of 5 checks passed
arnog added a commit that referenced this pull request Oct 6, 2026
…tions and enhance tests for exact results

Contributed by [KingArth0r](https://github.com/KingArth0r
@arnog

arnog commented Oct 6, 2026

Copy link
Copy Markdown
Member

Thanks, looks good, merged.

While reviewing it we found that EvaluateAt turned irrational bounds into floats (∫₂^√5 4 dx gave 0.944…), which made many piecewise results inexact. That is fixed on main now, so ∫₀³ ⌊x²⌋ dx gives 21 − 3√2 − √3 − √5 − √6 − √7.

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.

2 participants