RcdMathLib_doc
Open Source Library for Linear and Non-linear Algebra
givens_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 
23 #include <stdio.h>
24 
25 #include "qr_givens.h"
26 #include "matrix.h"
27 
28 void givens_test(void)
29 {
30  puts("############ Test the Givens algorithm ###############");
31  matrix_t xj, xij;
32 
33  xj = 100;
34  xij = 130;
35 
36  matrix_t c_s_t_r_vec[4];
37  qr_givens_get_params(xj, xij, c_s_t_r_vec);
38 
39  printf("c = %7.4f\n", c_s_t_r_vec[0]);
40  printf("s = %7.4f\n", c_s_t_r_vec[1]);
41  printf("t = %7.4f\n", c_s_t_r_vec[2]);
42  printf("r = %7.4f\n", c_s_t_r_vec[3]);
43 
44  puts("************ MATRIX A ************");
45  matrix_t A[10][5] = {
46  { 0.8147, 0.1576, 0.6557, 0.7060, 0.4387 },
47  { 0.9058, 0.9706, 0.0357, 0.0318, 0.3816 },
48  { 0.1270, 0.9572, 0.8491, 0.2769, 0.7655 },
49  { 0.9134, 0.4854, 0.9340, 0.0462, 0.7952 },
50  { 0.6324, 0.8003, 0.6787, 0.0971, 0.1869 },
51  { 0.0975, 0.1419, 0.7577, 0.8235, 0.4898 },
52  { 0.2785, 0.4218, 0.7431, 0.6948, 0.4456 },
53  { 0.5469, 0.9157, 0.3922, 0.3171, 0.6463 },
54  { 0.9575, 0.7922, 0.6555, 0.9502, 0.7094 },
55  { 0.9649, 0.9595, 0.1712, 0.0344, 0.7547 }
56  };
57 
58  uint8_t m, n;
59  m = 10;
60  n = 5;
61 
62  matrix_t copy_A[m][n];
63 
64  puts("+++++++ Reduced QR-form +++++++");
65  matrix_copy(m, n, A, copy_A);
66  matrix_t red_Q[m][n];
67  qr_givens_decomp(m, n, copy_A, n, red_Q, true);
68  printf("red_Q = ");
69  matrix_flex_print(m, n, red_Q, 7, 4);
70  puts("");
71  printf("red_R = ");
72  matrix_flex_print(n, n, copy_A, 7, 4);
73  puts("+++++++ Full QR-form +++++++");
74  matrix_copy(m, n, A, copy_A);
75  matrix_t full_Q[m][m];
76  qr_givens_decomp(m, n, copy_A, m, full_Q, false);
77  printf("full_Q = ");
78  matrix_flex_print(m, m, full_Q, 7, 4);
79  puts("");
80  printf("full_R = ");
81  matrix_flex_print(m, n, copy_A, 7, 4);
82 
83  puts("************ MATRIX B ************");
84  matrix_t B[7][7] =
85  {
86  { 0.8147, 0.5469, 0.8003, 0.0357, 0.6555, 0.8235, 0.7655 },
87  { 0.9058, 0.9575, 0.1419, 0.8491, 0.1712, 0.6948, 0.7952 },
88  { 0.1270, 0.9649, 0.4218, 0.9340, 0.7060, 0.3171, 0.1869 },
89  { 0.9134, 0.1576, 0.9157, 0.6787, 0.0318, 0.9502, 0.4898 },
90  { 0.6324, 0.9706, 0.7922, 0.7577, 0.2769, 0.0344, 0.4456 },
91  { 0.0975, 0.9572, 0.9595, 0.7431, 0.0462, 0.4387, 0.6463 },
92  { 0.2785, 0.4854, 0.6557, 0.3922, 0.0971, 0.3816, 0.7094 }
93  };
94  m = 7;
95  n = 7;
96  matrix_t copy_B[m][n];
97  puts("+++++++ Reduced QR-form +++++++");
98  matrix_copy(m, n, B, copy_B);
99  matrix_t red_Q1[m][n];
100  qr_givens_decomp(m, n, copy_B, n, red_Q1, true);
101  printf("red_Q = ");
102  matrix_flex_print(m, n, red_Q1, 7, 4);
103  puts("");
104  printf("red_R = ");
105  matrix_flex_print(n, n, copy_B, 7, 4);
106  puts("+++++++ Full QR-form +++++++");
107  matrix_copy(m, n, B, copy_B);
108  matrix_t full_Q1[m][m];
109  qr_givens_decomp(m, n, copy_B, m, full_Q1, false);
110  printf("full_Q = ");
111  matrix_flex_print(m, m, full_Q1, 7, 4);
112  puts("");
113  printf("full_R = ");
114  matrix_flex_print(m, n, copy_B, 7, 4);
115 
116 }
matrix.h
Matrix computations.
matrix_flex_print
void matrix_flex_print(uint8_t m, uint8_t n, matrix_t matrix[m][n], uint8_t before_dec, uint8_t after_dec)
Display the values of the matrix elements.
Definition: matrix.c:220
givens_test
void givens_test(void)
Examples of the Givens algorithm.
Definition: givens_test.c:28
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_t
#define matrix_t
Define the data type of the matrix elements.
Definition: matrix.h:38
qr_givens_get_params
void qr_givens_get_params(matrix_t xjj, matrix_t xij, matrix_t c_s_t_r_vec[])
Compute the Givens parameters.
Definition: qr_givens.c:110
qr_givens.h
Givens algorithm for the QR-decomposition. Provide necessary methods to construct Q- and R- matrices ...