@@ -125,6 +125,13 @@ def test_cpapr_mu(capsys):
125
125
capsys .readouterr ()
126
126
assert output ['nTotalIters' ] == 2
127
127
128
+ # Edge cases
129
+ # Confirm timeout works
130
+ non_correct_answer = ktensorInstance * 2
131
+ _ = ttb .cp_apr (tensorInstance , 2 , init = non_correct_answer , stoptime = - 1 )
132
+ out , _ = capsys .readouterr ()
133
+ assert "time limit exceeded" in out
134
+
128
135
@pytest .mark .indevelopment
129
136
def test_cpapr_pdnr (capsys ):
130
137
# Test simple case
@@ -139,6 +146,23 @@ def test_cpapr_pdnr(capsys):
139
146
capsys .readouterr ()
140
147
assert np .isclose (M .full ().data , ktensorInstance .full ().data , rtol = 1e-04 ).all ()
141
148
149
+ # Try solve with sptensor
150
+ sptensorInstance = ttb .sptensor .from_tensor_type (tensorInstance )
151
+ np .random .seed (123 )
152
+ M , _ , _ = ttb .cp_apr (sptensorInstance , 2 , algorithm = "pdnr" )
153
+ capsys .readouterr ()
154
+ assert np .isclose (M .full ().data , ktensorInstance .full ().data , rtol = 1e-04 ).all ()
155
+ M , _ , _ = ttb .cp_apr (sptensorInstance , 2 , algorithm = "pdnr" , precompinds = False )
156
+ capsys .readouterr ()
157
+ assert np .isclose (M .full ().data , ktensorInstance .full ().data , rtol = 1e-04 ).all ()
158
+
159
+ # Edge cases
160
+ # Confirm timeout works
161
+ non_correct_answer = ktensorInstance * 2
162
+ _ = ttb .cp_apr (tensorInstance , 2 , init = non_correct_answer , algorithm = "pdnr" , stoptime = - 1 )
163
+ out , _ = capsys .readouterr ()
164
+ assert "time limit exceeded" in out
165
+
142
166
@pytest .mark .indevelopment
143
167
def test_cpapr_pqnr (capsys ):
144
168
# Test simple case
@@ -165,6 +189,22 @@ def test_cpapr_pqnr(capsys):
165
189
capsys .readouterr ()
166
190
assert np .isclose (M .full ().data , ktensorInstance .full ().data , rtol = 1e-01 ).all ()
167
191
192
+ # Try solve with sptensor
193
+ sptensorInstance = ttb .sptensor .from_tensor_type (tensorInstance )
194
+ np .random .seed (123 )
195
+ M , _ , _ = ttb .cp_apr (sptensorInstance , 2 , algorithm = "pqnr" )
196
+ capsys .readouterr ()
197
+ assert np .isclose (M .full ().data , ktensorInstance .full ().data , rtol = 1e-01 ).all ()
198
+ M , _ , _ = ttb .cp_apr (sptensorInstance , 2 , algorithm = "pqnr" , precompinds = False )
199
+ capsys .readouterr ()
200
+ assert np .isclose (M .full ().data , ktensorInstance .full ().data , rtol = 1e-01 ).all ()
201
+
202
+ # Edge cases
203
+ # Confirm timeout works
204
+ _ = ttb .cp_apr (tensorInstance , 2 , algorithm = "pqnr" , stoptime = - 1 )
205
+ out , _ = capsys .readouterr ()
206
+ assert "time limit exceeded" in out
207
+
168
208
169
209
# PDNR tests below
170
210
@pytest .mark .indevelopment
@@ -367,3 +407,22 @@ def test_getSearchDirPqnr():
367
407
search , pred = ttb .getSearchDirPqnr (model_row , phi , 1e-6 , delta_model , delta_grad , phi , 1 , 5 , False )
368
408
# This only verifies that for the right shaped input nothing crashes. Doesn't verify correctness
369
409
assert True
410
+
411
+ def test_cp_apr_negative_tests ():
412
+ dense_tensor = ttb .tensor .from_data (np .ones ((2 , 2 , 2 )))
413
+ bad_weights = np .array ([8. ])
414
+ bad_factors = [np .array ([[1. ]])] * 3
415
+ bad_initial_guess_shape = ttb .ktensor .from_data (bad_weights , bad_factors )
416
+ with pytest .raises (AssertionError ):
417
+ ttb .cp_apr (dense_tensor , init = bad_initial_guess_shape , rank = 1 )
418
+ good_weights = np .array ([8. ] * 3 )
419
+ good_factor = np .array ([[1. , 1. , 1. ], [1. , 1. , 1. ]])
420
+ bad_initial_guess_factors = ttb .ktensor .from_data (good_weights , [- 1. * good_factor ] * 3 )
421
+ with pytest .raises (AssertionError ):
422
+ ttb .cp_apr (dense_tensor , init = bad_initial_guess_factors , rank = 3 )
423
+ bad_initial_guess_weight = ttb .ktensor .from_data (- 1. * good_weights , [good_factor ] * 3 )
424
+ with pytest .raises (AssertionError ):
425
+ ttb .cp_apr (dense_tensor , init = bad_initial_guess_weight , rank = 3 )
426
+
427
+ with pytest .raises (AssertionError ):
428
+ ttb .cp_apr (dense_tensor , rank = 1 , algorithm = 'UNSUPPORTED_ALG' )
0 commit comments