c++利用重载实现矩阵加减乘除

时间: 2023-08-02 12:05:45 浏览: 64
可以利用运算符重载来实现矩阵的加减乘除操作。以下是一个简单的实现示例: ```c++ #include <iostream> #include <vector> using namespace std; // 矩阵类定义 class Matrix { public: Matrix(int r, int c) : rows(r), cols(c), data(r, vector<double>(c)) {} // 矩阵加法运算符重载 Matrix operator+(const Matrix& other) const { if (this->rows != other.rows || this->cols != other.cols) { throw runtime_error("Matrix dimensions do not match."); } Matrix result(rows, cols); for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { result.data[i][j] = this->data[i][j] + other.data[i][j]; } } return result; } // 矩阵减法运算符重载 Matrix operator-(const Matrix& other) const { if (this->rows != other.rows || this->cols != other.cols) { throw runtime_error("Matrix dimensions do not match."); } Matrix result(rows, cols); for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { result.data[i][j] = this->data[i][j] - other.data[i][j]; } } return result; } // 矩阵乘法运算符重载 Matrix operator*(const Matrix& other) const { if (this->cols != other.rows) { throw runtime_error("Matrix dimensions do not match."); } Matrix result(this->rows, other.cols); for (int i = 0; i < this->rows; ++i) { for (int j = 0; j < other.cols; ++j) { double sum = 0; for (int k = 0; k < this->cols; ++k) { sum += this->data[i][k] * other.data[k][j]; } result.data[i][j] = sum; } } return result; } // 矩阵除法运算符重载 Matrix operator/(const Matrix& other) const { if (this->cols != other.rows) { throw runtime_error("Matrix dimensions do not match."); } Matrix inverse = other.inverse(); // 先求逆矩阵 return (*this) * inverse; // 矩阵乘法实现除法 } // 求逆矩阵 Matrix inverse() const { if (this->rows != this->cols) { throw runtime_error("Matrix is not square."); } int n = this->rows; Matrix result(n, n); // 先做初等变换,将原矩阵变成单位矩阵 for (int i = 0; i < n; ++i) { result.data[i][i] = 1; } for (int i = 0; i < n; ++i) { double factor = this->data[i][i]; for (int j = 0; j < n; ++j) { this->data[i][j] /= factor; result.data[i][j] /= factor; } for (int k = 0; k < n; ++k) { if (k == i) { continue; } double factor = this->data[k][i]; for (int j = 0; j < n; ++j) { this->data[k][j] -= factor * this->data[i][j]; result.data[k][j] -= factor * result.data[i][j]; } } } return result; } private: int rows, cols; // 矩阵行数和列数 vector<vector<double>> data; // 矩阵数据 }; int main() { Matrix A(2, 3); A.data = {{1, 2, 3}, {4, 5, 6}}; Matrix B(3, 2); B.data = {{7, 8}, {9, 10}, {11, 12}}; Matrix C = A + A; C = C - A; Matrix D = A * B; Matrix E = D / B; return 0; } ``` 在上面的示例代码中,我们定义了一个 `Matrix` 类来表示矩阵,并实现了矩阵加减乘除四种运算符重载。其中矩阵除法是通过先求逆矩阵再做矩阵乘法实现的。在 `Matrix` 类中还实现了求逆矩阵的函数 `inverse()`,用于求矩阵的逆矩阵。

相关推荐

最新推荐

recommend-type

C++实现图的邻接矩阵表示

主要为大家详细介绍了C++实现图的邻接矩阵表示,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

C++稀疏矩阵的各种基本运算并实现加法乘法

今天小编就为大家分享一篇关于C++稀疏矩阵的各种基本运算并实现加法乘法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
recommend-type

c++运算符重载矩阵运算

c++运算符重载矩阵运算 题目:对具有同行列数的整形矩阵进行加、减、乘、转置、赋值运算
recommend-type

C++ 类的赋值运算符''=''重载的方法实现

主要介绍了C++ 类的赋值运算符'='重载的方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

C++ Eigen库计算矩阵特征值及特征向量

主要为大家详细介绍了C++ Eigen库计算矩阵特征值及特征向量,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

list根据id查询pid 然后依次获取到所有的子节点数据

可以使用递归的方式来实现根据id查询pid并获取所有子节点数据。具体实现可以参考以下代码: ``` def get_children_nodes(nodes, parent_id): children = [] for node in nodes: if node['pid'] == parent_id: node['children'] = get_children_nodes(nodes, node['id']) children.append(node) return children # 测试数
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。