Skip to content

Cannot sum partially initialized Hermitian matrices that are not an isbits type #52895

@araujoms

Description

@araujoms

If T is an isbitstype, creating an uninitialized matrix a = Matrix{T}(undef,d,d) will initialize it with whatever is in memory, so the resulting matrix will be just a regular numerical matrix. Otherwise the matrix will be filled with #undef, and I'll get an error if I try to access them. So far so good.

I wanted to create, however, a Hermitian matrix, and initialize only the upper triangular. If I try to sum it with another Hermitian matrix, however, I get the error, even though I don't actually want to access the uninitialized elements. The code below shows what I mean, if you call it with matrix_sum(BigFloat).

Looking at the code of +, I see that it just adds the underlying parent and wraps it again with Hermitian, which by necessity accesses all elements. If one would sum instead only the upper (or lower) triangular this bug would be fixed, and presumably we would get a speedup as well.

A bit unrelated, but I noticed that GenericLinearAlgebra doesn't choke on such matrices, it can calculate the eigenvalues, for example.

using LinearAlgebra

function matrix_sum(T)
   d = 2
   a = Hermitian(Matrix{T}(undef,d,d))
   for i=1:d
       for j=i:d
           a.data[i,j] = T(1)
       end
   end
   b = a + a
end        

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions