# Unable to create vector out of AffExpr in some cases

**URL:** <https://discourse.julialang.org/t/unable-to-create-vector-out-of-affexpr-in-some-cases/107968>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [December 23, 2023, 10:05am UTC](https://discourse.julialang.org/t/unable-to-create-vector-out-of-affexpr-in-some-cases/107968 "2023-12-23T10:05:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![A\_C](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@A\_C](https://discourse.julialang.org/u/A_C)\
**Post date:** [December 23, 2023, 10:05am UTC](https://discourse.julialang.org/t/unable-to-create-vector-out-of-affexpr-in-some-cases/107968/1 "2023-12-23T10:05:12Z")

</div>

MWE:

```julia
using JuMP
model = Model()

x1 = @variable(model, base_name = "x1")
x2 = x1 + 1
x3::AffExpr = x1 

# Works: x1 is of type VariableRef
v1 = [sin(x1), x1]
# Works: x2 is of type AffExpr
v2 = [sin(x1), x2]
# Does not work: x3 is also of type AffExpr
v3 = [sin(x1), x3]

```

Hits error in Base.vect. So not sure if this is a JuMP issue or a Base issue.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [December 23, 2023, 2:06pm UTC](https://discourse.julialang.org/t/unable-to-create-vector-out-of-affexpr-in-some-cases/107968/2 "2023-12-23T14:06:10Z")

</div>

That sure looks like a bug. I think the bug is in `nlp_expr.jl` in JuMP:

```julia
function Base.convert(
    ::Type{<:GenericNonlinearExpr},
    x::GenericAffExpr{C,V},
) where {C,V}
    args = Any[]
    for (variable, coef) in x.terms
        if isone(coef)
            push!(args, variable)
        elseif !iszero(coef)
            push!(args, GenericNonlinearExpr{V}(:*, coef, variable))
        end
    end
    if !iszero(x.constant) || isempty(args)
        push!(args, x.constant)
    end
    if length(args) == 1 # <--- BUG is here
        return args[1]
    end
    return GenericNonlinearExpr{V}(:+, args)
end

```

When `args` is length 1, the return value does not go through the `GenericNonlinearExpr` constructor and can be of the wrong type.

When removing the `if length(args) == 1` statement, the example in the post goes through. This may also be the fix, as this seems to mainly function as an optimization.

---

<div class="post-metadata">

**Author:** ![A\_C](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@A\_C](https://discourse.julialang.org/u/A_C)\
**Post date:** [December 24, 2023, 2:58pm UTC](https://discourse.julialang.org/t/unable-to-create-vector-out-of-affexpr-in-some-cases/107968/4 "2023-12-24T14:58:45Z")

</div>

Thanks a lot. I have temporarily copied your version to my file and it works as expected.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [December 26, 2023, 9:09pm UTC](https://discourse.julialang.org/t/unable-to-create-vector-out-of-affexpr-in-some-cases/107968/5 "2023-12-26T21:09:51Z")

</div>

> [@A\_C](#):
>
> `v3 = [sin(x1), x3]`

Another work-around is `v3 = Any[sin(x1), x3]`.

I’ll take a look at fixing this.
