Julia: Problem with cat

Created on 27 May 2020  Â·  1Comment  Â·  Source: JuliaLang/julia

It is a problem I posted on discourse, but I did not get an authoritative answer. I hope to get a better one posting as an issue.

I have a problem with cat. I am using cat all the time to construct block-diagonal matrices:

julia> m=[1 2;3 4]
2×2 Array{Int64,2}:
 1  2
 3  4

julia> cat(m,m;dims=(1,2))
4×4 Array{Int64,2}:
 1  2  0  0
 3  4  0  0
 0  0  1  2
 0  0  3  4

I am using polynomials

julia> using Polynomials

julia> q=Polynomial([0,1])
Polynomial(x)

julia> m=[q q^0;0 q^0]
2×2 Array{Polynomial{Int64},2}:
 Polynomial(x)  Polynomial(1)
 Polynomial(0)  Polynomial(1)

julia> cat(m,m;dims=(1,2))
4×4 Array{Polynomial{Int64},2}:
    Polynomial(x)     Polynomial(1)  #undef            #undef
    Polynomial(0)     Polynomial(1)  #undef            #undef
 #undef            #undef               Polynomial(x)     Polynomial(1)
 #undef            #undef               Polynomial(0)     Polynomial(1)

I wonder why these #undef while Polynomials have a well-defined zero function:

julia> zero(q)
Polynomial(0)

I tracked down the problem to lines 1442–1444 of abstractarray.jl (julia1.4.2):

  if T <: Number && count(!iszero, catdims) > 1
        fill!(A, zero(T))
    end

Is not the test T<:Number wrong? Should not the right test be that T has the method zero ("duck-typing")?

Most helpful comment

I think we should require zero to be defined (i.e. just call it) if there would otherwise be uninitialized entries.

>All comments

I think we should require zero to be defined (i.e. just call it) if there would otherwise be uninitialized entries.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

StefanKarpinski picture StefanKarpinski  Â·  3Comments

arshpreetsingh picture arshpreetsingh  Â·  3Comments

yurivish picture yurivish  Â·  3Comments

TotalVerb picture TotalVerb  Â·  3Comments

Keno picture Keno  Â·  3Comments