RcdMathLib_doc
Open Source Library for Linear and Non-linear Algebra
moore_penrose_pinv_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 
28 #include "matrix.h"
29 
31 {
32  puts("\n\n++++++++++++++++++++++++++++++ Moore-Penrose Pseudo-Inverse ++++++++++++++++++++++++++++++\n");
33  uint8_t m, n;
34  uint8_t i = 0;
35 
36  //m<n
37  matrix_t matrix1_arr[3][4] = { { 1.0000, -3.6000, -1.2000, -2.8400 },
38  { 1.0000, -7.2400, -7.1800, -2.2400 },
39  { 1.0000, -12.0600, -1.3400, -3.6600 } };
40 
41  m = 3;
42  n = 4;
43  moore_penrose_pinv_compute_print(m, n, matrix1_arr, ++i);
44  matrix_t matrix2_arr[3][4] = {
45  { 1, -22, 30, -20 },
46  { 1, -26, -42, -10 },
47  { 1, 18, -30, -14 }
48  };
49  moore_penrose_pinv_compute_print(m, n, matrix2_arr, ++i);
50 
51  printf(
52  "########### Test %d: m < n : (2,3) ###########\n",
53  ++i);
54  matrix_t matrix3_arr[2][3] = {
55  { 3, 1, 1 },
56  { -1, 3, 1 }
57  };
58  m = 2;
59  n = 3;
60  moore_penrose_pinv_compute_print(m, n, matrix3_arr, i);
61 
62  printf(
63  "########### Test %d: m > n : (3,2) ###########\n",
64  ++i);
65  matrix_t matrix4_arr[3][2] = {
66  { 1, 2 },
67  { 2, 2 },
68  { 2, 1 }
69  };
70  m = 3;
71  n = 2;
72  moore_penrose_pinv_compute_print(m, n, matrix4_arr, i);
73 
74  printf(
75  "########### Test %d: m = n : (4,4) ###########\n",
76  ++i);
77 
78  matrix_t matrix5_arr[4][4] = {
79  { 6, 0, 0, 6 },
80  { 2, 5, 0, 4 },
81  { 0, 6, 2, 3 },
82  { 0, 1, 5, 5 }
83  };
84  m = 4;
85  n = 4;
86  moore_penrose_pinv_compute_print(m, n, matrix5_arr, i);
87 
88  printf(
89  "########### Test %d: m < n : (4,7) ###########\n",
90  ++i);
91  matrix_t matrix6_arr[4][7] = {
92  { 2, 5, 4, 6, 3, 2, 1 },
93  { 1, 2, 4, 6, 0, 0, 6 },
94  { 2, 1, 1, 5, 4, 6, 3 },
95  { 3, 3, 1, 0, 4, 5, 0 }
96  };
97  m = 4;
98  n = 7;
99  moore_penrose_pinv_compute_print(m, n, matrix6_arr, i);
100 
101  printf(
102  "########### Test %d: m = n (3, 3) ###########\n",
103  ++i);
104  matrix_t matrix7_arr[3][3] = {
105  { 1., 2., 3 },
106  { 4., 5., 6. },
107  { 7., 8., 10. }
108  };
109  m = 3;
110  n = 3;
111  moore_penrose_pinv_compute_print(m, n, matrix7_arr, i);
112 
113  printf(
114  "########### Test %d: m < n (5, 7) ###########\n",
115  ++i);
116  matrix_t matrix8_arr[5][7] = {
117  { 6, 2, 7, 3, 6, 0, 1 },
118  { 7, 1, 6, 3, 6, 4, 0 },
119  { 3, 5, 1, 1, 0, 2, 3 },
120  { 5, 5, 7, 3, 1, 3, 0 },
121  { 6, 5, 4, 1, 2, 7, 1 }
122  };
123  m = 5;
124  n = 7;
125  moore_penrose_pinv_compute_print(m, n, matrix8_arr, i);
126 
127  //m>n
128  printf(
129  "########### Test %d: m > n (7, 5) ###########\n",
130  ++i);
131  matrix_t matrix9_arr[7][5] = {
132  { 6.395, 2.897, 7.333, 3.777, 6.543 },
133  { 7.635, 1.235, 6.301, 3.356, 6.777 },
134  { 3.595, 5.901, 1.408, 1.697, 0.539 },
135  { 5.501, 5.562, 7.271, 3.907, 1.651 },
136  { 6.198, 5.069, 4.238, 1.388, 2.855 },
137  { 0.000, 1.999, 4.103, 0.561, 2.908 },
138  { 3.834, 3.172, 0.009, 7.069, 1.075 }
139  };
140 
141  m = 7;
142  n = 5;
143  moore_penrose_pinv_compute_print(m, n, matrix9_arr, i);
144 
145  printf(
146  "########### Test %d: m > n (5, 7) ###########\n",
147  ++i);
148  matrix_t matrix10_arr[5][7] = {
149  { 6.395, 2.897, 7.333, 3.777, 6.543, 0.000, 1.999 },
150  { 7.635, 1.235, 6.301, 3.356, 6.777, 4.103, 0.561 },
151  { 3.595, 5.901, 1.408, 1.697, 0.539, 2.908, 3.834 },
152  { 5.501, 5.562, 7.271, 3.907, 1.651, 3.172, 0.009 },
153  { 6.198, 5.069, 4.238, 1.388, 2.855, 7.069, 1.075 }
154  };
155  m = 5;
156  n = 7;
157  moore_penrose_pinv_compute_print(m, n, matrix10_arr, i);
158 
159 }
moore_penrose_pinv_compute_print
void moore_penrose_pinv_compute_print(uint8_t m, uint8_t n, matrix_t matrix[m][n], uint8_t i)
Compute and print the Moore–Penrose inverse of a matrix.
Definition: moore_penrose_pseudo_inverse.c:167
matrix.h
Matrix computations.
moore_penrose_pseudo_inverse.h
Moore–Penrose algorithm to compute the pseudo-inverse of a matrix.
matrix_t
#define matrix_t
Define the data type of the matrix elements.
Definition: matrix.h:38
moore_penrose_pinv_test
void moore_penrose_pinv_test(void)
Examples of the Moore–Penrose algorithm.
Definition: moore_penrose_pinv_test.c:30