Skip to content
This repository was archived by the owner on Aug 22, 2025. It is now read-only.

Commit 7114c94

Browse files
Merge pull request #74 from huanglangwen/oop
Fix issue of inconsistent size
2 parents d48ccbc + c6c5e08 commit 7114c94

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/differentiation/compute_jacobian_ad.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ function forwarddiff_color_jacobian(f,x::AbstractArray{<:Number},jac_cache::Forw
120120
for j in 1:chunksize
121121
col_index = (i-1)*chunksize + j
122122
(col_index > ncols) && return J
123-
J = J + mapreduce(i -> i==col_index ? partials.(vec(fx), j) : zeros(nrows), hcat, 1:ncols)
123+
Ji = mapreduce(i -> i==col_index ? partials.(vec(fx), j) : zeros(nrows), hcat, 1:ncols)
124+
J = J + (size(Ji)!=size(J) ? reshape(Ji,size(J)) : Ji) #branch when size(dx) == (1,) => size(Ji) == (1,) while size(J) == (1,1)
124125
end
125126
end
126127
end

test/test_ad.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,10 @@ J = forwarddiff_color_jacobian(oopf,x)
218218
@test J Matrix(I,4,4)
219219
J = zero(J)
220220
forwarddiff_color_jacobian!(J,iipf,x,dx=similar(x))
221-
@test J Matrix(I,4,4)
221+
@test J Matrix(I,4,4)
222+
223+
#1x1 SVector test
224+
x = SVector{1}([1.])
225+
f(x) = x
226+
J = forwarddiff_color_jacobian(f,x)
227+
@test J SMatrix{1,1}([1.])

0 commit comments

Comments
 (0)