bool LtBackSubstitute(const MatrixTemplate<T>& a, const VectorTemplate<T>& b, VectorTemplate<T>& x)
{ 
  Assert(a.isSquare());
  Assert(a.n == b.n);
  if(x.empty()) x.resize(a.n);
  else Assert(a.n == x.n);
  int n=a.n;
 	T aii,sum;
	for(int i=n-1; i>=0; i--) {
		aii=a(i,i);
		sum=b[i];
		for(int j=i+1; j<n; j++)
			sum-=a(j,i)*x[j];
		if(aii == 0) {
		  if(!FuzzyZero(sum,(T)kBackSubZeroTolerance)) {
		    //LOG4CXX_ERROR(KrisLibrary::logger(),"LtBackSubstitute: dividing by zero: "<<sum<<"/"<<aii);
		    return false;
		  }
		  x[i]=0;
		}
		else
		  x[i]=sum/aii;
	}
	return true;
}
Example #2
0
bool LBackSubstitute(const MatrixTemplate<T>& a, const VectorTemplate<T>& b, VectorTemplate<T>& x)
{
  Assert(a.isSquare());
  Assert(a.n == b.n);
  if(x.empty()) x.resize(a.n);
  else Assert(a.n == x.n);
  int n=a.n;
	T aii,sum;
	for(int i=0; i<n; i++) {
		aii=a(i,i);
		sum=b[i];
		for(int j=0; j<i; j++)
			sum-=a(i,j)*x[j];
		if(aii == 0) {
		  if(!FuzzyZero(sum,(T)kBackSubZeroTolerance)) {
		    //cerr<<"LBackSubstitute: dividing by zero: "<<sum<<"/"<<aii<<endl;
		    //cerr<<MatrixPrinter(a)<<endl;
		    return false;
		  }
		  x[i]=0;
		}
		else
		  x[i]=sum/aii;
	}
	return true;
}
void U1BackSubstitute(const MatrixTemplate<T>& a, const VectorTemplate<T>& b, VectorTemplate<T>& x)
{ 
  Assert(a.isSquare());
  Assert(a.n == b.n);
  if(x.empty()) x.resize(a.n);
  else Assert(a.n == x.n);
  int n=a.n;
  T sum;
	for(int i=n-1; i>=0; i--) {
		sum=b(i);
		for(int j=i+1; j<n; j++)
			sum-=a(i,j)*x[j];
    x[i]=sum;
	}
}
void L1BackSubstitute(const MatrixTemplate<T>& a, const VectorTemplate<T>& b, VectorTemplate<T>& x)
{
  Assert(a.isSquare());
  Assert(a.n == b.n);
  if(x.empty()) x.resize(a.n);
  else Assert(a.n == x.n);
  int n=a.n;
	T sum;
	for(int i=0; i<n; i++) {
		sum=b[i];
		for(int j=0; j<i; j++)
			sum-=a(i,j)*x[j];
		x[i]=sum;
	}
}