# Matrices and tensors with Symbolics.jl

**URL:** <https://discourse.julialang.org/t/matrices-and-tensors-with-symbolics-jl/119394>\
**Category:** Modelling & Simulations\
**Tags:** matrices, pluto, symbolics, pluto-and-latex, tensors\
**Created:** [September 14, 2024, 8:04am UTC](https://discourse.julialang.org/t/matrices-and-tensors-with-symbolics-jl/119394 "2024-09-14T08:04:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Damiano](https://avatars.discourse-cdn.com/v4/letter/d/ebca7d/32.png) [@Damiano](https://discourse.julialang.org/u/Damiano)\
**Post date:** [September 14, 2024, 8:04am UTC](https://discourse.julialang.org/t/matrices-and-tensors-with-symbolics-jl/119394/1 "2024-09-14T08:04:42Z")

</div>

Hi all,  
three quick issues I encountered. Please correct me if I should have created different topics of if the Category is wrong.

1. _Best way to create a symmetric matrix_  
This is the best I could come up with.

```julia
A = Array{Num,2}(undef,3,3)
for i in 1:3
	for j in 1:3
		if i >= j 
			A[i,j] = Symbolics.variable(:x,i,j)
			A[j,i] = Symbolics.variable(:x,i,j)
		end
	end
end

```

Any better way? I was hoping for something like

```julia
H = Symbolics.variables(:H, 1:3, 1:3)
H = transpose(H)

```

or

```julia
@variables H[1:3,1:3]
H = transpose(H)

```

1. _Pluto gives warning (or is it an error?) when creating Arrays indexed by more than two elements._

Behaviour in the REPL is what I would expect (final output cut for legibility):

```julia
julia> using Symbolics

julia> Symbolics.variables(:H, 1:2, 0:3, 0:3, 0:3)
2×4×4×4 Array{Num, 4}:
[:, :, 1, 1] =
 H₁ˏ₀ˏ₀ˏ₀ H₁ˏ₁ˏ₀ˏ₀ H₁ˏ₂ˏ₀ˏ₀ H₁ˏ₃ˏ₀ˏ₀
 H₂ˏ₀ˏ₀ˏ₀ H₂ˏ₁ˏ₀ˏ₀ H₂ˏ₂ˏ₀ˏ₀ H₂ˏ₃ˏ₀ˏ₀

[:, :, 2, 1] =
 H₁ˏ₀ˏ₁ˏ₀ H₁ˏ₁ˏ₁ˏ₀ H₁ˏ₂ˏ₁ˏ₀ H₁ˏ₃ˏ₁ˏ₀
 H₂ˏ₀ˏ₁ˏ₀ H₂ˏ₁ˏ₁ˏ₀ H₂ˏ₂ˏ₁ˏ₀ H₂ˏ₃ˏ₁ˏ₀

[:, :, 3, 1] =
 H₁ˏ₀ˏ₂ˏ₀ H₁ˏ₁ˏ₂ˏ₀ H₁ˏ₂ˏ₂ˏ₀ H₁ˏ₃ˏ₂ˏ₀
 H₂ˏ₀ˏ₂ˏ₀ H₂ˏ₁ˏ₂ˏ₀ H₂ˏ₂ˏ₂ˏ₀ H₂ˏ₃ˏ₂ˏ₀

[:, :, 4, 1] =
 H₁ˏ₀ˏ₃ˏ₀ H₁ˏ₁ˏ₃ˏ₀ H₁ˏ₂ˏ₃ˏ₀ H₁ˏ₃ˏ₃ˏ₀
 H₂ˏ₀ˏ₃ˏ₀ H₂ˏ₁ˏ₃ˏ₀ H₂ˏ₂ˏ₃ˏ₀ H₂ˏ₃ˏ₃ˏ₀
...

```

Behaviour in Pluto:

- first cell: `using Symbolics`
- second cell: `Symbolics.variables(:H, 1:2, 0:3, 0:3, 0:3)`
- output of second cell (not sure how it is best to format this):

```julia
Failed to show value:

Cannot latexify n-dimensional tensors with n≠1,2

Stack trace

Here is what happened, the most recent locations are first:

1. <mark> **error** (s::String)</mark> @ *[error.jl:35](https://github.com/JuliaLang/julia/tree/6f3fdf7b36250fb95f512a2b927ad2518c07d2b5/base/error.jl#L35)*

2. <mark> **_latexarray** (arr::Array{Symbolics.Num, 4}; adjustment::Symbol, transpose::Bool, double_linebreak::Bool, starred::Bool, arraystyle::Symbol, kwargs::@Kwargs{env::Symbol})</mark> @ *latexarray.jl:21*

3. <mark> **_latexraw** (arr::Array{Symbolics.Num, 4}; kwargs::@Kwargs{env::Symbol})</mark> @ *latexraw.jl:112*

4. <mark> **process_latexify** (args::Array{Symbolics.Num, 4}; kwargs::@Kwargs{env::Symbol})</mark> @ *latexify_function.jl:49*

5. <mark> **process_latexify** </mark> @ *latexify_function.jl:40*

6. <mark> **latexraw** </mark> @ *latexraw.jl:58*

7. <mark> **_latexequation** (eq::Array{Symbolics.Num, 4}; starred::Bool, kwargs::@Kwargs{})</mark> @ *latexequation.jl:5*

8. <mark> **_latexequation** (eq::Array{Symbolics.Num, 4})</mark> @ *latexequation.jl:4*

9. <mark> **process_latexify** (args::Array{Symbolics.Num, 4}; kwargs::@Kwargs{})</mark> @ *latexify_function.jl:49*

10. <mark> **process_latexify** </mark> @ *latexify_function.jl:40*

11. <mark> **latexify** (args::Array{Symbolics.Num, 4}; kwargs::@Kwargs{})</mark> @ *latexify_function.jl:27*

12. <mark> **latexify** </mark> @ *latexify_function.jl:25*

13. <mark> **show** (io::IOBuffer, ::MIME{Symbol("text/latex")}, x::Array{Symbolics.Num, 4})</mark> @ *latexify_recipes.jl:133*

14. <mark> **__binrepr** (m::MIME{Symbol("text/latex")}, x::Array{Symbolics.Num, 4}, context::Nothing)</mark> @ *[multimedia.jl:171](https://github.com/JuliaLang/julia/tree/6f3fdf7b36250fb95f512a2b927ad2518c07d2b5/base/multimedia.jl#L168)*

15. <mark> **_binrepr** </mark> @ *multimedia.jl:0*

16. <mark> **repr** (m::MIME{Symbol("text/latex")}, x::Array{Symbolics.Num, 4})</mark> @ *[multimedia.jl:159](https://github.com/JuliaLang/julia/tree/6f3fdf7b36250fb95f512a2b927ad2518c07d2b5/base/multimedia.jl#L159)*
 

```

The Array so created works just fine, but the first time I thought there was something wrong in my definition (instead it is just the Pluto Latex rendering?)

1. _Pluto and `undef` matrices_

Assuming `Symbolics.jl` is being used.  
Behaviour in the REPL is again what I would expect:

```julia
julia> B = Array{Num,2}(undef,3,3)
3×3 Matrix{Num}:
 #undef #undef #undef
 #undef #undef #undef
 #undef #undef #undef

```

Behaviour in Pluto is the following:

```julia
Failed to show value: 

UndefRefError: access to undefined reference
Stack trace

Here is what happened, the most recent locations are first:

    getindex @ essentials.jl:14
    getindex @ multidimensional.jl:696
    _broadcast_getindex @ broadcast.jl:675
    _getindex @ broadcast.jl:706
    _broadcast_getindex @ broadcast.jl:681
    getindex @ broadcast.jl:636
    copy @ broadcast.jl:942
    materialize @ broadcast.jl:903
    _latexarray(arr::Matrix{Symbolics.Num}; adjustment::Symbol, transpose::Bool, double_linebreak::Bool, starred::Bool, arraystyle::Symbol, kwargs::@Kwargs{env::Symbol}) @ latexarray.jl:48
    _latexarray @ latexarray.jl:17
    #_latexraw#46 @ latexraw.jl:112
    process_latexify(args::Matrix{Symbolics.Num}; kwargs::@Kwargs{env::Symbol}) @ latexify_function.jl:49
    process_latexify @ latexify_function.jl:40
    latexraw @ latexraw.jl:58
    _latexequation(eq::Matrix{Symbolics.Num}; starred::Bool, kwargs::@Kwargs{}) @ latexequation.jl:5
    _latexequation(eq::Matrix{Symbolics.Num}) @ latexequation.jl:4
    process_latexify(args::Matrix{Symbolics.Num}; kwargs::@Kwargs{}) @ latexify_function.jl:49
    process_latexify @ latexify_function.jl:40
    latexify(args::Matrix{Symbolics.Num}; kwargs::@Kwargs{}) @ latexify_function.jl:27
    latexify @ latexify_function.jl:25
    show(io::IOBuffer, ::MIME{Symbol("text/latex")}, x::Matrix{Symbolics.Num}) @ latexify_recipes.jl:133
    __binrepr(m::MIME{Symbol("text/latex")}, x::Matrix{Symbolics.Num}, context::Nothing) @ multimedia.jl:171
    _binrepr @ multimedia.jl:0
    repr(m::MIME{Symbol("text/latex")}, x::Matrix{Symbolics.Num}) @ multimedia.jl:159

```

Same comment as above, I thought I was misunderstanding something with Matrix initialization.

Thanks in advance for any comment or suggestion.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [September 14, 2024, 10:14am UTC](https://discourse.julialang.org/t/matrices-and-tensors-with-symbolics-jl/119394/2 "2024-09-14T10:14:59Z")

</div>

> [@Damiano](#):
>
> The Array so created works just fine, but the first time I thought there was something wrong in my definition (instead it is just the Pluto Latex rendering?

That needs a latexify.jl issue for higher order tensors

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [September 14, 2024, 10:15am UTC](https://discourse.julialang.org/t/matrices-and-tensors-with-symbolics-jl/119394/3 "2024-09-14T10:15:51Z")

</div>

> [@Damiano](#):
>
> Same comment as above, I thought I was misunderstanding something with Matrix initialization.

Again here, open up an issue in Latexify.jl for handling uninitialized arrays.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [September 14, 2024, 10:16am UTC](https://discourse.julialang.org/t/matrices-and-tensors-with-symbolics-jl/119394/4 "2024-09-14T10:16:36Z")

</div>

> [@Damiano](#):
>
> Any better way?

The loop way is probably the best way there

---

<div class="post-metadata">

**Author:** ![Damiano](https://avatars.discourse-cdn.com/v4/letter/d/ebca7d/32.png) [@Damiano](https://discourse.julialang.org/u/Damiano)\
**Post date:** [September 14, 2024, 2:17pm UTC](https://discourse.julialang.org/t/matrices-and-tensors-with-symbolics-jl/119394/5 "2024-09-14T14:17:11Z")

</div>

Understood, thank you for the incredibly quick answer!

I assume by “open up an issue” you mean on github. I will do it.  
Edit: I did it.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [February 25, 2025, 7:54am UTC](https://discourse.julialang.org/t/matrices-and-tensors-with-symbolics-jl/119394/6 "2025-02-25T07:54:01Z")

</div>

Is there a way to declare a symbolic `M x N` matrix, where `M, N` are unspecified integers?

For example in `sympy` I would do this:

```python
import sympy
n = sympy.symbols('n')
S = sympy.MatrixSymbol('S', n, n)

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 25, 2025, 1:29pm UTC](https://discourse.julialang.org/t/matrices-and-tensors-with-symbolics-jl/119394/7 "2025-02-25T13:29:04Z")

</div>

> [@e3c6](#):
>
> Is there a way to declare a symbolic `M x N` matrix, where `M, N` are unspecified integers?

Not at this time.
