/*
 *  Open BEAGLE
 *  Copyright (C) 2001-2004 by Christian Gagne and Marc Parizeau
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  Contact:
 *  Laboratoire de Vision et Systemes Numeriques
 *  Departement de genie electrique et de genie informatique
 *  Universite Laval, Quebec, Canada, G1K 7P4
 *  http://vision.gel.ulaval.ca
 *
 */

/*!
 *  \file   beagle/Matrix.hpp
 *  \brief  Definition of the class Matrix.
 *  \author Christian Gagne
 *  \author Marc Parizeau
 *  $Revision: 1.8 $
 *  $Date: 2004/10/28 16:50:42 $
 */
 
#ifndef Beagle_Matrix_hpp
#define Beagle_Matrix_hpp

#include <vector>

#include "beagle/config.hpp"
#include "beagle/macros.hpp"
#include "beagle/Object.hpp"
#include "beagle/AllocatorT.hpp"
#include "beagle/PointerT.hpp"
#include "beagle/ContainerT.hpp"
#include "beagle/Tokenizer.hpp"
#include "beagle/XMLNode.hpp"
#include "beagle/XMLParser.hpp"
#include "beagle/XMLStreamer.hpp"
#include "beagle/AssertException.hpp"

namespace Beagle {

// Forward declaration
class Vector;


/*!
 *  \class Matrix beagle/Matrix.hpp "beagle/Matrix.hpp"
 *  \brief Matrix class defined with several useful numeric / linear algebra methods.
 *  \ingroup OOF
 *  \ingroup Utils
 *  \note This class is based on "Matrice" of Marc Parizeau.
 */
class Matrix : public Object, public std::vector<double> {

public:

  //! Matrix allocator type.
  typedef AllocatorT<Matrix,Object::Alloc>
          Alloc;
  //! Matrix handle type.
  typedef PointerT<Matrix,Object::Handle>
          Handle;
  //! Matrix bag type.
  typedef ContainerT<Matrix,Object::Bag>
          Bag;

  explicit Matrix(unsigned int inNumberRows=0,
                  unsigned int inNumberColumns=0,
                  double inValue=0.0);
           Matrix(const std::vector<double>& inDoubleVector);
  virtual ~Matrix() { }  //!< Matrix destructor (do nothing).

  /*!
   *  \brief Get value of matrix a position (inRow,inColumn).
   *  \param inRow Row index of the value to get in the matrix.
   *  \param inColumn Column index of the value to get in the matrix.
   *  \return Value in matrix at (inRow,inColumn).
   *  \throw Beagle::AssertException If rows or columns indexes given are out-of-bound.
   */
  inline const double& operator()(unsigned int inRow, unsigned int inColumn) const
  {
    Beagle_BoundCheckAssertM(inRow,0,mRows-1);
    Beagle_BoundCheckAssertM(inColumn,0,(size()/mRows)-1);
    return (*this)[(inColumn*mRows)+inRow];
  }

  /*!
   *  \brief Get value of matrix a position (inRow,inColumn).
   *  \param inRow Row index of the value to get in the matrix.
   *  \param inColumn Column index of the value to get in the matrix.
   *  \return Value in matrix at (inRow,inColumn).
   *  \throw Beagle::AssertException If rows or columns indexes given are out-of-bound.
   */
  inline double& operator()(unsigned int inRow, unsigned int inColumn)
  {
    Beagle_BoundCheckAssertM(inRow,0,mRows-1);
    Beagle_BoundCheckAssertM(inColumn,0,(size()/mRows)-1);
    return (*this)[(inColumn*mRows)+inRow];
  }

  Matrix& operator=(const std::vector<double>& inRightVector);
  Matrix  operator+(const Matrix& inRightMatrix) const;
  Matrix& operator+=(const Matrix& inRightMatrix);
  Matrix  operator+(double inRightScalar) const;
  Matrix& operator+=(double inRightScalar);
  Matrix  operator-(const Matrix& inRightMatrix) const;
  Matrix& operator-=(const Matrix& inRightMatrix);
  Matrix  operator-(double inRightScalar) const;
  Matrix& operator-=(double inRightScalar);
  Matrix  operator*(const Matrix& inRightMatrix) const;
  Matrix  operator*(double inRightScalar) const;
  Matrix& operator*=(double inRightScalar);
  Matrix  operator/(double inRightScalar) const;
  Matrix& operator/=(double inRightScalar);

  /*!
   *  \brief Get number of columns of the matrix.
   *  \return Number of columns of the matrix.
   */
  inline unsigned int getNumberColumns() const
  {
    return (mRows!=0) ? (size()/mRows) : 0;
  }

  /*!
   *  \brief Get number of rows of the matrix.
   *  \return Number of rows of the matrix.
   */
  inline unsigned int getNumberRows() const
  {
    return mRows;
  }

  /*!
   *  \brief Return whether matrix is square, that is number of rows == number of columns.
   *  \return True is matrix is square, false is not.
   */
  inline bool isSquare() const
  {
    return (mRows!=0) ? (mRows==(size()/mRows)) : true;
  }

          double computeDeterminant() const;
          void   computeEigenSystem(Vector& outEigenValues, Matrix& outEigenVectors) const;
          void   extract(Matrix& outSubMatrix,
                         unsigned int inFirstRow,
                         unsigned int inLastRow,
                         unsigned int inFirstColumn,
                         unsigned int inLastColumn) const;
          double getMaximum() const;
          double getMaximum(unsigned int& outArgMaxRow, unsigned int& outArgMaxCol) const;
          double getMinimum() const;
          double getMinimum(unsigned int& outArgMinRow, unsigned int& outArgMinCol) const; 
          void   inverse();
  virtual bool   isEqual(const Object& inRightObj) const;
  virtual void   read(XMLNode::Handle& inNode);
          void   resize(unsigned int inNumberRows, unsigned int inNumberCols, double inValue=0.0);
          void   setIdentity(unsigned int inSize);
          void   transpose();
  virtual void   write(XMLStreamer& ioStreamer) const;
      
protected:

  void   backSubstituteLU(const std::vector<unsigned int>& inIndexes, Matrix& ioMatrixB) const;
  void   decomposeLU(Matrix& outDecompose, std::vector<unsigned int>& outIndexes, int& outD) const;
  double hypot(double a, double b) const;
  void   scaleLU(std::vector<double>& outScales) const;
  void   tql2(Vector& d, Vector& e, Matrix& V) const;
  void   tred2(Vector& d, Vector& e, Matrix& V) const;
  
  unsigned int mRows;    //!< Number of rows of the matrix.
  
};

}

#endif // Beagle_Matrix_hpp

