Closed
Description
I had to cook a MWE from a finite volume code.
Why isn't it giving the same result?
function foo!(out,x)
N = length(x)
z = zero(eltype(x))
for i in 1:N
yp = (i<N) ? x[i+1] : z
ym = (i>1) ? x[i-1] : z
d = yp - ym
if d<0
d = 2yp - ym + 0.1
end
out[i] = max(yp - ym, 0) + d
end
out
end
g0 = rand(10)
n = length(g0)
L = ForwardDiff.jacobian( x->foo!(similar(x),x), g0)
L1 = spdiagm(1=>ones(n-1), -1=>ones(n-1))
colors = matrix_colors(L1)
Ld = forwarddiff_color_jacobian!(L1, foo!, zeros(n); colorvec = colors)
norm(L-Ld,Inf)
julia> L
10×10 Matrix{Float64}:
0.0 2.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
-1.0 0.0 2.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 -1.0 0.0 2.0 0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 -1.0 0.0 2.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 -2.0 0.0 2.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 -1.0 0.0 2.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 -1.0 0.0 2.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 -1.0 0.0 2.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 -2.0 0.0 2.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 -1.0 0.0
julia> Ld
10×10 SparseMatrixCSC{Float64, Int64} with 18 stored entries:
⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
-1.0 ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ -1.0 ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ -1.0 ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ -1.0 ⋅ 1.0 ⋅ ⋅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