Matrices and tensors with Symbolics.jl

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.
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

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

or

@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> 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):
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> 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:

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.

That needs a latexify.jl issue for higher order tensors

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

The loop way is probably the best way there

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.

1 Like