From 4684857f18bb97f9b6ed8b4bf4622fd1d82dfcb5 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sat, 4 May 2013 21:12:09 -0400 Subject: [PATCH] Fix existing rank_k_approximation() tests; add new ones. --- tests/rank_k_approximation_tests.m | 48 +++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 10 deletions(-) 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)); -- 2.43.2