-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module LinearSolveForwardDiff | ||
|
||
using LinearSolve | ||
isdefined(Base, :get_extension) ? | ||
(import ForwardDiff; using ForwardDiff: Dual) : | ||
(import ..ForwardDiff; using ..ForwardDiff: Dual) | ||
|
||
function LinearSolve.solve!( | ||
cache::LinearSolve.LinearCache{A_,B}, | ||
alg::LinearSolve.AbstractFactorization; | ||
kwargs... | ||
) where {T, V, P, A_<:AbstractArray{<:Real}, B<:AbstractArray{<:Dual{T,V,P}}} | ||
@info "using solve! from LinearSolveForwardDiff.jl" | ||
dA = eltype(cache.A) <: Dual ? ForwardDiff.partials.(cache.A) : zero(cache.A) | ||
db = eltype(cache.b) <: Dual ? ForwardDiff.partials.(cache.b) : zero(cache.b) | ||
@show typeof(cache.A) | ||
@show typeof(cache.b) | ||
@show typeof(cache.u) | ||
A = eltype(cache.A) <: Dual ? ForwardDiff.value.(cache.A) : cache.A | ||
b = eltype(cache.b) <: Dual ? ForwardDiff.value.(cache.b) : cache.b | ||
u = eltype(cache.u) <: Dual ? ForwardDiff.value.(cache.u) : cache.u | ||
@show typeof(A), size(A) | ||
@show typeof(b), size(b) | ||
@show typeof(u), size(u) | ||
cache2 = remake(cache; A, b, u) | ||
res = LinearSolve.solve!(cache2, alg, kwargs...) | ||
dcache = remake(cache2; b = db - dA * res.u) | ||
dres = LinearSolve.solve!(dcache, alg, kwargs...) | ||
LinearSolve.SciMLBase.build_linear_solution(alg, Dual{T,V,P}.(res.u, dres.u), nothing, cache) | ||
end | ||
|
||
end # module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using Test | ||
using ForwardDiff | ||
using LinearSolve | ||
using FiniteDiff | ||
|
||
n = 4 | ||
A = rand(n, n); | ||
dA = zeros(n, n); | ||
b1 = rand(n); | ||
for alg in ( | ||
LUFactorization(), | ||
# RFLUFactorization(), | ||
# KrylovJL_GMRES(), | ||
) | ||
alg_str = string(alg) | ||
@show alg_str | ||
function fb(b) | ||
prob = LinearProblem(A, b) | ||
|
||
sol1 = solve(prob, alg) | ||
|
||
sum(sol1.u) | ||
end | ||
fb(b1) | ||
|
||
fid_jac = FiniteDiff.finite_difference_jacobian(fb, b1) |> vec | ||
@show fid_jac | ||
|
||
fod_jac = ForwardDiff.gradient(fb, b1) |> vec | ||
@show fod_jac | ||
|
||
@test fod_jac ≈ fid_jac rtol=1e-6 | ||
|
||
function fA(A) | ||
prob = LinearProblem(A, b1) | ||
|
||
sol1 = solve(prob, alg) | ||
|
||
sum(sol1.u) | ||
end | ||
fA(A) | ||
|
||
fid_jac = FiniteDiff.finite_difference_jacobian(fA, A) |> vec | ||
@show fid_jac | ||
|
||
# @test_throws MethodError fod_jac = ForwardDiff.gradient(fA, A) |> vec | ||
fod_jac = ForwardDiff.gradient(fA, A) |> vec | ||
# @show fod_jac | ||
|
||
# @test fod_jac ≈ fid_jac rtol=1e-6 | ||
end |