11using MatrixAlgebraKit
22using Test
33using TestExtras
4- using MatrixAlgebraKit: NoTruncation, TruncationIntersection, TruncationKeepAbove ,
5- TruncationKeepBelow , TruncationStrategy, findtruncated,
4+ using MatrixAlgebraKit: NoTruncation, TruncationIntersection, TruncationByOrder ,
5+ TruncationByValue , TruncationStrategy, findtruncated,
66 findtruncated_sorted
77
88@testset " truncate" begin
99 trunc = @constinferred TruncationStrategy ()
1010 @test trunc isa NoTruncation
1111
12- trunc = @constinferred TruncationStrategy (; atol= 1e-2 , rtol= 1e-3 )
13- @test trunc isa TruncationKeepAbove
14- @test trunc == TruncationKeepAbove (1e-2 , 1e-3 )
15- @test trunc. atol == 1e-2
16- @test trunc. rtol == 1e-3
12+ atol = 1e-2
13+ rtol = 1e-3
14+ maxrank = 10
1715
18- trunc = @constinferred TruncationStrategy (; maxrank= 10 )
19- @test trunc isa TruncationKeepSorted
20- @test trunc == truncrank (10 )
21- @test trunc. howmany == 10
16+ trunc = @constinferred TruncationStrategy (; atol, rtol)
17+ @test trunc isa TruncationByValue
18+ @test trunc == trunctol (; atol, rtol)
19+ @test trunc. atol == atol
20+ @test trunc. rtol == rtol
21+ @test trunc. rev
22+
23+ trunc = @constinferred TruncationStrategy (; maxrank)
24+ @test trunc isa TruncationByOrder
25+ @test trunc == truncrank (maxrank)
26+ @test trunc. howmany == maxrank
2227 @test trunc. by == abs
23- @test trunc. rev == true
28+ @test trunc. rev
2429
25- trunc = @constinferred TruncationStrategy (; atol= 1e-2 , rtol= 1e-3 , maxrank= 10 )
30+ trunc = @constinferred TruncationStrategy (; atol, rtol, maxrank)
2631 @test trunc isa TruncationIntersection
27- @test trunc == truncrank (10 ) & TruncationKeepAbove (1e-2 , 1e-3 )
28- @test trunc. components[1 ] == truncrank (10 )
29- @test trunc. components[2 ] == TruncationKeepAbove (1e-2 , 1e-3 )
32+ @test trunc == truncrank (maxrank) & trunctol (; atol, rtol)
3033
3134 values = [1 , 0.9 , 0.5 , - 0.3 , 0.01 ]
3235 @test @constinferred (findtruncated (values, truncrank (2 ))) == 1 : 2
@@ -35,42 +38,32 @@ using MatrixAlgebraKit: NoTruncation, TruncationIntersection, TruncationKeepAbov
3538 @test @constinferred (findtruncated_sorted (values, truncrank (2 ))) === 1 : 2
3639
3740 values = [1 , 0.9 , 0.5 , - 0.3 , 0.01 ]
38- for strategy in (TruncationKeepAbove (; atol= 0.4 , rtol= 0 ),
39- TruncationKeepAbove (0.4 , 0 ))
40- @test @constinferred (findtruncated (values, strategy)) == 1 : 3
41- @test @constinferred (findtruncated_sorted (values, strategy)) === 1 : 3
42- end
43- for strategy in (TruncationKeepBelow (; atol= 0.4 , rtol= 0 ),
44- TruncationKeepBelow (0.4 , 0 ))
45- @test @constinferred (findtruncated (values, strategy)) == 4 : 5
46- @test @constinferred (findtruncated_sorted (values, strategy)) === 4 : 5
47- end
41+ strategy = trunctol (; atol= 0.4 )
42+ @test @constinferred (findtruncated (values, strategy)) == 1 : 3
43+ @test @constinferred (findtruncated_sorted (values, strategy)) === 1 : 3
44+ strategy = trunctol (; atol= 0.4 , rev= false )
45+ @test @constinferred (findtruncated (values, strategy)) == 4 : 5
46+ @test @constinferred (findtruncated_sorted (values, strategy)) === 4 : 5
4847
4948 values = [0.01 , 1 , 0.9 , - 0.3 , 0.5 ]
50- for strategy in (TruncationKeepAbove (; atol= 0.4 , rtol= 0 ),
51- TruncationKeepAbove (; atol= 0.4 , rtol= 0 , by= abs),
52- TruncationKeepAbove (0.4 , 0 ),
53- TruncationKeepAbove (; atol= 0.2 , rtol= 0.0 , by= identity))
49+ for strategy in (trunctol (; atol= 0.4 ), trunctol (; atol= 0.2 , by= identity))
5450 @test @constinferred (findtruncated (values, strategy)) == [2 , 3 , 5 ]
5551 end
56- for strategy in (TruncationKeepAbove (; atol= 0.2 , rtol= 0 ),
57- TruncationKeepAbove (; atol= 0.2 , rtol= 0 , by= abs),
58- TruncationKeepAbove (0.2 , 0 ))
59- @test @constinferred (findtruncated (values, strategy)) == [2 , 3 , 4 , 5 ]
60- end
61- for strategy in (TruncationKeepBelow (; atol= 0.4 , rtol= 0 ),
62- TruncationKeepBelow (; atol= 0.4 , rtol= 0 , by= abs),
63- TruncationKeepBelow (0.4 , 0 ),
64- TruncationKeepBelow (; atol= 0.2 , rtol= 0.0 , by= identity))
52+ strategy = trunctol (; atol= 0.2 )
53+ @test @constinferred (findtruncated (values, strategy)) == [2 , 3 , 4 , 5 ]
54+
55+ for strategy in
56+ (trunctol (; atol= 0.4 , rev= false ), trunctol (; atol= 0.2 , by= identity, rev= false ))
6557 @test @constinferred (findtruncated (values, strategy)) == [1 , 4 ]
6658 end
67- for strategy in (TruncationKeepBelow (; atol= 0.2 , rtol= 0 ),
68- TruncationKeepBelow (; atol= 0.2 , rtol= 0 , by= abs),
69- TruncationKeepBelow (0.2 , 0 ))
70- @test @constinferred (findtruncated (values, strategy)) == [1 ]
71- end
72- for strategy in (truncerror (; atol= 0.2 , rtol= 0 ),)
73- @test issetequal (@constinferred (findtruncated (values, strategy)), 2 : 5 )
74- @test @constinferred (findtruncated_sorted (sort (values; by= abs, rev= true ), strategy)) == 1 : 4
75- end
59+ strategy = trunctol (; atol= 0.2 , rev= false )
60+ @test @constinferred (findtruncated (values, strategy)) == [1 ]
61+
62+ strategy = truncfilter (x -> 0.1 < x < 1 )
63+ @test @constinferred (findtruncated (values, strategy)) == [3 , 5 ]
64+
65+ strategy = truncerror (; atol= 0.2 , rtol= 0 )
66+ @test issetequal (@constinferred (findtruncated (values, strategy)), 2 : 5 )
67+ vals_sorted = sort (values; by= abs, rev= true )
68+ @test @constinferred (findtruncated_sorted (vals_sorted, strategy)) == 1 : 4
7669end
0 commit comments