@@ -125,14 +125,6 @@ function JacobianCache(
125125 JacobianCache {typeof(_x1),typeof(_x2),typeof(_fx),typeof(fx1),typeof(colorvec),typeof(sparsity),fdtype,returntype} (_x1,_x2,_fx,fx1,colorvec,sparsity)
126126end
127127
128- function _make_Ji (:: SparseMatrixCSC , rows_index,cols_index,dx,colorvec,color_i,nrows,ncols)
129- pick_inds = [i for i in 1 : length (rows_index) if colorvec[cols_index[i]] == color_i]
130- rows_index_c = rows_index[pick_inds]
131- cols_index_c = cols_index[pick_inds]
132- Ji = sparse (rows_index_c, cols_index_c, dx[rows_index_c],nrows,ncols)
133- Ji
134- end
135-
136128function _make_Ji (:: AbstractArray , rows_index,cols_index,dx,colorvec,color_i,nrows,ncols)
137129 pick_inds = [i for i in 1 : length (rows_index) if colorvec[cols_index[i]] == color_i]
138130 rows_index_c = rows_index[pick_inds]
@@ -145,12 +137,6 @@ function _make_Ji(::AbstractArray, rows_index,cols_index,dx,colorvec,color_i,nro
145137 Ji
146138end
147139
148- function _make_Ji (:: SparseMatrixCSC , xtype, dx, color_i, nrows, ncols)
149- Ji = sparse (1 : nrows,fill (color_i,nrows),dx,nrows,ncols)
150- Ji
151- end
152-
153-
154140function _make_Ji (:: AbstractArray , xtype, dx, color_i, nrows, ncols)
155141 Ji = mapreduce (i -> i== color_i ? dx : zero (dx), hcat, 1 : ncols)
156142 size (Ji) != (nrows, ncols) ? reshape (Ji, (nrows, ncols)) : Ji # branch when size(dx) == (1,) => size(Ji) == (1,) while size(J) == (1,1)
@@ -445,11 +431,7 @@ function finite_difference_jacobian!(
445431 end
446432
447433 if sparsity != = nothing
448- if J isa AbstractSparseMatrix
449- fill! (nonzeros (J),false )
450- else
451- fill! (J,false )
452- end
434+ fill_matrix! (J, false )
453435 end
454436
455437 # fast path if J and sparsity are both AbstractSparseMatrix and have the same sparsity pattern
@@ -497,11 +479,7 @@ function finite_difference_jacobian!(
497479 J[rows_index, cols_index] .+= (colorvec[cols_index] .== color_i) .* vfx1[rows_index]
498480 += means requires a zero'd out start
499481 =#
500- if J isa AbstractSparseMatrix
501- @. void_setindex! ((J. nzval,), getindex ((J. nzval,), rows_index) + (getindex ((_color,), cols_index) == color_i) * getindex ((vfx1,), rows_index), rows_index)
502- else
503- @. void_setindex! ((J,), getindex ((J,), rows_index, cols_index) + (getindex ((_color,), cols_index) == color_i) * getindex ((vfx1,), rows_index), rows_index, cols_index)
504- end
482+ fast_jacobian_setindex! (J, rows_index, cols_index, _color, color_i, vfx1)
505483 end
506484 # Now return x1 back to its original value
507485 @. x1 = x1 - epsilon * (_color == color_i)
@@ -535,11 +513,7 @@ function finite_difference_jacobian!(
535513 _colorediteration! (J,sparsity,rows_index,cols_index,vfx1,colorvec,color_i,n)
536514 end
537515 else
538- if J isa AbstractSparseMatrix
539- @. void_setindex! ((J. nzval,), getindex ((J. nzval,), rows_index) + (getindex ((_color,), cols_index) == color_i) * getindex ((vfx1,), rows_index), rows_index)
540- else
541- @. void_setindex! ((J,), getindex ((J,), rows_index, cols_index) + (getindex ((_color,), cols_index) == color_i) * getindex ((vfx1,), rows_index), rows_index, cols_index)
542- end
516+ fast_jacobian_setindex! (J, rows_index, cols_index, _color, color_i, vfx1)
543517 end
544518 @. x1 = x1 - epsilon * (_color == color_i)
545519 @. x = x + epsilon * (_color == color_i)
@@ -565,11 +539,7 @@ function finite_difference_jacobian!(
565539 _colorediteration! (J,sparsity,rows_index,cols_index,vfx,colorvec,color_i,n)
566540 end
567541 else
568- if J isa AbstractSparseMatrix
569- @. void_setindex! ((J. nzval,), getindex ((J. nzval,), rows_index) + (getindex ((_color,), cols_index) == color_i) * getindex ((vfx,),rows_index), rows_index)
570- else
571- @. void_setindex! ((J,), getindex ((J,), rows_index, cols_index) + (getindex ((_color,), cols_index) == color_i) * getindex ((vfx,), rows_index), rows_index, cols_index)
572- end
542+ fast_jacobian_setindex! (J, rows_index, cols_index, _color, color_i, vfx)
573543 end
574544 @. x1 = x1 - im * epsilon * (_color == color_i)
575545 end
583553function resize! (cache:: JacobianCache , i:: Int )
584554 resize! (cache. x1, i)
585555 resize! (cache. fx, i)
586- cache. fx1 != nothing && resize! (cache. fx1, i)
556+ cache. fx1 != = nothing && resize! (cache. fx1, i)
587557 cache. colorvec = 1 : i
588558 nothing
589559end
560+
561+ @inline fill_matrix! (J, v) = fill! (J, v)
562+
563+ @inline function fast_jacobian_setindex! (J, rows_index, cols_index, _color, color_i, vfx)
564+ @. void_setindex! ((J,), getindex ((J,), rows_index, cols_index) + (getindex ((_color,), cols_index) == color_i) * getindex ((vfx,), rows_index), rows_index, cols_index)
565+ end
0 commit comments