Пример #1
0
int operator==(const Matrix<T> &a,const Matrix<T> &b)
{
  if ( a.rows() != b.rows() || a.cols() != b.cols() )
    {
#ifdef USE_EXCEPTION
      throw WrongSize2D(a.rows(),a.cols(),b.rows(),b.cols()) ;
#else
      Error error("operator==(const Matrix<T>&,const Matrix<T>&)");
      if ( b.rows() != a.rows() )
	error << "Matrices are of diferent size, a.rows() = " << a.rows() << " and b.rows() = " << b.rows() << endl ;
      if ( b.cols() != a.cols())
	error << "Matrices are of diferent size, a.cols() = " << a.cols() << " and b.cols() = " << b.cols() << endl ;
      error.fatal() ;
#endif
    }
  
  int i, j, row = a.rows(), col = a.cols();
  int l = 1;
  
  for (i = 0; i < row; ++i)
    for (j = 0; j < col; ++j)
      l = l && ( a.elem(i,j) == b.elem(i,j) );
  
  return l;
}
Пример #2
0
Vector<T> operator*(const Matrix<T> &a, const Vector<T> &x)
{
  if ( a.cols() != x.size() )
    {
#ifdef USE_EXCEPTION
      throw WrongSize2D(a.rows(),a.cols(),x.size(),1);
#else
      Error error("Matrix<T> operator*(Matrix<T>& a,Vector<T>& b)");
      error << "a * b incommensurate, a.cols() = " << a.cols() << ", b.rows() = " << x.size() << endl ;
      error.fatal() ;
#endif
    }
  
  int i, k, row = a.rows(), size = a.cols();
  Vector<T> prod(row);
  T zero = (T)0;
  
  T *pptr,*aptr,*xptr ;
  aptr = a.m - 1 ;
  pptr = &prod[0] ;
  for (i = row; i > 0; --i){
    xptr = x.memory()-1 ;
    for (k = size, *pptr = zero; k > 0 ; --k)
      *pptr += *(++aptr) * *(++xptr) ;
    ++pptr ;
  }
  
  return prod;
}
Пример #3
0
Matrix<T>& Matrix<T>::operator-=(const Matrix<T> &a)
{
  if ( a.rows() != this->rows() || a.cols() != this->cols() )
    {
#ifdef USE_EXCEPTION
      throw WrongSize2D(this->rows(),this->cols(),a.rows(),a.cols());
#else
      Error error("Matrix<T>::operator-=") ;
      if ( rows() != a.rows() )
	error << "Matrices are of diferent size, a.rows() = " << rows() << " and b.rows() = " << a.rows() << endl ;
      if ( cols() != a.cols())
	error << "Matrices are of diferent size, a.cols() = " << cols() << " and b.cols() = " << a.cols() << endl ;
      error.fatal() ;
#endif
    }

  int i, size;
  T *aptr,*sptr ;
  aptr = a.m - 1 ;
  sptr = this->m - 1 ;
  size = this->rows()*this->cols() ;
  for (i = size; i > 0; --i){
      *(++sptr) -= *(++aptr) ;
  }
  return *this ;
}
Пример #4
0
MatrixRT<T>::MatrixRT(const Matrix<T>& plM) : Matrix<T>(4,4) {
  if(plM.rows() == 4 && plM.cols() == 4)
    *this = plM;
  else{
#ifdef USE_EXCEPTION
    throw(WrongSize2D(4,4,plM.rows(),plM.cols()));
#else
    Error error("MatrixRT<T>::MatrixRt(const Matrix<T>&)");
    error << "The matrix doesn't have the size (4,4)\n" ;
    error.fatal();
#endif
  }
}
Пример #5
0
void Matrix<T>::submatrix(int sr, int sc, Matrix<T> &a)
{
  int rwz,coz,i,j;
  
  if ( this->rows() % a.rows() != 0 || this->cols() % a.cols() != 0 || this->rows() < a.rows() || this->cols() < a.cols() )
    {
#ifdef USE_EXCEPTION
      throw WrongSize2D(this->rows(),this->cols(),a.rows(),a.cols()) ;
#else
      Error error("Matrix<T>::submatrix");
      error << "Matrix and submatrix incommensurate" ;
      error.fatal() ;
#endif
    }
  
  if ( sr >= this->rows()/a.rows() || sr < 0 || sc >= this->cols()/a.cols() || sc < 0 )
    {
#ifdef USE_EXCEPTION
      throw OutOfBound2D(sr,sc,0,this->rows()/a.rows()-1,0,this->cols()/a.cols()-1) ;
#else
      Error error("Matrix<T>::submatrix");
      error << "Submatrix location out of bounds.\nrowblock " << sr << ", " << rows()/a.rows() << " colblock " << sc << ", " << a.cols() << endl ;
      error.fatal() ;
#endif
    }
  rwz = sr*a.rows();
  coz = sc*a.cols();
  
#ifdef COLUMN_ORDER
  for ( i = a.rows()-1; i >= 0; --i )
    for(j=a.cols()-1;j>=0;--j)
      this->elem(i+rwz,j+coz) = a(i,j) ;
#else
  T *ptr, *aptr ;
  aptr = a.m - 1;
  for ( i = a.rows()-1; i >= 0; --i )
    {
      ptr = &m[(i+rwz)*cols()+coz]-1 ;
      for ( j = a.cols(); j > 0; --j)
	*(++ptr) = *(++aptr) ;
    }  
#endif
}
Пример #6
0
Matrix<T> operator*(const Matrix<T> &a,const Matrix<T> &b)
{
  if ( a.cols() != b.rows() )
    {
#ifdef USE_EXCEPTION
      throw WrongSize2D(a.rows(),a.cols(),b.rows(),b.cols()) ;
#else
      Error error("Matrix<T> operator*(Matrix<T>&,Matrix<T>&)");
      error << "Matrix<T> a * Matrix<T> b incommensurate, a.cols() = " << a.cols() << ", b.rows() = " << b.rows() << endl ;
      error.fatal() ;
#endif
    }

  int i, j, k, row=a.rows(), col=b.cols(),size = a.cols();
  Matrix<T> prod(row,col);
  T zero = (T)0;
  
#ifdef COLUMN_ORDER
  for(i=row-1;i>=0;--i)
    for(j=size-1;j>=0;--j)
      if(a(i,j) != zero){
	for(k=col-1;k>=0; --k)
	  prod(i,k) += a(i,j)* b(j,k) ;
      }
#else
  T *pptr,*aptr,*bptr ;
  aptr = a.m ;
  for (i = 0; i < row; ++i)
    for (j = 0; j < size; ++j){
      if ( *aptr != zero )
	{
	  pptr = prod[i]-1;
	  bptr = b[j]-1;
	  for (k = col; k > 0; --k){
	    *(++pptr) += *aptr * *(++bptr);
	  }
	}
      ++aptr;
    }
#endif
  return prod;
  
}