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

📄 matrix.inl

📁 BP人工神经网络训练源码
💻 INL
📖 第 1 页 / 共 2 页
字号:
// Matrix.inl: implementation of the CMatrix class.
// inline functions
// 朱桂斌
// 2001年3月6日
//////////////////////////////////////////////////////////////////////


// 构造函数

inline CMatrix::CMatrix (unsigned int row, unsigned int col)
{
	RowSiz = Row = row;
	ColSiz = Col = col;
	Val = new double* [row];
	for (unsigned int i=0; i < row; i++)
		Val[i] = new double [col];
}

// 拷贝构造函数

inline CMatrix::CMatrix (const CMatrix& m)
{
	RowSiz = Row = m.Row;
	ColSiz = Col = m.Col;
	
	Val = new double* [Row];
	unsigned int colsize = Col * sizeof(double);
	
	for (unsigned int i=0; i < Row; i++)
	{
		Val[i] = new double [Col];
		memcpy( Val[i], m.Val[i], colsize);
	}
}


// 析构函数

inline CMatrix::~CMatrix (void)
{
	for (unsigned int i=0; i < RowSiz; i++)
		delete [] Val[i];
	delete [] Val;
}


//  重新分配内存
inline void CMatrix::realloc (unsigned int row, unsigned int col)
{
	if (row == RowSiz && col == ColSiz)
	{
		Row = RowSiz;
		Col = ColSiz;
		return;
	}
	unsigned int i;
	double** Val1 = new double* [row];
	for (i=0; i < row; i++)
		Val1[i] = new double [col];
	
	unsigned int colSize = min(Col,col) * sizeof(double);
	unsigned int minRow = min(Row,row);
	
	for (i=0; i < minRow; i++)
		memcpy( Val1[i], Val[i], colSize);
	
	for (i=0; i < RowSiz; i++)
		delete [] Val[i];
	delete [] Val;
	
	RowSiz = Row = row;
	ColSiz = Col = col;
	Val = Val1;
	
	return;
}


//改变矩阵大小
inline void CMatrix::SetSize (unsigned int row, unsigned int col) 
{
	unsigned int i,j;
	unsigned int oldRow = Row;
	unsigned int oldCol = Col;
	
	if (row != RowSiz || col != ColSiz)
		realloc( row, col);
	
	if (row > oldRow)
		for (i=oldRow; i < row; i++)
			for (j=0; j < oldCol; j++)
				Val[i][j] = double(0);
			
			if (col > oldCol)
				for (i=0; i < row; i++)
					for (j=oldCol; j < col; j++)
						Val[i][j] = double(0);
					return;
}

// 下标操作符如(i,j)
inline double& CMatrix::operator () (unsigned int row, unsigned int col) 
{
	VERIFY(row < Row && col < Col);
	
	return Val[row][col];
}

// input stream function
/*inline istream& operator >> (istream& istrm, CMatrix& m)
{
	for (unsigned int i=0; i < m.Row; i++)
		for (unsigned int j=0; j < m.Col; j++)
			istrm >> m.Val[i][j];
		return istrm;
}

// output stream function
inline ostream& operator << (ostream &ostrm, CMatrix& m)
{
	for (unsigned int i=0; i < m.Row; i++)
	{
		for (unsigned int j=0; j < m.Col; j++)
			cout << m.Val[i][j] << '\t';
		cout << endl;
	}
	return ostrm;
}
*/

// 赋值操作符
inline CMatrix& CMatrix::operator = (const CMatrix& m) 
{
	if (Row != m.Row || Col != m.Col)
		realloc( m.Row,m.Col);
	
	unsigned int colbyte = m.Col * sizeof(double);
	
	for (unsigned int i=0; i < m.Row; i++)
		memcpy( Val[i], m.Val[i], colbyte);
	
	return *this;
}

// 拷贝内容,有可能扩大尺寸,但不缩小
inline CMatrix& CMatrix::CopyFrom(const CMatrix& m)
{
	if (Row < m.Row || Col < m.Col)
	{
		unsigned int newrow, newcol;
		newrow=max(Row,m.Row);
		newcol=max(Col,m.Col);
		realloc( newrow,newcol);
	}
	
	unsigned int colbyte = m.Col * sizeof(double);
	
	for (unsigned int i=0; i < m.Row; i++)
		memcpy( Val[i], m.Val[i], colbyte);
	
	return *this;
}

// 逻辑等于操作符
inline bool operator == (const CMatrix& m1, const CMatrix& m2) 
{
	bool retVal = false;
	
	if (m1.Row != m2.Row || m1.Col != m2.Col)
		return retVal;
	
	for (unsigned int i=0; i < m1.Row; i++)
		for (unsigned int j=0; j < m1.Col; i++)
			if (m1.Val[i][j] != m2.Val[i][j])
				return retVal;
			
			return true;
}

// 逻辑不等于操作符
inline bool operator != (const CMatrix& m1, const CMatrix& m2) 
{
    return (m1 == m2) ? false : true;
}


// 加、赋值结合操作
inline CMatrix& CMatrix::operator += (const CMatrix& m) 
{
	VERIFY(Row == m.Row && Col == m.Col);
	for (unsigned int i=0; i < m.Row; i++)
		for (unsigned int j=0; j < m.Col; j++)
			Val[i][j] += m.Val[i][j];
		return *this;
}


// 减、赋值结合操作
inline CMatrix& CMatrix::operator -= (const CMatrix& m) 
{
	
	VERIFY(Row == m.Row && Col == m.Col);
	
	for (unsigned int i=0; i < m.Row; i++)
		for (unsigned int j=0; j < m.Col; j++)
			Val[i][j] -= m.Val[i][j];
		return *this;
}

