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

Convex does not support broadcasting #479

Open
odow opened this issue Feb 23, 2022 · 4 comments
Open

Convex does not support broadcasting #479

odow opened this issue Feb 23, 2022 · 4 comments

Comments

@odow
Copy link
Member

odow commented Feb 23, 2022

Convex.jl lacks support for a lot of broadcasting operations.

This causes a bunch of problems like this:

julia> using Convex

julia> x = Variable(2)
Variable
size: (2, 1)
sign: real
vexity: affine
id: 181059

julia> A = rand(2, 2)
2×2 Matrix{Float64}:
 0.290045  0.201488
 0.890725  0.771457

julia> A * x .+ 1
ERROR: MethodError: no method matching iterate(::Convex.MultiplyAtom)
Closest candidates are:
  iterate(::Union{LinRange, StepRangeLen}) at range.jl:664
  iterate(::Union{LinRange, StepRangeLen}, ::Int64) at range.jl:664
  iterate(::T) where T<:Union{Base.KeySet{var"#s79", var"#s78"} where {var"#s79", var"#s78"<:Dict}, Base.ValueIterator{var"#s77"} where var"#s77"<:Dict} at dict.jl:693
  ...
Stacktrace:
 [1] copyto!(dest::Vector{Any}, src::Convex.MultiplyAtom)
   @ Base ./abstractarray.jl:843
 [2] _collect(cont::UnitRange{Int64}, itr::Convex.MultiplyAtom, #unused#::Base.HasEltype, isz::Base.HasLength)
   @ Base ./array.jl:608
 [3] collect(itr::Convex.MultiplyAtom)
   @ Base ./array.jl:602
 [4] broadcastable(x::Convex.MultiplyAtom)
   @ Base.Broadcast ./broadcast.jl:682
 [5] broadcasted(::Function, ::Convex.MultiplyAtom, ::Int64)
   @ Base.Broadcast ./broadcast.jl:1312
 [6] top-level scope
   @ REPL[4]:1

The purpose of this issue is to collate the currently open issues into a single thread, and to make it more discoverable for users coming to report bugs.

Replaces:

@RoyiAvital
Copy link
Contributor

In #717 a documentation warning was added.
It should be removed once this issue is solved.

@RoyiAvital
Copy link
Contributor

I had another case with type conversion.
The following will fail:

function SolveSoftSVM1D( vX :: Vector{T}, vY :: Vector{N}, paramC :: T ) where {T <: AbstractFloat, N <: Integer}

    numPts = length(vX);

    if length(vY) != numPts
        throw(DimensionMismatch(lazy"`vY`` has length $(length(vY)), `vX` has length $(length(vX))"));
    end

    paramW = Variable();
    paramB = Variable();
    vξ     = Variable(numPts);

    sConvProb = minimize( T(0.5) * sumsquares(paramW) + paramC * sum(pos(one(T) - T.(vY) .* (paramW * vX + paramB))) );
    solve!(sConvProb, ECOS.Optimizer; silent = true);

    return paramW.value, paramB.value, vξ.value;
    
end

While changing T.(vY) into vY will work

@odow
Copy link
Member Author

odow commented Feb 15, 2025

What's the full stack trace of the error?

@RoyiAvital
Copy link
Contributor

RoyiAvital commented Feb 16, 2025

Here is an MWE:

using Convex;
using ECOS;

function SolveSoftSVM1D( vX :: Vector{T}, vY :: Vector{N}, paramC :: T ) where {T <: AbstractFloat, N <: Integer}

    paramW = Variable();
    paramB = Variable();

    sConvProb = minimize( T(0.5) * sumsquares(paramW) + paramC * sum(pos(one(T) - T.(vY) .* (paramW * vX + paramB))) ); #<! Avoid casting with T.() in expression
    solve!(sConvProb, ECOS.Optimizer; silent = true);

    return paramW.value, paramB.value;
    
end

vX     = Float64.(collect(-3:3));
vY     = [-1, -1, -1, 1, 1, 1, 1];
paramC = 0.0;

paramW, paramB = SolveSoftSVM1D(vX, vY, paramC);

If you change T.(vY) into vY it will work.
The error:

ERROR: MethodError: no method matching iterate(::Convex.AdditionAtom)

Closest candidates are:
  iterate(::Base.AsyncGenerator, ::Base.AsyncGeneratorState)
   @ Base asyncmap.jl:362
  iterate(::Base.AsyncGenerator)
   @ Base asyncmap.jl:362
  iterate(::LibGit2.GitRevWalker)
   @ LibGit2 D:\Applications\JuliaWin\packages\julia\share\julia\stdlib\v1.10\LibGit2\src\walker.jl:29
  ...

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

No branches or pull requests

2 participants