# Scalar product in objective

**URL:** <https://discourse.julialang.org/t/scalar-product-in-objective/36550>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [March 26, 2020, 1:25pm UTC](https://discourse.julialang.org/t/scalar-product-in-objective/36550 "2020-03-26T13:25:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ohmsweetohm1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ohmsweetohm1/32/49126_2.png) [@ohmsweetohm1](https://discourse.julialang.org/u/ohmsweetohm1)\
**Post date:** [March 26, 2020, 1:25pm UTC](https://discourse.julialang.org/t/scalar-product-in-objective/36550/1 "2020-03-26T13:25:35Z")

</div>

I’ll illustrate the problem with the following simple example:

```julia
import JuMP
l = ones(4)
model= JuMP.Model()
JuMP.@variable(model, x)
JuMP.@variable(model, y1)
JuMP.@variable(model, y2)
epxr = [
    JuMP.@expression(model, y1 - 1), 
    JuMP.@expression(model, y2 - 2)]

```

In my objective function I want to have the scalar product of `l` and `expr`.  
This does not work:

```julia
JuMP.@objective(model, Min, x * 3 + l[2:3]' * epxr))

```

> MethodError: no method matching add\_to\_expression!(::GenericAffExpr{Float64,VariableRef}, ::Adjoint{Float64,Array{Float64,1}}, ::Array{GenericAffExpr{Float64,VariableRef},1})

FYI: If you would remove the term `x * 3` it would work.

This does work:

```julia
JuMP.@objective(model, Min, x * 3 + sum(l[2:3] .* epxr))

```

This does too:

```julia
exp2 = l[2:3]' * epxr
JuMP.@objective(model, Min, x * 3 + exp2)

```

Why?

---

<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:** [March 26, 2020, 1:43pm UTC](https://discourse.julialang.org/t/scalar-product-in-objective/36550/2 "2020-03-26T13:43:51Z")

</div>

That looks like a bug. Please open an issue at JuMP.jl.
