Skip to content

Commit f857eb2

Browse files
avoid accidental dense sparse matrices will fill!
JuliaLang/julia#17670
1 parent d6d21fd commit f857eb2

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/jacobians.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,12 +345,16 @@ function finite_difference_jacobian!(
345345
end
346346

347347
if sparsity !== nothing
348-
fill!(J,false)
348+
if J isa SparseMatrixCSC
349+
fill!(nonzeros(J),false)
350+
else
351+
fill!(J,false)
352+
end
349353
end
350354

351355
# fast path if J and sparsity are both SparseMatrixCSC and have the same sparsity pattern
352356
sparseCSC_common_sparsity = _use_sparseCSC_common_sparsity(J, sparsity)
353-
357+
354358
if fdtype == Val(:forward)
355359
vfx1 = _vec(fx1)
356360

@@ -424,7 +428,7 @@ function finite_difference_jacobian!(
424428
f(fx1, x1)
425429
f(fx, x)
426430
@. vfx1 = (vfx1 - vfx) / 2epsilon
427-
if ArrayInterface.fast_scalar_indexing(x1)
431+
if ArrayInterface.fast_scalar_indexing(x1)
428432
if sparseCSC_common_sparsity
429433
_colorediteration!(J,vfx1,colorvec,color_i,n)
430434
else
@@ -454,7 +458,7 @@ function finite_difference_jacobian!(
454458
@. x1 = x1 + im * epsilon * (_color == color_i)
455459
f(fx,x1)
456460
@. vfx = imag(vfx) / epsilon
457-
if ArrayInterface.fast_scalar_indexing(x1)
461+
if ArrayInterface.fast_scalar_indexing(x1)
458462
if sparseCSC_common_sparsity
459463
_colorediteration!(J,vfx,colorvec,color_i,n)
460464
else

0 commit comments

Comments
 (0)