Example #1
0
/** Test matrix classes */
int main()
{
	hilbert();
	submatrix();
	sumOfSquares();
	indexDenseSubmatrix();

	spGetCol();
	spGetRow();
	spTimes();
	spIndTimes();

	sprGetCol();
	sprGetRow();
	sprTimes();
	sprIndTimes();

	Symmetry();

	return 0;
}
Example #2
0
int Householder(Matrix m, Matrix &q, valarray<double>& main_diag, valarray<double>& minor_diag)
{
	int i,j, k, Rank;
	double h, f, g, h2;
	if(Symmetry(m)!=true)
		return 0;
	Rank = m.Getcols();
	for(i=0; i<Rank; i++)
		for(j=0; j<Rank; j++)
			q(i,j)=m(i,j);
	for(i=Rank-1; i>=1; i--)
	{ 
		h=0.0;
		if(i>1)
			for(k=0; k<i; k++)
				h=h+q(i,k)*q(i,k);
		if((abs(h - 0) < 1.0e-15))
		{
			minor_diag[i]=0.0;
			if(i==1) minor_diag[i]=q(i,i-1);
			main_diag[i]=0.0;
		}
		else
		{ 
			minor_diag[i]=sqrt(h);
			if(q(i,i-1)>0.0) minor_diag[i]=-minor_diag[i];
			h=h-q(i,i-1)*minor_diag[i];
			q(i,i-1)=q(i,i-1)-minor_diag[i];
			f=0.0;
			for(j=0; j<i; j++)
			{ 
				q(j,i) = q(i,j) / h;
				g = 0.0;
				for(k=0; k<=j; k++)
					g = g + q(j,k) * q(i,k);
				if(j+1<i)
					for(k=j+1; k<i; k++)
						g = g + q(k,j) * q(i,k);
				minor_diag[j] = g / h;
				f = f + g * q(j,i);
			}
			h2 = f / (h+h);
			for(j=0; j<i; j++)
			{ 
				f = q(i,j);
				g = minor_diag[j] -h2 * f;
				minor_diag[j] = g;
				for(k=0; k<=j; k++)
					q(j,k)=q(j,k)-f*minor_diag[k]-g*q(i,k);
			}
			main_diag[i]=h;
		}
	}
	for(i=0; i<Rank-1; i++) minor_diag[i]=minor_diag[i+1];
	minor_diag[Rank-1]=0.0;
	main_diag[0]=0.0;
	for(i=0; i<Rank; i++)
	{ if((main_diag[i]!=0.0)&&(i-1>=0))
	for(j=0; j<i; j++)
	{ 
		g=0.0;
		for(k=0; k<i; k++)
			g=g+q(i,k)*q(k,j);
		for(k=0; k<i; k++)
			q(k,j)=q(k,j)-g*q(k,i);
	}
	main_diag[i]=q(i,i);
	q(i,i)=1.0;
	if(i-1>=0)
		for(j=0; j<i; j++)
		{ 
			q(i,j)=0.0;
			q(j,i)=0.0;
		}
	}
	return 1;
}