Skip to content

Commit 3abe711

Browse files
committed
Add cond
1 parent 7d4405d commit 3abe711

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/tensors/linalg.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,21 @@ function LinearAlgebra.rank(t::AbstractTensorMap; atol::Real=0,
281281
return sum(((c, s),) -> count(>(tol), s), values(S))
282282
end
283283

284+
function LinearAlgebra.cond(t::AbstractTensorMap, p::Real=2)
285+
if p == 2
286+
if dim(t) == 0
287+
domain(t) == codomain(t) ||
288+
throw(SpaceMismatch("`cond` requires domain and codomain to be the same"))
289+
return zero(real(float(scalartype(t))))
290+
end
291+
v = svdvals(t)
292+
maxv = maximum(first, values(v))
293+
return iszero(maxv) ? oftype(maxv, Inf) : maxv / minimum(last, values(v))
294+
else
295+
throw(ArgumentError("cond currently only defined for p=2"))
296+
end
297+
end
298+
284299
# TensorMap trace
285300
function LinearAlgebra.tr(t::AbstractTensorMap)
286301
domain(t) == codomain(t) ||

0 commit comments

Comments
 (0)