Skip to content

ForwardDiff Overload Fixes #629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 32 commits into from
Jul 17, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
17b4364
don't set properties again
jClugstor Jul 2, 2025
0b7842a
make sure that when A, b, or u are accessed you get the Dual numbers
jClugstor Jul 2, 2025
b0d53ae
make sure cache u is updated
jClugstor Jul 2, 2025
e98a9b9
no infiltrate
jClugstor Jul 2, 2025
ad32c73
exclude nested duals
jClugstor Jul 3, 2025
16f5f0f
add Float32 support
jClugstor Jul 14, 2025
5dd9ce0
change to AbstractFloat
jClugstor Jul 14, 2025
43a0379
add recursive support for nested Duals
jClugstor Jul 14, 2025
f6db1ee
fix support for nested Duals
jClugstor Jul 14, 2025
c10a8bf
clean up
jClugstor Jul 14, 2025
7abb243
more clean up
jClugstor Jul 14, 2025
0717289
allow any number
jClugstor Jul 14, 2025
f829926
reuse list
jClugstor Jul 14, 2025
00431ad
add tests for nested duals
jClugstor Jul 14, 2025
6ac7bf1
no infiltrator
jClugstor Jul 14, 2025
8bae0f7
proper RAT indexing
jClugstor Jul 14, 2025
8e8b47e
all numbers
jClugstor Jul 14, 2025
308bc76
correct RAT index
jClugstor Jul 14, 2025
d9ed726
get rid of unecessary things
jClugstor Jul 15, 2025
fdc9774
get rid of log
jClugstor Jul 15, 2025
9c11cc2
add more tests
jClugstor Jul 16, 2025
96244fd
streamline
jClugstor Jul 16, 2025
762bfb1
make sure u is aliased
jClugstor Jul 16, 2025
248718f
add tests for sparse arrays and sparse solvers
jClugstor Jul 16, 2025
5497adc
make sure AbstractArrays of size 1 are accounted for
jClugstor Jul 16, 2025
e842eac
use AbstractVector, fix setproperty!
jClugstor Jul 16, 2025
1b1697a
add test for setting nested Duals
jClugstor Jul 16, 2025
2e003ee
fix test
jClugstor Jul 16, 2025
47d4d68
make sparse
jClugstor Jul 16, 2025
6323432
fix default solver
jClugstor Jul 16, 2025
b1ffa15
allow setting alg
jClugstor Jul 16, 2025
62127b8
Update ext/LinearSolveForwardDiffExt.jl
ChrisRackauckas Jul 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ext/LinearSolveForwardDiffExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ nodual_value(x::Dual{T, V, P}) where {T, V <: Dual, P} = x.value # Keep the inn
nodual_value(x::AbstractArray{<:Dual}) = map(nodual_value, x)


function partials_to_list(partial_matrix::Vector)
function partials_to_list(partial_matrix::AbstractArray{T, 1}) where {T}
p = eachindex(first(partial_matrix))
[[partial[i] for partial in partial_matrix] for i in p]
end
Expand Down
43 changes: 42 additions & 1 deletion test/forwarddiff_overloads.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using LinearSolve
using ForwardDiff
using Test
using SparseArrays

function h(p)
(A = [p[1] p[2]+1 p[2]^3;
Expand Down Expand Up @@ -48,6 +49,9 @@ new_A, new_b = h([ForwardDiff.Dual(5.0, 1.0, 0.0), ForwardDiff.Dual(5.0, 0.0, 1.
cache.A = new_A
cache.b = new_b

@test cache.A == new_A
@test cache.b == new_b

x_p = solve!(cache)
backslash_x_p = new_A \ new_b

Expand All @@ -61,7 +65,7 @@ cache = init(prob)

new_A, _ = h([ForwardDiff.Dual(5.0, 1.0, 0.0), ForwardDiff.Dual(5.0, 0.0, 1.0)])
cache.A = new_A
@test cache.A = new_A
@test cache.A == new_A

x_p = solve!(cache)
backslash_x_p = new_A \ b
Expand Down Expand Up @@ -139,3 +143,40 @@ end

@test ≈(ForwardDiff.hessian(slash_f_hes, [5.0]),
ForwardDiff.hessian(linprob_f_hes, [5.0]))


# Test aliasing

prob = LinearProblem(A, b)
cache = init(prob)

new_A, new_b = h([ForwardDiff.Dual(5.0, 1.0, 0.0), ForwardDiff.Dual(5.0, 0.0, 1.0)])
cache.A = new_A
cache.b = new_b

linu = [ForwardDiff.Dual(0.0, 0.0, 0.0), ForwardDiff.Dual(0.0, 0.0, 0.0),
ForwardDiff.Dual(0.0, 0.0, 0.0)]
cache.u = linu
x_p = solve!(cache)
backslash_x_p = new_A \ new_b

@test linu == cache.u


# Test Float Only solvers

A, b = h([ForwardDiff.Dual(5.0, 1.0, 0.0), ForwardDiff.Dual(5.0, 0.0, 1.0)])

prob = LinearProblem(sparse(A), sparse(b))
overload_x_p = solve(prob, KLUFactorization())
backslash_x_p = A \ b

@test ≈(overload_x_p, backslash_x_p, rtol = 1e-9)

A, b = h([ForwardDiff.Dual(5.0, 1.0, 0.0), ForwardDiff.Dual(5.0, 0.0, 1.0)])

prob = LinearProblem(A, b)
overload_x_p = solve(prob, UMFPACKFactorization())
backslash_x_p = A \ b

@test ≈(overload_x_p, backslash_x_p, rtol = 1e-9)
Loading