# How to define matrix objectives in JuMP models

**URL:** <https://discourse.julialang.org/t/how-to-define-matrix-objectives-in-jump-models/33556>\
**Category:** General Usage\
**Tags:** jump, optimization\
**Created:** [January 19, 2020, 6:02pm UTC](https://discourse.julialang.org/t/how-to-define-matrix-objectives-in-jump-models/33556 "2020-01-19T18:02:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tdp](https://avatars.discourse-cdn.com/v4/letter/t/50afbb/32.png) [@tdp](https://discourse.julialang.org/u/tdp)\
**Post date:** [January 19, 2020, 6:02pm UTC](https://discourse.julialang.org/t/how-to-define-matrix-objectives-in-jump-models/33556/1 "2020-01-19T18:02:43Z")

</div>

Hi,  
How do I declare a matrix objective in JuMP? That is, how do I declare `@objective(m, Min, ⋅ )` with `⋅` a matrix?

Thank you for any help.

## Details

I am trying to translate the Python code below to Julia. However, the following piece of Julia code returns `ERROR: MethodError: no method matching *(::LinearAlgebra.Adjoint{Int64,Array{Int64,1}}, ::VariableRef)`. What

### Julia

```julia-auto
using JuMP
using DSDP

obj_vec = [0; 1; 2]
m = Model(with_optimizer(DSDP.Optimizer))
@variable(m, X)
@objective(m, Min, obj_vec'*X)

```

### Python

```julia-auto
from cvxopt import matrix, solvers

obj_vec = matrix(range(3), (3, 1), 'd')
__sym_grams = [[0, 0, -0.5, 0, 0, 1, 0, 0, -0.5, 0, 0, 0, 0, 0, 0, 0],
               [0, 0, 0, -1, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, 0],
               [0, 0, 0, 0, 0, 0, 0, -0.5, 0, 0, 1, 0, 0, -0.5, 0, 0]]
sym_grams = matrix(__sym_grams)
Gs = [-sym_grams]
hs = [matrix([[10, 10, -1.50, 0],
              [10, 0, 0, 0.5],
              [-1.50, 0, 0, 0],
              [0, 0.5, 0, 100]])]

sol = solvers.sdp(c=obj_vec, Gs=Gs, hs=hs, solver='dsdp')
print(sol) # {'status': 'optimal', 'x': <3x1 matrix, tc='d'>, 'sl': <0x1 matrix, tc='d'>, 'ss': [<4x4 matrix, tc='d'>], 'y': <0x1 matrix, tc='d'>, 'zl': <0x1 matrix, tc='d'>, 'zs': [<4x4 matrix, tc='d'>], 'primal objective': 2.8327595103035703, 'dual objective': 2.832759388228712, 'gap': 1.221152672314929e-07, 'relative gap': 4.310823846844613e-08, 'primal infeasibility': 0.0, 'dual infeasibility': 1.7091586164057182e-12, 'residual as primal infeasibility certificate': None, 'residual as dual infeasibility certificate': None, 'primal slack': 5.425580955940478e-09, 'dual slack': 2.857198533975067e-10}

```

---

<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:** [January 19, 2020, 7:45pm UTC](https://discourse.julialang.org/t/how-to-define-matrix-objectives-in-jump-models/33556/2 "2020-01-19T19:45:34Z")

</div>

`@variable(m, X)` declares a scalar valued variable.  
To declare a vector of variables, use `@variable(model, x[1:3])`.

You probably want something like:

```julia
c = [0, 1, 2]
Gs = [
    0 0 -0.5 0 0 1 0 0 -0.5 0 0 0 0 0 0 0;
    0 0 0 -1 0 0 1 0 0 1 0 0 -1 0 0 0;
    0 0 0 0 0 0 0 -0.5 0 0 1 0 0 -0.5 0 0;
]

Hs = [
      10 10 -1.5 0;
      10 0 0 0.5;
    -1.5 0 0 0;
       0 0.5 0 100
]

using JuMP, SCS
model = Model(with_optimizer(SCS.Optimizer))
@variable(model, x[1:3])
@variable(model, s[1:4, 1:4], PSD)
@objective(model, Min, c' * x)
@constraint(model, -Gs' * x + vec(s) .== vec(Hs))
optimize!(model)
objective_value(model)

```

Here’s the JuMP documentation. It’s a good place to start reading: [Quick Start Guide · JuMP](https://www.juliaopt.org/JuMP.jl/v0.20.0/quickstart/)
