RcdMathLib_doc
Open Source Library for Linear and Non-linear Algebra
qr_pseudo_inverse.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 
26 #include "matrix.h"
27 #include "qr_common.h"
28 #include "qr_givens.h"
29 #include "qr_householder.h"
30 #include "pseudo_inverse.h"
31 
32 int8_t qr_get_pinv(uint8_t m, uint8_t n, matrix_t A[m][n],
33  matrix_t pinv_A[n][m], enum QR_ALGORITHM algo)
34 {
35  int8_t status;
36 
37  switch (algo) {
38  case QR_Householder:
39  {
40  puts("Householder !!!!");
41  matrix_t Q[m][m];
42  matrix_t R[m][n];
43  matrix_copy(m, n, A, R);
44  status = qr_householder_decomp(m, n, R, m, Q, false);
45  matrix_t inv_R[n][m];
46  matrix_get_inv_upp_triang(m, n, R, inv_R);
47 
48  // A+ = R^-1*Q'
50  matrix_mul(n, m, inv_R, m, m, Q, pinv_A);
51 
52  break;
53  }
54 
55  case QR_Givens:
56  {
57  puts("Givens !!!");
58 
59  matrix_t Q[m][m];
60  matrix_t R[m][n];
61  matrix_copy(m, n, A, R);
62  status = qr_givens_decomp(m, n, R, m, Q, false);
63  matrix_t inv_R[n][m];
64  matrix_get_inv_upp_triang(m, n, R, inv_R);
65 
66  // A+ = R^-1*Q'
68  matrix_mul(n, m, inv_R, m, m, Q, pinv_A);
69 
70  break;
71  }
72 
73  default:
74  {
75  matrix_t Q[m][m];
76  matrix_t R[m][n];
77  matrix_copy(m, n, A, R);
78  status = qr_householder_decomp(m, n, R, m, Q, false);
79  matrix_t inv_R[n][m];
80  matrix_get_inv_upp_triang(m, n, R, inv_R);
81 
82  // A+ = R^-1*Q'
84  matrix_mul(n, m, inv_R, m, m, Q, pinv_A);
85 
86  }
87  }
88 
89  return status;
90 }
qr_householder_decomp
int8_t qr_householder_decomp(uint8_t m, uint8_t n, matrix_t A[][n], uint8_t q_col_num, matrix_t Q[][q_col_num], bool reduced)
Computes the QR decomposition of the matrix A by using the Householder algorithm.
Definition: qr_householder.c:33
pseudo_inverse.h
Compute the pseudo-inverse of a matrix.
qr_householder.h
Householder algorithm for the QR-decomposition.
QR_ALGORITHM
QR_ALGORITHM
Possible algorithms to compute the QR-decomposition of a matrix.
Definition: qr_common.h:32
matrix.h
Matrix computations.
qr_get_pinv
int8_t qr_get_pinv(uint8_t m, uint8_t n, matrix_t A[m][n], matrix_t pinv_A[n][m], enum QR_ALGORITHM algo)
Calculate the pseudo inverse of a rectangular matrix using the QR decomposition.
Definition: qr_pseudo_inverse.c:32
qr_common.h
Common definitions and implementations for the QR-decomposition. Provide necessary methods to constru...
matrix_copy
void matrix_copy(uint8_t m, uint8_t n, matrix_t src_matrix[m][n], matrix_t dest_matrix[m][n])
Copy the elements of a matrix to another matrix.
Definition: matrix.c:83
qr_givens_decomp
int8_t qr_givens_decomp(uint8_t m, uint8_t n, matrix_t A[][n], uint8_t q_col_num, matrix_t Q[][q_col_num], bool reduced)
Computes the QR decomposition of the matrix A by using the Givens algorithm.
Definition: qr_givens.c:33
matrix_get_inv_upp_triang
void matrix_get_inv_upp_triang(uint8_t m, uint8_t n, matrix_t U[][n], matrix_t inv_U[][m])
Computes the inverse an upper triangular matrix.
Definition: matrix.c:795
matrix_t
#define matrix_t
Define the data type of the matrix elements.
Definition: matrix.h:38
matrix_in_place_transpose
void matrix_in_place_transpose(uint8_t m, matrix_t matrix[][m])
Computes the in-place transpose of a matrix.
Definition: matrix.c:69
qr_givens.h
Givens algorithm for the QR-decomposition. Provide necessary methods to construct Q- and R- matrices ...
matrix_mul
void matrix_mul(uint8_t a_line_num, uint8_t a_col_num, matrix_t a_matrix[a_line_num][a_col_num], uint8_t b_line_num, uint8_t b_col_num, matrix_t b_matrix[b_line_num][b_col_num], matrix_t dest_matrix[a_line_num][b_col_num])
Compute the multiplication of two matrices.
Definition: matrix.c:363