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

📄 matrixaaaa.h

📁 变分独立因子分析C++代码,H.Attias说比独立分量分析要好,但是这个程序分析效果不好,可能是程序问题,也可能是对理论理解不透
💻 H
字号:
// Matrix.h: interface and implementation of the CMatrix class.
//
/////////////////////////////////////////////////////////////////////////////////
#include <iostream>
using namespace std;
#include <memory.h>

template <class Type>
class CMatrix  
{
	int m_nr;
	int m_nc;
	Type** m_matrix;
	void create(const int nr, const int nc);
	void clear();
public:
	inline int getnr() const { return m_nr;}
	inline int getnc() const { return m_nc;}
	inline Type** getmatrix() const { return m_matrix;}
	CMatrix();
	CMatrix(const int nr,const int nc);
	CMatrix(Type** matrix, const int nr, const int nc);  
	CMatrix(const CMatrix & matrix);

	CMatrix & operator=(const CMatrix & matrix); // use it after construct

	inline Type* operator [] (const int i);
	inline const Type* operator [] (const int i) const;

	CMatrix operator -() const; 
	CMatrix operator +() const; 
	CMatrix operator ~() const; 

	CMatrix & operator += ( const CMatrix & matrix);
	CMatrix & operator -= ( const CMatrix & matrix);
	CMatrix   operator *= ( const CMatrix & matrix);
	CMatrix & operator *= ( const Type x);
	CMatrix & operator /= ( const Type x);

	CMatrix  operator + (const CMatrix & matrix);
	CMatrix  operator - (const CMatrix & matrix);
	CMatrix  operator * (const CMatrix & matrix);
	CMatrix  operator * (const Type x);
	CMatrix  operator / (const Type x);

	virtual ~CMatrix();
};



template <class Type>
CMatrix<Type>::CMatrix()
{
	m_nr=m_nc=0;
	m_matrix=0;
}

template <class Type>
CMatrix<Type>::~CMatrix()
{
	clear();
}
template <class Type>
void CMatrix<Type>::clear()
{
	if(m_matrix!=0)
	{
		for(int i=0;i<m_nr;i++) delete[] m_matrix[i];
		delete[] m_matrix;
		m_nr=m_nc=0;
	}
}

template <class Type>
void CMatrix<Type>::create(const int nr, const int nc)
{
	clear();
	m_nr=nr;
	m_nc=nc;
	m_matrix=new Type*[nr];
	for(int i=0;i<nr;i++) m_matrix[i]=new Type[nc];
}

template <class Type>
CMatrix<Type>::CMatrix(const int nr, const int nc)
{
	create(nr,nc);
}

template <class Type>
CMatrix<Type>::CMatrix(Type** matrix, const int nr, const int nc)
{
	create(nr,nc);
	for(int i=0;i<m_nr;i++)
	{
		memcpy(m_matrix[i],matrix[i],m_nc*sizeof(Type));
	}
}


template <class Type>
CMatrix<Type>::CMatrix(const CMatrix & matrix)
{
	create( matrix.getnr(),matrix.getnc() );	
	for(int i=0;i<m_nr;i++)
	{
		memcpy(m_matrix[i],matrix[i],m_nc*sizeof(Type));
	}
}

template <class Type>
CMatrix<Type> & CMatrix<Type>::operator=(const CMatrix<Type> & matrix)
{
    if(&matrix==this) return *this;    
	clear();

	create(matrix.getnr(),matrix.getnc());
	for(int i=0;i<m_nr;i++) 
	{
		memcpy(m_matrix[i], matrix.getmatrix()[i],m_nc*sizeof(Type) );
	}
	return *this;
}



template <class Type>
CMatrix<Type>  CMatrix<Type>::operator -() const
{
	Type** matrix=m_matrix;
	for(int i=0;i<m_nr;i++) 
		for(int j=0;j<m_nc;j++) 
			matrix[i][j]=-matrix[i][j];
	return CMatrix(matrix,m_nr,m_nc);
}

template <class Type>
CMatrix<Type>  CMatrix<Type>::operator +() const
{
	return CMatrix(*this);
}

template <class Type>
CMatrix<Type>  CMatrix<Type>::operator ~() const
{
	CMatrix m(m_nc,m_nr);
	for(int i=0;i<m_nc;i++)
	{
		for(int j=0;j<m_nr;j++)
		{
			m[i][j]=m_matrix[j][i];
		}
	}
	return CMatrix(m);
}


template <class Type>
inline Type*  CMatrix<Type>::operator [] (const int i)
{
	return m_matrix[i];
}

template <class Type>
inline const Type*  CMatrix<Type>::operator [] (const int i) const
{
	return m_matrix[i];
}



template <class Type> 
CMatrix<Type> CMatrix<Type>::operator + (const CMatrix<Type> & matrix) 
{
	CMatrix<Type> m;
	m=*this;
	for(int i=0;i<m_nr;i++)
		for(int j=0;j<m_nc;j++) m[i][j]+=matrix[i][j];
	return CMatrix(m);
}


template <class Type> 
CMatrix<Type> CMatrix<Type>::operator - (const CMatrix<Type> & matrix) 
{
	CMatrix<Type> m;
	m=*this;
	for(int i=0;i<m_nr;i++)
	{
		for(int j=0;j<m_nc;j++) m[i][j]-=matrix[i][j];
	}
	return CMatrix(m);
}

template <class Type> 
CMatrix<Type> CMatrix<Type>::operator * (const CMatrix<Type> & matrix) 
{
	CMatrix m(m_nr,matrix.getnc());
	for(int i=0;i<m_nr;i++)
	{
		for(int j=0;j<matrix.getnc();j++)
		{
			m[i][j]=0;
			for(int k=0;k<m_nc;k++)
			{
				m[i][j]+=m_matrix[i][k]*matrix[k][j];
			}
		}
	}
	return CMatrix(m);
}

template <class Type> 
CMatrix<Type> CMatrix<Type>::operator * (const Type x) 
{
	CMatrix<Type> m;
	m=*this;
	for(int i=0;i<m_nr;i++)
	{
		for(int j=0;j<m_nc;j++) m[i][j]*=x;
	}
	return CMatrix(m);
}

template <class Type> 
CMatrix<Type> CMatrix<Type>::operator / (const Type x) 
{
	CMatrix<Type> m;
	m=*this;
	for(int i=0;i<m_nr;i++)
	{
		for(int j=0;j<m_nc;j++) m[i][j]/=x;
	}
	return CMatrix(m);
}



//////////////////////////////////////////////////////////////////////////
template<class Type>
void displaymatrix(Type** matrix, int nr, int nc)
{
	for(int i=0;i<nr;i++)
	{
		for(int j=0;j<nc;j++)
		{
			cout<<matrix[i][j]<<"  ";
		}cout<<endl;
	}cout<<endl;
}

template<class Type>
void display(const CMatrix<Type> &matrix)
{
	displaymatrix(matrix.getmatrix(),matrix.getnr(),matrix.getnc());
}
//////////////////////////////////////////////////////////////////////////




void testmatrix()
{
	const int r=3, c=5;
	double** a=new double*[r];
	for(int i=0;i<r;i++)
	{
		a[i]=new double[c];
		for(int j=0;j<c;j++)
		{
			a[i][j]=i*c+j;
		}
	}
	CMatrix<double> m(a,r,c);display(m);
	CMatrix<double> m1; m1=~m;display(m1);
	
	display(m*m1);
	
	
	
}


⌨️ 快捷键说明

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