# How to use @expression macro with non-numeric indexes

**URL:** https://discourse.julialang.org/t/how-to-use-expression-macro-with-non-numeric-indexes/116782
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [July 8, 2024, 4:47pm UTC](https://discourse.julialang.org/t/how-to-use-expression-macro-with-non-numeric-indexes/116782 "2024-07-08T16:47:23Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![asuski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asuski/32/210522_2.png) [@asuski](https://discourse.julialang.org/u/asuski)
#### Post date: [July 8, 2024, 4:47pm UTC](https://discourse.julialang.org/t/how-to-use-expression-macro-with-non-numeric-indexes/116782/1 "2024-07-08T16:47:23Z")

</div>

Hi,

I am building an optimization model where I would like to use JuMP _expressions_ to smoothly add elements of constraints across different parts of the model. However, the sets I am working with and over which I am initializing my variables are not numeric but often lists of strings. In this case, _@expresssion_ macro returns _DenseAxisArray_ instead of _Matrix{AffExpr}_, which I cannot easily manipulate, add together and use functions such as [`add_to_expression!`]

Here is a simple example:

```julia
using JuMP
model = Model()
I = [1, 2, 3] #first set
J = ["A", "B", "C"] #second set

@variable(model, x[i in I, j in J] >= 0)
@expression(model, expr[i in I, j in J], x) #produces the DenseAxisArray

```

Is there any other way to do it?

---

<div class="post-metadata">

### Author: ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)
#### Post date: [July 9, 2024, 8:36am UTC](https://discourse.julialang.org/t/how-to-use-expression-macro-with-non-numeric-indexes/116782/2 "2024-07-09T08:36:24Z")

</div>

It’s actually still stored as a `Matrix` under the hood, see

> <https://github.com/jump-dev/JuMP.jl/blob/301d46e81cb66c74c6e22cd89fb89ced740f157b/src/Containers/DenseAxisArray.jl#L22>

You can access the matrix using `x.data` or `expr.data`.  
Alternatively, you can use

```julia
@variable(model, x[i in eachindex(I), j in eachindex(J)] >= 0)

```

so that JuMP just uses a `Matrix` but then you cannot index it with the elements of `I` and `J` but with their indices instead.

---

<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: [July 13, 2024, 12:41am UTC](https://discourse.julialang.org/t/how-to-use-expression-macro-with-non-numeric-indexes/116782/3 "2024-07-13T00:41:27Z")

</div>

Hi @asuski, welcome to the forum 😄

Can you share a larger example of what you are trying to do? In most cases, you should be able to write similar code depending on whether the data structure is a matrix or dense axis array.

See [Containers · JuMP](https://jump.dev/JuMP.jl/stable/manual/containers/#DenseAxisArray), which explains the various operations you can do with a dense axis array.
