Skip to content

Commit 4dc3fb5

Browse files
committed
Add svdvals and eigvals
1 parent 04964b7 commit 4dc3fb5

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/TensorAlgebra.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module TensorAlgebra
22

3-
export contract, contract!, eig, lq, qr, svd
3+
export contract, contract!, eig, eigvals, lq, qr, svd, svdvals
44

55
include("blockedtuple.jl")
66
include("blockedpermutation.jl")

src/factorizations.jl

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,30 @@ function eig(
112112
return D, splitdims(V, axes_V)
113113
end
114114

115+
"""
116+
eigvals(A::AbstractArray, labels_A, labels_codomain, labels_domain; kwargs...) -> D
117+
eigvals(A::AbstractArray, biperm::BlockedPermutation{2}; kwargs...) -> D
118+
119+
Compute the eigenvalues of a generic N-dimensional array, by interpreting it as
120+
a linear map from the domain to the codomain indices. These can be specified either via
121+
their labels, or directly through a `biperm`. The output is a vector of eigenvalues.
122+
123+
## Keyword arguments
124+
125+
- `ishermitian::Bool`: specify if the matrix is Hermitian, which can be used to speed up the
126+
computation. If `false`, the output `eltype` will always be `<:Complex`.
127+
- Other keywords are passed on directly to MatrixAlgebraKit
128+
"""
129+
function eigvals(A::AbstractArray, labels_A, labels_codomain, labels_domain; kwargs...)
130+
biperm = blockedperm_indexin(Tuple.((labels_A, labels_codomain, labels_domain))...)
131+
return eigvals(A, biperm; kwargs...)
132+
end
133+
function eigvals(A::AbstractArray, biperm::BlockedPermutation{2}; kwargs...)
134+
A_mat = fusedims(A, biperm)
135+
ishermitian = @something ishermitian LinearAlgebra.ishermitian(A_mat)
136+
return (ishermitian ? eigh_vals : eig_vals)(A_mat; kwargs...)
137+
end
138+
115139
# TODO: separate out the algorithm selection step from the implementation
116140
"""
117141
svd(A::AbstractArray, labels_A, labels_codomain, labels_domain; kwargs...) -> U, S, Vᴴ
@@ -156,3 +180,20 @@ function svd(
156180
axes_Vᴴ = (axes(Vᴴ, 1), axes_domain...)
157181
return splitdims(U, axes_U), S, splitdims(Vᴴ, axes_Vᴴ)
158182
end
183+
184+
"""
185+
svdvals(A::AbstractArray, labels_A, labels_codomain, labels_domain) -> S
186+
svdvals(A::AbstractArray, biperm::BlockedPermutation{2}) -> S
187+
188+
Compute the singular values of a generic N-dimensional array, by interpreting it as
189+
a linear map from the domain to the codomain indices. These can be specified either via
190+
their labels, or directly through a `biperm`. The output is a vector of singular values.
191+
"""
192+
function svdvals(A::AbstractArray, labels_A, labels_codomain, labels_domain)
193+
biperm = blockedperm_indexin(Tuple.((labels_A, labels_codomain, labels_domain))...)
194+
return svdvals(A, biperm)
195+
end
196+
function svdvals(A::AbstractArray, biperm::BlockedPermutation{2})
197+
A_mat = fusedims(A, biperm)
198+
return svd_vals(A_mat)
199+
end

0 commit comments

Comments
 (0)