From: Michael Orlitzky Date: Sun, 5 May 2013 01:12:09 +0000 (-0400) Subject: Fix existing rank_k_approximation() tests; add new ones. X-Git-Url: http://gitweb.michael.orlitzky.com/?p=octave.git;a=commitdiff_plain;h=4684857f18bb97f9b6ed8b4bf4622fd1d82dfcb5;hp=b39f6d3f6aaaebe891c09a8923547f25c2d43b84 Fix existing rank_k_approximation() tests; add new ones. --- diff --git a/tests/rank_k_approximation_tests.m b/tests/rank_k_approximation_tests.m index 8288752..f1339b6 100644 --- a/tests/rank_k_approximation_tests.m +++ b/tests/rank_k_approximation_tests.m @@ -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));