]> gitweb.michael.orlitzky.com - octave.git/blobdiff - tests/rank_k_approximation_tests.m
Fix existing rank_k_approximation() tests; add new ones.
[octave.git] / tests / rank_k_approximation_tests.m
index 82887526fe789ff483d2dd25a6ecc05c296daecf..f1339b64db61f962b51b194ae32410ba1d2a5dc3 100644 (file)
@@ -1,20 +1,48 @@
-U = [4/5, 3/5, 0  ; ...
-     0  , 4/5, 3/5; ...
-     3/5, 0  , 4/5 ];
+U = [ 0, 0, 1; ...
+      0, 1, 0; ...
+      1, 0, 0 ];
 
-S = [ 1, 0, 0;
-      0, 2, 0;
-      0, 0, 3 ];
+S = [ 3, 0, 0; ...
+      0, 2, 0; ...
+      0, 0, 1 ];
 
-V = [ 0, 1, 0;
-      1, 0, 0;
+V = [ 0, 1, 0; ...
+      1, 0, 0; ...
       0, 0, 1 ];
 
 A = U*S*V';
 
 expected = A;
+% This should be exact.
 actual = rank_k_approximation(A, 3);
 
 unit_test_equals("Full rank approximation of a matrix is itself", ...
-                expected, ...
-                actual);
+                actual, ...
+                expected);
+
+
+
+% This too.
+actual = rank_k_approximation(A, 13);
+
+unit_test_equals("Full rank approximation of a matrix is itself", ...
+                actual, ...
+                expected);
+
+
+S2 = S;
+S2(3,3) = 0;
+expected = U*S2*V';
+actual = rank_k_approximation(A, 2);
+unit_test_equals("Rank 2 approximation of a matrix is correct", ...
+                true, ...
+                (norm(actual - expected) <= 1e-10));
+
+
+S1 = S2;
+S1(2,2) = 0;
+expected = U*S1*V';
+actual = rank_k_approximation(A, 1);
+unit_test_equals("Rank 1 approximation of a matrix is correct", ...
+                true, ...
+                (norm(actual - expected) <= 1e-10));