From e4aa683a530ca8726b655d646aefff208711adfb Mon Sep 17 00:00:00 2001 From: Yue Zhengyuan Date: Sun, 8 Feb 2026 13:42:28 +0800 Subject: [PATCH 1/2] Short-circuit truncrank to notrunc when kept rank is larger than input --- src/factorizations/truncation.jl | 6 ++++++ test/tensors/factorizations.jl | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/src/factorizations/truncation.jl b/src/factorizations/truncation.jl index e8e113ec1..d45a9d04b 100644 --- a/src/factorizations/truncation.jl +++ b/src/factorizations/truncation.jl @@ -171,6 +171,9 @@ function MAK.findtruncated(values::SectorVector, strategy::TruncationByOrder) # dimensions are all 1 so no need to account for weight if FusionStyle(I) isa UniqueFusion + if length(parent(values)) <= strategy.howmany + return MAK.findtruncated(values, NoTruncation()) + end perm = partialsortperm(parent(values), 1:strategy.howmany; strategy.by, strategy.rev) result = similar(values, Bool) fill!(parent(result), false) @@ -183,6 +186,9 @@ function MAK.findtruncated(values::SectorVector, strategy::TruncationByOrder) for (c, v) in pairs(dims) fill!(v, dim(c)) end + if sum(dims) <= strategy.howmany + return MAK.findtruncated(values, NoTruncation()) + end # allocate logical array for the output result = similar(values, Bool) diff --git a/test/tensors/factorizations.jl b/test/tensors/factorizations.jl index 7e6510b6c..48dfaf6f1 100644 --- a/test/tensors/factorizations.jl +++ b/test/tensors/factorizations.jl @@ -254,6 +254,14 @@ for V in spacelist @test isisometric(U) @test isisometric(Vᴴ; side = :right) + # when rank of t is already smaller than truncrank + t_rank = ceil(Int, min(dim(codomain(t)), dim(domain(t)))) + U, S, Vᴴ, ϵ = @constinferred svd_trunc(t; trunc = truncrank(t_rank + 1)) + @test U * S * Vᴴ ≈ t + @test ϵ ≈ 0 + @test isisometric(U) + @test isisometric(Vᴴ; side = :right) + # dimension of S is a float for IsingBimodule nvals = round(Int, dim(domain(S)) / 2) trunc = truncrank(nvals) From 9f4a3394a91ce338a8323334e735538a14d4a047 Mon Sep 17 00:00:00 2001 From: Yue Zhengyuan Date: Sun, 8 Feb 2026 13:56:26 +0800 Subject: [PATCH 2/2] Consider strategy.rev --- src/factorizations/truncation.jl | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/factorizations/truncation.jl b/src/factorizations/truncation.jl index d45a9d04b..ff9c8a2e6 100644 --- a/src/factorizations/truncation.jl +++ b/src/factorizations/truncation.jl @@ -171,10 +171,8 @@ function MAK.findtruncated(values::SectorVector, strategy::TruncationByOrder) # dimensions are all 1 so no need to account for weight if FusionStyle(I) isa UniqueFusion - if length(parent(values)) <= strategy.howmany - return MAK.findtruncated(values, NoTruncation()) - end - perm = partialsortperm(parent(values), 1:strategy.howmany; strategy.by, strategy.rev) + howmany = min(length(parent(values)), strategy.howmany) + perm = partialsortperm(parent(values), 1:howmany; strategy.by, strategy.rev) result = similar(values, Bool) fill!(parent(result), false) parent(result)[perm] .= true @@ -186,9 +184,6 @@ function MAK.findtruncated(values::SectorVector, strategy::TruncationByOrder) for (c, v) in pairs(dims) fill!(v, dim(c)) end - if sum(dims) <= strategy.howmany - return MAK.findtruncated(values, NoTruncation()) - end # allocate logical array for the output result = similar(values, Bool)