// $Id: tvector.cc,v 1.14 2001-08-06 22:48:43 cmv Exp $
//                         C.Magneville          04/99
#include "machdefs.h"
#include <stdlib.h>
#include "pexceptions.h"
#include "tvector.h"

/*!
  \class SOPHYA::TVector
  \ingroup TArray
  Class of vector (line or column)
  \sa TMatrix TArray
  */

////////////////////////////////////////////////////////////////
//**** Createur, Destructeur

//! Default constructor
template <class T>
TVector<T>::TVector()
  : TMatrix<T>()
{
  arrtype_ = 2;   // Type = Vector
}

//! construct a vector
/*!
  \param n : number of elements
  \param lcv : line or column vector ?
  \param mm : memory mapping type
  \sa SelectVectorType
 */
template <class T>
TVector<T>::TVector(sa_size_t n, short lcv, short mm)
// Constructeur 
  : TMatrix<T>(1,1,mm)
{
  arrtype_ = 2;   // Type = Vector
  lcv = SelectVectorType(lcv);
  ReSize(n,lcv);
}

//! Constructor by copy
/*!
  \warning datas are \b SHARED with \b a.
  \sa NDataBlock::NDataBlock(const NDataBlock<T>&)
*/
template <class T>
TVector<T>::TVector(const TVector<T>& a)
// Constructeur par copie
  : TMatrix<T>(a)
{
  arrtype_ = 2;   // Type = Vector
}

//! Constructor by copy
/*!
  \param share : if true, share data. If false copy data
 */
template <class T>
TVector<T>::TVector(const TVector<T>& a, bool share)
// Constructeur par copie avec possibilite de forcer le partage ou non.
: TMatrix<T>(a, share)
{
  arrtype_ = 2;   // Type = Vector
}

//! Constructor from a TArray
template <class T>
TVector<T>::TVector(const TArray<T>& a)
: TMatrix<T>(a)
{
  if ( (size_[0] != 1) && (size_[1] != 1) )
    throw SzMismatchError("TVector<T>::TVector(const TArray<T>& a) NRows()!=1 && NCols()!=1 ");
  arrtype_ = 2;   // Type = Vector
}


//! Constructor of a vector from a TArray \b a
/*!
  \param a : TArray to be copied or shared
  \param share : if true, share data. If false copy data
  \param mm : define the memory mapping type
  \param lcv : line or column vector ?
  \sa SelectVectorType
 */
template <class T>
TVector<T>::TVector(const TArray<T>& a, bool share, short lcv, short mm)
: TMatrix<T>(a, share, mm)
{
  if ( (size_[0] != 1) && (size_[1] != 1) )
    throw SzMismatchError("TVector<T>::TVector(const TArray<T>& a) NRows()!=1 && NCols()!=1 ");
  arrtype_ = 2;   // Type = Vector
  if ( (size_[0] == 1) && (size_[1] == 1) ) {
    if (lcv == SameVectorType) lcv = a.GetVectorType();
    if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
    veceli_ = (lcv ==  ColumnVector ) ?  marowi_ : macoli_;
  }
}

//! Constructor of a vector from a TArray \b a , with a different data type
template <class T>
TVector<T>::TVector(const BaseArray& a)
: TMatrix<T>(a)
{
  if ( (size_[0] != 1) && (size_[1] != 1) )
    throw SzMismatchError("TVector<T>::TVector(const BaseArray& a) NRows()!=1 && NCols()!=1 ");
  arrtype_ = 2;   // Type = Vector
}

//! Destructor
template <class T>
TVector<T>::~TVector()
{
}

//! Resize the vector
/*!
  \param n : number of elements
  \param lcv : line or column vector ?
  \sa SelectVectorType
 */
template <class T>
void TVector<T>::ReSize(sa_size_t n, short lcv)
{
  if( n == 0 ) 
    throw(SzMismatchError("TVector::ReSize() n = 0 "));
  sa_size_t r,c;
  if (lcv == SameVectorType)  lcv = GetVectorType();
  else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
  if (lcv == ColumnVector) { r = n;  c = 1; }
  else { c = n; r = 1; }
  TMatrix<T>::ReSize(r,c);
  veceli_ = (lcv ==  ColumnVector ) ?  marowi_ : macoli_;
}

//! Re-allocate space for the vector
/*!
  \param n : number of elements
  \param lcv : line or column vector ?
  \param force : if true re-allocation is forced, if not it occurs
          only if the required space is greater than the old one.
  \sa ReSize SelectVectorType
 */
template <class T>
void TVector<T>::Realloc(sa_size_t n, short lcv, bool force)
{
  if( n == 0 ) 
    throw(SzMismatchError("TVector::Realloc() n = 0 "));
  sa_size_t r,c;
  if (lcv == SameVectorType)  lcv = GetVectorType();
  else if ( (lcv != ColumnVector) && (lcv != RowVector) ) lcv = GetDefaultVectorType();
  if (lcv == ColumnVector) { r = n;  c = 1; }
  else { c = n; r = 1; }
  TMatrix<T>::Realloc(r,c,SameMemoryMapping,force);
  veceli_ = (lcv ==  ColumnVector ) ?  marowi_ : macoli_;
}

// $CHECK$ Reza 03/2000  Doit-on declarer cette methode const ?
//! Return a subvector define by \b Range \b relt
template <class T>
TVector<T> TVector<T>::SubVector(Range relt) const
{
  Range rr, cr;
  if (GetVectorType() == ColumnVector )  rr = relt; 
  else cr = relt;
  TMatrix<T> const & mtx = (*this);
  TVector sv( mtx(rr, cr) , true, GetVectorType(), GetMemoryMapping() );
  return(sv);  
}

//! Return the norm \f$ \sum{V(i)^2} \f$
template <class T>
T TVector<T>::Norm2() const
{
  T ret = 0;
  for(sa_size_t k=0; k<Size(); k++)   ret += (*this)(k)*(*this)(k);
  return ret;
}

//! Return info on number of rows, column and type \b T
template <class T>
string TVector<T>::InfoString() const
{
  string rs = "TVector<";
  rs += typeid(T).name();
  char buff[64];
  sprintf(buff, ">(%ld) (nr=%ld, nc=%ld)", (long)NElts(), (long)NRows(), (long)NCols());
  rs += buff;
  return(rs);  

}

///////////////////////////////////////////////////////////////
#ifdef __CXX_PRAGMA_TEMPLATES__
#pragma define_template TVector<uint_2>
#pragma define_template TVector<uint_8>
#pragma define_template TVector<int_4>
#pragma define_template TVector<int_8>
#pragma define_template TVector<r_4>
#pragma define_template TVector<r_8>
#pragma define_template TVector< complex<r_4> > 
#pragma define_template TVector< complex<r_8> > 
#endif

#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
template class TVector<uint_2>;
template class TVector<uint_8>;
template class TVector<int_4>;
template class TVector<int_8>;
template class TVector<r_4>;
template class TVector<r_8>;
template class TVector< complex<r_4> >;
template class TVector< complex<r_8> >;
#endif

