From 33cc83b83ad81ad07ee4c17a01ca8facf46933e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20M=C3=BCller-Widmann?= Date: Tue, 6 Oct 2026 23:57:54 +0200 Subject: [PATCH] Fix ExpMethodHigham2005 for immutable arrays with non-BLAS eltypes The lu! fallback of `ldiv_for_generated!` checked aliasing with `pointer_from_objref`, which throws for immutable array types such as `FixedSizeArray`, e.g. under ForwardDiff. The generated code always passes distinct cache slots, so copy unconditionally. Fixes #306 Co-Authored-By: Claude Opus 5.5 --- Project.toml | 4 +++- src/exp_noalloc.jl | 4 +--- test/basictests.jl | 17 ++++++++++++++++- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/Project.toml b/Project.toml index 8a5f1724..2f921bd5 100644 --- a/Project.toml +++ b/Project.toml @@ -27,6 +27,7 @@ Adapt = "4.3" Aqua = "0.8" ArrayInterface = "7.1" DoubleFloats = "1.1.14" +FixedSizeArrays = "1" ForwardDiff = "1" GPUArraysCore = "0.1, 0.2" GenericSchur = "0.5.3" @@ -48,6 +49,7 @@ julia = "1.10" [extras] Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" DoubleFloats = "497a8b3b-efae-58df-a0af-a86822472b78" +FixedSizeArrays = "3821ddf9-e5b5-40d5-8e25-6813ab96b5e2" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" JLArrays = "27aeb0d3-9eb9-45fb-866b-73c2ecf80fcb" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" @@ -57,4 +59,4 @@ StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["Aqua", "DoubleFloats", "ForwardDiff", "JLArrays", "Test", "SafeTestsets", "SciMLTesting", "StaticArrays", "Random"] +test = ["Aqua", "DoubleFloats", "FixedSizeArrays", "ForwardDiff", "JLArrays", "Test", "SafeTestsets", "SciMLTesting", "StaticArrays", "Random"] diff --git a/src/exp_noalloc.jl b/src/exp_noalloc.jl index 7d553fc3..d33322c5 100644 --- a/src/exp_noalloc.jl +++ b/src/exp_noalloc.jl @@ -84,9 +84,7 @@ end function ldiv_for_generated!(C, A, B, ::Nothing) # lu! fallback (GPU, BigFloat, ...) F = lu!(A) ldiv!(F, B) # Result stored in B - if (pointer_from_objref(C) != pointer_from_objref(B)) # Aliasing allowed - copyto!(C, B) - end + copyto!(C, B) return C end diff --git a/test/basictests.jl b/test/basictests.jl index ea622895..2ee47ba5 100644 --- a/test/basictests.jl +++ b/test/basictests.jl @@ -2,7 +2,7 @@ using Test, LinearAlgebra, Random, SparseArrays, ExponentialUtilities using ExponentialUtilities: getH, getV, exponential!, ExpMethodNative, ExpMethodDiagonalization, ExpMethodHigham2005, ExpMethodGeneric, ExpMethodHigham2005Base, alloc_mem -using ForwardDiff, StaticArrays, DoubleFloats +using ForwardDiff, StaticArrays, DoubleFloats, FixedSizeArrays using GenericSchur using JLArrays @@ -221,6 +221,21 @@ end end end +# Immutable array type; duals take the lu! fallback (#306) +@testset "ExpMethodHigham2005 on FixedSizeArrays" begin + Random.seed!(306) + A = 3 .* rand(4, 4) # needs squaring + E = exponential!(FixedSizeMatrix(A)) + @test E isa FixedSizeMatrix{Float64} + @test E ≈ exp(A) + + Jref = ForwardDiff.jacobian(exp_generic, A) + for method in (ExpMethodHigham2005(true), ExpMethodHigham2005(false), ExpMethodHigham2005Base()) + J = ForwardDiff.jacobian(x -> exponential!(FixedSizeMatrix(x), method), A) + @test J ≈ Jref + end +end + @testset "Krylov products with ForwardDiff duals" begin v = [0.3, 0.7, -0.2] A = [0.1 0.2 0.0; 0.3 0.4 0.1; 0.0 0.2 -0.3]