// 乘以标量并赋值
inline CMatrix& CMatrix::operator *= (const double& c) 
{
	for (unsigned int i=0; i < Row; i++)
		for (unsigned int j=0; j < Col; j++)
			Val[i][j] *= c;
		return *this;
}


// 乘以矩阵并赋值
inline CMatrix& CMatrix::operator *= (const CMatrix& m) 
{
	VERIFY(Col == m.Row);
	
	*this = *this * m;
	return *this;
}


// 除以标量并赋值
inline CMatrix& CMatrix::operator /= (const double& c) 
{
	for (unsigned int i=0; i < Row; i++)
		for (unsigned int j=0; j < Col; j++)
			Val[i][j] /= c;
		
		return *this;
}

// 乘方赋值
inline CMatrix& CMatrix::operator ^= (const unsigned int& pow) 
{
	for (unsigned int i=2; i <= pow; i++)
		*this = *this * *this;
	
	return *this;
}

// 取负运算
inline CMatrix CMatrix::operator - () 
{
	CMatrix temp(Row,Col);
	
	for (unsigned int i=0; i < Row; i++)
		for (unsigned int j=0; j < Col; j++)
			temp.Val[i][j] = - Val[i][j];
		
		return temp;
}

// 加运算
inline CMatrix operator + (const CMatrix& m1, const CMatrix& m2) 
{
	VERIFY(m1.Row == m2.Row || m1.Col == m2.Col);
	
	CMatrix temp(m1.Row,m1.Col);
	
	for (unsigned int i=0; i < m1.Row; i++)
		for (unsigned int j=0; j < m1.Col; j++)
			temp.Val[i][j] = m1.Val[i][j] + m2.Val[i][j];
		
		return temp;
}

// 减运算
inline CMatrix operator - (const CMatrix& m1, const CMatrix& m2) 
{
	VERIFY(m1.Row == m2.Row || m1.Col == m2.Col);
	
	CMatrix temp(m1.Row,m1.Col);
	
	for (unsigned int i=0; i < m1.Row; i++)
		for (unsigned int j=0; j < m1.Col; j++)
			temp.Val[i][j] = m1.Val[i][j] - m2.Val[i][j];
		
		return temp;
}


// 乘以标量
inline CMatrix operator * (const CMatrix& m, const double& no) 
{
	CMatrix temp(m.Row,m.Col);
	
	for (unsigned int i=0; i < m.Row; i++)
		for (unsigned int j=0; j < m.Col; j++)
			temp.Val[i][j] = no * m.Val[i][j];
		
		return temp;
}

//加标量
inline CMatrix operator + (const CMatrix& m, const double& no)
{
	CMatrix temp(m.Row,m.Col);
	
	for (unsigned int i=0; i < m.Row; i++)
		for (unsigned int j=0; j < m.Col; j++)
			temp.Val[i][j] = no + m.Val[i][j];
		
		return temp;
}

//标量减矩阵
inline CMatrix operator - (const double& no, const CMatrix& m)
{
	CMatrix temp(m.Row,m.Col);
	
	for (unsigned int i=0; i < m.Row; i++)
		for (unsigned int j=0; j < m.Col; j++)
			temp.Val[i][j] = no - m.Val[i][j];
		
		return temp;
	
}
// 相乘运算(multiplication)
inline  CMatrix operator * (const CMatrix& m1, const CMatrix& m2) 
{
	VERIFY(m1.Col == m2.Row);
	
	CMatrix temp(m1.Row,m2.Col);
	
	for (unsigned int i=0; i < m1.Row; i++)
		for (unsigned int j=0; j < m2.Col; j++)
		{
			temp.Val[i][j] = double(0);
			for (unsigned int k=0; k < m1.Col; k++)
				temp.Val[i][j] += m1.Val[i][k] * m2.Val[k][j];
		}
		return temp;
}

// 乘方(power)
inline CMatrix operator ^ (const CMatrix& m, const unsigned int& pow) 
{
	CMatrix temp(m);
	
	for (unsigned int i=2; i <= pow; i++)
		temp = temp * temp;
	
	return temp;
}


// 转置运算(transpose)
inline CMatrix operator  ~ (const CMatrix& m) 
{
	CMatrix temp(m.Col,m.Row);
	
	for (unsigned int i=0; i < m.Row; i++)
		for (unsigned int j=0; j < m.Col; j++)
			temp.Val[j][i] = m.Val[i][j];
		
		return temp;
}


// 取逆运算(inversion)
inline CMatrix operator ! (CMatrix m) 
{
	unsigned int i,j,k;
	double a1,a2,*rowptr;
	
	VERIFY (m.Row == m.Col);
	
	CMatrix temp(m.Row,m.Col);
	
	temp.Unit();
	for (k=0; k < m.Row; k++)
	{
		int indx = m.pivot(k);
		VERIFY (indx != -1);
		
		if (indx != 0)
		{
			rowptr = temp.Val[k];
			temp.Val[k] = temp.Val[indx];
			temp.Val[indx] = rowptr;
		}
		a1 = m.Val[k][k];
		for (j=0; j < m.Row; j++)
		{
			m.Val[k][j] /= a1;
			temp.Val[k][j] /= a1;
		}
		for (i=0; i < m.Row; i++)
			if (i != k)
			{
				a2 = m.Val[i][k];
				for (j=0; j < m.Row; j++)
				{
					m.Val[i][j] -= a2 * m.Val[k][j];
					temp.Val[i][j] -= a2 * temp.Val[k][j];
				}
			}
	}

⌨️ 快捷键说明

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