-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSquareMatrix.cpp
More file actions
142 lines (107 loc) · 3.67 KB
/
CSquareMatrix.cpp
File metadata and controls
142 lines (107 loc) · 3.67 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
#include <iostream>
#include <array>
#include <random>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <chrono>
template <typename T, size_t Size>
class CSquareMatrix {
private:
std::array<std::array<T, Size>, Size> data_;
public:
CSquareMatrix(): data_{} {}
CSquareMatrix(const CSquareMatrix& other) = default;
CSquareMatrix& operator=(const CSquareMatrix& other) = default;
~CSquareMatrix() = default;
std::array<T, Size>& operator[](size_t index) {
return data_[index];
}
const std::array<T, Size>& operator[](size_t index) const {
return data_[index];
}
void generateFullMatrix() {
static std::random_device rd;
static std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(1, 10);
for (auto& row : data_) {
for (auto& num : row) {
num = dis(gen);
}
}
}
};
template<typename T, size_t Size1, size_t Size2>
CSquareMatrix<T, Size1> multiplyMatrices(const CSquareMatrix<T, Size1>& mat1, const CSquareMatrix<T, Size2>& mat2) {
if (Size1 != Size2) {
throw std::invalid_argument("Matrices must have the same size for multiplication");
}
CSquareMatrix<T, Size1> result;
for (size_t i = 0; i < Size1; i++) {
for (size_t j = 0; j < Size1; j++) {
for (size_t k = 0; k < Size1; k++) {
result[i][j] += mat1[i][k]*mat2[k][j];
}
}
}
return result;
}
template<typename T, size_t Size1, size_t Size2>
void writeOriginalMatricesFile(const CSquareMatrix<T, Size1>& mat1, const CSquareMatrix<T, Size2>& mat2) {
if (Size1 != Size2) {
throw std::invalid_argument("Matrices must have the same size for multiplication");
}
std::ofstream file("original_matrices.txt");
if (!file.is_open()) {
throw std::runtime_error("Couldn't open the file");
}
for (size_t i = 0; i < Size1; i++) {
for (size_t j = 0; j < Size1; j++) {
file << mat1[i][j] << " ";
}
file << "\n";
}
file << '\n';
for (size_t i = 0; i < Size2; i++) {
for (size_t j = 0; j < Size2; j++) {
file << mat2[i][j] << " ";
}
file << "\n";
}
file.close();
}
template<typename T, size_t Size1, size_t Size2>
void multiplitionCheck(const CSquareMatrix<T, Size1>& mat1, const CSquareMatrix<T, Size2>& mat2) {
std::ofstream file("result_matrix.txt");
if (!file.is_open()) {
throw std::runtime_error("Couldn't open the file");
}
CSquareMatrix<int, Size1> res_mat;
auto start_multiplication = std::chrono::high_resolution_clock::now();
res_mat = multiplyMatrices(mat1, mat2);
auto end_multiplication = std::chrono::high_resolution_clock::now();
auto time_multiplication = std::chrono::duration_cast<std::chrono::microseconds>(end_multiplication - start_multiplication);
file << "Multiplication time: " << time_multiplication.count() << " microseconds\n";
file << "Number of operations: " << (2*Size1 - 1)*Size1*Size1 << "\n";
for (size_t i = 0; i < Size1; i++) {
for (size_t j = 0; j < Size1; j++) {
file << res_mat[i][j] << " ";
}
file << "\n";
}
file.close();
}
int main() {
CSquareMatrix<int, 1600> mat1;
mat1.generateFullMatrix();
CSquareMatrix<int, 1600> mat2;
mat2.generateFullMatrix();
try {
writeOriginalMatricesFile(mat1, mat2);
multiplitionCheck(mat1, mat2);
system("python verification_of_the_result.py");
std::cout << "Matrices are multiplied";
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what();
}
}