-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQR_Decomposition.c
More file actions
79 lines (65 loc) · 1.95 KB
/
Copy pathQR_Decomposition.c
File metadata and controls
79 lines (65 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdio.h>
#include <math.h>
void printMatrix(double A[][100], int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
printf("%.4f\t", A[i][j]);
}
printf("\n");
}
}
void qrDecomposition(double A[][100], double Q[][100], double R[][100], int rows, int cols) {
double temp[100][100] = {0};
for (int j = 0; j < cols; j++) {
for (int i = 0; i < rows; i++) {
temp[i][j] = A[i][j];
}
}
for (int j = 0; j < cols; j++) {
for (int i = 0; i < j; i++) {
double dot_product = 0;
for (int k = 0; k < rows; k++) {
dot_product += Q[k][i] * temp[k][j];
}
for (int k = 0; k < rows; k++) {
temp[k][j] -= dot_product * Q[k][i];
}
}
double norm = 0;
for (int i = 0; i < rows; i++) {
norm += temp[i][j] * temp[i][j];
}
norm = sqrt(norm);
for (int i = 0; i < rows; i++) {
Q[i][j] = temp[i][j] / norm;
}
for (int i = 0; i < cols; i++) {
double dot_product = 0;
for (int k = 0; k < rows; k++) {
dot_product += Q[k][j] * A[k][i];
}
R[j][i] = dot_product;
}
}
}
int main() {
int rows, cols;
printf("Enter the number of rows and columns of the matrix: ");
scanf("%d %d", &rows, &cols);
double A[100][100], Q[100][100] = {0}, R[100][100] = {0};
printf("Enter the elements of the matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%lf", &A[i][j]);
}
}
qrDecomposition(A, Q, R, rows, cols);
printf("Matrix Q:\n");
printMatrix(Q, rows, cols);
printf("\nMatrix R:\n");
printMatrix(R, cols, cols);
return 0;
}