# @NLexpression involving multilinear algebra

**URL:** <https://discourse.julialang.org/t/nlexpression-involving-multilinear-algebra/90656>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [November 22, 2022, 4:33pm UTC](https://discourse.julialang.org/t/nlexpression-involving-multilinear-algebra/90656 "2022-11-22T16:33:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@Thomas](https://discourse.julialang.org/u/Thomas)\
**Post date:** [November 22, 2022, 4:33pm UTC](https://discourse.julialang.org/t/nlexpression-involving-multilinear-algebra/90656/1 "2022-11-22T16:33:52Z")

</div>

What is the recommended way to implement

```julia
@NLexpression(model, tr(kron(A,B)*kron(C,D,E))

```

where `A,B,C,D,E` are (references of) matrices of JuMP variables created by `@variable` of compatible sizes (so matrix multiplication can be performed).

---

<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:** [November 22, 2022, 5:28pm UTC](https://discourse.julialang.org/t/nlexpression-involving-multilinear-algebra/90656/2 "2022-11-22T17:28:50Z")

</div>

There isn’t a good way to do this in JuMP at the moment. See also: [WIP: [Nonlinear] begin experiments with NonlinearExpr by odow · Pull Request #3106 · jump-dev/JuMP.jl · GitHub](https://github.com/jump-dev/JuMP.jl/pull/3106)

You might be interested instead in [GitHub - jump-dev/Convex.jl: A Julia package for disciplined convex programming](https://github.com/jump-dev/Convex.jl)

---

<div class="post-metadata">

**Author:** ![Thomas](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@Thomas](https://discourse.julialang.org/u/Thomas)\
**Post date:** [November 22, 2022, 6:08pm UTC](https://discourse.julialang.org/t/nlexpression-involving-multilinear-algebra/90656/3 "2022-11-22T18:08:07Z")

</div>

Thank you; I’ll have a look at the WIP branch.

Do you @odow think the following could work?

```julia
# L (and likewise R) is intended to be the left and right matrix
# L::Matrix{NonlinearExpression} built by hand at the Julia level (and not in JuMP)
@NLexpression(model, sum(L[i,j]*R[j,i] for i in 1:size(L,1) for j in 1:size(L,2))

```

Here if I could build `ANL::Matrix{NonlinearExpression}` where each entry is just a variable in the original `A` but as a nonlinear expression, e.g. ANL[1,1] = @NLexpression(model, A[1,1]) then `kron` and matrix multiplication could work at the Julia level.

---

<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:** [November 22, 2022, 6:12pm UTC](https://discourse.julialang.org/t/nlexpression-involving-multilinear-algebra/90656/4 "2022-11-22T18:12:25Z")

</div>

One approach you could do is to create a series of projected quadratic equality constraints:

```julia
model = Model()
@variable(model, A[1:2, 1:2])
@variable(model, B[1:2, 1:2])
@variable(model, C[1:4, 1:4])
@variable(model, D[1:4, 1:4])
@constraint(model, D .== kron(A, B))
@variable(model, E[1:16, 1:16])
@constraint(model, E .== kron(D, C)) # kron(A, B, C)
@objective(model, Min, tr(E))

```

You could also write the `kron` expression as an explicit outer product with sums, similar to what you’re doing.

> I’ll have a look at the WIP branch.

To clarify, you shouldn’t use this. It’s still under development.
