jump-dev / JuMP.jl

Modeling language for Mathematical Optimization (linear, mixed-integer, conic, semidefinite, nonlinear)
http://jump.dev/JuMP.jl/
Other
2.17k stars 390 forks source link

Y' when Y is of type ::Matrix{NonlinearExpr} #3723

Closed basberr closed 2 months ago

basberr commented 2 months ago

Hi,

I encountered an issue which might be a bug (or not).


using JuMP

model = Model()

@variables(model, begin
    x[1:2]
    X[1:2, 1:2]
end)

@expression(model, Y, [cos(x[1]) 0 ; 0 x[2]])

@expression(model, XY, X*Y') # this returns an error
# @expression(model, YX, Y*X') # this is ok

ERROR: MethodError: promote_operation(::typeof(), ::Type{VariableRef}, ::Type{Union{}}) is ambiguous. Candidates: promote_operation(op::typeof(), ::Type{S}, A::Type{<:AbstractArray{T}}) where {S, T} @ MutableArithmetics ~/.julia/packages/MutableArithmetics/xVyia/src/implementations/LinearAlgebra.jl:179 promote_operation(::Union{typeof(), typeof(+), typeof(-)}, V::Type{<:AbstractVariableRef}, C::Type{<:Union{Number, LinearAlgebra.UniformScaling}}) @ JuMP ~/.julia/packages/JuMP/glJ0u/src/mutable_arithmetics.jl:38 Possible fix, define promote_operation(::typeof(), ::Type{S}, ::Type{Union{}}) where S<:AbstractVariableRef

Note that with a transpose it works well.

Actually, Y' is not defined:

Y'

2×2 adjoint(::Matrix{NonlinearExpr}) with eltype Union{}: Error showing value of type LinearAlgebra.Adjoint{Union{}, Matrix{NonlinearExpr}}: ERROR: MethodError: no method matching conj(::NonlinearExpr)

I'm on julia v1.10.2 and JuMP v1.21.0

odow commented 2 months ago

Thanks for the report. I can reproduce, so I'll get this fixed. I'm surprised I didn't run into this during testing!