Skip to content

Commit 66bcf26

Browse files
use an internal vec
1 parent 85dacce commit 66bcf26

2 files changed

Lines changed: 23 additions & 22 deletions

File tree

src/FiniteDiff.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ using LinearAlgebra, SparseArrays, StaticArrays, ArrayInterface, Requires
55

66
import Base: resize!
77

8+
_vec(x) = vec(x)
9+
_vec(x::Number) = x
10+
811
include("iteration_utils.jl")
912
include("epsilons.jl")
1013
include("derivatives.jl")
1114
include("gradients.jl")
1215
include("jacobians.jl")
1316
include("hessians.jl")
1417

18+
19+
1520
end # module

src/jacobians.jl

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,6 @@ function _make_Ji(::AbstractArray, xtype, dx, color_i, nrows, ncols)
124124
size(Ji)!=(nrows, ncols) ? reshape(Ji,(nrows,ncols)) : Ji #branch when size(dx) == (1,) => size(Ji) == (1,) while size(J) == (1,1)
125125
end
126126

127-
function Base.vec(x::Number)
128-
x
129-
end
130-
131127
function finite_difference_jacobian(f, x::AbstractArray{<:Number},
132128
fdtype :: Type{T1}=Val{:forward},
133129
returntype :: Type{T2}=eltype(x),
@@ -165,16 +161,16 @@ function finite_difference_jacobian(
165161
x1, fx, fx1 = cache.x1, cache.fx, cache.fx1
166162

167163
if !(f_in isa Nothing)
168-
vecfx = vec(f_in)
164+
vecfx = _vec(f_in)
169165
elseif fdtype == Val{:forward}
170-
vecfx = vec(f(x))
166+
vecfx = _vec(f(x))
171167
elseif fdtype == Val{:complex} && returntype <: Real
172168
vecfx = real(fx)
173169
else
174-
vecfx = vec(fx)
170+
vecfx = _vec(fx)
175171
end
176-
vecx = vec(x)
177-
vecx1 = vec(x1)
172+
vecx = _vec(x)
173+
vecx1 = _vec(x1)
178174
J = jac_prototype isa Nothing ? (sparsity isa Nothing ? false.*vecfx.*vecx' : zeros(eltype(x),size(sparsity))) : zero(jac_prototype)
179175
nrows, ncols = size(J)
180176

@@ -191,15 +187,15 @@ function finite_difference_jacobian(
191187
epsilon = compute_epsilon(Val{:forward}, x_save, relstep, absstep, dir)
192188
_vecx1 = Base.setindex(vecx,x_save+epsilon,color_i)
193189
_x1 = reshape(_vecx1,size(x))
194-
vecfx1 = vec(f(_x1))
190+
vecfx1 = _vec(f(_x1))
195191
dx = (vecfx1-vecfx)/epsilon
196192
J = J + _make_Ji(J, eltype(x), dx, color_i, nrows, ncols)
197193
else
198194
tmp = norm(vecx .* (colorvec .== color_i))
199195
epsilon = compute_epsilon(Val{:forward}, sqrt(tmp), relstep, absstep, dir)
200196
_vecx = @. vecx + epsilon * (colorvec == color_i)
201197
_x = reshape(_vecx,size(x))
202-
vecfx1 = vec(f(_x))
198+
vecfx1 = _vec(f(_x))
203199
dx = (vecfx1-vecfx)/epsilon
204200
Ji = _make_Ji(J,rows_index,cols_index,dx,colorvec,color_i,nrows,ncols)
205201
J = J + Ji
@@ -215,8 +211,8 @@ function finite_difference_jacobian(
215211
_vecx = Base.setindex(vecx,x_save-epsilon,color_i)
216212
_x1 = reshape(_vecx1,size(x))
217213
_x = reshape(_vecx,size(x))
218-
vecfx1 = vec(f(_x1))
219-
vecfx = vec(f(_x))
214+
vecfx1 = _vec(f(_x1))
215+
vecfx = _vec(f(_x))
220216
dx = (vecfx1-vecfx)/(2epsilon)
221217
J = J + _make_Ji(J, eltype(x), dx, color_i, nrows, ncols)
222218
else
@@ -226,8 +222,8 @@ function finite_difference_jacobian(
226222
_vecx = @. vecx - epsilon * (colorvec == color_i)
227223
_x1 = reshape(_vecx1,size(x))
228224
_x = reshape(_vecx, size(x))
229-
vecfx1 = vec(f(_x1))
230-
vecfx = vec(f(_x))
225+
vecfx1 = _vec(f(_x1))
226+
vecfx = _vec(f(_x))
231227
dx = (vecfx1-vecfx)/(2epsilon)
232228
Ji = _make_Ji(J,rows_index,cols_index,dx,colorvec,color_i,nrows,ncols)
233229
J = J + Ji
@@ -240,13 +236,13 @@ function finite_difference_jacobian(
240236
x_save = vecx[color_i]
241237
_vecx = Base.setindex(complex.(vecx),x_save+im*epsilon,color_i)
242238
_x = reshape(_vecx,size(x))
243-
vecfx = vec(f(_x))
239+
vecfx = _vec(f(_x))
244240
dx = imag(vecfx)/epsilon
245241
J = J + _make_Ji(J, eltype(x), dx, color_i, nrows, ncols)
246242
else
247243
_vecx = @. vecx + im * epsilon * (colorvec == color_i)
248244
_x = reshape(_vecx,size(x))
249-
vecfx = vec(f(_x))
245+
vecfx = _vec(f(_x))
250246
dx = imag(vecfx)/epsilon
251247
Ji = _make_Ji(J,rows_index,cols_index,dx,colorvec,color_i,nrows,ncols)
252248
J = J + Ji
@@ -301,7 +297,7 @@ function finite_difference_jacobian!(
301297

302298
x1, fx, fx1 = cache.x1, cache.fx, cache.fx1
303299
copyto!(x1, x)
304-
vfx = vec(fx)
300+
vfx = _vec(fx)
305301

306302
rows_index = nothing
307303
cols_index = nothing
@@ -314,13 +310,13 @@ function finite_difference_jacobian!(
314310
end
315311

316312
if fdtype == Val{:forward}
317-
vfx1 = vec(fx1)
313+
vfx1 = _vec(fx1)
318314

319315
if f_in isa Nothing
320316
f(fx, x)
321-
vfx = vec(fx)
317+
vfx = _vec(fx)
322318
else
323-
vfx = vec(f_in)
319+
vfx = _vec(f_in)
324320
end
325321

326322
@inbounds for color_i 1:maximum(colorvec)
@@ -362,7 +358,7 @@ function finite_difference_jacobian!(
362358
end
363359
end #for ends here
364360
elseif fdtype == Val{:central}
365-
vfx1 = vec(fx1)
361+
vfx1 = _vec(fx1)
366362
@inbounds for color_i 1:maximum(colorvec)
367363
if sparsity isa Nothing
368364
x_save = ArrayInterface.allowed_getindex(x,color_i)

0 commit comments

Comments
 (0)