template
class Matrix {
public:
vector<vector> matrix;
code
Matrix() {}
Matrix(const vector<vector<T>> &reference) : matrix(reference) {}
Matrix(size_t n, size_t m) {
matrix = vector<vector<T>>(n, vector<T>(m, 0));
}
Matrix(size_t n) {
matrix = vector<vector<T>>(n, vector<T>(n, 0));
}
static Matrix E(size_t n) {
Matrix result(n);
for (size_t i = 0; i < result.matrix.size(); ++i) {
result.matrix[i][i] = 1;
}
return result;
}
friend istream& operator>>(istream &in, Matrix &matrix) {
size_t n, m;
in >> n >> m;
matrix.matrix.resize(n, vector<T>(m));
for (size_t i = 0; i < n; ++i) {
for (size_t j = 0; j < m; ++j) {
in >> matrix.matrix[i][j];
}
}
return in;
}
friend ostream& operator<<(ostream &out, const Matrix &matrix) {
for (const auto &row : matrix.matrix) {
for (const auto &elem : row) {
out << elem << " ";
}
out << endl;
}
return out;
}
//Проверка на равенство
bool operator=(const Matrix<T> &a) {
size_t n = a.matrix.size(), m = a.matrix[0].size();
assert(n == matrix.size());
assert(m == matrix[0].size());
for (size_t i = 0; i < n; ++i) {
for (size_t j = 0; j < m; ++j) {
if (a.matrix[i][j] != matrix[i][j]) {
return false;
}
}
}
return true;
}
};
//Умножение на матрицу
template
Matrix operator*(const Matrix &a, const Matrix &b) {
assert(a.matrix[0].size() == b.matrix.size());
size_t n = a.matrix.size(), m = b.matrix[0].size(), k = b.matrix.size();
Matrix res(n, m);
for (size_t i = 0; i < n; ++i) {
for (size_t j = 0; j < m; ++j) {
for (size_t p = 0; p < k; ++p) {
res.matrix[i][j] += a.matrix[i][p] * b.matrix[p][j];
}
}
}
return res;
}
Assistant
The issue you're encountering is due to the incorrect implementation of the operator== in your code. You have defined the operator= function that seems to intend to be an operator==, but it does not return the correct result for comparison (it's named incorrectly and also not doing what the equality operator is supposed to do, which is to return a boolean).
You need to implement the equality operator (operator==) that checks if two matrices are equal element by element. Here's how you can fix it:
Change the operator = function to operator ==.
Ensure that it properly returns a bool.
Here is the corrected part of your code:
cpp
//Умножение на число
template <typename T, typename S>
Matrix operator*(const Matrix &a, const S &alpha) {
size_t n = a.matrix.size(), m = a.matrix[0].size();
Matrix res(n, m);
for (size_t i = 0; i < n; ++i) {
for (size_t j = 0; j < m; ++j) {
res.matrix[i][j] = a.matrix[i][j] * alpha;
}
}
return res;
}
//Прибавление матрицы
template
Matrix operator+(const Matrix &a, const Matrix &b) {
size_t n = a.matrix.size(), m = a.matrix[0].size();
assert(n == b.matrix.size());
assert(m == b.matrix[0].size());
Matrix res(n, m);
for (size_t i = 0; i < n; ++i) {
for (size_t j = 0; j < m; ++j) {
res.matrix[i][j] = a.matrix[i][j] + b.matrix[i][j];
}
}
return res;
}
//Прибавление числа
template <typename T, typename S>
Matrix operator+(const Matrix &a, const S &alpha) {
size_t n = a.matrix.size(), m = a.matrix[0].size();
assert(n == m);
return a + Matrix::E(n) * alpha;
}
//Вычитание матрицы
template
Matrix operator-(const Matrix &a, const Matrix &b) {
size_t n = a.matrix.size(), m = a.matrix[0].size();
assert(n == b.matrix.size());
assert(m == b.matrix[0].size());
Matrix res(n, m);
for (size_t i = 0; i < n; ++i) {
for (size_t j = 0; j < m; ++j) {
res.matrix[i][j] = a.matrix[i][j] - b.matrix[i][j];
}
}
return res;
}
//Вычитание числа
template <typename T, typename S>
Matrix operator-(const Matrix &a, const S &alpha) {
size_t n = a.matrix.size(), m = a.matrix[0].size();
assert(n == m);
return a - Matrix::E(n) * alpha;
}
//Унарный минус
template
Matrix operator-(const Matrix &a) {
size_t n = a.matrix.size(), m = a.matrix[0].size();
Matrix res(n, m);
for (size_t i = 0; i < n; ++i) {
for (size_t j = 0; j < m; ++j) {
res.matrix[i][j] = -a.matrix[i][j];
}
}
return res;
}
signed main() {
cout.precision(10);
code
//Matrix<double> m;
//cin >> m;
// auto e = Matrix<double>::E(3); // Ematrix 3x3
for (int a = -10; a <= 10; ++a) {
for (int b = -10; b <= 10; ++b) {
for (int c = -10; c <= 10; ++c) {
Matrix<int> A = Matrix<int>({
{3, 0, -8},
{4, 3, -4},
{0, 0, -5}
});
Matrix<int> B = Matrix<int>({
{a, 0, c-a},
{-b, a, b},
{0, 0, c}
});
((A*B) - (B*A)) == Matrix<int>(3);
//cout << (((A*B) - (B*A)) == Matrix<int>(3) ? "" : "FAIL") << endl;
}
}
}
}
Invalid operands to binary expression ('Matrix' and 'Matrix')
// Equality operator
template <typename T>
bool operator==(const Matrix<T> &a, const Matrix<T> &b) {
size_t n = a.matrix.size(), m = a.matrix[0].size();
if (n != b.matrix.size() || m != b.matrix[0].size()) {
return false;
}
for (size_t i = 0; i < n; ++i) {
for (size_t j = 0; j < m; ++j) {
if (a.matrix[i][j] != b.matrix[i][j]) {
return false;
}
}
}
return true;
}
Replace your = operator function with the above == operator. Once you do this, you can compare matrices correctly in expressions like ((A*B) - (B*A)) == Matrix<int>(3).
Make sure you also adjust the places where you use = to compare matrices in your code to use == instead.