www.pudn.com > LJ_Pre_Modified_1.4.rar > Matrix.cpp


// Matrix.cpp: implementation of the CMatrix class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "LJ.h" 
#include "Matrix.h" 
 
//添加的头文件                                                                            
#include                                                                                    
#include                                                                                  
#include                                                                                 
//#include  
    
 
 
#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; imtx[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; imtx[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; imtx[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; imtx[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