namespace AMDiS {

  template<typename T>
  void DimVec<T>::multMatrixVec(DimMat<T> &m, DimVec<T> &v)
  {
    T *mIt, *thisIt;
    for (thisIt = this->begin(), mIt = m.begin();
	 thisIt != this->end();
	 thisIt++) {
      *thisIt = 0;
      for (T* vIt = v.begin(); vIt != v.end(); vIt++, mIt++) {
	*thisIt += *vIt * *mIt;
      }
    }
  }

  template<typename T, GeoIndex d>
  double absteukl(const FixVec<T,d>& a,const FixVec<T,d>& b)
  {
    double erg = 0.0;

    for (int i = 0; i < a.getSize() ; i++) {
      erg = erg + ((a[i] - b[i]) * (a[i] - b[i]));
    }

    return sqrt(erg);
  }

  template<typename T>
  void WorldVector<T>::multMatrixVec(WorldMatrix<T> &m, WorldVector<T> &v) 
  {
    FUNCNAME("WorldVector<T>::multMatrix()");
    TEST_EXIT_DBG(m.getNumRows() == this->getSize())("invalide size\n");
    TEST_EXIT_DBG(v.getSize() == this->getSize())("invalide size\n");

    T *mIt, *thisIt;
    for (thisIt = this->begin(), mIt = m.begin();
	 thisIt != this->end(); 
	 thisIt++) {
      *thisIt = 0;
      
      for (T* vIt = v.begin(); vIt != v.end(); vIt++, mIt++) {
	*thisIt += *vIt * *mIt;
      }
    }
  }

  template<typename T>
  bool WorldMatrix<T>::isDiagMatrix() const
  {
    for (int i = 0; i < this->getSize(); i++)
      for (int j = i + 1; j < this->getSize(); j++)
	if (abs((*this)[i][j]) > DBL_TOL || abs((*this)[j][i]) > DBL_TOL) 
	  return(false);

    return(true);
  }

  template<typename T>
  bool WorldMatrix<T>::isSymmetric() const
  {
    for (int i = 0; i < this->getSize(); i++)
      for (int j = i + 1; j < this->getSize(); j++)
	if (abs((*this)[i][j] - (*this)[j][i]) > DBL_TOL) 
	  return false;

    return true;
  }
 
  template<typename T>
  void WorldMatrix<T>::setDiag(T value)
  {
    for (int i = 0; i < this->rows; i++) {
      this->valArray[i * this->cols + i] = value;
    }
  }

  template<typename T>
  void WorldMatrix<T>::vecProduct(const WorldVector<T>& v1, const WorldVector<T>& v2)
  {
    FUNCNAME("WorldMatrix<T>::vecProduct()");

    TEST_EXIT_DBG(v1.getSize() == v2.getSize())("invalid size 1\n");
    TEST_EXIT_DBG(v1.getSize() == this->getSize())("invalid size 2\n");

    T *thisIt = this->begin();

    for (T* v1It = v1.begin(); v1It != v1.end(); v1It++) {
      for (T* v2It = v2.begin(); v2It != v2.end(); v2It++, thisIt++) {
	*thisIt = *v1It * *v2It;
      }
    }
  }
  
  template<typename T>
  const WorldMatrix<T>& operator*=(WorldMatrix<T>& m, T scal)
  {
    for (T* mIt = m.begin(); mIt != m.end(); mIt++) {
      *mIt *= scal;
    }

    return m;
  }

  template<typename T>
  const WorldMatrix<T>& operator-=(WorldMatrix<T>& m1, const WorldMatrix<T>& m2)
  {
    T *m1It, *m2It;
    for (m1It = m1.begin(), m2It = m2.begin();
	 m1It != m1.end(); 
	 m1It++, m2It++) {
      *m1It -= *m2It;
    }

    return m1;
  }

  template<typename T>
  const WorldMatrix<T>& operator+=(WorldMatrix<T>& m1, const WorldMatrix<T>& m2)
  {
    T* m1It, *m2It;
    for (m1It = m1.begin(), m2It = m2.begin();
	 m1It != m1.end(); 
	 m1It++, m2It++) {
      *m1It += *m2It;
    }

    return m1;
  }
}