Skip to content

Commit 5833a89

Browse files
committed
add simplex! tests and bug fixes
1 parent a7c9cae commit 5833a89

File tree

1 file changed

+49
-13
lines changed

1 file changed

+49
-13
lines changed

test/runtests.jl

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ const VERBOSE = true
9595
@testset "Products" begin
9696
A = randn(10, 20)
9797
B = randn(10, 20)
98-
@test all(slicewise_dot(A, A) . A*A') # test this separately since it uses a different routine when the argument is the same
99-
@test all(slicewise_dot(A, B) . A*B')
98+
@test slicewise_dot(A, A) A*A' # test this separately since it uses a different routine when the argument is the same
99+
@test slicewise_dot(A, B) A*B'
100100
end
101101

102102
@testset "Constraints" begin
@@ -114,19 +114,19 @@ const VERBOSE = true
114114

115115
A = Array{Float64}(reshape(1:12, 3,4))
116116
l1normalize_rows!(A)
117-
@test all(A . [0 0 0 1; 0 0 0 1; 0 0 0 1])
117+
@test A [0 0 0 1; 0 0 0 1; 0 0 0 1]
118118

119119
A = Array{Float64}(reshape(1:12, 3,4))
120120
l1normalize_cols!(A)
121-
@test all(A . [0 0 0 0; 0 0 0 0; 1 1 1 1])
121+
@test A [0 0 0 0; 0 0 0 0; 1 1 1 1]
122122

123123
A = Array{Float64}(ones(2, 3))
124124
l1normalize_rows!(A)
125-
@test all(A . ones(2, 3) / 3)
125+
@test A ones(2, 3) / 3
126126

127127
A = Array{Float64}(ones(2, 3))
128128
l1normalize_cols!(A)
129-
@test all(A . ones(2, 3) / 2)
129+
@test A ones(2, 3) / 2
130130

131131
A = Array{Float64}(reshape(1:12, 3,2,2))
132132
l1scale_1slices!(A)
@@ -149,11 +149,11 @@ const VERBOSE = true
149149

150150
A = Array{Float64}(ones(2, 3))
151151
l2normalize_rows!(A)
152-
@test all(A . ones(2, 3) / 3)
152+
@test A ones(2, 3) / 3
153153

154154
A = Array{Float64}(ones(2, 3))
155155
l2normalize_cols!(A)
156-
@test all(A . ones(2, 3) / 2)
156+
@test A ones(2, 3) / 2
157157
end
158158

159159
@testset "Linfinity" begin
@@ -186,6 +186,32 @@ const VERBOSE = true
186186
@test A == [0 0 0 0; 0 0 0 0; 1 1 1 1]
187187
end
188188

189+
@testset "Simplex" begin
190+
v = [2.0, 0]
191+
simplex!(v)
192+
@test v == [1.0, 0.0]
193+
194+
v = [0.5, 0.6]
195+
simplex!(v)
196+
@test v [0.45, 0.55]
197+
198+
v = zeros(10)
199+
simplex!(v)
200+
@test v ones(10) / 10
201+
202+
v = -ones(10)
203+
simplex!(v)
204+
@test v ones(10) / 10
205+
206+
v = collect(0:0.1:1)
207+
simplex!(v)
208+
@test v [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.1, 0.2, 0.3, 0.4]
209+
210+
A = Array{Float64}(reshape(1:12,3,4)) / 12
211+
simplex_rows!(A)
212+
@test A [0.0 1.0 4.0 7.0; 0.0 1.0 4.0 7.0; 0.0 1.0 4.0 7.0] / 12
213+
end
214+
189215
@testset "Composition" begin
190216
c! = (l1scale_cols!, nonnegative!)
191217
@test typeof(c!) <: Function
@@ -579,7 +605,9 @@ end
579605
decomposition, stats_data = fact(Y; model=CPDecomposition, rank=3, maxiter=2, do_subblock_updates=true)
580606
end
581607

582-
@testset "ModelFactorization" begin
608+
@testset "Tucker1Factorization" begin
609+
fact = BlockTensorFactorization.factorize
610+
583611
# Regular run of Tucker1
584612
C = abs_randn(5, 11, 12)
585613
A = abs_randn(10, 5)
@@ -595,7 +623,10 @@ end
595623
stats=[Iteration, ObjectiveValue, GradientNNCone, RelativeError]
596624
);
597625

598-
@test stats[end, :iteration] < 1000 # ensure we did not hit the maximum number of iterations
626+
@test stats[end, :Iteration] < 1000 # ensure we did not hit the maximum number of iterations
627+
end
628+
@testset "CPFactorization" begin
629+
fact = BlockTensorFactorization.factorize
599630

600631
# Semi-interesting run of CPDecomposition
601632
N = 100
@@ -611,8 +642,9 @@ end
611642
options = (
612643
tolerance=.01,
613644
maxiter=1000,
645+
rank=R,
614646
converged=RelativeError,
615-
constraints=[l1scale_cols! nonnegative!, simplex_cols!],
647+
constraints=[l1scale_cols! nonnegative!, nonnegative!],
616648
constrain_init=true,
617649
constrain_output=true,
618650
momentum=true,
@@ -624,7 +656,11 @@ end
624656

625657
decomposition_randn, stats, kwargs = fact(Y; model=CPDecomposition, options...);
626658

627-
@test stats[end, :iteration] < 1000 # ensure we did not hit the maximum number of iterations
659+
@test stats[end, :Iteration] < 1000 # ensure we did not hit the maximum number of iterations
660+
end
661+
662+
@testset "TuckerFactorization" begin
663+
fact = BlockTensorFactorization.factorize
628664

629665
# Regular run of Tucker
630666
G = abs_randn(2,3,4)
@@ -645,7 +681,7 @@ end
645681
stats=[Iteration, ObjectiveValue, GradientNNCone, RelativeError, EuclideanLipschitz, EuclideanStepSize]
646682
);
647683

648-
@test stats[end, :iteration] < 1000 # ensure we did not hit the maximum number of iterations
684+
@test stats[end, :Iteration] < 1000 # ensure we did not hit the maximum number of iterations
649685
end
650686
end
651687

0 commit comments

Comments
 (0)