# JuMP Dot Product/Norm/NLobjective

**URL:** <https://discourse.julialang.org/t/jump-dot-product-norm-nlobjective/11273>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [May 30, 2018, 7:53pm UTC](https://discourse.julialang.org/t/jump-dot-product-norm-nlobjective/11273 "2018-05-30T19:53:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_McVay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_mcvay/32/4307_2.png) [@Paul\_McVay](https://discourse.julialang.org/u/Paul_McVay)\
**Post date:** [May 30, 2018, 7:53pm UTC](https://discourse.julialang.org/t/jump-dot-product-norm-nlobjective/11273/1 "2018-05-30T19:53:19Z")

</div>

I’ve run into a number of issues with quadratic objectives in JuMP. A simple example is:

```julia
m = Model(solver = CplexSolver())
@variable(m, w[1:10])
@expression(m, dotproduct, sum(w[i]*w[i] for i = 1:10))

```

If I try to make the dotproduct squared the objective with `@objective(m, Min, dotproduct^2)` I get an error

```julia
ERROR: *(::QuadExpr,::QuadExpr) is not defined. Are you trying to build a nonlinear problem? Make sure you use @NLconstraint/@NLobjective.
Stacktrace:
 [1] power_by_squaring(::JuMP.GenericQuadExpr{Float64,JuMP.Variable}, ::Int64) at ./intfuncs.jl:169
 [2] literal_pow(::Base.#^, ::JuMP.GenericQuadExpr{Float64,JuMP.Variable}, ::Type{Val{2}}) at ./intfuncs.jl:208
 [3] macro expansion at ./REPL.jl:97 [inlined]
 [4] (::Base.REPL.##1#2{Base.REPL.REPLBackend})() at ./event.jl:73

```

If I try it with `@NLobjective(m, Min, dotproduct^2)`, I get the error

```julia
ERROR: MethodError: no method matching parseNLExpr_runtime(::JuMP.Model, ::JuMP.GenericQuadExpr{Float64,JuMP.Variable}, ::Array{ReverseDiffSparse.NodeData,1}, ::Int64, ::Array{Float64,1})
Closest candidates are:
  parseNLExpr_runtime(::JuMP.Model, !Matched::Number, ::Any, ::Any, ::Any) at /home/paul/.julia/v0.6/JuMP/src/parsenlp.jl:196
  parseNLExpr_runtime(::JuMP.Model, !Matched::JuMP.Variable, ::Any, ::Any, ::Any) at /home/paul/.julia/v0.6/JuMP/src/parsenlp.jl:202
  parseNLExpr_runtime(::JuMP.Model, !Matched::JuMP.NonlinearExpression, ::Any, ::Any, ::Any) at /home/paul/.julia/v0.6/JuMP/src/parsenlp.jl:208
  ...
Stacktrace:
 [1] macro expansion at ./REPL.jl:97 [inlined]
 [2] (::Base.REPL.##1#2{Base.REPL.REPLBackend})() at ./event.jl:73

```

Aren’t those the only two ways to add the objectives?

---

<div class="post-metadata">

**Author:** ![abelsiqueira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abelsiqueira/32/47269_2.png) [@abelsiqueira](https://discourse.julialang.org/u/abelsiqueira)\
**Post date:** [May 30, 2018, 8:04pm UTC](https://discourse.julialang.org/t/jump-dot-product-norm-nlobjective/11273/2 "2018-05-30T20:04:58Z")

</div>

Could you try with `@NLexpression` and `@NLobjective`?

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [May 30, 2018, 8:07pm UTC](https://discourse.julialang.org/t/jump-dot-product-norm-nlobjective/11273/3 "2018-05-30T20:07:46Z")

</div>

Note that you ought to also be able to just do `w⋅w`, though I’m not completely sure that `@NLexpression` and `@NLobjective` will cooperate with that.

---

<div class="post-metadata">

**Author:** ![Juser](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@Juser](https://discourse.julialang.org/u/Juser)\
**Post date:** [May 31, 2018, 5:41am UTC](https://discourse.julialang.org/t/jump-dot-product-norm-nlobjective/11273/4 "2018-05-31T05:41:47Z")

</div>

I think your problem is that `dotproduct^2` is not quadratic since `dotproduct` is quadratic.

---

<div class="post-metadata">

**Author:** ![Paul\_McVay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_mcvay/32/4307_2.png) [@Paul\_McVay](https://discourse.julialang.org/u/Paul_McVay)\
**Post date:** [May 31, 2018, 3:28pm UTC](https://discourse.julialang.org/t/jump-dot-product-norm-nlobjective/11273/5 "2018-05-31T15:28:10Z")

</div>

It seemed to work with `@NLexpression` and `@NLobjective`

ExpandingMan: Yes, the dot product only works with the linear expression and objective right now which is why I had to write it the other way
