Skip to content
Closed
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
24 changes: 16 additions & 8 deletions src/nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -330,14 +330,16 @@ Base.isreal(::GenericNonlinearExpr) = true

# Univariate operators

_is_real(::Any) = false
_is_real(::Real) = true
_is_real(::AbstractVariableRef) = true
_is_real(::GenericAffExpr{<:Real}) = true
_is_real(::GenericQuadExpr{<:Real}) = true
_is_real(::GenericNonlinearExpr) = true
_is_real(::NonlinearExpression) = true
_is_real(::NonlinearParameter) = true
_is_real(::Type) = false
_is_real(::Type{<:Real}) = true
_is_real(::Type{<:AbstractVariableRef}) = true
_is_real(::Type{<:GenericAffExpr{<:Real}}) = true
_is_real(::Type{<:GenericQuadExpr{<:Real}}) = true
_is_real(::Type{<:GenericNonlinearExpr}) = true
_is_real(::Type{<:NonlinearExpression}) = true
_is_real(::Type{<:NonlinearParameter}) = true
_is_real(::Type{<:AbstractArray{T}}) where {T} = _is_real(T)
_is_real(x) = _is_real(typeof(x))

function _throw_if_not_real(x)
if !_is_real(x)
Expand Down Expand Up @@ -575,6 +577,8 @@ function moi_function(f::GenericNonlinearExpr{V}) where {V}
for i in length(f.args):-1:1
if f.args[i] isa GenericNonlinearExpr{V}
push!(stack, (ret, i, f.args[i]))
elseif f.args[i] isa AbstractArray
ret.args[i] = moi_function.(f.args[i])
else
ret.args[i] = moi_function(f.args[i])
end
Expand All @@ -586,6 +590,8 @@ function moi_function(f::GenericNonlinearExpr{V}) where {V}
for j in length(arg.args):-1:1
if arg.args[j] isa GenericNonlinearExpr{V}
push!(stack, (child, j, arg.args[j]))
elseif arg.args[j] isa AbstractArray
child.args[j] = moi_function.(arg.args[j])
else
child.args[j] = moi_function(arg.args[j])
end
Expand All @@ -611,6 +617,8 @@ function jump_function(model::GenericModel, f::MOI.ScalarNonlinearFunction)
for child in reverse(arg.args)
push!(stack, (new_ret, child))
end
elseif arg isa AbstractArray
push!(parent.args, jump_function.(model, arg))
else
push!(parent.args, jump_function(model, arg))
end
Expand Down
11 changes: 11 additions & 0 deletions test/test_nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
module TestNLPExpr

using JuMP
using LinearAlgebra
using Test

import LinearAlgebra
Expand Down Expand Up @@ -1232,4 +1233,14 @@ function test_extension_euler_to_exp(
return
end

function test_array()
model = Model()
@variable(model, x)
op_norm = NonlinearOperator(:det, det)
@objective(model, Min, op_norm([x]))
f = MOI.get(model, MOI.ObjectiveFunction{MOI.ScalarNonlinearFunction}())
@test f.head == :norm
@test f.args == [[index(x)]]
end

end # module
Loading