@@ -119,14 +119,23 @@ function _compute_truncerr(Σdata, truncdim, p=2)
119
119
p, zero (S))
120
120
end
121
121
122
- function _findnexttruncvalue (S, truncdim:: SectorDict{I,Int} ) where {I<: Sector }
122
+ function _findnexttruncvalue (S, truncdim:: SectorDict{I,Int} ; by= identity,
123
+ rev:: Bool = true ) where {I<: Sector }
123
124
# early return
124
125
(isempty (S) || all (iszero, values (truncdim))) && return nothing
125
- σmin, imin = findmin (keys (truncdim)) do c
126
- d = truncdim[c]
127
- return S[c][d]
126
+ if rev
127
+ σmin, imin = findmin (keys (truncdim)) do c
128
+ d = truncdim[c]
129
+ return by (S[c][d])
130
+ end
131
+ return σmin, keys (truncdim)[imin]
132
+ else
133
+ σmax, imax = findmax (keys (truncdim)) do c
134
+ d = truncdim[c]
135
+ return by (S[c][d])
136
+ end
137
+ return σmax, keys (truncdim)[imax]
128
138
end
129
- return σmin, keys (truncdim)[imin]
130
139
end
131
140
132
141
# implementations
@@ -173,12 +182,18 @@ function findtruncated_sorted(Sd::SectorDict, strategy::TruncationError)
173
182
end
174
183
175
184
function findtruncated_sorted (Sd:: SectorDict , strategy:: TruncationKeepSorted )
176
- @assert strategy. by === abs && strategy. rev == true " Not implemented"
185
+ return findtruncated (Sd, strategy)
186
+ end
187
+ function findtruncated (Sd:: SectorDict , strategy:: TruncationKeepSorted )
188
+ permutations = SectorDict (c => (sortperm (d; strategy. by, strategy. rev))
189
+ for (c, d) in Sd)
190
+ Sd = SectorDict (c => sort (d; strategy. by, strategy. rev) for (c, d) in Sd)
191
+
177
192
I = keytype (Sd)
178
193
truncdim = SectorDict {I,Int} (c => length (d) for (c, d) in Sd)
179
194
totaldim = sum (dim (c) * d for (c, d) in truncdim; init= 0 )
180
195
while true
181
- next = _findnexttruncvalue (Sd, truncdim)
196
+ next = _findnexttruncvalue (Sd, truncdim; strategy . by, strategy . rev )
182
197
isnothing (next) && break
183
198
_, cmin = next
184
199
truncdim[cmin] -= 1
@@ -191,7 +206,7 @@ function findtruncated_sorted(Sd::SectorDict, strategy::TruncationKeepSorted)
191
206
delete! (truncdim, cmin)
192
207
end
193
208
end
194
- return SectorDict {I,Base.OneTo{Int}} (c => Base. OneTo (d) for (c, d) in truncdim)
209
+ return SectorDict (c => permutations[c][ Base. OneTo (d)] for (c, d) in truncdim)
195
210
end
196
211
197
212
function findtruncated_sorted (Sd:: SectorDict , strategy:: TruncationSpace )
0 commit comments