diff --git a/ext/SparseDiffToolsPolyesterExt.jl b/ext/SparseDiffToolsPolyesterExt.jl index 8271ae3f..8708646d 100644 --- a/ext/SparseDiffToolsPolyesterExt.jl +++ b/ext/SparseDiffToolsPolyesterExt.jl @@ -34,8 +34,9 @@ function polyesterforwarddiff_color_jacobian(J::AbstractMatrix{<:Number}, f::F, rows_index = [rows_index[i] for i in 1:length(rows_index)] cols_index = [cols_index[i] for i in 1:length(cols_index)] else - rows_index = 1:nrows - cols_index = 1:ncols + cartind = vec(CartesianIndices(x)) + rows_index = Base.Iterators.map(first ∘ Tuple, cartind) + cols_index = Base.Iterators.map(last ∘ Tuple, cartind) end if J isa AbstractSparseMatrix diff --git a/src/differentiation/compute_jacobian_ad.jl b/src/differentiation/compute_jacobian_ad.jl index bfaad2c5..20eecc6e 100644 --- a/src/differentiation/compute_jacobian_ad.jl +++ b/src/differentiation/compute_jacobian_ad.jl @@ -348,8 +348,9 @@ function forwarddiff_color_jacobian!(J::AbstractMatrix{<:Number}, if FiniteDiff._use_findstructralnz(sparsity) rows_index, cols_index = ArrayInterface.findstructralnz(sparsity) else - rows_index = 1:size(J, 1) - cols_index = 1:size(J, 2) + cartind = vec(CartesianIndices(x)) + rows_index = Base.Iterators.map(first ∘ Tuple, cartind) + cols_index = Base.Iterators.map(last ∘ Tuple, cartind) end # fast path if J and sparsity are both AbstractSparseMatrix and have the same sparsity pattern