-
Notifications
You must be signed in to change notification settings - Fork 0
/
gramshmidt.cpp
155 lines (114 loc) · 2.96 KB
/
gramshmidt.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
#include "stdio.h"
#include "stdlib.h"
#include "ac_fixed.h"
#define DATA_BITS 32
#define DATA_INTEGER_BITS 28 // = DATA_BITS - DATA_FRACT_BITS
#define AC_FIXED ac_fixed<DATA_BITS,DATA_INTEGER_BITS,true, AC_TRN, AC_SAT_SYM>
#include "matrix.h"
void Gramshmidt(Matrix *H, Matrix *Q, Matrix *R);
#define PRINT(num) printf ("%0-+f\n", num.to_double());
int main (void)
{
int x=0, i=0, j=0, tmp=0;
double *arr = new double[24];
double *arr2 = new double [15];
for (i=0; i<24; i++)
{
*(arr + i) = i+1;
}
Matrix H(6, 4, arr);
Matrix Q(H.M, H.M);
Matrix R(H.M, H.N);
H.print();
Gramshmidt(&H, &Q, &R);
printf("Q = \n");
Q.print();
printf("R = \n");
R.print();
Matrix tr_result(4, 6);
Matrix mul_result(H.M, H.N);
Q.matrix_multiply(&R, &mul_result);
printf("Multiplication result = \n");
mul_result.print();
////////////////////////////////////////////
/*
Matrix b(10, 5, arr);
//b.print();
//b.set(3, 4, 100);
//printf("sum = %f, %d\n", (float)b.get(2, 3));
//b.print();
//c.print();
//printf("b printed\n");
//c.print();
//printf("a printed\n");
b.copy_to(&a);
a.transpose(&tr);
tr.print();
tr.copy_to(arr2, 10, 5);
printf("arr2 printing \n ");
for (i=0; i<50; i++)
{
printf("%f ",(float)*(arr + i));
}
printf("\n ");
AC_FIXED num = 167;
AC_FIXED norm;
tr.row_norm(1, &norm);
printf("norm = %f\n",(float)norm.to_double() );
printf("Program ENd\n");
delete [] arr;
*/
delete [] arr;
delete [] arr2;
return 0;
}
void skinny_decompose(Matrix *H, Matrix *Q, Matrix *R)
{
int i=0, j=0, k=0, x=0, y=0;
AC_FIXED temp = 0;
H->copy_to(Q);
for (k=0; k<H->N; k++)
{
//printf("----\n");
Q->column_norm(k, &temp);
R->data[k][k] = temp;
Matrix temp4(H->M, 1);
for (x=0; x<H->M; x++)
{
temp4.data[x][1] = Q->data[x][k];
}
for (x=0; x<H->M; x++)
{
Q->data[x][k] = temp4.data[x][1]/temp;
}
for (j=k+1; j<H->N; j++)
{
AC_FIXED temp2 = 0;
for (x=0; x<H->M; x++)
{
temp2 = temp2 + (H->data[x][j] * Q->data[x][k]);
}
R->data[k][j] = temp2;
Matrix temp3(H->M, 1);
for (x=0; x<H->M; x++)
{
temp3.data[x][1] = (R->data[k][j] * Q->data[x][k]);
}
for (x=0; x<H->M; x++)
{
Q->data[x][j] = Q->data[x][j] - temp3.data[x][1];
//printf("%+-0 .9f ", Q->data[x][j].to_double());
}
}
}
}
void fat_decompose(Matrix *H, Matrix *Q, Matrix *R)
{
}
void Gramshmidt(Matrix *H, Matrix *Q, Matrix *R)
{
if (H->M > H->N)
skinny_decompose(H, Q, R);
else
fat_decompose(H, Q, R);
}