RcdMathLib_doc
Open Source Library for Linear and Non-linear Algebra
svd_test.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
3  * 2020 Freie Universität Berlin
4  *
5  * This file is subject to the terms and conditions of the GNU Lesser General
6  * Public License v2.1. See the file LICENSE in the top level directory for more
7  * details.
8  */
9 
24 #include <stdio.h>
25 #include <inttypes.h>
26 
27 #include "svd.h"
28 #include "matrix.h"
29 #include "vector.h"
30 
31 
32 void svd_test(void)
33 {
34  puts("############ Test the SVD algorithm ###############");
35  uint8_t m, n;
36  uint8_t i = 0;
37 
38  //m<n
39  matrix_t matrix1_arr[3][4] = { { 1.0000, -3.6000, -1.2000, -2.8400 },
40  { 1.0000, -7.2400, -7.1800, -2.2400 },
41  { 1.0000, -12.0600, -1.3400, -3.6600 } };
42 
43  m = 3;
44  n = 4;
45  matrix_t matrix2_arr[3][4] = {
46  { 1, -22, 30, -20 },
47  { 1, -26, -42, -10 },
48  { 1, 18, -30, -14 }
49  };
50  svd_compute_print_U_S_V_s(m, n, matrix1_arr, ++i);
51  svd_compute_print_U_S_V_s(m, n, matrix2_arr, ++i);
52 
53  printf(
54  "######## Test %d: Singular Value Decomposition m < n : (2,3) ########\n",
55  ++i);
56  matrix_t matrix3_arr[2][3] = {
57  { 3, 1, 1 },
58  { -1, 3, 1 }
59  };
60  m = 2;
61  n = 3;
62  svd_compute_print_U_S_V_s(m, n, matrix3_arr, i);
63 
64  printf(
65  "######## Test %d: Singular Value Decomposition m > n : (3,2) ########\n",
66  ++i);
67  matrix_t matrix4_arr[3][2] = {
68  { 1, 2 },
69  { 2, 2 },
70  { 2, 1 }
71  };
72  m = 3;
73  n = 2;
74  svd_compute_print_U_S_V_s(m, n, matrix4_arr, i);
75 
76  printf(
77  "######## Test %d: Singular Value Decomposition m = n : (4,4) ########\n",
78  ++i);
79 
80  matrix_t matrix5_arr[4][4] = {
81  { 6, 0, 0, 6 },
82  { 2, 5, 0, 4 },
83  { 0, 6, 2, 3 },
84  { 0, 1, 5, 5 }
85  };
86  m = 4;
87  n = 4;
88  svd_compute_print_U_S_V_s(m, n, matrix5_arr, i);
89 
90  printf(
91  "######## Test %d: Singular Value Decomposition m < n : (4,7) ########\n",
92  ++i);
93  matrix_t matrix6_arr[4][7] = {
94  { 2, 5, 4, 6, 3, 2, 1 },
95  { 1, 2, 4, 6, 0, 0, 6 },
96  { 2, 1, 1, 5, 4, 6, 3 },
97  { 3, 3, 1, 0, 4, 5, 0 }
98  };
99  m = 4;
100  n = 7;
101  svd_compute_print_U_S_V_s(m, n, matrix6_arr, i);
102 
103  printf(
104  "######## Test %d: Singular Value Decomposition m = n : (3,3) ########\n",
105  ++i);
106  matrix_t matrix7_arr[3][3] = {
107  { 1., 2., 3 },
108  { 4., 5., 6. },
109  { 7., 8., 10. }
110  };
111  m = 3;
112  n = 3;
113  svd_compute_print_U_S_V_s(m, n, matrix7_arr, i);
114 
115  printf(
116  "######## Test %d: Singular Value Decomposition m < n : (5,7) ########\n",
117  ++i);
118  matrix_t matrix8_arr[5][7] = {
119  { 6, 2, 7, 3, 6, 0, 1 },
120  { 7, 1, 6, 3, 6, 4, 0 },
121  { 3, 5, 1, 1, 0, 2, 3 },
122  { 5, 5, 7, 3, 1, 3, 0 },
123  { 6, 5, 4, 1, 2, 7, 1 }
124  };
125  m = 5;
126  n = 7;
127  svd_compute_print_U_S_V_s(m, n, matrix8_arr, i);
128 
129  //m>n
130  printf(
131  "######## Test %d: Singular Value Decomposition m > n : (7,5) ########\n",
132  ++i);
133  matrix_t matrix9_arr[7][5] = {
134  { 6.395, 2.897, 7.333, 3.777, 6.543 },
135  { 7.635, 1.235, 6.301, 3.356, 6.777 },
136  { 3.595, 5.901, 1.408, 1.697, 0.539 },
137  { 5.501, 5.562, 7.271, 3.907, 1.651 },
138  { 6.198, 5.069, 4.238, 1.388, 2.855 },
139  { 0.000, 1.999, 4.103, 0.561, 2.908 },
140  { 3.834, 3.172, 0.009, 7.069, 1.075 }
141  };
142 
143  m = 7;
144  n = 5;
145  svd_compute_print_U_S_V_s(m, n, matrix9_arr, i);
146 
147  printf(
148  "######## Test %d: Singular Value Decomposition m > n : (5,7) ########\n",
149  ++i);
150  matrix_t matrix10_arr[5][7] = {
151  { 6.395, 2.897, 7.333, 3.777, 6.543, 0.000, 1.999 },
152  { 7.635, 1.235, 6.301, 3.356, 6.777, 4.103, 0.561 },
153  { 3.595, 5.901, 1.408, 1.697, 0.539, 2.908, 3.834 },
154  { 5.501, 5.562, 7.271, 3.907, 1.651, 3.172, 0.009 },
155  { 6.198, 5.069, 4.238, 1.388, 2.855, 7.069, 1.075 }
156  };
157  m = 5;
158  n = 7;
159  svd_compute_print_U_S_V_s(m, n, matrix10_arr, i);
160 }
matrix.h
Matrix computations.
svd_compute_print_U_S_V_s
void svd_compute_print_U_S_V_s(uint8_t m, uint8_t n, matrix_t matrix_arr[m][n], uint8_t i)
Compute and print the SVD of a matrix.
Definition: svd.c:766
svd.h
Algorithm for the Singular Value Decomposition (SVD).
matrix_t
#define matrix_t
Define the data type of the matrix elements.
Definition: matrix.h:38
vector.h
Vector computations.
svd_test
void svd_test(void)
Examples of the Givens algorithm.
Definition: svd_test.c:32