Closed DanielVandH closed 1 year ago
The issue, if you do want to allow for non-sparse patterns with no problem, probably comes from this part of jacobians.jl
# J is a sparse matrix, so decompress on the fly
@. vfx1 = (vfx1 - vfx) / epsilon
if ArrayInterfaceCore.fast_scalar_indexing(x1)
if sparseCSC_common_sparsity
_colorediteration!(J,vfx1,colorvec,color_i,n)
else
_colorediteration!(J,sparsity,rows_index,cols_index,vfx1,colorvec,color_i,n)
end
else
#=
J.nzval[rows_index] .+= (colorvec[cols_index] .== color_i) .* vfx1[rows_index]
or
J[rows_index, cols_index] .+= (colorvec[cols_index] .== color_i) .* vfx1[rows_index]
+= means requires a zero'd out start
=#
if J isa AbstractSparseMatrix
@. void_setindex!((J.nzval,), getindex((J.nzval,), rows_index) + (getindex((_color,), cols_index) == color_i) * getindex((vfx1,), rows_index), rows_index)
else
@. void_setindex!((J,), getindex((J,), rows_index, cols_index) + (getindex((_color,), cols_index) == color_i) * getindex((vfx1,), rows_index), rows_index, cols_index)
end
end
which says that J
is sparse, and then also checks that J
is an abstract sparse matrix anyway - maybe some of the ideas were confused at the time? The problematic part of this is _coloriteration
, which calls this method:
@inline function _colorediteration!(J,sparsity,rows_index,cols_index,vfx,colorvec,color_i,ncols)
@inbounds for i in 1:length(cols_index)
if colorvec[cols_index[i]] == color_i
J[rows_index[i],cols_index[i]] = vfx[rows_index[i]]
end
end
end
But rows_index
and cols_index
are both nothing
in this case (the sparsity
argument isn't even used actually - nor is it in the other _colorediteration!
for sparse matrices?). It seems like there just isn't intended to be any support for non-sparse matrices, despite in some places in the code looking like that was maybe intended initially.
Yeah it was kind of intended, but then we realized everything like Diagonal, BlockBandedMatrix, etc. all went the sparse route. Your fix is good.
If I provided a non-sparse sparsity pattern for the Jacobian, the warning is not so obvious that it's about not having a sparsity pattern:
It could be nice to throw an error in this case, e.g. defining something like (code below isn't tested)
(I think that's how
new
would be used? Haven't used it really.)Worth a pull request? Maybe the error message isn't really about the sparsity pattern at all since jacobians.jl actually has a case for
J
not being anAbstractSparseMatrix
, in which case the proposal above is not needed but this is still worth the issue I guess. I'm just assuming it should be sparse since the README claims "sparsity should be a sparse or structured matrix (Tridiagonal, Banded, etc. according to the ArrayInterfaceCore.jl specs)".