Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix adjoint of GenericNonlinearExpr #3724

Merged
merged 5 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading