121121
122122function _make_Ji (:: AbstractArray , xtype, dx, color_i, nrows, ncols)
123123 Ji = mapreduce (i -> i== color_i ? dx : zero (dx), hcat, 1 : ncols)
124- size (Ji)!= (nrows, ncols) ? reshape (Ji,(nrows,ncols)) : Ji # branch when size(dx) == (1,) => size(Ji) == (1,) while size(J) == (1,1)
124+ size (Ji) != (nrows, ncols) ? reshape (Ji, (nrows, ncols)) : Ji # branch when size(dx) == (1,) => size(Ji) == (1,) while size(J) == (1,1)
125125end
126126
127127function finite_difference_jacobian (f, x,
@@ -186,17 +186,15 @@ function finite_difference_jacobian(
186186 x_save = ArrayInterface. allowed_getindex (vecx, i)
187187 epsilon = compute_epsilon (Val{:forward }, x_save, relstep, absstep, dir)
188188 _vecx1 = Base. setindex (vecx, x_save+ epsilon, i)
189- _x1 = reshape (_vecx1,size (x))
189+ _x1 = reshape (_vecx1, axes (x))
190190 vecfx1 = _vec (f (_x1))
191191 dx = (vecfx1- vecfx) / epsilon
192192 return dx
193193 end
194194
195195 if jac_prototype isa Nothing && sparsity isa Nothing
196196 J = mapreduce (calculate_Ji_forward, hcat, 1 : maximum (colorvec))
197- if maximum (colorvec) == 1
198- J = reshape (J, 1 , 1 )
199- end
197+ J = _mat (J)
200198 else
201199 @inbounds for color_i ∈ 1 : maximum (colorvec)
202200 if sparsity isa Nothing
@@ -206,7 +204,7 @@ function finite_difference_jacobian(
206204 tmp = norm (vecx .* (colorvec .== color_i))
207205 epsilon = compute_epsilon (Val{:forward }, sqrt (tmp), relstep, absstep, dir)
208206 _vecx = @. vecx + epsilon * (colorvec == color_i)
209- _x = reshape (_vecx,size (x))
207+ _x = reshape (_vecx, axes (x))
210208 vecfx1 = _vec (f (_x))
211209 dx = (vecfx1- vecfx)/ epsilon
212210 Ji = _make_Ji (J,rows_index,cols_index,dx,colorvec,color_i,nrows,ncols)
@@ -222,8 +220,8 @@ function finite_difference_jacobian(
222220 epsilon = compute_epsilon (Val{:forward }, x1_save, relstep, absstep, dir)
223221 _vecx1 = Base. setindex (vecx1,x1_save+ epsilon,i)
224222 _vecx = Base. setindex (vecx,x_save- epsilon,i)
225- _x1 = reshape (_vecx1,size (x))
226- _x = reshape (_vecx,size (x))
223+ _x1 = reshape (_vecx1, axes (x))
224+ _x = reshape (_vecx, axes (x))
227225 vecfx1 = _vec (f (_x1))
228226 vecfx = _vec (f (_x))
229227 dx = (vecfx1- vecfx)/ (2 epsilon)
@@ -232,9 +230,7 @@ function finite_difference_jacobian(
232230
233231 if jac_prototype isa Nothing && sparsity isa Nothing
234232 J = mapreduce (calculate_Ji_central, hcat, 1 : maximum (colorvec))
235- if maximum (colorvec) == 1
236- J = reshape (J, 1 , 1 )
237- end
233+ J = _mat (J)
238234 else
239235 @inbounds for color_i ∈ 1 : maximum (colorvec)
240236 if sparsity isa Nothing
@@ -245,8 +241,8 @@ function finite_difference_jacobian(
245241 epsilon = compute_epsilon (Val{:forward }, sqrt (tmp), relstep, absstep, dir)
246242 _vecx1 = @. vecx1 + epsilon * (colorvec == color_i)
247243 _vecx = @. vecx - epsilon * (colorvec == color_i)
248- _x1 = reshape (_vecx1,size (x))
249- _x = reshape (_vecx, size (x))
244+ _x1 = reshape (_vecx1, axes (x))
245+ _x = reshape (_vecx, axes (x))
250246 vecfx1 = _vec (f (_x1))
251247 vecfx = _vec (f (_x))
252248 dx = (vecfx1- vecfx)/ (2 epsilon)
@@ -257,29 +253,27 @@ function finite_difference_jacobian(
257253 end
258254 elseif fdtype == Val{:complex } && returntype <: Real
259255 epsilon = eps (eltype (x))
260-
256+
261257 function calculate_Ji_complex (i)
262258 x_save = ArrayInterface. allowed_getindex (vecx,i)
263259 _vecx = Base. setindex (complex .(vecx),x_save+ im* epsilon,i)
264- _x = reshape (_vecx,size (x))
260+ _x = reshape (_vecx, axes (x))
265261 vecfx = _vec (f (_x))
266262 dx = imag (vecfx)/ epsilon
267263 return dx
268264 end
269-
265+
270266 if jac_prototype isa Nothing && sparsity isa Nothing
271267 J = mapreduce (calculate_Ji_complex, hcat, 1 : maximum (colorvec))
272- if maximum (colorvec) == 1
273- J = reshape (J, 1 , 1 )
274- end
268+ J = _mat (J)
275269 else
276270 @inbounds for color_i ∈ 1 : maximum (colorvec)
277271 if sparsity isa Nothing
278272 dx = calculate_Ji_complex (color_i)
279273 J = J + _make_Ji (J, eltype (x), dx, color_i, nrows, ncols)
280274 else
281275 _vecx = @. vecx + im * epsilon * (colorvec == color_i)
282- _x = reshape (_vecx,size (x))
276+ _x = reshape (_vecx, axes (x))
283277 vecfx = _vec (f (_x))
284278 dx = imag (vecfx)/ epsilon
285279 Ji = _make_Ji (J,rows_index,cols_index,dx,colorvec,color_i,nrows,ncols)
@@ -332,7 +326,7 @@ function finite_difference_jacobian!(
332326 dir = true ) where {T1,T2,T3,cType,sType,fdtype,returntype}
333327
334328 m, n = size (J)
335- _color = reshape (colorvec,size (x)... )
329+ _color = reshape (colorvec, axes (x)... )
336330
337331 x1, fx, fx1 = cache. x1, cache. fx, cache. fx1
338332 copyto! (x1, x)
0 commit comments