Skip to content

Commit

Permalink
Fix adjoint of GenericNonlinearExpr (#3724)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Apr 10, 2024
1 parent 05d4876 commit d573737
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,19 @@ struct GenericNonlinearExpr{V<:AbstractVariableRef} <: AbstractJuMPScalar
head::Symbol,
args::Vararg{Any},
) where {V<:AbstractVariableRef}
for arg in args
_throw_if_not_real(arg)
end
return new{V}(head, Any[a for a in args])
end

function GenericNonlinearExpr{V}(
head::Symbol,
args::Vector{Any},
) where {V<:AbstractVariableRef}
for arg in args
_throw_if_not_real(arg)
end
return new{V}(head, args)
end
end
Expand Down Expand Up @@ -291,6 +297,12 @@ function Base.one(::Type{GenericNonlinearExpr{V}}) where {V}
return GenericNonlinearExpr{V}(:+, 1.0)
end

Base.conj(x::GenericNonlinearExpr) = x
Base.real(x::GenericNonlinearExpr) = x
Base.imag(x::GenericNonlinearExpr) = zero(x)
Base.abs2(x::GenericNonlinearExpr) = x^2
Base.isreal(::GenericNonlinearExpr) = true

# Univariate operators

_is_real(::Any) = false
Expand Down
57 changes: 57 additions & 0 deletions test/test_nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1035,4 +1035,61 @@ function test_convert_float_nonlinear_expr()
return
end

function test_nlp_adjoint()
model = Model()
@variable(model, x)
y = sin(x)
@test y' === y
@test conj(y) === y
@test real(y) === y
@test isequal_canonical(imag(y), zero(y))
@test isequal_canonical(abs2(y), y^2)
@test isreal(y)
return
end

function test_nlp_matrix_adjoint()
model = Model()
@variable(model, x[1:2])
y = sin.(x)
@test isequal_canonical(
@expression(model, expr, y' * y),
NonlinearExpr(:+, Any[0.0, y[2]*y[2], y[1]*y[1]]),
)
return
end

function test_error_complex_literal_pow()
model = Model()
@variable(model, x in ComplexPlane())
@test_throws(
ErrorException(
"Cannot build `GenericNonlinearExpr` because a term is complex-" *
"valued: `($x)::$(typeof(x))`",
),
x^3,
)
return
end

function test_error_nonlinear_expr_complex_constructor()
model = Model()
@variable(model, x in ComplexPlane())
@test_throws(
ErrorException(
"Cannot build `GenericNonlinearExpr` because a term is complex-" *
"valued: `($x)::$(typeof(x))`",
),
NonlinearExpr(:^, Any[x, 3]),
)
@test_throws(
ErrorException(
"Cannot build `GenericNonlinearExpr` because a term is complex-" *
"valued: `($x)::$(typeof(x))`",
),
NonlinearExpr(:^, x, 3),
)
return
end

end # module
1 change: 0 additions & 1 deletion test/test_operator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,6 @@ function test_complex_pow()
@test y^0 == (1.0 + 0im)
@test y^1 == y
@test y^2 == y * y
@test isequal_canonical(y^3, NonlinearExpr(:^, Any[y, 3]))
return
end

Expand Down

0 comments on commit d573737

Please sign in to comment.