Skip to content

Commit a724b99

Browse files
committed
change the trunctation scheme
1 parent 9445c6b commit a724b99

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/tensors/truncation.jl

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,37 @@ end
6262
function _compute_truncdim(Σdata, trunc::TruncationDimension, p=2)
6363
I = keytype(Σdata)
6464
truncdim = SectorDict{I,Int}(c => length(v) for (c, v) in Σdata)
65-
while sum(dim(c) * d for (c, d) in truncdim) > trunc.dim
65+
S = []
66+
for (c, v) in Σdata
67+
for i in 1:dim(c)
68+
push!(S, v)
69+
end
70+
end
71+
72+
S = sort(vcat(S...); by=x -> -x)
73+
74+
n = trunc.dim
75+
76+
cutoff = eps(scalartype(S))
77+
deg_threshold = 1.0 - sqrt(cutoff)
78+
n_above_cutoff = count(>=(cutoff), S / S[1])
79+
n = Int(min(n, n_above_cutoff))
80+
if n > 0 && n < length(S)
81+
82+
# 从初始截断点开始向后检查
83+
last_index = n
84+
for i in n:(length(S) - 1)
85+
# 如果下一个奇异值与当前奇异值非常接近
86+
if S[i + 1] >= S[i] * deg_threshold
87+
last_index = i + 1
88+
else
89+
# 一旦发现差异足够大的相邻奇异值,停止扩展
90+
break
91+
end
92+
end
93+
n = last_index
94+
end
95+
while sum(dim(c) * d for (c, d) in truncdim) > n
6696
cmin = _findnexttruncvalue(Σdata, truncdim, p)
6797
isnothing(cmin) && break
6898
truncdim[cmin] -= 1

0 commit comments

Comments
 (0)