RcdMathLib_doc
Open Source Library for Linear and Non-linear Algebra
qr_common.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 
22 #include <inttypes.h>
23 #include <stdio.h>
24 #include <string.h>
25 
26 #include "matrix.h"
27 
28 void qr_common_backward_subst(uint8_t m, uint8_t n, matrix_t U[][n],
29  matrix_t b[m], matrix_t x_sol[m])
30 {
31  int8_t i;
32  uint8_t j;
33  matrix_t sum;
34 
35  if (m != n) {
36  puts("The matrix should be square !!!");
37  return;
38  }
39 
40  //clear x_sol
41  memset(x_sol, 0, sizeof(matrix_t) * m);
42 
43  if (U[m - 1][m - 1] != 0) {
44  x_sol[m - 1] = b[m - 1] / U[m - 1][m - 1];
45  }
46 
47  for (i = m - 2; i >= 0; i--) {
48  sum = 0.0;
49  for (j = i + 1; j < m; j++) {
50  sum += U[i][j] * x_sol[j];
51  }
52 
53  if (U[i][i] != 0) {
54  x_sol[i] = (b[i] - sum) / U[i][i];
55  }
56  }
57 }
58 
59 void qr_common_get_reduced_QR(uint8_t m, uint8_t n, matrix_t Q[m][m],
60  matrix_t R[m][n], matrix_t reduc_Q[m][n],
61  matrix_t reduc_R[n][n])
62 {
63  if (m >= n) {
64  matrix_part_copy(m, m, Q, 0, m - 1, 0, n - 1, m, n, reduc_Q);
65  matrix_part_copy(m, n, R, 0, n - 1, 0, n - 1, n, n, reduc_R);
66  }
67  else {
68  puts(
69  "The row number of A should be greater than the column number of A");
70  }
71 }
qr_common_backward_subst
void qr_common_backward_subst(uint8_t m, uint8_t n, matrix_t U[][n], matrix_t b[m], matrix_t x_sol[m])
Implements the backward substitution algorithm.
Definition: qr_common.c:28
qr_common_get_reduced_QR
void qr_common_get_reduced_QR(uint8_t m, uint8_t n, matrix_t Q[m][m], matrix_t R[m][n], matrix_t reduc_Q[m][n], matrix_t reduc_R[n][n])
Compute the reduced form of the QR-decomposition algorithm.
Definition: qr_common.c:59
matrix.h
Matrix computations.
matrix_t
#define matrix_t
Define the data type of the matrix elements.
Definition: matrix.h:38
matrix_part_copy
void matrix_part_copy(uint8_t m, uint8_t n, matrix_t src_matrix[m][n], uint8_t start_row_ind, uint8_t end_row_ind, uint8_t start_col_ind, uint8_t end_col_ind, uint8_t dest_row_num, uint8_t dest_col_num, matrix_t dest_matrix[][dest_col_num])
Copy a part of a matrix to another matrix or sub-matrix.
Definition: matrix.c:89