Skip to content

iterator for diagonal of a matrix ? #30250

@jlapeyre

Description

@jlapeyre

This is so obvious that I guess it exists elsewhere, but I don't know about it. But, if not, maybe it belongs (after some modifications) in LinearAlgebra ? ... Or some other package ?

A prototype iterator over the diagonal of a matrix is in this package.

It's called diagonal. It's like diag but does very little allocation.

julia> N = 1000;

julia> m = rand(N, N);

julia> @btime sum(diag($m));         # copy
  1.718 μs (1 allocation: 7.94 KiB)

julia> @btime sum(diagonal($m));    # no copy
  936.793 ns (1 allocation: 32 bytes)

Off diagonals are not yet supported.

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