Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rendering higher order tensors and uninitialized arrays. #307

Open
damianodegaspari opened this issue Sep 14, 2024 · 5 comments
Open

Rendering higher order tensors and uninitialized arrays. #307

damianodegaspari opened this issue Sep 14, 2024 · 5 comments

Comments

@damianodegaspari
Copy link

Hi,

I am opening this issue as suggest to me in the Julia discord topic Matrices and tensors with Symbolics.jl. It is about two problems I encountered while using Symbolics.jl with Pluto.jl. I rewrite everything here.

  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.

@ChrisRackauckas
Copy link
Contributor

The Pluto part is pretty much irrelevant. Pluto's automatic display of Symbolics is to use the Latex output, so it just boils down to latexify(expr). And the Symbolics part is not necessary either, since this boils down to just the Latexify recipes on Array types, higher dimensional and undef. So it's clear that one can create an MWE that is just Latexify.jl which is probably step one of getting a solution.

@ChrisRackauckas
Copy link
Contributor

though I don't know if there is any precedent on "standard" way to show 4-tensors in Latex?

For undef, just using a \cdot or something might make sense.

@damianodegaspari
Copy link
Author

damianodegaspari commented Sep 14, 2024

Following your comments I tried to create a MWE, in the REPL.

  _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.10.5 (2024-08-27)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> using Latexify

julia> undef_array = Array{Any,2}(undef,4,4)
4×4 Matrix{Any}:
 #undef  #undef  #undef  #undef
 #undef  #undef  #undef  #undef
 #undef  #undef  #undef  #undef
 #undef  #undef  #undef  #undef

julia> highrank_array = zeros(3,3,3)
3×3×3 Array{Float64, 3}:
[:, :, 1] =
 0.0  0.0  0.0
 0.0  0.0  0.0
 0.0  0.0  0.0

[:, :, 2] =
 0.0  0.0  0.0
 0.0  0.0  0.0
 0.0  0.0  0.0

[:, :, 3] =
 0.0  0.0  0.0
 0.0  0.0  0.0
 0.0  0.0  0.0

julia> latexify(undef_array)
ERROR: UndefRefError: access to undefined reference
Stacktrace:
  [1] getindex
    @ ./essentials.jl:14 [inlined]
  [2] getindex
    @ ./multidimensional.jl:696 [inlined]
  [3] _broadcast_getindex
    @ ./broadcast.jl:675 [inlined]
  [4] _getindex
    @ ./broadcast.jl:706 [inlined]
  [5] _broadcast_getindex
    @ ./broadcast.jl:681 [inlined]
  [6] getindex
    @ ./broadcast.jl:636 [inlined]
  [7] copy
    @ ./broadcast.jl:942 [inlined]
  [8] materialize
    @ ./broadcast.jl:903 [inlined]
  [9] _latexarray(arr::Matrix{…}; adjustment::Symbol, transpose::Bool, double_linebreak::Bool, starred::Bool, arraystyle::Symbol, kwargs::@Kwargs{…})
    @ Latexify ~/.julia/packages/Latexify/ieukI/src/latexarray.jl:48
 [10] _latexarray
    @ ~/.julia/packages/Latexify/ieukI/src/latexarray.jl:17 [inlined]
 [11] #_latexraw#46
    @ ~/.julia/packages/Latexify/ieukI/src/latexraw.jl:112 [inlined]
 [12] process_latexify(args::Matrix{Any}; kwargs::@Kwargs{env::Symbol})
    @ Latexify ~/.julia/packages/Latexify/ieukI/src/latexify_function.jl:49
 [13] process_latexify
    @ ~/.julia/packages/Latexify/ieukI/src/latexify_function.jl:40 [inlined]
 [14] latexraw
    @ ~/.julia/packages/Latexify/ieukI/src/latexraw.jl:58 [inlined]
 [15] _latexequation(eq::Matrix{Any}; starred::Bool, kwargs::@Kwargs{})
    @ Latexify ~/.julia/packages/Latexify/ieukI/src/latexequation.jl:5
 [16] _latexequation(eq::Matrix{Any})
    @ Latexify ~/.julia/packages/Latexify/ieukI/src/latexequation.jl:4
 [17] process_latexify(args::Matrix{Any}; kwargs::@Kwargs{})
    @ Latexify ~/.julia/packages/Latexify/ieukI/src/latexify_function.jl:49
 [18] process_latexify
    @ ~/.julia/packages/Latexify/ieukI/src/latexify_function.jl:40 [inlined]
 [19] latexify(args::Matrix{Any}; kwargs::@Kwargs{})
    @ Latexify ~/.julia/packages/Latexify/ieukI/src/latexify_function.jl:27
 [20] latexify(args::Matrix{Any})
    @ Latexify ~/.julia/packages/Latexify/ieukI/src/latexify_function.jl:25
 [21] top-level scope
    @ REPL[4]:1
