C++矩阵类(2)

2019-02-14 21:55

{ for(int i=0; irow; i++){ for(int j=0; jcol; j++) if(fabs(this->get(i,j)) <= 1.0e-10) cout << setiosflags(ios::left) << setw(12) << 0.0 << ' ';

else cout << setiosflags(ios::left) << setw(12) << this->get(i,j) << ' ';

cout << endl; } }

// 获取矩阵元素

// 注意这里矩阵下标从(0,0)开始 double Matrix::get(const int i, const int j)const

{ return this->mtx[i*this->col + j]; }

// 修改矩阵元素 void Matrix::set(const int i, const int j, const double e)

{ this->mtx[i*this->col + j] = e;

} // 重载赋值操作符,由于成员变量中含有动态分配 Matrix &Matrix::operator= (const Matrix &obj) { if(this == &obj) // 将一个矩阵赋给它自己时简单做返回即可 return *this; delete[] this->mtx; // 首先删除目的操作数的动态空间 this->row = obj.getRow(); this->col = obj.getCol(); this->n = obj.getN(); this->mtx = new double[n]; // 重新分配空间保存obj的数组 for(int i=0; imtx[i] = obj.getMtx()[i];

return *this; }

// 负号操作符,返回值为该矩阵的负矩阵,原矩阵不变 Matrix Matrix::operator- ()const { // 为了不改变原来的矩阵,此处从新构造一个矩阵 Matrix _A(this->row, this->col);

for(int i=0; i<_A.n; i++) _A.mtx[i] = -(this->mtx[i]); return _A; }

// 矩阵求和,对应位置元素相加 Matrix operator+ (const Matrix &A, const Matrix &B)

{ Matrix AB(A.row, A.col); if(A.row!=B.row || A.col!=B.col){ cout << \如果矩阵A和B行列数不一致则不可相加 exit(0); } for(int i=0; i

// 矩阵减法,用加上一个负矩阵来实现 Matrix operator- (const Matrix &A, const Matrix &B)

{ return (A + (-B)); }

// 矩阵乘法 Matrix operator* (const Matrix &A, const Matrix &B)

{ if(A.col != B.row){ // A的列数必须和B的行数一致 cout << \ exit(0); } Matrix AB(A.row, B.col); // AB用于保存乘积 for(int i=0; i

return AB; }

// 矩阵与实数相乘

Matrix operator* (const double &a, const Matrix &B) {

Matrix aB(B);

for(int i=0; i

for(int j=0; j

return aB; }

// 矩阵的转置 将(i,j)与(j,i)互换

// 此函数返回一个矩阵的转置矩阵,并不改变原来的矩阵 Matrix trv(const Matrix &A) {

Matrix AT(A.col, A.row); for(int i=0; i

for(int j=0; j

return AT; }

// 矩阵行列式值,采用列主元消去法 double det(Matrix A) { if(A.row != A.col) { // 矩阵必须为n*n的才可进行行列式求值 cout << \ return 0.0; // 如果不满足行列数相等返回0.0

} double detValue = 1.0; // 用于保存行列式值 for(int i=0; i

double max = fabs(A.get(i,i)); // 主元初始默认为右下方矩阵首个元素 int ind = i; // 主元行号默认为右下方矩阵首行

for(int j=i+1; j max){ // 遇到绝对值更大的元素 max = fabs(A.get(j,i)); // 更新主元值 ind = j; // 更新主元行号

} }//loop j //------------------- 移动主元行 -----------------------------

if(max <= 1.0e-10) return 0.0; // 右下方矩阵首行为零,显然行列式值为零 if(ind != i){ // 主元行非右下方矩阵首行

for(int k=i; k

}

detValue = -detValue; // 互换行列式两行,行列式值反号 } //------------------- 消元 ----------------------------------

for(j=i+1; j

for(int k=i; k

} detValue *= A.get(i,i); // 每步消元都会产生一个对角线上元素,将其累乘 }// loop i // 注意矩阵最后一个元素在消元的过程中没有被累乘到 return detValue * A.get(A.getRow()-1, A.getRow()-1);

}//det() // A的逆矩阵 高斯-若当消去法,按列选主元 Matrix inv(Matrix A) { if(A.row != A.col){ // 只可求狭义逆矩阵,即行列数相同 cout << \ exit(0); } // 构造一个与A行列相同的单位阵B Matrix B(A.row,A.col); for(int r=0; r

// 对矩阵A进行A.row次消元运算,每次保证第K列只有对角线上非零 // 同时以同样的操作施与矩阵B,结果A变为单位阵B为所求逆阵 for(int k=0; k

double max = fabs(A.get(k,k)); // 主元初始默认为右下方矩阵首个元素 int ind = k; // 主元行号默认为右下方矩阵首行

// 结果第ind行为列主元行 for(int n=k+1; n max){ // 遇到绝对值更大的元素 max = fabs(A.get(n,k)); // 更新主元值 ind = n; // 更新主元行号

} } //------------------- 移动主元行 -------------------------------- if(ind != k){ // 主元行不是右下方矩阵首行

for(int m=k; m

double tempa = A.get(k,m); A.set(k, m, A.get(ind,m)); A.set(ind, m, tempa);

} for(m=0; m

} } //--------------------- 消元 -----------------------------------

// 第k次消元操作,以第k行作为主元行,将其上下各行的第k列元素化为零 // 同时以同样的参数对B施以同样的操作,此时可以将B看作A矩阵的一部分 for(int i=0; i

for(j=0; j

}//end if }//loop i double Mkk = 1.0/A.get(k,k); for(int j=0; j

}//loop k return B; }//inv()

NO 03

#pragma once

#include \#include \//矩阵类-声明

class Matrix {

private: //矩阵宽度 long width;


C++矩阵类(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:天津重点中学2010年八年级数学下学期期末测试题人教版[1]

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: