# JuMP: defining objective function uses matrix multiplication

**URL:** https://discourse.julialang.org/t/jump-defining-objective-function-uses-matrix-multiplication/50169
**Category:** Optimization (Mathematical)
**Created:** [November 15, 2020, 1:51am UTC](https://discourse.julialang.org/t/jump-defining-objective-function-uses-matrix-multiplication/50169 "2020-11-15T01:51:03Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![kausban](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kausban/32/19442_2.png) [@kausban](https://discourse.julialang.org/u/kausban)
#### Post date: [November 15, 2020, 1:51am UTC](https://discourse.julialang.org/t/jump-defining-objective-function-uses-matrix-multiplication/50169/1 "2020-11-15T01:51:03Z")

</div>

Hi I am trying to recreate a relatively simple optimization function that I have working with python using (casadi + ipopt).

I am having some trouble figuring out how I can describe the objective function since it involves matrix/vectors. What would be the correct method to handle such a function with JuMP?

Reference working python code (Function in line 11): [http://ix.io/2Ecl](http://ix.io/2Ecl)  
What I have got with Julia so far: [http://ix.io/2Ecp](http://ix.io/2Ecp) (line 55 in solve\_InfMat())

Thanks for your help 🙂

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [November 15, 2020, 2:10am UTC](https://discourse.julialang.org/t/jump-defining-objective-function-uses-matrix-multiplication/50169/2 "2020-11-15T02:10:15Z")

</div>

I do not know Python so many things are not clear to me in your example, from where comes `U`? Are `I` variables? (I am not sure of what comes from a `.mat` file.) If I am not wrong Ipopt supports arbitrary functions so you could code a function that does the same your objective function in python does and pass it to Ipopt by JuMP. I think [this post](https://discourse.julialang.org/t/conditional-inside-a-function-to-be-optimized/48563/16) summarises some of the difficulties of working with non-linear objectives in Ipopt.

---

<div class="post-metadata">

### Author: ![kausban](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kausban/32/19442_2.png) [@kausban](https://discourse.julialang.org/u/kausban)
#### Post date: [November 15, 2020, 2:40am UTC](https://discourse.julialang.org/t/jump-defining-objective-function-uses-matrix-multiplication/50169/3 "2020-11-15T02:40:43Z")

</div>

Sorry, i should’ve been more explicit.

I is an MxN matrix (45x37). I am pulling that from the mat file. Some experimental data in reality.

U is an Nx1 vector. This is the unknown I am trying to solve for.

Z is a Mx1 vector. That’s the known target. I set it to zeros. But explicitly set z[4]=-2.0 in my example code.

I am of course able to describe the same function in both languages see (lines 11 in Python and 55 in Julia version).

Thanks for sharing the post. I noticed this linked there: [https://jump.dev/JuMP.jl/stable/nlp/#User-defined-functions-with-vector-inputs-1](https://jump.dev/JuMP.jl/stable/nlp/#User-defined-functions-with-vector-inputs-1)

I couldn’t quite grasp the work around. I am new to Julia so perhaps I misunderstand something. Its a relatively simple linear equation:

obj\_function(U) = 0.5 \* ((U’ \* (I’ \* I)) \* U) + transpose(-1 \* (I’ \* z)) \* U

How could something like this be passed without using vectors/matrices?

For reference. I use IPOPT to minimize this function. The only constrains are that each element of U is within 0-400^2

---

<div class="post-metadata">

### Author: ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)
#### Post date: [November 15, 2020, 8:29pm UTC](https://discourse.julialang.org/t/jump-defining-objective-function-uses-matrix-multiplication/50169/4 "2020-11-15T20:29:36Z")

</div>

JuMP doesn’t have the same features as CasADi for writing nonlinear problems with matrix-valued and vector-valued subexpressions. However, it looks like you’re in luck because your objective appears to be quadratic with respect to U. You can therefore write something like:

```julia
@objective(m, Min, 0.5 * ((U’ * (I’ * I)) * U) + transpose(-1 * (I’ * z)) * U)

```

(possibly with modifications to make sure the output is a scalar and not a 1x1 matrix).

---

<div class="post-metadata">

### Author: ![kausban](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kausban/32/19442_2.png) [@kausban](https://discourse.julialang.org/u/kausban)
#### Post date: [November 15, 2020, 10:52pm UTC](https://discourse.julialang.org/t/jump-defining-objective-function-uses-matrix-multiplication/50169/5 "2020-11-15T22:52:24Z")

</div>

Thanks. That way of writing the objective does indeed solve the problem correctly.
