Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/implementations/truncation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,20 @@ end
# specific implementations for finding truncated values
findtruncated(values::AbstractVector, ::NoTruncation) = Colon()

function findtruncated(values::AbstractVector, strategy::TruncationKeepSorted)
if issorted(values; by=strategy.sortby, rev=strategy.rev)
return convert(Vector{Int}, findtruncated_sorted(values, strategy))
else
return findtruncated_unsorted(values, strategy)
end
end
function findtruncated_sorted(values::AbstractVector, strategy::TruncationKeepSorted)
howmany = min(strategy.howmany, length(values))
return 1:howmany
end
# TODO: this may also permute the eigenvalues, decide if we want to allow this or not
# can be solved by going to simply sorting the resulting `ind`
function findtruncated(values::AbstractVector, strategy::TruncationKeepSorted)
function findtruncated_unsorted(values::AbstractVector, strategy::TruncationKeepSorted)
Comment thread
mtfishman marked this conversation as resolved.
Outdated
sorted = sortperm(values; by=strategy.sortby, rev=strategy.rev)
howmany = min(strategy.howmany, length(sorted))
ind = sorted[1:howmany]
Expand All @@ -182,15 +193,38 @@ function findtruncated(values::AbstractVector, strategy::TruncationKeepFiltered)
end

function findtruncated(values::AbstractVector, strategy::TruncationKeepBelow)
if issorted(values; by=abs, rev=true)
return convert(Vector{Int}, findtruncated_sorted(values, strategy))
else
return findtruncated_unsorted(values, strategy)
end
end
function findtruncated_sorted(values::AbstractVector, strategy::TruncationKeepBelow)
atol = max(strategy.atol, strategy.rtol * first(values))
i = @something findfirst(≤(atol), values) length(values) + 1
return i:length(values)
end
Comment thread
mtfishman marked this conversation as resolved.
function findtruncated_unsorted(values::AbstractVector, strategy::TruncationKeepBelow)
atol = max(strategy.atol, strategy.rtol * maximum(values))
return findall(≤(atol), values)
end

function findtruncated(values::AbstractVector, strategy::TruncationKeepAbove)
if issorted(values; by=abs, rev=true)
return convert(Vector{Int}, findtruncated_sorted(values, strategy))
else
return findtruncated_unsorted(values, strategy)
end
end
function findtruncated_sorted(values::AbstractVector, strategy::TruncationKeepAbove)
atol = max(strategy.atol, strategy.rtol * first(values))
i = @something findlast(≥(atol), values) 0
return 1:i
end
Comment thread
mtfishman marked this conversation as resolved.
function findtruncated_unsorted(values::AbstractVector, strategy::TruncationKeepAbove)
atol = max(strategy.atol, strategy.rtol * maximum(values))
return findall(≥(atol), values)
end

function findtruncated(values::AbstractVector, strategy::TruncationIntersection)
inds = map(Base.Fix1(findtruncated, values), strategy.components)
Expand Down
12 changes: 10 additions & 2 deletions test/truncate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ using MatrixAlgebraKit
using Test
using TestExtras
using MatrixAlgebraKit: NoTruncation, TruncationIntersection, TruncationKeepAbove,
TruncationStrategy, findtruncated
TruncationKeepBelow, TruncationStrategy, findtruncated

@testset "truncate" begin
trunc = @constinferred TruncationStrategy()
Expand All @@ -28,7 +28,15 @@ using MatrixAlgebraKit: NoTruncation, TruncationIntersection, TruncationKeepAbov
@test trunc.components[2] == TruncationKeepAbove(1e-2, 1e-3)

values = [1, 0.9, 0.5, 0.3, 0.01]
@test @constinferred(findtruncated(values, truncrank(2))) == [1, 2]
@test @constinferred(findtruncated(values, truncrank(2))) == 1:2
@test @constinferred(findtruncated(values, truncrank(2; rev=false))) == [5, 4]
@test @constinferred(findtruncated(values, truncrank(2; by=-))) == [5, 4]

values = [1, 0.9, 0.5, 0.3, 0.01]
@test @constinferred(findtruncated(values, TruncationKeepAbove(0.4, 0.0))) == 1:3
@test @constinferred(findtruncated(values, TruncationKeepBelow(0.4, 0.0))) == 4:5

values = [0.01, 1, 0.9, 0.3, 0.5]
@test @constinferred(findtruncated(values, TruncationKeepAbove(0.4, 0.0))) == [2, 3, 5]
@test @constinferred(findtruncated(values, TruncationKeepBelow(0.4, 0.0))) == [1, 4]
end
Loading