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

Error when parsing an inner product in an expression #2005

Closed
mtanneau opened this issue Jul 9, 2019 · 1 comment · Fixed by #2107
Closed

Error when parsing an inner product in an expression #2005

mtanneau opened this issue Jul 9, 2019 · 1 comment · Fixed by #2107

Comments

@mtanneau
Copy link
Contributor

mtanneau commented Jul 9, 2019

It seems that an inner product between two vectors is not identified as a scalar when parsed by JuMP.

using LinearAlgebra
using JuMP

u = ones(1)
v = ones(1)

m = Model()

@variable(m, x)
@constraint(m, (1.0 + u'v) * x == 0.0)  # u, v are vectors so u'v is a scalar

raises the error

ERROR: MethodError: no method matching *(::Adjoint{Float64,Array{Float64,1}}, ::VariableRef)
Closest candidates are:
*(::Any, ::Any, ::Any, ::Any...) at operators.jl:502
*(::Adjoint{T,#s623} where #s623<:(AbstractArray{T,1} where T) where T, ::Adjoint{#s623,#s622} where #s622<:(Transpose{T,#s623} where #s623<:(AbstractArray{T,1} where T) where T) where #s623) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.1/LinearAlgebra/src/adjtrans.jl:215
*(::Adjoint{T,#s623} where #s623<:(AbstractArray{T,1} where T) where T, ::Adjoint{#s623,#s622} where #s622<:LinearAlgebra.AbstractTriangular where #s623) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.1/LinearAlgebra/src/triangular.jl:1954

Note that the two following lines do not raise an error

@constraint(m, (1.0 + dot(u, v)) * x == 0.0)

and

(1.0 + u'v) * x  # returns 2 x
@odow odow added the Type: Bug label Jul 9, 2019
@odow
Copy link
Member

odow commented Jul 9, 2019

What's weirder is that:

m = Model()
@variable(m, x)
u = v = ones(1)
@constraint(m, u' * v * x == 0)    # Works
@constraint(m, (u' * v) * x == 0)  # Doesn't work
@constraint(m, x * (u' * v) == 0)  # Doesn't work

Tidying up @macroexpand @constraint(m, (u' * v) * x == 0) shows that it gets parsed as

ssa_0 = (JuMP.Val){false}()
ssa_1 = (JuMP._destructive_add_with_reorder!)(ssa_0, u', x, v)
ssa_2 = (JuMP._destructive_add_with_reorder!)(ssa_1, -1.0, 0)
ssa_3 = (JuMP._destructive_add_with_reorder!)(ssa_2, 0)

when it should be parsed as

ssa_0 = (JuMP.Val){false}()
ssa_1 = (JuMP._destructive_add_with_reorder!)(ssa_0, u', v, x)
ssa_2 = (JuMP._destructive_add_with_reorder!)(ssa_1, -1.0, 0)
ssa_3 = (JuMP._destructive_add_with_reorder!)(ssa_2, 0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

2 participants