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
| class Matrix { private: vector<vector<double>> data; size_t rows, cols; public: Matrix(size_t r, size_t c, double initValue = 0.0) : rows(r), cols(c), data(r, vector<double>(c, initValue)) {} Matrix(const Matrix& other) : rows(other.rows), cols(other.cols), data(other.data) {} Matrix(initializer_list<initializer_list<double>> init) { rows = init.size(); cols = rows > 0 ? init.begin()->size() : 0; data.resize(rows); size_t i = 0; for (const auto& row : init) { data[i].resize(cols); size_t j = 0; for (double val : row) { if (j < cols) data[i][j] = val; j++; } i++; } } Matrix& operator=(const Matrix& other) { if (this != &other) { rows = other.rows; cols = other.cols; data = other.data; } return *this; } vector<double>& operator[](size_t row) { if (row >= rows) throw out_of_range("Row index out of range"); return data[row]; } const vector<double>& operator[](size_t row) const { if (row >= rows) throw out_of_range("Row index out of range"); return data[row]; } double& operator()(size_t row, size_t col) { if (row >= rows || col >= cols) { throw out_of_range("Matrix index out of range"); } return data[row][col]; } const double& operator()(size_t row, size_t col) const { if (row >= rows || col >= cols) { throw out_of_range("Matrix index out of range"); } return data[row][col]; } Matrix operator+(const Matrix& other) const { if (rows != other.rows || cols != other.cols) { throw invalid_argument("Matrix dimensions must match for addition"); } Matrix result(rows, cols); for (size_t i = 0; i < rows; i++) { for (size_t j = 0; j < cols; j++) { result.data[i][j] = data[i][j] + other.data[i][j]; } } return result; } Matrix operator-(const Matrix& other) const { if (rows != other.rows || cols != other.cols) { throw invalid_argument("Matrix dimensions must match for subtraction"); } Matrix result(rows, cols); for (size_t i = 0; i < rows; i++) { for (size_t j = 0; j < cols; j++) { result.data[i][j] = data[i][j] - other.data[i][j]; } } return result; } Matrix operator*(const Matrix& other) const { if (cols != other.rows) { throw invalid_argument("Invalid matrix dimensions for multiplication"); } Matrix result(rows, other.cols); for (size_t i = 0; i < rows; i++) { for (size_t j = 0; j < other.cols; j++) { for (size_t k = 0; k < cols; k++) { result.data[i][j] += data[i][k] * other.data[k][j]; } } } return result; } Matrix operator*(double scalar) const { Matrix result(rows, cols); for (size_t i = 0; i < rows; i++) { for (size_t j = 0; j < cols; j++) { result.data[i][j] = data[i][j] * scalar; } } return result; } Matrix& operator+=(const Matrix& other) { *this = *this + other; return *this; } Matrix& operator*=(double scalar) { for (size_t i = 0; i < rows; i++) { for (size_t j = 0; j < cols; j++) { data[i][j] *= scalar; } } return *this; } bool operator==(const Matrix& other) const { if (rows != other.rows || cols != other.cols) return false; const double EPSILON = 1e-9; for (size_t i = 0; i < rows; i++) { for (size_t j = 0; j < cols; j++) { if (abs(data[i][j] - other.data[i][j]) > EPSILON) { return false; } } } return true; } bool operator!=(const Matrix& other) const { return !(*this == other); } size_t getRows() const { return rows; } size_t getCols() const { return cols; } Matrix transpose() const { Matrix result(cols, rows); for (size_t i = 0; i < rows; i++) { for (size_t j = 0; j < cols; j++) { result.data[j][i] = data[i][j]; } } return result; } friend ostream& operator<<(ostream& os, const Matrix& m); friend Matrix operator*(double scalar, const Matrix& m); };
ostream& operator<<(ostream& os, const Matrix& m) { for (size_t i = 0; i < m.rows; i++) { os << "["; for (size_t j = 0; j < m.cols; j++) { os << m.data[i][j]; if (j < m.cols - 1) os << ", "; } os << "]" << endl; } return os; }
Matrix operator*(double scalar, const Matrix& m) { return m * scalar; }
void matrixDemo() { cout << "\n=== 矩阵类运算符重载演示 ===" << endl; Matrix m1{{1, 2}, {3, 4}}; Matrix m2{{5, 6}, {7, 8}}; Matrix m3(2, 2, 0); cout << "矩阵 m1:" << endl << m1 << endl; cout << "矩阵 m2:" << endl << m2 << endl; cout << "m1 + m2:" << endl << (m1 + m2) << endl; cout << "m1 - m2:" << endl << (m1 - m2) << endl; cout << "m1 * m2:" << endl << (m1 * m2) << endl; cout << "m1 * 2.5:" << endl << (m1 * 2.5) << endl; cout << "3.0 * m2:" << endl << (3.0 * m2) << endl; cout << "使用下标访问:" << endl; cout << "m1[0][1] = " << m1[0][1] << endl; cout << "使用函数调用运算符:" << endl; cout << "m1(1,0) = " << m1(1, 0) << endl; m3(0, 0) = 10; m3(0, 1) = 20; m3(1, 0) = 30; m3(1, 1) = 40; cout << "修改后的 m3:" << endl << m3 << endl; cout << "m1 的转置:" << endl << m1.transpose() << endl; m3 += m1; cout << "m3 += m1:" << endl << m3 << endl; cout << "m1 == m2: " << (m1 == m2) << endl; cout << "m1 != m2: " << (m1 != m2) << endl; }
|