|
1 | 1 | using MatrixAlgebraKit:
|
2 | 2 | MatrixAlgebraKit, default_eig_algorithm, default_eigh_algorithm, eig_full!, eigh_full!
|
3 | 3 |
|
4 |
| -function MatrixAlgebraKit.default_eig_algorithm( |
5 |
| - arrayt::Type{<:AbstractBlockSparseMatrix}; kwargs... |
| 4 | +function initialize_blocksparse_eig_output( |
| 5 | + f, A::AbstractMatrix, alg::BlockPermutedDiagonalAlgorithm |
6 | 6 | )
|
7 |
| - alg = default_eig_algorithm(blocktype(arrayt); kwargs...) |
8 |
| - return BlockPermutedDiagonalAlgorithm(alg) |
9 |
| -end |
10 |
| - |
11 |
| -function MatrixAlgebraKit.initialize_output( |
12 |
| - ::typeof(eig_full!), A::AbstractBlockSparseMatrix, alg::BlockPermutedDiagonalAlgorithm |
13 |
| -) |
14 |
| - D = similar(A, complex(eltype(A))) |
15 |
| - V = similar(A, complex(eltype(A))) |
| 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)) |
16 | 10 | return (D, V)
|
17 | 11 | end
|
18 | 12 |
|
19 |
| -function MatrixAlgebraKit.eig_full!( |
20 |
| - A::AbstractBlockSparseMatrix, (D, V), alg::BlockPermutedDiagonalAlgorithm |
| 13 | +function blocksparse_eig_full!( |
| 14 | + f, A::AbstractMatrix, (D, V), alg::BlockPermutedDiagonalAlgorithm |
21 | 15 | )
|
22 | 16 | for I in blockdiagindices(A)
|
23 |
| - d, v = eig_full!(A[I], alg.alg) |
24 |
| - D[I] = d |
25 |
| - V[I] = v |
| 17 | + d, v = f(@view!(A[I]), alg.alg) |
| 18 | + D[I], V[I] = d, v |
26 | 19 | end
|
27 | 20 | return (D, V)
|
28 | 21 | end
|
29 | 22 |
|
30 |
| -function MatrixAlgebraKit.default_eigh_algorithm( |
31 |
| - arrayt::Type{<:AbstractBlockSparseMatrix}; kwargs... |
32 |
| -) |
33 |
| - alg = default_eigh_algorithm(blocktype(arrayt); kwargs...) |
34 |
| - return BlockPermutedDiagonalAlgorithm(alg) |
35 |
| -end |
36 |
| - |
37 |
| -function MatrixAlgebraKit.initialize_output( |
38 |
| - ::typeof(eigh_full!), A::AbstractBlockSparseMatrix, alg::BlockPermutedDiagonalAlgorithm |
39 |
| -) |
40 |
| - D = similar(A, complex(eltype(A))) |
41 |
| - V = similar(A, complex(eltype(A))) |
42 |
| - return (D, V) |
| 23 | +for f in [:default_eig_algorithm, :default_eigh_algorithm] |
| 24 | + @eval begin |
| 25 | + function MatrixAlgebraKit.$f(arrayt::Type{<:AbstractBlockSparseMatrix}; kwargs...) |
| 26 | + alg = $f(blocktype(arrayt); kwargs...) |
| 27 | + return BlockPermutedDiagonalAlgorithm(alg) |
| 28 | + end |
| 29 | + end |
43 | 30 | end
|
44 | 31 |
|
45 |
| -function MatrixAlgebraKit.eigh_full!( |
46 |
| - A::AbstractBlockSparseMatrix, (D, V), alg::BlockPermutedDiagonalAlgorithm |
47 |
| -) |
48 |
| - for I in blockdiagindices(A) |
49 |
| - d, v = eigh_full!(A[I], alg.alg) |
50 |
| - D[I] = d |
51 |
| - V[I] = v |
| 32 | +for f in [:eig_full!, :eigh_full!] |
| 33 | + @eval begin |
| 34 | + function MatrixAlgebraKit.initialize_output( |
| 35 | + ::typeof($f), A::AbstractBlockSparseMatrix, alg::BlockPermutedDiagonalAlgorithm |
| 36 | + ) |
| 37 | + return initialize_blocksparse_eig_output($f, A, alg) |
| 38 | + end |
| 39 | + function MatrixAlgebraKit.$f( |
| 40 | + A::AbstractBlockSparseMatrix, (D, V), alg::BlockPermutedDiagonalAlgorithm |
| 41 | + ) |
| 42 | + return blocksparse_eig_full!($f, A, (D, V), alg) |
| 43 | + end |
52 | 44 | end
|
53 |
| - return (D, V) |
54 | 45 | end
|
0 commit comments