Skip to content

Commit bd18fe1

Browse files
committed
[Nonlinear] fix _UnsafeVectorView with [email protected]
1 parent 981ddb5 commit bd18fe1

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/Nonlinear/ReverseAD/utils.jl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,25 @@ struct _UnsafeVectorView{T} <: DenseVector{T}
4242
ptr::Ptr{T}
4343
end
4444

45-
Base.getindex(x::_UnsafeVectorView, i) = unsafe_load(x.ptr, i + x.offset)
45+
function Base.getindex(x::_UnsafeVectorView, i::Integer)
46+
return unsafe_load(x.ptr, i + x.offset)
47+
end
48+
49+
Base.getindex(x::_UnsafeVectorView, i::CartesianIndex{1}) = getindex(x, i[1])
4650

47-
function Base.setindex!(x::_UnsafeVectorView, value, i)
51+
function Base.setindex!(x::_UnsafeVectorView{T}, value::T, i::Integer) where {T}
4852
unsafe_store!(x.ptr, value, i + x.offset)
4953
return value
5054
end
5155

56+
function Base.setindex!(
57+
x::_UnsafeVectorView{T},
58+
value::T,
59+
i::CartesianIndex{1},
60+
) where {T}
61+
return setindex!(x, value, i[1])
62+
end
63+
5264
Base.length(v::_UnsafeVectorView) = v.len
5365

5466
Base.size(v::_UnsafeVectorView) = (v.len,)

test/Nonlinear/ReverseAD.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,33 @@ function test_toposort_subexpressions()
13041304
return
13051305
end
13061306

1307+
function test_eval_user_defined_operator_ForwardDiff_gradient!()
1308+
model = MOI.Nonlinear.Model()
1309+
x = MOI.VariableIndex.(1:4)
1310+
p = MOI.Nonlinear.add_parameter(model, 2.0)
1311+
ex = MOI.Nonlinear.add_expression(model, :($p * $(x[1])))
1312+
ψ(x) = sin(x)
1313+
t(x, y) = x + 3y
1314+
MOI.Nonlinear.register_operator(model, , 1, ψ)
1315+
MOI.Nonlinear.register_operator(model, :t, 2, t)
1316+
MOI.Nonlinear.add_constraint(
1317+
model,
1318+
:($ex^3 + sin($(x[2])) / ψ($(x[2])) + t($(x[3]), $(x[4]))),
1319+
MOI.LessThan(0.0),
1320+
)
1321+
d = MOI.Nonlinear.Evaluator(model, MOI.Nonlinear.SparseReverseMode(), x)
1322+
MOI.initialize(d, [:Jac])
1323+
X = [1.1, 1.2, 1.3, 1.4]
1324+
g = [NaN]
1325+
MOI.eval_constraint(d, g, X)
1326+
@test only(g) 17.148
1327+
@test MOI.jacobian_structure(d) == [(1, 1), (1, 2), (1, 3), (1, 4)]
1328+
J = [NaN, NaN, NaN, NaN]
1329+
MOI.eval_constraint_jacobian(d, J, X)
1330+
@test J [2.0^3 * 3.0 * 1.1^2, 0.0, 1.0, 3.0]
1331+
return
1332+
end
1333+
13071334
end # module
13081335

13091336
TestReverseAD.runtests()

0 commit comments

Comments
 (0)