# Array variable - matrix multplication in JuMP

**URL:** <https://discourse.julialang.org/t/array-variable-matrix-multplication-in-jump/49801>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [November 8, 2020, 7:57pm UTC](https://discourse.julialang.org/t/array-variable-matrix-multplication-in-jump/49801 "2020-11-08T19:57:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shashankchahar](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@shashankchahar](https://discourse.julialang.org/u/shashankchahar)\
**Post date:** [November 8, 2020, 7:57pm UTC](https://discourse.julialang.org/t/array-variable-matrix-multplication-in-jump/49801/1 "2020-11-08T19:57:02Z")

</div>

Hello, I am new to Julia and trying to convert one of the tasks that I used to do in Excel into Julia.

I need to find a matrix X. Matrix X represents discrete Markov model transition probabilities with the final column as a default state.

I know the default probabilities so year 1 to 30 so I am trying to solve for the Matrix X.

I am using JuMP and defining my variable matrix as follows:

```julia
@variable(model, 0 <= x[1:8, 1:8] <= 1)

```

Now, let’s say I want to define the probability of being in a default state after 3 years. It would be given by x ^ 3. However, Julia / JuMP does not like this operation. and I get the following error:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/b/7b3beed1161c3b796b9ab7661a465e1093427bbd.png)

Ultimately, I want to use this to calculate the NL objective to solve.

On a side note, x\*x works (x^2 doesn’t)

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/1/212ba6dc62b1c776db0de9e758b76fc3d0191cb1.png)

If you are interested in the problem I am trying to solve - it’s here:

> [@Excel to Julia](https://discourse.julialang.org/t/excel-to-julia/49678):
>
> A bit of background - I am an actuary who is trying to learn Julia. In doing so, I am trying to move some of the models that I would usually make in Excel to Julia. I am stuck on one particular problem which Excel makes quite easy for me. I am looking for ways to do the same in Julia. The problem is as follows: I know the probabilities of default for various ratings (AAA to CCC) at various maturities (year 1 to 30 - discrete). I don’t know the underlying transition matrix that produced this ta…

---

<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 8, 2020, 8:56pm UTC](https://discourse.julialang.org/t/array-variable-matrix-multplication-in-jump/49801/2 "2020-11-08T20:56:05Z")

</div>

Hi there! Take a read of the first post in [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757). It explains how you can format your post, and why you should provide code instead of screenshots.

JuMP only supports matrix operators up to quadratic terms. It doesn’t support cubic.

My guess is you want something along the lines of:

```julia
model = Model()
@variable(model, 0 <= x[1:8, 1:8] <= 1)
@variable(model, y[1:8, 1:31])
@constraint(model, [t = 1:30], x * y[:, t] .== y[:, t + 1])

```

---

<div class="post-metadata">

**Author:** ![shashankchahar](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@shashankchahar](https://discourse.julialang.org/u/shashankchahar)\
**Post date:** [November 9, 2020, 1:12pm UTC](https://discourse.julialang.org/t/array-variable-matrix-multplication-in-jump/49801/3 "2020-11-09T13:12:30Z")

</div>

Hi,

Thanks @odow, I will remember about the screenshots in future.

Re:

@constraint(model, [t = 1:30], x \* y[:, t] .== y[:, t + 1])

I think I get the idea. Shouldn’t y be the same size as x on the first two dimensions and 1:31 on the third dimension?

I will try the following:

```julia
model = Model()
@variable(model, 0 <= x[1:8, 1:8] <= 1)
@variable(model, y[1:8, 1:8, 1:31])
@constraint(model, first_t, y[:, :, 1] .== x[:, :]) # I think this is needed
@constraint(model, [t = 1:30], x * y[:, :, t] .== y[:, :, t + 1])

```

Then, I can set my objective to be based on y.

Let me try this out. Thanks!
