Skip to content

Fix \ for Dual #236

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 18 commits into from
Apr 11, 2025
16 changes: 16 additions & 0 deletions src/overloads/arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,22 @@ function LinearAlgebra.logabsdet(A::AbstractMatrix{D}) where {D<:Dual}
t1, t2 = LinearAlgebra.logabsdet(tracers)
return (D(p1, t1), D(p2, t2))
end
function LinearAlgebra.:\(A::AbstractMatrix{D}, B::AbstractVector) where {D<:Dual}
primals, tracers = split_dual_array(A)
p = primals \ B
t = tracers \ B
return D.(p, t)
end
function LinearAlgebra.:\(A::AbstractMatrix, B::AbstractVector{D}) where {D<:Dual}
return D.(A) \ B
end
function LinearAlgebra.:\(A::AbstractMatrix{D}, B::AbstractVector{D}) where {D<:Dual}
A_primals, A_tracers = split_dual_array(A)
B_primals, B_tracers = split_dual_array(B)
p = A_primals \ B_primals
t = A_tracers \ B_tracers
return D.(p, t)
end

#==============#
# SparseArrays #
Expand Down
16 changes: 16 additions & 0 deletions test/test_constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,19 @@ end
end
end
end

# https://github.com/adrhill/SparseConnectivityTracer.jl/issues/235
@testset "Left division on Dual with BigFloat" begin
@testset "$T" for T in union(GRADIENT_TRACERS, HESSIAN_TRACERS)
P = IndexSetGradientPattern{Int,BitSet}
T = GradientTracer{P}

p = P(BitSet(2))
t_full = T(p)

dualized_A = Dual.(rand(BigFloat, 2, 2), t_full)
dualized_B = Dual.(rand(BigFloat, 2), t_full)

@test_nowarn dualized_A \ dualized_B
end
end
Loading