Skip to content

Add a way to forward diff a function that uses linsolve inside #646

@yolhan83

Description

@yolhan83

Hello, I think there should be a workaround for this mwe

using DifferentiationInterface,LinearSolve
import ForwardDiff

function f!(c,b,A,linsolve)
    A[1,1] = b[1]
    linsolve.A = A
    linsolve.b = b
    c .= solve!(linsolve).u
    return c
end

A = rand(3,3)
b = rand(3)
c = rand(3)
prob = LinearProblem(A, b)
linsolve = init(prob)
jacobian(f!,c,AutoForwardDiff(),b,Cache(A),ConstantOrCache(linsolve))

the reason is doesn't work is pretty clear linsolve.A wants a Matrix{Float64} and we're giving it a Dual, there may be a way using PrealocationTools.jl but I'm not sure how to tell linsolve to use a DiffCache or a Lazy one.

related: https://discourse.julialang.org/t/differentiationinterface-forwarddiff-unable-to-diff-ldiv-any-other-options-to-solve-linear-system-w-o-allocations-and-differentiate/131076/30
Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions