Beispiel #1
0
void tst_lupdecompose(void)
{
	double EPS=1e-10;
	Matrix ww;
	ww.EyeMat(3);
	ww(1,0)=10;//make it not diagnal dominate.
	Matrix ll,uu,pp;
	ww.LUPdecompose(ll,uu,pp);
	Matrix r0,r1,r2;
	MatrixMultiply(ll,uu,r0);
	MatrixMultiply(pp,ww,r1);
	MatrixSub(r0,r1,r2);
	
	assert(r2.fnorm()<EPS);
	ww.EyeMat(20);
	
	for(int i=0;i<20*20;i++){
		ww.data[i]=(rand() % 100 - 50) /100.0;//make it not diagnal dominate.
	}
	ww.LUPdecompose(ll,uu,pp);
	MatrixMultiply(ll,uu,r0);
	MatrixMultiply(pp,ww,r1);
	MatrixSub(r0,r1,r2);
	assert(r2.fnorm()<EPS);
	try{
		Matrix a(3,4);
		a.LUPdecompose(ll,uu,pp);		
	}
	catch(MatrixException e){
		assert(e.matrixExceptionType==DIM_NOT_MATCH);
	}
}
Beispiel #2
0
void tst_getinversion(void)
{
	double EPS=1e-10;
	srand ( 60637);
	Matrix mm(20,20);
	for(int i=0; i<20*20; i++)mm(i/20,i%20)=(rand() % 10000)/10000.0;
	Matrix invmm;
	mm.GetInversion(invmm);
	Matrix i20;
	i20.EyeMat(20);
	Matrix r0,r1;
	MatrixMultiply(invmm,mm,r0);
	MatrixSub(r0,i20,r1);
	assert(r1.fnorm()<EPS);
	const int n = 20;
	Matrix m(n,n);
	for (int i=0; i < n; i++) m(i,i) = 0.01;
	Matrix inverse;
	m.GetInversion(inverse);
	for (int i=0; i < n; i++) assert(fabs(inverse(i,i)-100)<EPS);
	try{
		Matrix a(3,3);
		for(int i=0;i<2;i++)
			a.data[i*3+i]=i;
		//a is not full rank;
		Matrix b;
		a.GetInversion(b);
	}
	catch(MatrixException e){
		assert(e.matrixExceptionType==NOT_FULL_RANK);
	}
	try{
		Matrix a(3,4);
		for(int i=0;i<3;i++)
			a.data[i*4+i]=1;
		Matrix b;
		a.GetInversion(b);
	}
	catch(MatrixException e){
		assert(e.matrixExceptionType==DIM_NOT_MATCH);
	}
	for(int hh=0;hh<10000;hh++){
	try{
		int dim=rand() % 100;
		Matrix mm(dim,dim);
		for(int i=0;i<dim*dim;i++)
			mm.data[i]=((rand() -50) % 100)/100.0;
		Matrix imm;
		mm.GetInversion(imm);
		Matrix ll,uu,pp;
		imm.LUPdecompose(ll,uu,pp);
		double condmax=0;
		double condmin=1e20;
		for(int i=0;i<dim;i++){
			if(fabs(uu.data[i*dim+i])<condmin)
				condmin=fabs(uu.data[i*dim+i]);
			if(fabs(uu.data[i*dim+i])>condmax)
				condmax=fabs(uu.data[i*dim+i]);
		}
		double cond=condmax/condmin;
		
		Matrix immmm;
		MatrixMultiply(imm,mm,immmm);
		Matrix eyedim;
		eyedim.EyeMat(dim);
		
		immmm.subtract(eyedim);
		assert(immmm.fnorm()<1e-5);
	}
	catch(MatrixException e){
		string s=e.what();
	}
	}
}