Skip to content

Commit

Permalink
Add direct SArray dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
avik-pal committed Dec 6, 2023
1 parent 55513d4 commit 7a8170f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
11 changes: 0 additions & 11 deletions src/highlevel/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,3 @@ init_jacobian(J::SparseMatrixCSC, ::Type{T}, fx, x; kwargs...) where {T} = T.(J)

__maybe_copy_x(_, x) = x
__maybe_copy_x(_, ::Nothing) = nothing

# Create a mutable version of the input array
function __make_mutable(x)
if ArrayInterface.can_setindex(x)
return x
else
y = similar(x)
copyto!(y, x)
return y
end
end
6 changes: 4 additions & 2 deletions src/highlevel/finite_diff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ __getfield(c::FiniteDiffJacobianCache, ::Val{:jac_prototype}) = c.jac_prototype

function sparse_jacobian_cache(fd::Union{AutoSparseFiniteDiff, AutoFiniteDiff},
sd::AbstractMaybeSparsityDetection, f::F, x; fx = nothing) where {F}
x = __make_mutable(x) # FiniteDiff is bad at handling immutables
coloring_result = sd(fd, f, x)
fx = fx === nothing ? similar(f(x)) : fx
if coloring_result isa NoMatrixColoring
Expand All @@ -26,7 +25,6 @@ end

function sparse_jacobian_cache(fd::Union{AutoSparseFiniteDiff, AutoFiniteDiff},
sd::AbstractMaybeSparsityDetection, f!::F, fx, x) where {F}
x = __make_mutable(x) # FiniteDiff is bad at handling immutables
coloring_result = sd(fd, f!, fx, x)
if coloring_result isa NoMatrixColoring
cache = FiniteDiff.JacobianCache(x, fx)
Expand All @@ -50,3 +48,7 @@ function sparse_jacobian!(J::AbstractMatrix, _, cache::FiniteDiffJacobianCache,
FiniteDiff.finite_difference_jacobian!(J, f!, x, cache.cache)
return J
end

function sparse_jacobian_static_array(_, cache::FiniteDiffJacobianCache, f, x::SArray)
return FiniteDiff.finite_difference_jacobian(f, x, cache.cache)
end

0 comments on commit 7a8170f

Please sign in to comment.