# Multiplication of matrix expression and variables leads to stack overflow and matmul error

**URL:** https://discourse.julialang.org/t/multiplication-of-matrix-expression-and-variables-leads-to-stack-overflow-and-matmul-error/111760
**Category:** Optimization (Mathematical)
**Tags:** jump, optimization, stackoverflow, nonlinear
**Created:** [March 18, 2024, 10:31am UTC](https://discourse.julialang.org/t/multiplication-of-matrix-expression-and-variables-leads-to-stack-overflow-and-matmul-error/111760 "2024-03-18T10:31:07Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Rahul\_Narwar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rahul_narwar/32/207814_2.png) [@Rahul\_Narwar](https://discourse.julialang.org/u/Rahul_Narwar)
#### Post date: [March 18, 2024, 10:31am UTC](https://discourse.julialang.org/t/multiplication-of-matrix-expression-and-variables-leads-to-stack-overflow-and-matmul-error/111760/1 "2024-03-18T10:31:07Z")

</div>

I have an optimal orbital transfer problem:

```julia
h = 2
n = 6
m = 3

model = Model(NLopt.Optimizer)
set_optimizer_attribute(model, "algorithm", :AUGLAG)
local_optimizer = NLopt.Opt(:LD_LBFGS, n*(h+1) + m*h)
local_optimizer.xtol_rel = 1e-4
set_optimizer_attribute(model, "local_optimizer", local_optimizer)

@variables model begin
X[1:6, 1:h+1]
U[1:3, 1:h]
27000.0 <= T[1:h]
end

```

Trying to multiply an expression matrix

```julia
B
6×3 Matrix{Any}:
  ((2 X[1,1]²) / (X[1,1] * sqrt(3.986004415e14 / X[1,1]))) * (X[2,1] * sin(X[6,1])) … 0
  (1.0 / (X[1,1] * sqrt(3.986004415e14 / X[1,1]))) * ((X[1,1] * (-X[2,1]² + 1)) * sin(X[6,1])) 0
 0 (((X[1,1] * (-X[2,1]² + 1)) / (1.0 + (X[2,1] * cos(X[6,1])))) * cos(X[6,1] + X[4,1])) / (X[1,1] * sqrt(3.986004415e14 / X[1,1]))
  (-(X[1,1] * (-X[2,1]² + 1)) * cos(X[6,1])) / (X[2,1] * (X[1,1] * sqrt(3.986004415e14 / X[1,1]))) -((((X[1,1] * (-X[2,1]² + 1)) / (1.0 + (X[2,1] * cos(X[6,1])))) * sin(X[6,1] + X[4,1])) * cos(X[3,1])) / ((X[1,1] * sqrt(3.986004415e14 / X[1,1])) * sin(X[3,1]))
 0 (((X[1,1] * (-X[2,1]² + 1)) / (1.0 + (X[2,1] * cos(X[6,1])))) * sin(X[6,1] + X[4,1])) / ((X[1,1] * sqrt(3.986004415e14 / X[1,1])) * sin(X[3,1]))
  ((X[1,1] * (-X[2,1]² + 1)) * cos(X[6,1])) / (X[2,1] * (X[1,1] * sqrt(3.986004415e14 / X[1,1]))) … 0

```

with

```julia
U[:, i] = JuMP.VariableRef[U[1,1], U[2,1], U[3,1]]

```

by executing:

```julia
B1 = B(X[:,1])
B1*U[:,1]

```

Results in the following stack overflow error:

```julia
operate(::typeof(*), ::Matrix{Any}, ::Vector{JuMP.VariableRef})@LinearAlgebra.jl:403
mul(::Matrix{Any}, ::Vector{JuMP.VariableRef})@shortcuts.jl:58
*(::Matrix{Any}, ::Vector{JuMP.VariableRef})@dispatch.jl:360
 [Repeated 100 times]
operate(::typeof(*), ::Matrix{Any}, ::Vector{JuMP.VariableRef})@LinearAlgebra.jl:403
mul(::Matrix{Any}, ::Vector{JuMP.VariableRef})@shortcuts.jl:58
*(::Matrix{Any}, ::Vector{JuMP.VariableRef})@dispatch.jl:360

```

Whereas doing:

```julia
B1*U[:,:]

```

Gives the correct result, a 6x2 matrix.

Help in understanding and resolving the problem is appreciated!

---

<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: [March 18, 2024, 11:13am UTC](https://discourse.julialang.org/t/multiplication-of-matrix-expression-and-variables-leads-to-stack-overflow-and-matmul-error/111760/2 "2024-03-18T11:13:48Z")

</div>

Please link when cross-posting: [Multiplication of matrix expression and variables leads to stack overflow and matmul error · Issue #3714 · jump-dev/JuMP.jl · GitHub](https://github.com/jump-dev/JuMP.jl/issues/3714)

---

<div class="post-metadata">

### Author: ![Rahul\_Narwar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rahul_narwar/32/207814_2.png) [@Rahul\_Narwar](https://discourse.julialang.org/u/Rahul_Narwar)
#### Post date: [March 18, 2024, 12:00pm UTC](https://discourse.julialang.org/t/multiplication-of-matrix-expression-and-variables-leads-to-stack-overflow-and-matmul-error/111760/3 "2024-03-18T12:00:10Z")

</div>

The issue was resolved by upgrading the packages to the latest release.
