Skip to content
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

[documentation] Sensitivity analysis #839

Open
amontoison opened this issue Dec 6, 2023 · 0 comments
Open

[documentation] Sensitivity analysis #839

amontoison opened this issue Dec 6, 2023 · 0 comments
Assignees

Comments

@amontoison
Copy link
Member

I should add an example in the documentation to explain how we can do sensitivity analysis with Krylov.jl.

using ChainRulesCore
using Krylov
using Test
using LinearAlgebra
using SparseArrays
using ForwardDiff
import ForwardDiff: Dual, partials, value

include("test/test_utils.jl")

A, b = sparse_laplacian(FC=Float64)

function Krylov.cg(_A::SparseMatrixCSC{Dual{T, V, N}, Int64}, _b::Vector{Dual{T, V, N}}; options...) where {T, V, N}
  A = SparseMatrixCSC(_A.m, _A.n, _A.colptr, _A.rowval, value.(_A.nzval))
  dA = SparseMatrixCSC(_A.m, _A.n, _A.colptr, _A.rowval, partials.(_A.nzval, 1))
  b = value.(_b)
  db = partials.(_b,1)
  x, stats = cg(A,b)
  nb = db - dA*x
  dx, dstats = cg(dA,nb)
  return (Dual.(x, dx), stats)
end

x, stats = cg(A,b)


dA = ForwardDiff.Dual.(A, A)
db = ForwardDiff.Dual.(b, b)

It solves the normal value system Ax = b and the tangent system
A*dx + dA*x= db (or A*dx = b - dA*x).

@amontoison amontoison self-assigned this Dec 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant