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

Array of Variable is Unexpected behavior #223

Closed
utotch opened this issue Feb 15, 2018 · 4 comments · Fixed by #293
Closed

Array of Variable is Unexpected behavior #223

utotch opened this issue Feb 15, 2018 · 4 comments · Fixed by #293

Comments

@utotch
Copy link

utotch commented Feb 15, 2018

Hello.
I found that I can't make "Array of Variable" by using Convex.jl.
I think it is unexpected behavior but is it a supposed specification?

I think a collection of Variable is useful in some situations and this behavior limits my coding style...

-- simple example

using Convex
a = Variable(2)
[a]
MethodError: no method matching +(::Convex.NoSign)
Closest candidates are:
  +(::Convex.NoSign, ::Convex.NoSign) at /opt/julia/v0.6/Convex/src/dcp.jl:93
  +(::Convex.NoSign, ::Convex.Positive) at /opt/julia/v0.6/Convex/src/dcp.jl:94
  +(::Convex.NoSign, ::Convex.Negative) at /opt/julia/v0.6/Convex/src/dcp.jl:96
  ...

Stacktrace:
 [1] sum(::Tuple{Convex.NoSign}) at ./tuple.jl:323
 [2] sign(::Convex.HcatAtom) at /opt/julia/v0.6/Convex/src/atoms/affine/stack.jl:26
 [3] show(::IOContext{Base.AbstractIOBuffer{Array{UInt8,1}}}, ::Convex.TransposeAtom) at /opt/julia/v0.6/Convex/src/utilities/show.jl:56
 [4] limitstringmime(::MIME{Symbol("text/plain")}, ::Convex.TransposeAtom) at /opt/julia/v0.6/IJulia/src/inline.jl:24
 [5] display_dict(::Convex.TransposeAtom) at /opt/julia/v0.6/IJulia/src/execute_request.jl:28
 [6] (::Compat.#inner#17{Array{Any,1},IJulia.#display_dict,Tuple{Convex.TransposeAtom}})() at /opt/julia/v0.6/Compat/src/Compat.jl:488
 [7] execute_request(::ZMQ.Socket, ::IJulia.Msg) at /opt/julia/v0.6/IJulia/src/execute_request.jl:186
 [8] (::Compat.#inner#17{Array{Any,1},IJulia.#execute_request,Tuple{ZMQ.Socket,IJulia.Msg}})() at /opt/julia/v0.6/Compat/src/Compat.jl:488
 [9] eventloop(::ZMQ.Socket) at /opt/julia/v0.6/IJulia/src/eventloop.jl:8
 [10] (::IJulia.##14#17)() at ./task.jl:335

-- and can't make Array of Variable

using Convex
A = [Variable(2), Variable(3,4)]
Cannot horizontally stack expressions of varying number of rows

Stacktrace:
 [1] Convex.HcatAtom(::Convex.Variable, ::Convex.Variable) at /opt/julia/v0.6/Convex/src/atoms/affine/stack.jl:16
 [2] hcat(::Convex.Variable, ::Convex.Variable) at /opt/julia/v0.6/Convex/src/atoms/affine/stack.jl:113
 [3] include_string(::String, ::String) at ./loading.jl:522

@utotch
Copy link
Author

utotch commented Feb 15, 2018

I found that "append!" don't work but "push!" works well.

@Non-Contradiction
Copy link

Maybe you can use other structures to hold the collection of variables instead of array, like tuple or dict?

As far as I can see, the array or variables (when it works) may not behave like what you want.
For example, [Variable(2), Variable(3)][1] is a Variable(1), not Variable(2) you may expect.
I think it is because variables are treated like vectors/matrices, and [] is overloaded to concatenate the vectors/matrices. So A = [Variable(2), Variable(3,4)] error message is
"Cannot horizontally stack expressions of varying number of rows"

@utotch
Copy link
Author

utotch commented Feb 15, 2018

I implemented my problem without using append!.
I prepare empty array [] and push! Variables.

Thus I was able to use Array for a collection of Variables, but I think overriding [] is confusing.

@ericphanson
Copy link
Collaborator

Note that

a = Variable(2)
[a] 

works fine in the current version of Convex. Regarding

A = [Variable(2), Variable(3,4)]

the problem seems to be these lines: https://github.com/JuliaOpt/Convex.jl/blob/fe366821b77d8243f2ca21a7deb35f4d50c579ce/src/atoms/affine/stack.jl#L135-L139

What's interesting is that Convex.jl's tests pass when I just comment them out, and moreover

A = [Variable(2), Variable(3,4)]

works fine.

Maybe we can just remove those problematic lines? I see they were introduced years ago in a much older version of Julia, and maybe they are no longer necessary. I will submit a pull request to remove the Base.vect override for discussion.

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

Successfully merging a pull request may close this issue.

3 participants