JuliaPhysics / JuliaPhysics/Unitful.jl

Error constructing and showing array of heterogeneous units

Open
#534 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Julia
Stars
675
Forks
124
Avg merge
3h 38m
Merged PRs (30d)
1

Description

The following two attempts at constructing matrices where entries have different units both fail with the same error, but at different stages of the process. The first example I can understand why it fails, what units are the zeros supposed to have? Perhaps there's a good answer to this but it's not obvious to me. The second example could perhaps be made to work?
```julia
using LinearAlgebra, Unitful.DefaultSymbols
julia> diagm(Any[1, 1kg])
ERROR: MethodError: no method matching zero(::Type{Any})
Closest candidates are:
zero(::Type{Union{Missing, T}}) where T at ~/julia-1.7.2/share/julia/base/missing.jl:105
zero(::Union{Type{P}, P}) where P<:Dates.Period at ~/julia-1.7.2/share/julia/stdlib/v1.7/Dates/src/periods.jl:53
zero(::MutableArithmetics.AbstractMutable) at ~/.julia/packages/MutableArithmetics/BOohi/src/dispatch.jl:735
...
Stacktrace:
[1] zero(#unused#::Type{Any})
@ Base ./missing.jl:106
[2] diagm_container(size::Nothing, kv::Pair{Int64, Vector{Any}})
@ LinearAlgebra ~/julia-1.7.2/share/julia/stdlib/v1.7/LinearAlgebra/src/dense.jl:308
[3] _diagm(size::Nothing, kv::Pair{Int64, Vector{Any}})
@ LinearAlgebra ~/julia-1.7.2/share/julia/stdlib/v1.7/LinearAlgebra/src/dense.jl:285
[4] diagm(kv::Pair{Int64, Vector{Any}})
@ LinearAlgebra ~/julia-1.7.2/share/julia/stdlib/v1.7/LinearAlgebra/src/dense.jl:282
[5] diagm(v::Vector{Any})
@ LinearAlgebra ~/julia-1.7.2/share/julia/stdlib/v1.7/LinearAlgebra/src/dense.jl:332
[6] top-level scope
@ REPL[16]:1
```

```julia
julia> Diagonal(Any[1, 1kg])
2×2 Diagonal{Any, Vector{Any}}:
Error showing value of type Diagonal{Any, Vector{Any}}:
ERROR: MethodError: no method matching zero(::Type{Any})
Closest candidates are:
zero(::Type{Union{Missing, T}}) where T at ~/julia-1.7.2/share/julia/base/missing.jl:105
zero(::Union{Type{P}, P}) where P<:Dates.Period at ~/julia-1.7.2/share/julia/stdlib/v1.7/Dates/src/periods.jl:53
zero(::MutableArithmetics.AbstractMutable) at ~/.julia/packages/MutableArithmetics/BOohi/src/dispatch.jl:735
...
Stacktrace:
[1] zero(#unused#::Type{Any})
@ Base ./missing.jl:106
[2] diagzero(#unused#::Diagonal{Any, Vector{Any}}, i::Int64, j::Int64)
@ LinearAlgebra ~/julia-1.7.2/share/julia/stdlib/v1.7/LinearAlgebra/src/diagonal.jl:109
[3] getindex
@ ~/julia-1.7.2/share/julia/stdlib/v1.7/LinearAlgebra/src/diagonal.jl:105 [inlined]
[4] isassigned(::Diagonal{Any, Vector{Any}}, ::Int64, ::Int64)
@ Base ./abstractarray.jl:553
[5] alignment(io::IOContext{Base.TTY}, X::AbstractVecOrMat, rows::Vector{Int64}, cols::Vector{Int64}, cols_if_complete::Int64, cols_otherwise::Int64, sep::Int64)
@ Base ./arrayshow.jl:67
[6] _print_matrix(io::IOContext{Base.TTY}, X::AbstractVecOrMat, pre::String, sep::String, post::String, hdots::String, vdots::String, ddots::String, hmod::Int64, vmod::Int64, rowsA::UnitRange{Int64}, colsA::UnitRange{Int64})
@ Base ./arrayshow.jl:204
[7] print_matrix(io::IOContext{Base.TTY}, X::Diagonal{Any, Vector{Any}}, pre::String, sep::String, post::String, hdots::String, vdots::String, ddots::String, hmod::Int64, vmod::Int64) (repeats 2 times)
@ Base ./arrayshow.jl:169
[8] print_array
@ ./arrayshow.jl:355 [inlined]
[9] show(io::IOContext{Base.TTY}, #unused#::MIME{Symbol("text/plain")}, X::Diagonal{Any, Vector{Any}})
@ Base ./arrayshow.jl:396
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing both examples and read the LinearAlgebra dense.jl and diagonal.jl stack frames cited in the report. Determine the intended behavior for heterogeneous-unit diagonals, including construction and display, then add focused tests showing the accepted behavior and run the relevant LinearAlgebra tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
data
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.