Skip to content

Add a non-mutating exponential - #296

Merged
ChrisRackauckas merged 2 commits into
SciML:masterfrom
singhharsh1708:exponential-non-mutating
Nov 15, 2026
Merged

ChrisRackauckas merged 2 commits into
SciML:masterfrom
singhharsh1708:exponential-non-mutating

Conversation

@singhharsh1708

@singhharsh1708 singhharsh1708 commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

Adds exponential(A, [method, [cache]]), the non-mutating counterpart of exponential!: it exponentiates a copy of A and leaves A unchanged, including for immutable matrices.
#125 asked for this after the old exp_generic overwrote its input; exp_generic has since been removed, and callers still write exponential!(copy(A)), as this package's own tests do. The optional arguments go straight to exponential!, so every method and a preallocated cache work as before.
The new testset checks the default, ExpMethodGeneric, and ExpMethodHigham2005 with a cache against exp, plus an SMatrix, and that A is untouched; Core passes on 1.10 and QA, including the docstring and docs-rendering checks for the new export, on 1.12.
Fixes #125.
AI Disclosure: Claude assisted with this change.

@ChrisRackauckas

Copy link
Copy Markdown
Member

🤖 Automated comment from an AI agent running as @ChrisRackauckas — not written or reviewed by Chris.

Independent review (Devin CLI 3000.11.3, model fusion-claude-opus-5-5-high-sidekick-swe-2-medium): MERGE, risk low.

Full review

VERDICT: MERGE
RISK: low

PR: #296 (fixes #125)

The change is one line, exponential(A, args...) = exponential!(copy(A), args...) (src/exp.jl:66), plus a docstring, an export, a @docs entry and one testset. It is exactly what the maintainer approved in the linked issue. I reran the author's claims and they hold. My probes found no wrong results: every method returns exp(A) to rounding error and leaves A unchanged.

Blocking findings

None.

