MichielStock / Kronecker.jl

A general-purpose toolbox for efficient Kronecker-based algebra.
MIT License
86 stars 14 forks source link

Optimized diag for square matrices and diagonal matrix multiplication #105

Closed jishnub closed 2 years ago

jishnub commented 3 years ago

These methods avoids allocations while multiplying diagonal matrices:

On master:

julia> D1 = Diagonal(ones(300));

julia> K = kronecker(D1, D1);

julia> D = Diagonal(ones(size(K,2)));

julia> K * D;
ERROR: OutOfMemoryError()

julia> K + D;
ERROR: OutOfMemoryError()

After this PR:

julia> @btime $K * $D;
  229.064 μs (12 allocations: 1.38 MiB)

julia> @btime $K + $D;
  225.001 μs (12 allocations: 1.38 MiB)

julia> @btime $K + I;
  187.425 μs (12 allocations: 1.38 MiB)

There's also an optimized diag for square matrices, using the Kronecker product of the diagonals.

Also fixed certain method ambiguities in multiplications with triangular matrices.

codecov-commenter commented 3 years ago

Codecov Report

Merging #105 (48fca80) into master (ca6f2ad) will decrease coverage by 0.06%. The diff coverage is 89.18%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #105      +/-   ##
==========================================
- Coverage   90.36%   90.30%   -0.07%     
==========================================
  Files          10       10              
  Lines         758      784      +26     
==========================================
+ Hits          685      708      +23     
- Misses         73       76       +3     
Impacted Files Coverage Δ
src/kroneckerpowers.jl 89.65% <75.00%> (-3.53%) :arrow_down:
src/base.jl 92.72% <100.00%> (+0.25%) :arrow_up:
src/vectrick.jl 94.81% <100.00%> (+0.11%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update ca6f2ad...48fca80. Read the comment docs.

jishnub commented 2 years ago

gentle bump, would be nice to have this

MichielStock commented 2 years ago

Sorry, lost in the pile of unread emails. Great job!