Exemple #1
0
// sub-determinant
double DblMatrix::GetHelpDeterminant(int k)
{
	int h=GetHeight();
	assert(h+1==GetWidth());
	DblMatrix HelpDet;
	// replace the
	for(int i=0; i<h; i++)
	{
		HelpDet.push_back(DblVector());
		for(int j=0; j<h; j++)
		{
			if(j==k)
				HelpDet[i].push_back((*this)[i][h]);
			else
				HelpDet[i].push_back((*this)[i][j]);
		}
	}
	return HelpDet.GetDeterminantNxN();
}
Exemple #2
0
// solve linear NxN equation system (cramer)
int DblMatrix::SolveLinearCramer(DblVector& Solution)
{
	// get main determinant
	DblMatrix DetMat;
	double Det=0.0;
	int w = GetWidth();
	int h = GetHeight();
	for(int i=0; i<h; i++)
	{
		DetMat.push_back(DblVector());
		for(int j=0; j<w-1; j++)
			DetMat[i].push_back((*this)[i][j]);
	}
	Det = DetMat.GetDeterminantNxN();
	if(fabs(Det)<THE_EPS_DEF) return 0;
	// get solutions
	Solution.clear();
	for(int k=0; k<h; k++)
		Solution.push_back(this->GetHelpDeterminant(k)/Det);

	return 1;
}
Exemple #3
0
double DblMatrix::GetDeterminantNxN()
{
	int w=GetWidth();
	assert(w=GetHeight());
	if(w==1) return (*this)[0][0];
	if(w==2) return GetDeterminant2x2();
	if(w==3) return GetDeterminant3x3();
	double Det=0.0;
	// laplace's method (recursion)
	double s=1.0;
	if(w%2==1) s=-1.0;
	for(int i=0; i<w; i++)
	{
		
		DblMatrix SubDet;
		SubDet.assign(w-1, DblVector());
		for(int j=1; j<w; j++)
		{
			for(int k=0; k<w; k++)
			{
				if(k==i)
				{
					continue;
				}
				else
				{
					SubDet[j-1].push_back((*this)[j][k]);
				}
			}
		}
		double v = (*this)[0][i];
		
		double a = s * v * SubDet.GetDeterminantNxN();
		//std::cout << "s " << s << std::endl;
		Det += a;
		s*=-1.0;
	}
	return Det;
}