JuliaArrays / JuliaArrays/FillArrays.jl

Operations with Eye do not preserve sparsity

Open
#265 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
187
Forks
41
Avg merge
17h 29m
Merged PRs (30d)
1

Description

Hi, I've noticed that SquareEye, since it is a LinearAlgebra.Diagonal preserves sparsity in many operations. For example:

julia> using FillArrays

julia> using SparseArrays

julia> E = Eye(2)
2×2 Eye{Float64}

julia> M = sprand(2,2, 0.5)
2×2 SparseMatrixCSC{Float64, Int64} with 3 stored entries:
 0.906128   0.408236
 0.0285949   ⋅ 

julia> E + M
2×2 SparseMatrixCSC{Float64, Int64} with 4 stored entries:
 1.90613    0.408236
 0.0285949  1.0

julia> kron(E, E)  # although it would be nice is this were another SquareEye!
4×4 Diagonal{Float64, Vector{Float64}}:
 1.0   ⋅    ⋅    ⋅ 
  ⋅   1.0   ⋅    ⋅ 
  ⋅    ⋅   1.0   ⋅ 
  ⋅    ⋅    ⋅   1.0

However, general Eye typically converts to a dense matrix:

julia> E = Eye(2,2)
2×2 Eye{Float64}

julia> E + M
2×2 Matrix{Float64}:
 1.90613    0.408236
 0.0285949  1.0

julia> kron(E,E)
4×4 Matrix{Float64}:
 1.0  0.0  0.0  0.0
 0.0  1.0  0.0  0.0
 0.0  0.0  1.0  0.0
 0.0  0.0  0.0  1.0

This was quite surprising to me. Could we add some operator overloads to keep things sparse in the Eye case?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names Eye, SquareEye, +, and kron but no files or tests; inspect the Eye and SquareEye definitions and existing operator methods first. Reproduce the supplied Julia examples, then add focused coverage showing that Eye operations retain sparse representations where appropriate and that the resulting behavior is consistent.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
data
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.