Non-blocking findings

  1. Integer matrices throw, but Base exp handles them (confirmed by running). exponential([1 2; 0 1]) throws InexactError: Int64(0.95), while exp([1 2; 0 1]) works. exponential! already fails this way, and exponential inherits it because copy keeps the eltype. A non-mutating API has no reason to keep that restriction. Copying into similar(A, float(eltype(A))) would remove it. Don't use float(A): it returns A itself, not a copy, when A is already a float matrix. This is optional; the issue only asked for the plain forwarder.

  2. Some wrapper types throw, all inherited from exponential! (confirmed by running):

    • exponential(Symmetric(...)) throws ArgumentError: Cannot set a non-diagonal index in a symmetric matrix.
    • exponential(Diagonal(...)) throws pointer_from_objref cannot be used on immutable objects.
    • Sparse input gives the existing "use expv" error, which is the intended behaviour.

    These are not regressions. They are just more visible now that a "safe" non-mutating name exists. A follow-up issue would be worthwhile.

  3. The new testset splits a helper from the testset that uses it (test/basictests.jl:182-191). It sits between the exp_generic(A) = exponential!(copy(A), ExpMethodGeneric()) helper and the "exp_generic" testset. Moving it above line 182 would fix that. Alternatively, the helper could become exponential(A, ExpMethodGeneric()), which is the forwarding the issue suggested. The PR body says callers still write exponential!(copy(A)), but the PR updates none of them.

  4. Test coverage is minimal but adequate for a forwarder (confirmed by running). The test uses one 3×3 matrix with small norm, so scaling-and-squaring is never exercised. I checked that the test can actually fail: a mutant without the copy (exponential(A, args...) = exponential!(A, args...)) fails four of the five assertions, including A == A0. My probe with a larger-norm 8×8 matrix (10*randn) passed for every method.

  5. New exported name exponential. It is generic enough to collide with another package's export. I grepped every package in ~/.julia/packages and found no other package that exports exponential. The only other definition is a function in Reactant's StableHLO dialect module (https://github.com/EnzymeAD/Reactant.jl), which no package exports. A new export is a minor-version feature. Project.toml is 1.37.0, already bumped by Bump version to 1.37.0 #297, and the repo bumps versions in separate PRs, so this PR shouldn't bump it.

  6. No rule violations found. No new dependencies, no non-public API use. The new export has a docstring and a docs entry. The PR has one concern and no narrating comments. The PR body has an "AI Disclosure" line.

What I ran

All runs are confirmed. The repo/ checkout was left untouched (git status is clean). Tests ran in a copy at scratch/repocopy.

  • Formatting and spelling: julia +1.12 --project=@runic -m Runic --check src/exp.jl src/ExponentialUtilities.jl test/basictests.jl was clean, and typos src ext test docs was clean.
  • Core tests on 1.10: GROUP=Core julia +1.10 --project -e 'using Pkg; Pkg.test()' gave 1143 pass, 1 broken, 0 fail. The broken test is pre-existing; the diff adds no @test_broken. Log: scratch/core_1.10.log.
  • Core tests on 1.12: GROUP=Core julia +1.12 ... gave 1144 pass, 1 broken, 0 fail. Log: scratch/core_1.12.log.
  • QA on 1.12: GROUP=QA julia +1.12 ... gave 28 pass, 2 broken (pre-existing), 0 fail. Log: scratch/qa_1.12.log. This matches the PR body's claims for Core on 1.10 and QA on 1.12.
  • Behaviour probes (scratch/probe.jl, Julia 1.11, dev'd checkout):
    • I called exponential with each method: the default (no method), ExpMethodHigham2005(), ExpMethodHigham2005(false), ExpMethodHigham2005Base, ExpMethodGeneric, ExpMethodNative, ExpMethodDiagonalization, and Higham2005 with an alloc_mem cache.
    • Inputs were the PR's 3×3 matrix and an 8×8 10*randn matrix.
    • Relative error against exp was at most 4e-14 (Diagonalization on 8×8); all others were ≤ 6.5e-15. In every case A was unchanged and the result was not aliased to A.
    • Complex, Float32 (eltype preserved), SMatrix, MMatrix (input unchanged), Adjoint, a view (parent unchanged), scalar and 1×1 inputs all worked.
    • A ForwardDiff.jacobian through exponential matched central finite differences to 7e-10.
    • hasdoc and isexported are both true, and there is a single method.
    • Int, Symmetric, Diagonal, sparse and non-square inputs throw, as listed above.
  • Mutation-detection mutant (scratch/mutant.jl): the PR's testset fails against a no-copy implementation.
  • Sibling PRs: I compared hunks with the open PRs Replace @generated Padé functions and gate the 1.12 workaround (#41) #305 and Fix ExpMethodHigham2005 for immutable arrays with non-BLAS eltypes #307. Their test/basictests.jl hunks are at lines 2, 221, 264 and 296, so neither overlaps line 180. Neither touches src/exp.jl. Issue exp_generic mutates #125 links no parent or tracking issue.

What I did not verify

  • Docs build (julia --project=docs docs/make.jl). The PR body says QA covers the docs-rendering check. I only confirmed that QA passes and the docstring exists.
  • GPU (exponential(::AbstractGPUArray) → copy → exponential!): no CUDA hardware here. I only reasoned from reading that it works.
  • Windows, 32-bit and the pre Julia channel.
  • Downstream packages beyond the local depot grep for name collisions.

🤖 Posted by an AI agent — harness: Devin CLI 3000.11.3 (review), Claude Code 2.1.285 (posting) · model: fusion-claude-opus-5-5-high-sidekick-swe-2-medium
Conversation: local Claude session 3c311569-dda6-4687-bc9f-65febb212c33 (fleet master)

@ChrisRackauckas

Copy link
Copy Markdown
Member

🤖 Automated comment from an AI agent running as @ChrisRackauckas — not written or reviewed by Chris.

PR: #296

Reviewed head: 462afa2
Raw SHA-256: 89dfa51e09c57d7ac85adbe1059387185adc71ed0077e7c127ec7814fbd69b88
Source SHA-256: eff46b073f63530753d025228a4ac82867a58429f7f30f7c3cba921443cf3dc1
Source: standard-review-v1 (harness-log)
Reviewer session: 01a1d14f-cf69-7022-93e7-d4f3a12ce23b
Slot: 1
Confidence: unknown
Own risk fixed before author rating/discussion: yes

Risk assessment

  • Risk: medium-low (ML7)
  • Verdict: MERGE
  • Reviewed head: 462afa2

Review report

VERDICT: MERGE
Round: 1
Head: 462afa2
Slot: 1
CI: green
Session: 01a1d14f-cf69-7022-93e7-d4f3a12ce23b

Risk

medium-low (ML7: additive exported API, documented and tested; existing calls and defaults unchanged)

Blocking findings

None.

  • Verdict: MERGE
  • Round: 1
  • Blocking priorities: none

Non-blocking findings

None introduced by this diff. No tests were deleted, skipped or weakened; the new accuracy assertions use the independent LinearAlgebra.exp implementation.

What I ran

AI reviewer: codex-cli 0.164.0, model gpt-6.1-sol; local session 01a1d14f-cf69-7022-93e7-d4f3a12ce23b (no session URL). Independent risk was recorded before runtime checks; no author rating or prior review was read.

All Julia checks used an isolated copy of the head in scratch/head, the supplied layered JULIA_DEPOT_PATH, the requested TMPDIR, and one Julia thread. The checkout remains unchanged.

  • julia --version: julia version 1.13.2. No tracked Julia manifest or interpreter pin exists; dependencies were resolved under the project's compat bounds in scratch environments.
  • uv run --locked python --version: Python 3.15.0; uv warned that --locked has no effect outside a project. This Julia repository has no Python project or uv.lock.
  • GROUP=Core julia --project -e 'using Pkg; Pkg.test()': 1144 passed, 1 broken, 2m28.7s; ExponentialUtilities tests passed. The broken assertion at test/basictests.jl:662 is present unchanged on origin/master.
  • julia --project=@runic -m Runic --check src/ExponentialUtilities.jl src/exp.jl test/basictests.jl, typos src ext test docs, and git diff --check origin/master...HEAD: exit 0.
  • julia --project=docs docs/review_make.jl: exit 0. This scratch driver contains the unchanged docs/make.jl build portion, omitting deployment. Doctests, cross-references and document checks passed. An initial archive-only attempt lacked Git metadata; supplying scratch Git metadata resolved that setup error.
  • julia --project=scratch/probe-env scratch/probes.jl: 210 analytic value/cache assertions and 11 storage/error assertions passed. Covered nilpotent and rotation references, real/complex Float32/Float64, BigFloat, method selection, cache reuse, views, static matrices and input preservation. One exploratory JLArray assertion errored with Scalar indexing is disallowed in the existing LU path.
  • Follow-up probes: 8/8 analytic derivative assertions and 4/4 custom-method/cache-forwarding assertions passed. A clean origin/master source snapshot reproduced the same JLArray exception through exponential!(copy(A)) (1/1 baseline assertion), establishing that it is inherited.

What I did not verify

QA, CUDA hardware, other Julia versions/platforms, documentation deployment, or CI logs. CI status is supplied by the review job. PR body, comments, reviews and author claims were not inspected.

AUTHOR_GRADE: {"task":"feature","score":5,"difficulty":1,"rounds":1,"author_harness":"unknown","author_model":"unknown","note":"Focused documented non-mutating wrapper; Core passes and independent analytic, derivative, aliasing and forwarding probes support correctness. The exploratory JLArray error also reproduces on origin/master."}

Head link: 462afa2


🤖 Posted by an AI agent — harness: codex-cli 0.164.0 · model: gpt-6.1-sol
Conversation: local session 01a1d14f-cf69-7022-93e7-d4f3a12ce23b (no session URL)

@ChrisRackauckas
ChrisRackauckas merged commit bdba446 into SciML:master Nov 15, 2026
38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

exp_generic mutates

2 participants