Skip to content

Commit 7829739

Browse files
committed
matrix multiplication implemented
1 parent 236afbc commit 7829739

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

Variants/Lab_12/var_12/main.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,28 @@ int main() {
144144

145145
Matrix summ = Matrix(5, 1) + Matrix(5, 2);
146146
summ.print();
147+
summ -= Matrix(5, 4);
148+
summ.print();
149+
150+
// проверка умножения
151+
152+
Matrix mult1({
153+
{1, 2, 3},
154+
{4, 5, 6},
155+
{7, 8, 9},
156+
});
157+
158+
Matrix mult2({
159+
{6, 1, 8},
160+
{0, 4, 2},
161+
{6, 1, 8},
162+
});
163+
164+
mult1.print();
165+
cout << " *\n" << endl;
166+
mult2.print();
167+
168+
cout << " =\n" << endl;
169+
mult1 *= mult2;
170+
mult1.print();
147171
}

Variants/Lab_12/var_12/matrix.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,50 @@ Matrix& Matrix::operator+=(const Matrix& m) {
140140
return *this;
141141
}
142142

143+
Matrix& Matrix::operator-=(const Matrix& m) {
144+
try {
145+
if (arr.size() == m.arr.size()) {
146+
for (int x = 0; x < m.arr.size(); x++) {
147+
for (int y = 0; y < m.arr.size(); y++) {
148+
arr[y][x] -= m.arr[y][x];
149+
}
150+
}
151+
} else {
152+
throw "Matrices have different sizes";
153+
}
154+
} catch (const char *s) {
155+
cerr << "Error in + operator: " << s << endl;
156+
exit(1);
157+
}
158+
return *this;
159+
}
160+
143161
Matrix operator+(Matrix a, const Matrix& b) {
144162
a += b;
145163
return a;
164+
}
165+
166+
Matrix operator-(Matrix a, const Matrix& b) {
167+
a -= b;
168+
return a;
169+
}
170+
171+
Matrix& Matrix::operator*=(const Matrix& right) {
172+
unsigned int size = this->size();
173+
Matrix result(size, 0);
174+
for (int result_x = 0; result_x < size; result_x++) {
175+
for (int result_y = 0; result_y < size; result_y++) {
176+
double summ = 0;
177+
for (int i = 0; i < size; i++)
178+
summ += arr[result_y][i] * right.arr[i][result_x];
179+
result.arr[result_y][result_x] = summ;
180+
}
181+
}
182+
*this = result;
183+
return *this;
184+
}
185+
186+
Matrix operator*(Matrix a, const Matrix& b) {
187+
a *= b;
188+
return a;
146189
}

Variants/Lab_12/var_12/matrix.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ class Matrix {
1111
vector <vector <double>> arr;
1212

1313
public:
14+
// конструтроры (не лего)
1415
Matrix(unsigned int size = 1, double def = 0);
15-
Matrix(vector <vector <double>> temp);
16+
Matrix(vector <vector <double>> temp);
1617

18+
// сеттеры
1719
Matrix& set(unsigned int x, unsigned int y, double value);
1820
Matrix& set(vector <vector <double>> temp);
1921
Matrix& set(Matrix& m);
2022

23+
// геттеры
2124
double get(unsigned int x, unsigned int y);
2225
const unsigned int size();
2326

@@ -30,6 +33,10 @@ class Matrix {
3033
// операторы
3134

3235
Matrix& operator+=(const Matrix& x);
36+
Matrix& operator-=(const Matrix& x);
37+
Matrix& operator*=(const Matrix& x);
38+
3339
friend Matrix operator+(Matrix a, const Matrix &b);
34-
40+
friend Matrix operator-(Matrix a, const Matrix &b);
41+
friend Matrix operator*(Matrix a, const Matrix &b);
3542
};

0 commit comments

Comments
 (0)