C++矩阵类实现:仿照Eigen库,实现基本运算和功能函数
C++矩阵类实现:仿照Eigen库,实现基本运算和功能函数
本文将介绍如何用C++实现一个简单的矩阵类,仿照Eigen库的矩阵实现,包含基本运算(加减乘)和功能函数(转置、打印)。
代码示例
#include <iostream>
#include <vector>
class Matrix {
private:
std::vector<std::vector<double>> data;
int rows;
int cols;
public:
Matrix(int rows, int cols) : rows(rows), cols(cols) {
data.resize(rows, std::vector<double>(cols, 0.0));
}
Matrix(const std::vector<std::vector<double>>& input) {
rows = input.size();
cols = input[0].size();
data = input;
}
int getRows() const {
return rows;
}
int getCols() const {
return cols;
}
void setValue(int row, int col, double value) {
data[row][col] = value;
}
double getValue(int row, int col) const {
return data[row][col];
}
Matrix operator+(const Matrix& other) const {
if (rows != other.rows || cols != other.cols) {
throw std::invalid_argument('Matrix dimensions must match.');
}
Matrix result(rows, cols);
for (int i = 0; i < rows; i++) {
for (int 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 std::invalid_argument('Matrix dimensions must match.');
}
Matrix result(rows, cols);
for (int i = 0; i < rows; i++) {
for (int 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 std::invalid_argument('Matrix dimensions are not compatible for multiplication.');
}
Matrix result(rows, other.cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < other.cols; j++) {
for (int k = 0; k < cols; k++) {
result.data[i][j] += data[i][k] * other.data[k][j];
}
}
}
return result;
}
Matrix transpose() const {
Matrix result(cols, rows);
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
result.data[i][j] = data[j][i];
}
}
return result;
}
void print() const {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
std::cout << data[i][j] << ' ';
}
std::cout << std::endl;
}
}
};
int main() {
std::vector<std::vector<double>> input1 = {{1, 2, 3}, {4, 5, 6}};
std::vector<std::vector<double>> input2 = {{7, 8}, {9, 10}, {11, 12}};
Matrix matrix1(input1);
Matrix matrix2(input2);
Matrix result = matrix1 * matrix2;
result.print();
return 0;
}
矩阵类功能
该矩阵类Matrix具有以下功能:
- 构造函数:
- 可以根据行数和列数构造一个零矩阵。
- 可以使用一个二维向量初始化矩阵。
getRows()和getCols()函数: 返回矩阵的行数和列数。setValue()和getValue()函数: 设置和获取特定位置的元素值。operator+()、operator-()和operator*()函数: 实现矩阵的加法、减法和乘法运算,注意运算符两边的矩阵维度必须匹配。transpose()函数: 返回矩阵的转置矩阵。print()函数: 打印矩阵的元素。
代码说明
data: 存储矩阵元素的二维向量。rows: 矩阵行数。cols: 矩阵列数。- 构造函数: 初始化矩阵数据和行列数。
operator+()、operator-()和operator*(): 重载运算符,实现矩阵的基本运算,注意维度匹配。transpose(): 实现矩阵转置,将行变为列,将列变为行。print(): 打印矩阵,方便查看矩阵内容。
示例代码说明
- 使用
std::vector<std::vector<double>>初始化两个矩阵matrix1和matrix2。 - 计算两个矩阵的乘积
result = matrix1 * matrix2。 - 打印结果矩阵
result。
总结
本文提供了一个简单的C++矩阵类示例,它实现了基本运算和功能函数,可以作为基础来进行更复杂的矩阵操作的开发。你可以根据需要进一步扩展该类,添加其他运算和功能函数。
原文地址: https://www.cveoy.top/t/topic/p2W5 著作权归作者所有。请勿转载和采集!