Skip to content

Commit 36d9fba

Browse files
committed
Start implementing truncation
1 parent e3bc69a commit 36d9fba

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/factorizations/eig.jl

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
using BlockArrays: blocksizes
2-
using LinearAlgebra: LinearAlgebra
2+
using DiagonalArrays: diagonal
3+
using LinearAlgebra: LinearAlgebra, Diagonal
34
using MatrixAlgebraKit:
45
MatrixAlgebraKit,
6+
TruncationStrategy,
57
default_eig_algorithm,
68
default_eigh_algorithm,
9+
diagview,
710
eig_full!,
11+
eig_trunc!,
812
eig_vals!,
913
eigh_full!,
10-
eigh_vals!
14+
eigh_trunc!,
15+
eigh_vals!,
16+
findtruncated
1117

1218
for f in [:default_eig_algorithm, :default_eigh_algorithm]
1319
@eval begin
@@ -37,7 +43,7 @@ for f in [:eig_full!, :eigh_full!]
3743
for I in eachunstoredblockdiagindex(A)
3844
# TODO: Support setting `LinearAlgebra.I` directly, and/or
3945
# using `FillArrays.Eye`.
40-
V[I] = LinearAlgebra.I(first(blocksizes(A)[Int.(Tuple(I))...]))
46+
V[I] = LinearAlgebra.I(size(@view(V[I]), 1))
4147
end
4248
return (D, V)
4349
end
@@ -61,3 +67,24 @@ for f in [:eig_vals!, :eigh_vals!]
6167
end
6268
end
6369
end
70+
71+
const TBlockDV = Tuple{AbstractBlockSparseMatrix,AbstractBlockSparseMatrix}
72+
73+
for f in [:eig_trunc!, :eigh_trunc!]
74+
@eval begin
75+
function MatrixAlgebraKit.truncate!(
76+
::typeof($f), (D, V)::TBlockDV, strategy::TruncationStrategy
77+
)
78+
return MatrixAlgebraKit.truncate!(
79+
$f, (D, V), BlockPermutedDiagonalTruncationStrategy(strategy)
80+
)
81+
end
82+
function MatrixAlgebraKit.truncate!(
83+
::typeof($f), (D, V)::TBlockDV, strategy::BlockPermutedDiagonalTruncationStrategy
84+
)
85+
d = diagview(D)
86+
ind = findtruncated(d, strategy)
87+
return diagonal(d[ind]), V[:, ind]
88+
end
89+
end
90+
end

src/factorizations/truncation.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct BlockPermutedDiagonalTruncationStrategy{T<:TruncationStrategy} <: Truncat
2222
end
2323

2424
const TBlockUSVᴴ = Tuple{
25-
<:AbstractBlockSparseMatrix,<:AbstractBlockSparseMatrix,<:AbstractBlockSparseMatrix
25+
AbstractBlockSparseMatrix,AbstractBlockSparseMatrix,AbstractBlockSparseMatrix
2626
}
2727

2828
function MatrixAlgebraKit.truncate!(

0 commit comments

Comments
 (0)