JuliaDiff / JuliaDiff/ForwardDiff.jl

Sparse backslash

Open
#363 11 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
1k
Forks
160
PR merge metrics
No merged PRs in 30d

Description

I get a StackOverflowError if I try to differentiate some code with a sparse matrix dependent on the variable and I use backslash with it. MWE:

using SparseArrays, ForwardDiff, LinearAlgebra
A = sparse([1.0 1.0 ; 0.0 1.0])
f(x) = sparse(A + x * x') \ ones(2)
x = ones(2)
f(x)
ForwardDiff.jacobian(f, x)

Compared to the "full" version, which works:

f_full(x) = (A + x * x') \ ones(2)
x = ones(2)
f_full(x)
ForwardDiff.jacobian(f_full, x)

I feel like this could be solved by adding a rule for underlying machinery of dual numbers for backslash, something akin to

function Base.:\(M::Dual{T}, x)
    MRf = factorize(realpart.(M))
    sol = MRf \ x
    sol -= Dual(0.0, 1.0) * (MRf \ (dualpart.(M) * sol))
    return sol
end

(That's what I do when I use DualNumbers.jl.) This should work and allow to only use LinearAlgebra with real-valued types because

But I am not sure how to suggest changes directly in ForwardDiff.jl. Does that make sense?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided MWE at the ForwardDiff.jacobian entry point and compare the sparse \ path with the working full-matrix path. Reproduce the StackOverflowError, then verify that differentiation of the sparse solve completes and returns a Jacobian without changing the full-matrix behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.