Skip to content

Commit b5a3ab5

Browse files
authored
norm performance optimization (#351)
* specialize UniqueFusion norm * replace `sum` with manual loop because less allocations for some reason
1 parent 68cff49 commit b5a3ab5

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/tensors/linalg.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,22 @@ function _norm(blockiter, p::Real, init::Real)
269269
return isempty(b) ? init : oftype(init, LinearAlgebra.normInf(b))
270270
end
271271
elseif p > 0 # finite positive p
272-
np = sum(blockiter; init) do (c, b)
273-
return oftype(init, dim(c) * norm(b, p)^p)
272+
np = init
273+
for (c, b) in blockiter
274+
np += oftype(init, dim(c) * norm(b, p)^p)
274275
end
275276
return np^(inv(oftype(np, p)))
276277
else
277278
msg = "Norm with non-positive p is not defined for `AbstractTensorMap`"
278279
throw(ArgumentError(msg))
279280
end
280281
end
282+
function LinearAlgebra.norm(t::TensorMap, p::Real = 2)
283+
InnerProductStyle(t) === EuclideanInnerProduct() || throw_invalid_innerproduct(:norm)
284+
# performance specialization:
285+
FusionStyle(sectortype(t)) isa UniqueFusion && return norm(t.data, p)
286+
return _norm(blocks(t), p, float(zero(real(scalartype(t)))))
287+
end
281288

282289
_default_rtol(t) = eps(real(float(scalartype(t)))) * min(dim(domain(t)), dim(codomain(t)))
283290

0 commit comments

Comments
 (0)