]> gitweb.michael.orlitzky.com - octave.git/blob - tests/rank_k_approximation_tests.m
Fix existing rank_k_approximation() tests; add new ones.
[octave.git] / tests / rank_k_approximation_tests.m
1 U = [ 0, 0, 1; ...
2 0, 1, 0; ...
3 1, 0, 0 ];
4
5 S = [ 3, 0, 0; ...
6 0, 2, 0; ...
7 0, 0, 1 ];
8
9 V = [ 0, 1, 0; ...
10 1, 0, 0; ...
11 0, 0, 1 ];
12
13 A = U*S*V';
14
15 expected = A;
16 % This should be exact.
17 actual = rank_k_approximation(A, 3);
18
19 unit_test_equals("Full rank approximation of a matrix is itself", ...
20 actual, ...
21 expected);
22
23
24
25 % This too.
26 actual = rank_k_approximation(A, 13);
27
28 unit_test_equals("Full rank approximation of a matrix is itself", ...
29 actual, ...
30 expected);
31
32
33 S2 = S;
34 S2(3,3) = 0;
35 expected = U*S2*V';
36 actual = rank_k_approximation(A, 2);
37 unit_test_equals("Rank 2 approximation of a matrix is correct", ...
38 true, ...
39 (norm(actual - expected) <= 1e-10));
40
41
42 S1 = S2;
43 S1(2,2) = 0;
44 expected = U*S1*V';
45 actual = rank_k_approximation(A, 1);
46 unit_test_equals("Rank 1 approximation of a matrix is correct", ...
47 true, ...
48 (norm(actual - expected) <= 1e-10));