-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.hpp
More file actions
222 lines (194 loc) · 5.88 KB
/
matrix.hpp
File metadata and controls
222 lines (194 loc) · 5.88 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
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
#pragma once
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <typename T>
void SquareMatrix<T>::fill_with_rands(int &_dim_size)
{
dim_size = _dim_size;
data.resize(dim_size*dim_size);
std::random_device rd;
std::mt19937 rng(rd());
std::uniform_real_distribution<T> uni(-1, 1);
for(int j = 0; j < dim_size; j++)
{
for(int i = 0; i < dim_size; i++)
{
if(i > j) // to ensure it is symmetric
{
T val = uni(rng);
set(i, j, val);
set(j, i, val);
}
if(i == j)
set(j, i, 0);
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <typename T>
void SquareMatrix<T>::print()
{
if(dim_size*dim_size < MAX_PRINTING_SIZE)
{
for(int j = 0; j < dim_size; j++)
{
for (int i = 0; i < dim_size; i++)
{
std::cout << get(i, j) << std::setprecision(4) << " ";
}
std::cout << std::endl;
}
}
else
{
std::cout << "Warning! matrix is too large to print!" << std::endl;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
template <typename T>
void SquareMatrix<T>::read_from_file(const std::string &_file_name)
{
std::ifstream file_desc;
file_desc.open(_file_name);
std::vector<T> tmp_vals;
if(file_desc.is_open())
{
while (!file_desc.eof())
{
std::string line;
file_desc >> line;
//std::cout << line << std::endl;
if(is_float(line))
{
tmp_vals.push_back(to_float(line));
}
}
int num_elems = tmp_vals.size();
dim_size = (sqrt(8*num_elems + 1) - 1)/2; // assuming we have just read N*(N+1)/2 elements, and try to find N
std::cout << "dim size: " << dim_size << std::endl;
std::cout << "elements read: " << num_elems << std::endl;
int cnt = 0;
data.resize(dim_size*dim_size);
for(int j = 0; j < dim_size; j++)
{
for (int i = 0; i < dim_size; i++)
{
if(i >= j)
{
set(i, j, tmp_vals[cnt]);
set(j, i, tmp_vals[cnt]);
cnt++;
}
}
}
}
else
{
throw "mtx file does not exist!";
}
file_desc.close();
}*/
int get_number_of_lines_in_file(const std::string &_file_name)
{
char newLine = '.';
int numLines = 0;
std::string text;
std::ifstream openFile(_file_name.c_str());
if(!openFile)
{
return 0;
}
while(getline(openFile, text, '\n'))
{
for(unsigned int i=0; i< text.length(); i++)
{
if(text.at(i) == newLine)
{
numLines++;
}
}
}
return numLines;
}
bool check_if_h_vector_provided(const std::string &_file_name, int dim_size)
{
int lines_number = get_number_of_lines_in_file(_file_name);
if(lines_number == 0)
return false;
int expected_lines_number_with_h = (dim_size - 1)*((dim_size - 1) + 1)/2 + dim_size;
int expected_lines_number_without_h = (dim_size - 1)*((dim_size - 1) + 1)/2;
#ifdef __DEBUG_INFO__
std::cout << "lines number: " << lines_number << std::endl;
std::cout << "expected_lines_number_WITH_h: " << expected_lines_number_with_h << std::endl;
std::cout << "expected_lines_number_WITHOUT_h: " << expected_lines_number_without_h << std::endl;
#endif
bool h_is_provided;
if(lines_number == expected_lines_number_with_h)
{
h_is_provided = true;
}
else if(lines_number == expected_lines_number_without_h)
{
h_is_provided = false;
}
else
{
std::cout << "input file " << _file_name << " does not have enough lines to construct square symmetric matrix" << std::endl;
throw "Aborting...";
}
return h_is_provided;
}
template <typename T>
void SquareMatrix<T>::read_from_file(const std::string &_file_name)
{
bool h_is_provided = check_if_h_vector_provided(_file_name, dim_size);
std::ifstream file_desc;
file_desc.open(_file_name);
if(file_desc.is_open())
{
if(h_is_provided) // then skip h vector here, and read it later
{
for (int i = 0; i < dim_size; i++)
{
std::string line;
if(file_desc.eof())
{
std::cout << "Error! matrix dimension and number of elements in file mismatches" << std::endl;
throw "Aborting...";
}
file_desc >> line;
}
}
for(int j = 0; j < dim_size; j++)
{
for (int i = 0; i < dim_size; i++)
{
if(i > j)
{
std::string line;
if(file_desc.eof())
{
std::cout << "Error! matrix dimension and number of elements in file mismatches" << std::endl;
throw "Aborting...";
}
file_desc >> line;
T val = 0;
if(is_float(line))
{
val = to_float(line);
}
set(i, j, val);
set(j, i, val);
}
if(i == j)
set(i, j, 0);
}
}
}
else
{
throw "mtx file does not exist!";
}
file_desc.close();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////