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

Merged
merged 3 commits into from
Oct 20, 2024

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.12%. Comparing base (313a04f) to head (dbe4321).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #562      +/-   ##
==========================================
+ Coverage   84.09%   84.12%   +0.03%     
==========================================
  Files          12       12              
  Lines        9102     9120      +18     
==========================================
+ Hits         7654     7672      +18     
  Misses       1448     1448              

☔ 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.

@jishnub
Copy link
Contributor Author

jishnub commented Oct 4, 2024

I've updated the method for a SparseMatrixCSC. The fallback method handles views and adjoint/transpose.

@jishnub jishnub merged commit 70c06b1 into main Oct 20, 2024
10 checks passed
@jishnub jishnub deleted the jishnub/diag_triple_product branch October 20, 2024 05:53
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