Skip to content

Commit e3bc69a

Browse files
committed
Define block sparse eig_vals
1 parent 8af8e5b commit e3bc69a

File tree

2 files changed

+43
-22
lines changed

2 files changed

+43
-22
lines changed

src/blocksparsearrayinterface/blocksparsearrayinterface.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ end
4545
function eachstoredblockdiagindex(a::AbstractArray)
4646
return eachblockstoredindex(a) blockdiagindices(a)
4747
end
48+
function eachunstoredblockdiagindex(a::AbstractArray)
49+
return setdiff(blockdiagindices(a), eachblockstoredindex(a))
50+
end
4851

4952
# Like `BlockArrays.eachblock` but only iterating
5053
# over stored blocks.

src/factorizations/eig.jl

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,13 @@
1+
using BlockArrays: blocksizes
2+
using LinearAlgebra: LinearAlgebra
13
using MatrixAlgebraKit:
2-
MatrixAlgebraKit, default_eig_algorithm, default_eigh_algorithm, eig_full!, eigh_full!
3-
4-
function initialize_blocksparse_eig_output(
5-
f, A::AbstractMatrix, alg::BlockPermutedDiagonalAlgorithm
6-
)
7-
Td, Tv = fieldtypes(Base.promote_op(f, blocktype(A), typeof(alg.alg)))
8-
D = similar(A, BlockType(Td))
9-
V = similar(A, BlockType(Tv))
10-
return (D, V)
11-
end
12-
13-
function blocksparse_eig_full!(
14-
f, A::AbstractMatrix, (D, V), alg::BlockPermutedDiagonalAlgorithm
15-
)
16-
for I in blockdiagindices(A)
17-
d, v = f(@view!(A[I]), alg.alg)
18-
D[I], V[I] = d, v
19-
end
20-
return (D, V)
21-
end
4+
MatrixAlgebraKit,
5+
default_eig_algorithm,
6+
default_eigh_algorithm,
7+
eig_full!,
8+
eig_vals!,
9+
eigh_full!,
10+
eigh_vals!
2211

2312
for f in [:default_eig_algorithm, :default_eigh_algorithm]
2413
@eval begin
@@ -34,12 +23,41 @@ for f in [:eig_full!, :eigh_full!]
3423
function MatrixAlgebraKit.initialize_output(
3524
::typeof($f), A::AbstractBlockSparseMatrix, alg::BlockPermutedDiagonalAlgorithm
3625
)
37-
return initialize_blocksparse_eig_output($f, A, alg)
26+
Td, Tv = fieldtypes(Base.promote_op($f, blocktype(A), typeof(alg.alg)))
27+
D = similar(A, BlockType(Td))
28+
V = similar(A, BlockType(Tv))
29+
return (D, V)
3830
end
3931
function MatrixAlgebraKit.$f(
4032
A::AbstractBlockSparseMatrix, (D, V), alg::BlockPermutedDiagonalAlgorithm
4133
)
42-
return blocksparse_eig_full!($f, A, (D, V), alg)
34+
for I in eachstoredblockdiagindex(A)
35+
D[I], V[I] = $f(@view(A[I]), alg.alg)
36+
end
37+
for I in eachunstoredblockdiagindex(A)
38+
# TODO: Support setting `LinearAlgebra.I` directly, and/or
39+
# using `FillArrays.Eye`.
40+
V[I] = LinearAlgebra.I(first(blocksizes(A)[Int.(Tuple(I))...]))
41+
end
42+
return (D, V)
43+
end
44+
end
45+
end
46+
47+
for f in [:eig_vals!, :eigh_vals!]
48+
@eval begin
49+
function MatrixAlgebraKit.initialize_output(
50+
::typeof($f), A::AbstractBlockSparseMatrix, alg::BlockPermutedDiagonalAlgorithm
51+
)
52+
return similar(A, axes(A, 1))
53+
end
54+
function MatrixAlgebraKit.$f(
55+
A::AbstractBlockSparseMatrix, D, alg::BlockPermutedDiagonalAlgorithm
56+
)
57+
for I in eachblockstoredindex(A)
58+
D[I] = $f(@view!(A[I]), alg.alg)
59+
end
60+
return D
4361
end
4462
end
4563
end

0 commit comments

Comments
 (0)