设为首页 加入收藏

TOP

C++数值计算——矩阵类的实现(一)(二)
2023-07-23 13:25:21 】 浏览:58
Tags:计算
mber_of_column != this->Number_of_column || A.Number_of_row != this->Number_of_row){ for(int i = 0; i < this->Number_of_row; i++){ delete[] p_Matrix[i]; } delete[] p_Matrix; p_Matrix = new double*[A.Number_of_row]; for( int i = 0; i < A.Number_of_row; i++){ p_Matrix[i] = new double[A.Number_of_column]; } this->Number_of_row = A.Number_of_row; this->Number_of_column = A.Number_of_column; for(int i = 0; i < this->Number_of_row; i++){ for(int j = 0; j < this->Number_of_column; j++){ this->p_Matrix[i][j] = A.p_Matrix[i][j]; } } } else{ for(int i = 0; i < this->Number_of_row; i++){ for(int j = 0; j < this->Number_of_column; j++){ this->p_Matrix[i][j] = A.p_Matrix[i][j]; } } } return *this; } //重载减法 Matrix operator- (const Matrix &A){ Matrix tempMatrix(A.Number_of_row, A.Number_of_column); if (A.Number_of_column != this->Number_of_column || A.Number_of_row != this->Number_of_row){ cout << "Matrices are in different size!" << endl; } else{ for(int i = 0; i < this->Number_of_row; i++){ for(int j = 0; j < A.Number_of_column; j++){ tempMatrix.p_Matrix[i][j] = this->p_Matrix[i][j] - A.p_Matrix[i][j]; } } } return tempMatrix; } //重载乘法 //数乘 Matrix operator*(double value){ Matrix tempMatrix(this->Number_of_row, this->Number_of_column); for(int i = 0; i < this->Number_of_row; i++){ for(int j = 0; j < this->Number_of_column; j++){ tempMatrix.p_Matrix[i][j] = value*this->p_Matrix[i][j]; } } return tempMatrix; } friend Matrix operator*(double value, const Matrix &A){ Matrix tempMatrix(A.Number_of_row, A.Number_of_column); for(int i = 0; i < A.Number_of_row; i++){ for(int j = 0; j < A.Number_of_column; j++){ tempMatrix.p_Matrix[i][j] = value*A.p_Matrix[i][j]; } } return tempMatrix; } //矩阵相乘 friend Matrix operator*(Matrix &A, Matrix &B){ Matrix tempMatrix(A.Number_of_row, B.Number_of_column); if(A.Number_of_column != B.Number_of_row){ cout<<"Invalid!"<<endl; } else{ double s; for(int i = 0; i < A.Number_of_row; i++){ for(int k = 0; k < A.Number_of_column; k++){ s=A.p_Matrix[i][k]; for(int j = 0; j < B.Number_of_column; j++){ tempMatrix.p_Matrix[i][j] += s*B.p_Matrix[k][j]; } } } } return tempMatrix; }

到此已经实现了矩阵的一些基本功能,在接下来的博客中,我将完善其他矩阵的功能,并且逐步实现一些古老的数值代数算法。

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C++实现公司设备管理系统 下一篇简单了解下最近正火的SwissTable

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目