Some type information was truncated. Use `show(err)` to see complete types.

julia> latexify(highrank_array)
ERROR: Cannot latexify n-dimensional tensors with n≠1,2
Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:35
  [2] _latexarray(arr::Array{…}; adjustment::Symbol, transpose::Bool, double_linebreak::Bool, starred::Bool, arraystyle::Symbol, kwargs::@Kwargs{…})
    @ Latexify ~/.julia/packages/Latexify/ieukI/src/latexarray.jl:21
  [3] _latexraw(arr::Array{Float64, 3}; kwargs::@Kwargs{env::Symbol})
    @ Latexify ~/.julia/packages/Latexify/ieukI/src/latexraw.jl:112
  [4] process_latexify(args::Array{Float64, 3}; kwargs::@Kwargs{env::Symbol})
    @ Latexify ~/.julia/packages/Latexify/ieukI/src/latexify_function.jl:49
  [5] process_latexify
    @ ~/.julia/packages/Latexify/ieukI/src/latexify_function.jl:40 [inlined]
  [6] latexraw
    @ ~/.julia/packages/Latexify/ieukI/src/latexraw.jl:58 [inlined]
  [7] _latexequation(eq::Array{Float64, 3}; starred::Bool, kwargs::@Kwargs{})
    @ Latexify ~/.julia/packages/Latexify/ieukI/src/latexequation.jl:5
  [8] _latexequation(eq::Array{Float64, 3})
    @ Latexify ~/.julia/packages/Latexify/ieukI/src/latexequation.jl:4
  [9] process_latexify(args::Array{Float64, 3}; kwargs::@Kwargs{})
    @ Latexify ~/.julia/packages/Latexify/ieukI/src/latexify_function.jl:49
 [10] process_latexify
    @ ~/.julia/packages/Latexify/ieukI/src/latexify_function.jl:40 [inlined]
 [11] latexify(args::Array{Float64, 3}; kwargs::@Kwargs{})
    @ Latexify ~/.julia/packages/Latexify/ieukI/src/latexify_function.jl:27
 [12] latexify(args::Array{Float64, 3})
    @ Latexify ~/.julia/packages/Latexify/ieukI/src/latexify_function.jl:25
 [13] top-level scope
    @ REPL[5]:1
Some type information was truncated. Use `show(err)` to see complete types.

(@v1.10) pkg> status
Status `~/.julia/environments/v1.10/Project.toml`
  [6e4b80f9] BenchmarkTools v1.5.0
  [336ed68f] CSV v0.10.14
  [13f3f980] CairoMakie v0.12.10
  [a93c6f00] DataFrames v1.6.1
  [cd3eb016] HTTP v1.10.8
  [23fbe1c1] Latexify v0.16.5
  [91a5bcdd] Plots v1.40.8
  [c3e4b0f8] Pluto v0.19.46
  [1fd47b50] QuadGK v2.11.0
  [0c5d862f] Symbolics v6.11.0
  [10745b16] Statistics v1.10.0

(@v1.10) pkg> 


@ChrisRackauckas
Copy link
Contributor

Yup that seems to be it. Now I don't know what to do from there, but someone who does the latexification part can propose a representation at least. Or maybe the right solution is just a better behavior for "cannot latexify" that isn't just an error when in mime type display mode.

@gustaphe
Copy link
Collaborator

The n ≠ 1,2 error was introduced in #288 , but was an indexing crash before that. Unless you know of a standardized representation, I think this error message is correct for explicit latexification.
For implicit latexification like that in Pluto, maybe the correct solution is for the show method (in Pluto or Symbolics? I don't know) to watch out for this. Or a steamroll keyword in Latexify, to prefer a "missing" symbol over an error.

On the question of #undef, that was simple enough, let me know if you have any opinions on #308 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants