forked from chrundle/householder-QR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linearalgebra.h
379 lines (286 loc) · 11 KB
/
linearalgebra.h
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include <cmath>
#include <stdlib.h>
#include <stdio.h>
/* ----------------------- norm ----------------------- */
/* Given an array and its length, this function
computes the 2-norm of the array.
Input variables:
x : pointer to array for which the 2-norm should
be computed.
length: number of entries in x.
Features: This implementation has time complexity
O(length) and requires O(1) additional memory. */
double norm (double * x, int length) {
int i, length5;
double a, sum = 0;
length5 = length % 5;
for(i = 0; i < length5; i++) {
sum += x[i] * x[i];
}
for(; i < length; i += 5) {
sum += x[i] * x[i] + x[i + 1] * x[i + 1] + x[i + 2] * x[i + 2]
+ x[i + 3] * x[i + 3] + x[i + 4] * x[i + 4];
}
return sqrt(sum);
}
void backsolve( double * x, double ** A, double * b , int n){
int i,j ;
for (i = n; i > 0; i--) {
/* code */
x[i] = b[i] ;
for ( j = i+1 ; j < n+1 ; j++) {
x[i] = x[i] - A[j][i]*x[j];
}
//printf("%f\n",A[i][i] );
x[i] = x[i]/A[i][i] ;
}
}
/* ----------------------- vec_copy ----------------------- */
/* Given two arrays of the same length and their length,
this function stores the values from the first array
in the second array.
Input variables:
x : pointer to array whose entries are to be
copied.
y : pointer to array in which the components
of x are to be stored.
length: number of entries in x and in y.
Features: This implementation has time complexity
O(length) and requires O(1) additional memory. */
void vec_copy (double * x, double * y, int length) {
int i, length5;
length5 = length % 5;
for(i = 0; i < length5; i++) {
y[i] = x[i];
}
for(; i < length; i += 5) {
y[i] = x[i];
y[i + 1] = x[i + 1];
y[i + 2] = x[i + 2];
y[i + 3] = x[i + 3];
y[i + 4] = x[i + 4];
}
}
/* ------------------- partialvec_copy ------------------- */
/* Given two arrays, the length of the second array, and
an index this function stores the values from the
subarray x[index : index + length] in the array
y[0 : length].
Input variables:
x : pointer to array whose entries are to be
copied.
y : pointer to array in which the components
of x are to be stored.
length: number of entries in y.
index : starting index of subarray of x to be
copied to y.
Example: Suppose x is a pointer to the array
{1, 2, 3, 4, 5}, y is a pointer to the array {0, 0, 0},
length = 3, and index = 2. Then after executing
partialvec_copy(x, y, 3, 2), the array pointed to by
y is now {3, 4, 5}.
Features: This implementation has time complexity
O(length) and requires O(1) additional memory. */
void partialvec_copy (double * x, double * y, int length, int index) {
int i, length5;
length5 = length % 5;
for(i = 0; i < length5; i++) {
y[i] = x[i + index];
}
for(; i < length; i += 5) {
y[i] = x[i + index];
y[i + 1] = x[i + index + 1];
y[i + 2] = x[i + index + 2];
y[i + 3] = x[i + index + 3];
y[i + 4] = x[i + index + 4];
}
}
/* ----------------------- scalar_div ----------------------- */
/* Given two arrays of the same length, their length, and a
scalar value this function divides the values from the
first array by the scalar value and stores the computed
number in the second array.
Input variables:
x : pointer to array whose components are to be
divided by r and stored in second array, y.
r : scalar used in division.
length: number of entries in x and in y.
y : pointer to array in which the components
of x are to be stored.
Features: This implementation has time complexity
O(length) and requires O(1) additional memory. */
void scalar_div (double * x, double r, int length, double * y) {
int i, length5;
length5 = length % 5;
for(i = 0; i < length5; i++) {
y[i] = x[i]/r;
}
for(; i < length; i += 5) {
y[i] = x[i]/r;
y[i + 1] = x[i + 1]/r;
y[i + 2] = x[i + 2]/r;
y[i + 3] = x[i + 3]/r;
y[i + 4] = x[i + 4]/r;
}
}
/* ----------------------- scalar_sub ----------------------- */
/* Given two arrays of the same length, their length, and a
scalar value this function multiplies the values from the
first array by the scalar value and then subtracts the
computed components from the components the second array.
Input variables:
x : pointer to array whose components are to be
multiplied by r then subtracted from the
components of the second array, y.
r : scalar used in multiplication.
length: number of entries in x and in y.
y : pointer to array in which the components
of x are to be stored.
Features: This implementation has time complexity
O(length) and requires O(1) additional memory. */
void scalar_sub (double * x, double r, int length, double * y) {
int i, length5;
length5 = length % 5;
for(i = 0; i < length5; i++) {
y[i] -= r * x[i];
}
for(; i < length; i += 5) {
y[i] -= r * x[i];
y[i + 1] -= r * x[i + 1];
y[i + 2] -= r * x[i + 2];
y[i + 3] -= r * x[i + 3];
y[i + 4] -= r * x[i + 4];
}
}
/* --------------------- partialscalar_sub --------------------- */
/* Given two arrays, the length of the second array, a scalar
value, and an index, this function multiplies the values
starting at the given index from the first array by the
scalar value and then subtracts the computed components from
the components the second array.
Input variables:
x : pointer to array whose components are to be
multiplied by r then subtracted from the
components of the second array, y.
r : scalar used in multiplication.
length: number of entries in y.
index :
y : pointer to array in which the components
of x are to be stored.
Example: Suppose x is a pointer to the array
{1, 2, 3, 4, 5}, y is a pointer to the array {0, 0, 0},
length = 3, r = -1, and index = 2. Then after executing
partialscalar_sub(x, -1, 3, 2, y), the array pointed to
by y is now {-3, -4, -5}.
Features: This implementation has time complexity
O(length) and requires O(1) additional memory. */
void partialscalar_sub (double * x, double r, int length,
int index, double * y)
{
int i, length5;
length5 = length % 5;
for(i = 0; i < length5; i++) {
y[i + index] -= r * x[i];
}
for(; i < length; i += 5) {
y[i + index] -= r * x[i];
y[i + index + 1] -= r * x[i + 1];
y[i + index + 2] -= r * x[i + 2];
y[i + index + 3] -= r * x[i + 3];
y[i + index + 4] -= r * x[i + 4];
}
}
/* --------------------- dot_product --------------------- */
/* Given two arrays of the same length and their length,
this function returns the dot product of the two
arrays.
Input variables:
x : pointer to first array.
y : pointer to second array.
length: number of entries in x and in y.
Features: This implementation has time complexity
O(length) and requires O(1) additional memory. */
double dot_product (double * x, double * y, int length) {
int i, length5;
double sum = 0;
length5 = length % 5;
for(i = 0; i < length5; i++) {
sum += x[i] * y[i];
}
for(; i < length; i += 5) {
sum += x[i] * y[i] + x[i + 1] * y[i + 1] + x[i + 2] * y[i + 2]
+ x[i + 3] * y[i + 3] + x[i + 4] * y[i + 4];
}
return sum;
}
/* ------------------ partialdot_product ------------------ */
/* Given two arrays of the same length, their length, and
an index this function returns the dot product of the
two subarrays x[index : length] and y[index : length].
Input variables:
x : pointer to first array.
y : pointer to second array.
length: number of entries in x and in y.
index : starting index for subarrays.
Example: Suppose x is a pointer to the array
{1, 2, 3, 4}, y is a pointer to the array {5, 6, 7, 8},
length = 4, and index = 2. Then the value returned by
executing partialdot_product(x, y, 4, 2) is 53, which
is computed by
x[2] * y[2] + x[3] * y[3] = 3 * 7 + 4 * 8
= 21 + 32
= 53.
Features: This implementation has time complexity
O(length) and requires O(1) additional memory. */
double partialdot_product (double * x, double * y, int length, int index) {
int i, length5;
double sum = 0;
length5 = length % 5;
for(i = index; i < length5; i++) {
sum += x[i] * y[i];
}
for(; i < length; i += 5) {
sum += x[i] * y[i] + x[i + 1] * y[i + 1] + x[i + 2] * y[i + 2]
+ x[i + 3] * y[i + 3] + x[i + 4] * y[i + 4];
}
return sum;
}
/* -------------------- subdot_product -------------------- */
/* Given two arrays, the length of the second array, and
an index this function returns the dot product of the
two subarrays x[index : index + length] and
y[0 : length]. It is necessary that index + length is
at most the length of the first array.
Input variables:
x : pointer to first array.
y : pointer to second array.
length: number of entries in y.
index : starting index for subarray of x.
Example: Suppose x is a pointer to the array
{1, 2, 3, 4, 5}, y is a pointer to the array
{-1, -2, -3}, length = 3, and index = 2. Then the value
returned by executing subdot_product(x, y, 3, 2) is 53,
which is computed by
x[2] * y[0] + x[3] * y[1] + x[4] * y[2]
= 3 * -1 + 4 * -2 + 5 * -3
= - 3 - 8 - 15
= -26.
Features: This implementation has time complexity
O(length) and requires O(1) additional memory. */
double subdot_product (double * x, double * y, int length, int index) {
int i, length5;
double sum = 0;
length5 = length % 5;
for(i = 0; i < length5; i++) {
sum += x[i + index] * y[i];
}
for(; i < length; i += 5) {
sum += x[i + index] * y[i] + x[i + index + 1] * y[i + 1]
+ x[i + index + 2] * y[i + 2]
+ x[i + index + 3] * y[i + 3]
+ x[i + index + 4] * y[i + 4];
}
return sum;
}
double norm2_col( double *vec, int vec_dim);
void write_vec(FILE *fid, char *name, const double *vec, int n);