-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulMatriсe.cpp
More file actions
163 lines (147 loc) · 4.83 KB
/
mulMatriсe.cpp
File metadata and controls
163 lines (147 loc) · 4.83 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
#include <fstream>
#include <iostream>
#include <vector>
#include <sstream>
#include <string>
#include <chrono>
#include <time.h>
using namespace std;
using namespace std::chrono;
vector<vector<float>> readMatrix(const string &name_file)
{
fstream file;
vector<vector<float>> vec;
file.open(name_file);
string line;
while (getline(file, line))
{
stringstream ss(line);
string temp;
vector<float> row;
while (ss >> temp)
{
try
{
float value = stof(temp);
row.push_back(value);
}
catch (const std::invalid_argument &e)
{
cerr << "Invalid argument in file " << name_file << ": " << temp << endl;
file.close();
return {};
}
catch (const std::out_of_range &e)
{
cerr << "Out of range in file " << name_file << ": " << temp << endl;
file.close();
return {};
}
}
if (!row.empty())
{
vec.push_back(row);
}
}
file.close();
return vec;
}
void writeMatrix(vector<vector<float>> vec, const string &name_file)
{
ofstream file;
file.open(name_file);
int size_row = vec.size(), size_column = vec[0].size();
for (int i = 0; i < size_row; i++)
{
for (int j = 0; j < size_column; j++)
{
file << vec[i][j] << " ";
}
file << "\n";
}
file.close();
}
void writeTime(const string &content, const string &name_file)
{
ofstream file;
file.open(name_file);
file << content;
file.close();
}
vector<vector<float>> mulMatrices(vector<vector<float>> vec1, vector<vector<float>> vec2)
{
int row1 = vec1.size();
if (row1 == 0)
{
cerr << "Error: Matrix 1 is empty." << endl;
return {};
}
int column1 = vec1[0].size();
int row2 = vec2.size();
if (row2 == 0)
{
cerr << "Error: Matrix 2 is empty." << endl;
return {};
}
int column2 = vec2[0].size();
vector<vector<float>> vec_result(row1, vector<float>(column2, 0.0f));
if (column1 != row2)
{
cerr << "Error: Matrix dimensions incompatible for multiplication." << endl;
return {};
}
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < column2; j++)
{
float value = 0;
for (int z = 0; z < column1; z++)
{
value += vec1[i][z] * vec2[z][j];
}
vec_result[i][j] = value;
}
}
return vec_result;
}
int run(const string &input1, const string &input2, const string &output)
{
auto start = high_resolution_clock::now();
vector<vector<float>> vec1 = readMatrix(input1);
vector<vector<float>> vec2 = readMatrix(input2);
if (vec1.empty() || vec2.empty())
{
cerr << "Error: One or both input matrices are empty. Cannot multiply." << endl;
return -1;
}
vector<vector<float>> result = mulMatrices(vec1, vec2);
if (result.empty())
{
cerr << "Error: Error during multiplication" << endl;
return -1;
}
writeMatrix(result, output);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << "Times: " << duration.count() << "\n";
return duration.count();
}
int main()
{
int time_10 = run("Matrix_1/matrix1_10.txt", "Matrix_2/matrix2_10.txt", "Output/output_10.txt");
int time_20 = run("Matrix_1/matrix1_20.txt", "Matrix_2/matrix2_20.txt", "Output/output_20.txt");
int time_30 = run("Matrix_1/matrix1_30.txt", "Matrix_2/matrix2_30.txt", "Output/output_30.txt");
int time_40 = run("Matrix_1/matrix1_40.txt", "Matrix_2/matrix2_40.txt", "Output/output_40.txt");
int time_50 = run("Matrix_1/matrix1_50.txt", "Matrix_2/matrix2_50.txt", "Output/output_50.txt");
int time_60 = run("Matrix_1/matrix1_60.txt", "Matrix_2/matrix2_60.txt", "Output/output_60.txt");
int time_70 = run("Matrix_1/matrix1_70.txt", "Matrix_2/matrix2_70.txt", "Output/output_70.txt");
int time_80 = run("Matrix_1/matrix1_80.txt", "Matrix_2/matrix2_80.txt", "Output/output_80.txt");
int time_90 = run("Matrix_1/matrix1_90.txt", "Matrix_2/matrix2_90.txt", "Output/output_90.txt");
int time_100 = run("Matrix_1/matrix1_100.txt", "Matrix_2/matrix2_100.txt", "Output/output_100.txt");
int time_1000 = run("Matrix_1/matrix1_1000.txt", "Matrix_2/matrix2_1000.txt", "Output/output_1000.txt");
stringstream total_times;
total_times << time_10 << ", " << time_20 << ", " << time_30 << ", " << time_40 << ", " << time_50 << ", " << time_60 << ", " << time_70 << ", " << time_80 << ", " << time_90 << ", " << time_100 << ", " << time_1000;
string times = total_times.str();
writeTime(times, "times.txt");
return 0;
}