Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diagonal-sandwiched triple product for SparseMatrixCSC #562

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jishnub
Copy link
Contributor

@jishnub jishnub commented Oct 2, 2024

After this,

julia> using SparseArrays, LinearAlgebra

julia> D = Diagonal(1:4)
4×4 Diagonal{Int64, UnitRange{Int64}}:
 1      
   2    
     3  
       4

julia> S = sprand(4, 4, 0.2)
4×4 SparseMatrixCSC{Float64, Int64} with 6 stored entries:
 0.8632  0.206049           0.921636
                 0.49266     
                 0.329707    
                          0.69844

julia> D * S * D
4×4 SparseMatrixCSC{Float64, Int64} with 6 stored entries:
 0.8632  0.412098           3.68655
                 2.95596     
                 2.96736     
                         11.175

This operation of pre- and post-multiplication by Diagonals is not uncommon, as it scales the rows and columns of the sandwiched matrix. After this PR, the result is a sparse matrix as well.

The new implementation changes the following behavior:

julia> D = Diagonal(StepRangeLen(NaN, 0, 4));

julia> D * S * D
4×4 SparseMatrixCSC{Float64, Int64} with 3 stored entries:
                   
           NaN      
           NaN      
 NaN                

whereas, previously, this would have been a dense matrix of NaNs. This new behavior is consistent with sparse matrices having a strong zero.

The implementation isn't optimal from a performance perspective, as it uses a sequence of multiplications instead of doing it all in one go. However, the performance may be improved iteratively.

Copy link

codecov bot commented Oct 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 84.13%. Comparing base (313a04f) to head (acc046f).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #562      +/-   ##
==========================================
+ Coverage   84.09%   84.13%   +0.04%     
==========================================
  Files          12       12              
  Lines        9102     9101       -1     
==========================================
+ Hits         7654     7657       +3     
+ Misses       1448     1444       -4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dkarrasch
Copy link
Member

I'd suggest to write it all out in one pass through the nzvals.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants