⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 matrix.cpp

📁 针对工业无线传感器网络的应用特点
💻 CPP
字号:
// Matrix.cpp: implementation of the CMatrix class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "LJ.h"
#include "Matrix.h"

//添加的头文件                                                                           
#include<math.h>                                                                                   
#include<stdlib.h>                                                                                 
#include<iomanip.h>                                                                                
//#include<iostream.h> 
   


#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////


// 带默认参数值的构造函数                                                                          
// 构造一个row行col列的零矩阵                                                                      
CMatrix::CMatrix(int row, int col)                                                                   
{                                                                                                  
	this->row = row;
    this->col = col;
	this->n   = row * col; 
	this->mtx = new double[n];                                               
	for(int i=0; i<n; i++)                                                                           
		this->mtx[i] = 0.0;                                                                            
}   

// 用一个数组初始化矩阵    
CMatrix::CMatrix(int row, int col, double mtx[])                                                     
{                                                                                                  
	this->row = row;
    this->col = col;                                                         
	this->n   = row * col; 
	this->mtx = new double[n];                                               
	for(int i=0; i<n; i++)                                                                           
		this->mtx[i] = mtx[i]; 
}

// 拷贝构造函数,因为成员变量含有动态空间,防止传递参数                                            
// 等操作发生错误                                                                                  
CMatrix::CMatrix(const CMatrix &obj)                                                                  
{                                                                                                  
	this->row = obj.getRow(); 
	this->col = obj.getCol(); 
	this->n   = obj.getN();                                                                          
	this->mtx = new double[n];                                                                       
	for(int i=0; i<n; i++)                                                                           
		this->mtx[i] = obj.getMtx()[i];                                                                
}          

// 获取矩阵元素
// 注意这里矩阵下标从(0,0)开始                                                                     
double CMatrix::get(const int i, const int j)//const                                                  
{                                                                                                  
	return this->mtx[i*this->col + j];                                                               
}

// 修改矩阵元素                                                                                    
void CMatrix::set(const int i, const int j, const double e)                                         
{                                                                                                  
	this->mtx[i*this->col + j] = e;                                                                  
} 

// 重载赋值操作符,由于成员变量中含有动态分配                                                      
CMatrix &CMatrix::operator= (const CMatrix &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; i<n; i++)                                                                           
		this->mtx[i] = obj.getMtx()[i];                                                                
	return *this;                                                                                    
}   

// 负号操作符,返回值为该矩阵的负矩阵,原矩阵不变                                                  
CMatrix CMatrix::operator- ()const                                                                   
{                                                                                                  
	// 为了不改变原来的矩阵,此处从新构造一个矩阵                                                    
	CMatrix _A(this->row, this->col);                                                                 
	for(int i=0; i<_A.n; i++)                                                                        
		_A.mtx[i] = -(this->mtx[i]);                                                                   
	return _A;                                                                                       
}


// 矩阵求和,对应位置元素相加                                                                      
CMatrix operator+ (const CMatrix &A, const CMatrix &B)                                                
{                                                                                                  
	//CMatrix AB(A.row, A.col);
	CMatrix AB(A.getRow(), A.getCol());
	//if(A.row!=B.row || A.col!=B.col)
	if(A.getRow() != B.getRow() || A.getCol() != B.getCol())
	{    
		//cout << "Can't do A+B\n"; // 如果矩阵A和B行列数不一致则不可相加                                
		exit(0);                                                                                       
	}                                                                                                
	//for(int i=0; i<AB.n; i++)                                                                        
	for(int i=0; i<AB.getN(); i++)
		AB.mtx[i] = A.mtx[i] + B.mtx[i];                                                               
	return AB;                                                                                       
}

// 矩阵减法,用加上一个负矩阵来实现                                                                
CMatrix operator- (const CMatrix &A, const CMatrix &B)                                                
{                                                                                                  
	return (A + (-B));                                                                               
}

// 矩阵乘法                                                                                        
//CMatrix operator* (const CMatrix &A, const CMatrix &B)
CMatrix operator* (CMatrix &A, CMatrix &B)
{   
	//if(A.col != B.row)
	if(A.getCol() != B.getRow())
	{ // A的列数必须和B的行数一致                                                  
		//cout << "Can't multiply\n";                                                                    
		exit(0);
		//&, nbsp;                                                                        
	}                                                                                                
	//CMatrix AB(A.row, B.col); // AB用于保存乘积                                                       
	CMatrix AB(A.getRow(), B.getCol()); // AB用于保存乘积
	/*
	for(int i=0; i<AB.row; i++)
		for(int j=0; j<AB.col; j++)
			for(int k=0; k<A.col; k++)
				AB.set(i, j, AB.get(i,j) + A.get(i,k)*B.get(k,j)); 
	*/
	for(int i=0; i<AB.getRow(); i++)
		for(int j=0; j<AB.getCol(); j++)
			for(int k=0; k<A.getCol(); k++)
				AB.set(i, j, AB.get(i,j) + A.get(i,k)*B.get(k,j)); 
	return AB;                                                                                       
}

// 矩阵与实数相乘
//CMatrix operator* (const double &a, const CMatrix &B)
CMatrix operator* (double &a, CMatrix &B)
{
	CMatrix aB(B);
/*
	for(int i=0; i<aB.row; i++)
		for(int j=0; j<aB.col; j++)
			aB.set(i,j, a*B.get(i,j));
*/
	for(int i=0; i<aB.getRow(); i++)
		for(int j=0; j<aB.getCol(); j++)
			aB.set(i,j, a*B.get(i,j));
	return aB;
}


/*
CMatrix::CMatrix()
{
	
}

CMatrix::~CMatrix()
{
	
}
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -