-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathblas.cpp
More file actions
480 lines (420 loc) · 11.6 KB
/
Copy pathblas.cpp
File metadata and controls
480 lines (420 loc) · 11.6 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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <numeric>
#include <iomanip>
#include <string>
#include <sstream>
#include "blas.hpp"
using namespace std;
/***********************************************/
/* FP64 API */
/***********************************************/
// Print a matrix (for debugging) using a basic format
void print_matrix_simple(const vector<vector<double>> &matrix)
{
int rows = matrix.size();
if (rows == 0)
return;
int cols = matrix[0].size();
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
cout << matrix[i][j] << "\t";
}
cout << endl;
}
cout << endl;
}
// Remove trailing zeros and potentially the decimal point
string trim_trailing_zeros(string s)
{
size_t decimal_pos = s.find('.');
if (decimal_pos != string::npos)
{
size_t last_digit = s.length() - 1;
while (last_digit > decimal_pos && s[last_digit] == '0')
{
s.pop_back();
last_digit--;
}
if (last_digit == decimal_pos)
{
s.pop_back(); // Remove trailing decimal point if no digits after it
}
}
return s;
}
// Print a matrix (for debugging)
void print_matrix(const vector<vector<double>> &matrix)
{
int rows = matrix.size();
if (rows == 0)
return;
int cols = matrix[0].size();
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
double value = matrix[i][j];
if (abs(value) < 1e-9)
{
cout << "0\t";
continue;
}
string formatted_value;
bool printed = false;
for (int p = 6; p >= 0; --p)
{
stringstream ss;
ss << fixed << setprecision(p) << value;
formatted_value = ss.str();
string trimmed_value = trim_trailing_zeros(formatted_value);
if (trimmed_value.length() <= 7)
{
cout << trimmed_value << "\t";
printed = true;
break;
}
}
if (!printed)
{
string original_value_str;
stringstream ss_orig;
ss_orig << value;
original_value_str = ss_orig.str();
if (original_value_str.length() > 7)
{
cout << original_value_str.substr(0, 7) << "\t";
}
else
{
cout << original_value_str << "\t";
}
}
}
cout << endl;
}
cout << endl;
}
/*
void print_matrix(const vector<vector<double>> &matrix)
{
int rows = matrix.size();
if (rows == 0)
return;
int cols = matrix[0].size();
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
double value = matrix[i][j];
// Check if the value is zero
if (abs(value) < 1e-9)
{ // Using a small tolerance for floating-point comparison
cout << "0\t";
continue;
}
string formatted_value;
bool printed = false;
for (int p = 6; p >= 0; --p)
{
stringstream ss;
ss << fixed << setprecision(p) << value;
formatted_value = ss.str();
if (formatted_value.length() <= 7)
{
cout << formatted_value << "\t";
printed = true;
break;
}
}
if (!printed)
{
string original_value_str;
stringstream ss_orig;
ss_orig << value;
original_value_str = ss_orig.str();
if (original_value_str.length() > 7)
{
cout << original_value_str.substr(0, 7) << "\t";
}
else
{
cout << original_value_str << "\t";
}
}
}
cout << endl;
}
cout << endl;
}
*/
// Calculate the Frobenius norm of a matrix
double frobenius_norm(const vector<vector<double>> &matrix)
{
double sum_of_squares = 0.0;
for (const auto &row : matrix)
{
for (double val : row)
{
sum_of_squares += val * val;
}
}
return sqrt(sum_of_squares);
}
// Calculate the infinity norm (row sum norm) of a matrix
double infinity_norm(const vector<vector<double>> &matrix)
{
double max_row_sum = 0.0;
for (const auto &row : matrix)
{
double row_sum = 0.0;
for (double val : row)
{
row_sum += abs(val);
}
if (row_sum > max_row_sum)
{
max_row_sum = row_sum;
}
}
return max_row_sum;
}
// Calculate the L2 norm (Euclidean norm) of a vector
double l2_norm(const vector<double> &v)
{
double sum_of_squares = 0.0;
for (double val : v)
{
sum_of_squares += val * val;
}
return sqrt(sum_of_squares);
}
// -----------------------------------------------------------
// Matrix and Vector Operations
// -----------------------------------------------------------
// Multiply a matrix A by a vector x
vector<double> multiply_matrix_vector(const vector<vector<double>> &A, const vector<double> &x)
{
int rows_A = A.size();
if (rows_A == 0)
{
return {}; // Empty matrix results in an empty vector
}
int cols_A = A[0].size();
int size_x = x.size();
if (cols_A != size_x)
{
throw invalid_argument("Error: The number of columns in the matrix A must be equal to the size of the vector x for multiplication.");
}
vector<double> result(rows_A, 0.0);
for (int i = 0; i < rows_A; ++i)
{
for (int j = 0; j < cols_A; ++j)
{
result[i] += A[i][j] * x[j];
}
}
return result;
}
// Subtract two vectors x - y
vector<double> subtract_vectors(const vector<double> &x, const vector<double> &y)
{
int size_x = x.size();
int size_y = y.size();
if (size_x != size_y)
{
throw invalid_argument("Error: The sizes of the vectors x and y must be equal for subtraction.");
}
vector<double> result(size_x);
for (int i = 0; i < size_x; ++i)
{
result[i] = x[i] - y[i];
}
return result;
}
// Subtract two matrices
vector<vector<double>> subtract_matrices(const vector<vector<double>> &A, const vector<vector<double>> &B)
{
int rows_A = A.size();
int cols_A = (rows_A > 0) ? A[0].size() : 0;
int rows_B = B.size();
int cols_B = (rows_B > 0) ? B[0].size() : 0;
if (rows_A != rows_B || cols_A != cols_B)
{
cerr << "Error: Matrices must have the same dimensions for subtraction." << endl;
return {};
}
vector<vector<double>> result(rows_A, vector<double>(cols_A));
for (int i = 0; i < rows_A; ++i)
{
for (int j = 0; j < cols_A; ++j)
{
result[i][j] = A[i][j] - B[i][j];
}
}
return result;
}
// Matrix multiplication
// FPC_INSTRUMENT_FUNC
vector<vector<double>> matrix_multiply(const vector<vector<double>> &A, const vector<vector<double>> &B)
{
int rows_A = A.size();
if (rows_A == 0)
{
return {};
}
int cols_A = A[0].size();
int rows_B = B.size();
if (rows_B == 0)
{
return {};
}
int cols_B = B[0].size();
if (cols_A != rows_B)
{
cerr << "Error: Number of columns must be equal to the number of rows for multiplication." << endl;
return {};
}
vector<vector<double>> result(rows_A, vector<double>(cols_B, 0.0));
for (int i = 0; i < rows_A; ++i)
{
for (int j = 0; j < cols_B; ++j)
{
for (int k = 0; k < cols_A; ++k)
{
result[i][j] += A[i][k] * B[k][j];
}
}
}
// Using Kahan Summation Algorithm
/*for (int i = 0; i < rows_A; ++i)
{
for (int j = 0; j < cols_B; ++j)
{
double sum = 0.0;
double c = 0.0; // Compensation for lost low-order bits
for (int k = 0; k < cols_B; ++k)
{
double y = A[i][k] * B[k][j] - c;
double t = sum + y;
c = (t - sum) - y; // High-order bits of y lost
sum = t;
}
result[i][j] = sum;
}
}*/
return result;
}
std::vector<std::vector<double>> transpose_matrix(const std::vector<std::vector<double>> &matrix)
{
if (matrix.empty() || matrix[0].empty())
{
return {}; // Return an empty matrix for empty input
}
size_t rows = matrix.size();
size_t cols = matrix[0].size();
std::vector<std::vector<double>> transposed_matrix(cols, std::vector<double>(rows));
for (size_t i = 0; i < rows; ++i)
{
for (size_t j = 0; j < cols; ++j)
{
transposed_matrix[j][i] = matrix[i][j];
}
}
return transposed_matrix;
}
std::vector<std::vector<double>> multiply_matrix_constant(const std::vector<std::vector<double>> &A, const double &c)
{
size_t rows_A = A.size();
if (rows_A == 0)
{
return {};
}
size_t cols_A = A[0].size();
vector<vector<double>> result(A);
for (size_t row = 0; row < rows_A; ++row)
{
for (size_t col = 0; col < cols_A; ++col)
{
result[row][col] *= c;
}
}
return result;
}
/***********************************************/
/* FP32 API */
/***********************************************/
// Matrix multiplication
vector<vector<float>> matrix_multiply(const vector<vector<float>> &A, const vector<vector<float>> &B)
{
int rows_A = A.size();
if (rows_A == 0)
{
return {};
}
int cols_A = A[0].size();
int rows_B = B.size();
if (rows_B == 0)
{
return {};
}
int cols_B = B[0].size();
if (cols_A != rows_B)
{
cerr << "Error: Number of columns must be equal to the number of rows for multiplication." << endl;
return {};
}
vector<vector<float>> result(rows_A, vector<float>(cols_B, 0.0));
for (int i = 0; i < rows_A; ++i)
{
for (int j = 0; j < cols_B; ++j)
{
for (int k = 0; k < cols_A; ++k)
{
result[i][j] += A[i][k] * B[k][j];
}
}
}
return result;
}
std::vector<std::vector<float>> multiply_matrix_constant(const std::vector<std::vector<float>> &A, const float &c)
{
size_t rows_A = A.size();
if (rows_A == 0)
{
return {};
}
size_t cols_A = A[0].size();
vector<vector<float>> result(A);
for (size_t row = 0; row < rows_A; ++row)
{
for (size_t col = 0; col < cols_A; ++col)
{
result[row][col] *= c;
}
}
return result;
}
std::vector<std::vector<float>> transpose_matrix(const std::vector<std::vector<float>> &matrix)
{
if (matrix.empty() || matrix[0].empty())
{
return {}; // Return an empty matrix for empty input
}
size_t rows = matrix.size();
size_t cols = matrix[0].size();
std::vector<std::vector<float>> transposed_matrix(cols, std::vector<float>(rows));
for (size_t i = 0; i < rows; ++i)
{
for (size_t j = 0; j < cols; ++j)
{
transposed_matrix[j][i] = matrix[i][j];
}
}
return transposed_matrix;
}