-
Notifications
You must be signed in to change notification settings - Fork 0
/
qr.cpp
203 lines (149 loc) · 3.16 KB
/
qr.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
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
// my first program in C++
#include <iostream>
#include <stdlib.h>
#include <assert.h>
#include <fstream>
#include <string>
using namespace std;
int ** readmatrix(string filename);
int ** matrix_empty(int dim);
int** rand_matrix(int dim, int max);
int inner_product(int *v, int *w);
int* projection(int *e, int *a);
bool is_symmetric (int** matrix);
void matrix_print(int ** matrix);
int main ()
{
cout << "Please provide a file containing a symmetric matrix\n";
string str;
cin >> str;
matrix_print(readmatrix(str));
}
int ** readmatrix(string filename)
{
string line;
ifstream file(filename);
int ** matrix;
if (file.is_open())
{
int rows = 0;
int cols = 0;
if(getline(file,line))
{
//tokenise first line seperately to get dimensions of line
char *line_ = (char *) line.c_str();
rows = atoi(strtok(line_, " "));
cols = atoi(strtok(NULL, " "));
if (rows != cols)
{
cout << "You must provide a square matrix!";
return NULL;
}
matrix = matrix_empty(rows);
int lines = 0;
int i = 0;
int j = 0;
while (getline (file,line))
{
cout << line << '\n';
char *int_str = strtok(line_, " ");
while(int_str != NULL)
{
int_str = strtok(line_, " ");
matrix[i][j] = atoi(int_str);
i++;
}
j++;
}
file.close();
}
else
{
cout << "Unable to open file";
}
}
return matrix;
}
int ** matrix_empty(int dim)
{
int** matrix = new int*[dim];
for(int i = 0; i < dim; i++)
{
matrix[i] = new int[dim];
}
return matrix;
}
int** rand_matrix(int dim, int max)
{
int ** matrix = matrix_empty(dim);
int entry;
for (int i = 0; i < dim; i++)
{
for (int j = i; j < dim; j++)
{
entry = rand() % max;
matrix[i][j] = entry;
if (i != j)
{
// only need to do opposite entry if we're not on the diagonal
matrix[i][j] = entry;
}
}
}
return NULL;
}
// calculates v^(T)w
int inner_product(int *v, int *w)
{
//PRE: v and w have the same size
assert(sizeof(v) == sizeof(w));
int dim = sizeof(v);
int accum = 0;
for (int i = 0; i < dim; i++)
{
accum += (v[i]*w[i]);
}
return accum;
}
// projection of a in direction of e
int* projection(int *e, int *a)
{
//(<e,a>/<e,e>) e
//PRE: dimension of e = dimension of a
assert(sizeof(e) == sizeof(a));
int dim = sizeof(e);
float frac = (float) inner_product(e, a) / (float) inner_product(e, e);
int *res = new int[dim];
for (int i = 0; i < dim; i++)
{
res[i] = e[i] * frac;
}
return res;
}
bool is_symmetric (int** matrix)
{
// PRE: matrix is square
assert(sizeof(matrix) == sizeof(matrix[0]));
int dim = sizeof(matrix);
for (int i = 0; i < dim; i++)
{
for (int j = 0; j < dim; j++)
{
if (i != j && matrix[i][j] != matrix[j][i])
{
return false;
}
}
}
return true;
}
void matrix_print(int ** matrix)
{
int rows = sizeof(matrix);
int cols = sizeof(matrix[0]);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < rows; j++) {
cout << matrix[i][j];
}
}
}