-
Notifications
You must be signed in to change notification settings - Fork 5
/
householderQR.cpp
159 lines (137 loc) · 4.72 KB
/
householderQR.cpp
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <cmath>
#include <stdlib.h>
#include <stdio.h>
#include "../linearalgebra.h"
/* ----------------------- householder ----------------------- */
/* Given a matrix A of dimension m by n (with n <= m) and
arrays v_i of dimension m-i, for i = 1, ..., m - 1,
respectively, this algorithm computes n reflection vectors
and the factor R of a full QR decomposition of A, where R
is a m by n upper triangular matrix. The n reflection
vectors are stored in the arrays v_1, ..., v_n and the
columns of A are overwritten by the columns of R.
Input variables:
a: pointer to array of arrays, the ith array of
which should correspond to the ith column of the
matrix A. During the algorithm, the columns of R
will overwrite the columns of A.
v: pointer to array of arrays in which the ith
reflection vector of dimension m - i will be
stored.
m: number of rows of A.
n: number of columns of A.
Features: The number of flops for this implementation is
~ 2 * m * n^2 - (2/3) * n^3 and requires O(1) additional
memory. */
void householder (double ** a, double ** v, int m, int n) {
int i, j;
double vnorm, vTa, vpartdot;
for(i = 0; i < n; i++) {
/* set v[i] equal to subvector a[i][i : m] */
partialvec_copy(a[i], v[i], m - i, i);
/* vpartdot = ||v[i]||^2 - v[i][0] * v[i][0]; since vpartdot
is unaffected by the change in v[i][0], storing this value
prevents the need to recalculate the entire norm of v[i]
after updating v[i][0] in the following step */
vpartdot = partialdot_product(v[i], v[i], m - i, 1);
/* set v[i][0] = v[i][0] + sign(v[i][0]) * ||v[i]|| */
if(v[i][0] < 0) {
v[i][0] -= sqrt(v[i][0] * v[i][0] + vpartdot);
}
else {
v[i][0] += sqrt(v[i][0] * v[i][0] + vpartdot);
}
/* normalize v[i] */
vnorm = sqrt(v[i][0] * v[i][0] + vpartdot);
scalar_div(v[i], vnorm, m - i, v[i]);
for(j = i; j < n; j++) {
/* set a[j][i:m] = a[j][i:m] - 2 * (v[i]^T a[j][i:m]) * v[i] */
vTa = subdot_product(a[j], v[i], m - i, i);
vTa *= 2;
partialscalar_sub(v[i], vTa, m - i, i, a[j]);
}
}
}
int main () {
int i, j, m, n;
double x;
/* let user set the dimension of matrix A */
printf("Enter the dimension m (where A is a m by n matrix): ");
scanf("%i", &m);
printf("Enter the dimension n (where A is a m by n matrix): ");
scanf("%i", &n);
/* check if m < n */
if(m < n) {
printf("For a successful factorization, this implementation "
"requires n <= m.\nTerminating program.\n");
return 0;
}
/* allocate memory for A and vectors v */
double ** a = new double*[n];
double ** v = new double*[n];
for(i = 0; i < n; i++) {
a[i] = new double[m];
v[i] = new double[m - i];
}
/* initialize the values in matrix A */
for(i = 0; i < n; i++) {
for(j = 0; j < m; j++) {
if(j < i) {
a[i][j] = 0;
}
else {
a[i][j] = j - i + 1; // this choice of values was arbitrary
}
}
}
/* print the matrix A before calling houheholder */
printf("A = \n");
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
printf("%9.6g ", a[j][i]);
}
printf("\n");
}
printf("\n");
householder(a, v, m, n);
/* print the matrix R (stored in A) after calling houheholder */
printf("R = \n");
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
printf("%9.6g ", a[j][i]);
}
printf("\n");
}
printf("\n");
/* print the vectors v after calling householder */
for(i = 0; i < n; i++) {
printf("v[%i] = ", i);
for(j = 0; j < m - i; j++) {
printf("%9.6g ", v[i][j]);
}
printf("\n");
}
printf("\n");
/* print numerical evidence that v's are normalized */
printf("Numerical verification that v_1, ..., v_%i are "
"normalized:\n", n);
for(i = 1; i < n; i++) {
x = dot_product(v[i - 1], v[i - 1], m - i + 1);
printf("||v[%i]|| = %lg, ", i, x);
if(i % 5 == 0) {
printf("\n");
}
}
x = dot_product(v[n - 1], v[n - 1], m - n + 1);
printf("||v[%i]|| = %lg.", n, x);
if(n % 5 != 0) printf("\n");
printf("\n");
/* free memory */
for(i = 0; i < n; i++) {
delete[] a[i];
delete[] v[i];
}
delete[] a;
delete[] v;
return 0;
}