//                                               -*- C++ -*-
/**
 *  @file  ComplexTensorImplementation.cxx
 *  @brief ComplexTensorImplementation implements the Tensor classes
 *
 *  Copyright 2005-2015 Airbus-EDF-IMACS-Phimeca
 *
 *  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 3 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
 *  along with this library.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  @author schueller
 *  @date   2012-04-18 17:56:46 +0200 (Wed, 18 Apr 2012)
 */
#include "ComplexTensorImplementation.hxx"
#include "PersistentObjectFactory.hxx"
#include "Os.hxx"

BEGIN_NAMESPACE_OPENTURNS

CLASSNAMEINIT(ComplexTensorImplementation);

static Factory<ComplexTensorImplementation> RegisteredFactory("ComplexTensorImplementation");

/* Default constructor */
ComplexTensorImplementation::ComplexTensorImplementation()
  : PersistentCollection<NumericalComplex>()
  , nbRows_(0)
  , nbColumns_(0)
  , nbSheets_(0)
{
  // Nothing to do
}

/* Constructor with size (rowDim, colDim and sheetDim) */
/* The ComplexTensorImplementation is made up of a collection of rowDim*colDim*sheetDim elements */
/* The ComplexTensorImplementation is viewed as a set of column vectors read one after another, one sheet after another */
ComplexTensorImplementation::ComplexTensorImplementation(const UnsignedInteger rowDim,
    const UnsignedInteger colDim,
    const UnsignedInteger sheetDim)
  : PersistentCollection<NumericalComplex>(rowDim * colDim * sheetDim, 0.0)
  , nbRows_(rowDim)
  , nbColumns_(colDim)
  , nbSheets_(sheetDim)
{
  // Nothing to do
}

/* Constructor from external collection */
ComplexTensorImplementation::ComplexTensorImplementation(const UnsignedInteger rowDim,
    const UnsignedInteger colDim,
    const UnsignedInteger sheetDim,
    const Collection<NumericalComplex> & elementsValues)
  : PersistentCollection<NumericalComplex>(rowDim * colDim * sheetDim, 0.0)
  , nbRows_(rowDim)
  , nbColumns_(colDim)
  , nbSheets_(sheetDim)
{
  const UnsignedInteger tensorSize = std::min(rowDim * colDim * sheetDim, elementsValues.getSize());
  memcpy(&(*this)[0], &elementsValues[0], tensorSize * sizeof(NumericalComplex));
}


/* Virtual constructor */
ComplexTensorImplementation * ComplexTensorImplementation::clone() const
{
  return new ComplexTensorImplementation(*this);
}


/* Set small elements to zero */
ComplexTensorImplementation ComplexTensorImplementation::clean(const NumericalScalar threshold) const
{
  // Nothing to do for nonpositive threshold
  if (threshold <= 0.0) return *this;
  ComplexTensorImplementation result(nbRows_, nbColumns_, nbSheets_);
  for (UnsignedInteger k = 0; k < nbSheets_; ++k)
    result.setSheet(k, getSheet(k).clean(threshold));
  return result;
}

/* String converter */
String ComplexTensorImplementation::__repr__() const
{
  return OSS(true) << "class=" << getClassName()
         << " name=" << getName()
         << " rows=" << getNbRows()
         << " columns=" << getNbColumns()
         << " sheets=" << getNbSheets()
         << " values=" << PersistentCollection<NumericalComplex>::__repr__();
}

String ComplexTensorImplementation::__str__(const String & offset) const
{
  OSS oss(false);
  for (UnsignedInteger k = 0; k < getNbSheets(); ++k)
  {
    oss << (k == 0 ? "" : Os::GetEndOfLine()) << offset << "sheet " << k << ":" << Os::GetEndOfLine();
    oss << offset << getSheet(k).__str__(offset) << Os::GetEndOfLine();
  }
  return oss;
}

/* Get the dimensions of the ComplexTensorImplementation : number of rows */
UnsignedInteger ComplexTensorImplementation::getNbRows() const
{
  return nbRows_;
}

/* Get the dimensions of the ComplexTensorImplementation : number of columns */
UnsignedInteger ComplexTensorImplementation::getNbColumns() const
{
  return nbColumns_;
}

/* Get the dimensions of the ComplexTensorImplementation : number of sheets */
UnsignedInteger ComplexTensorImplementation::getNbSheets() const
{
  return nbSheets_;
}

/* Operator () gives access to the elements of the ComplexTensorImplementation (to modify these elements) */
/* The element of the ComplexTensorImplementation is designated by its row number i, its column number j and its sheet number k */
/* the first element of the ComplexTensorImplementation is t(0,0,0) */
NumericalComplex & ComplexTensorImplementation::operator() (const UnsignedInteger i,
    const UnsignedInteger j,
    const UnsignedInteger k)
{
  if ((i >= nbRows_) || (j >= nbColumns_) || (k >= nbSheets_)) throw InvalidDimensionException(HERE);

  return (*this)[this->convertPosition(i, j, k)];
}

/* Operator () gives access to the elements of the ComplexTensorImplementation (read only) */
/* The element of the ComplexTensorImplementation is designated by its row number i, its column number j and its sheet number k */
const NumericalComplex & ComplexTensorImplementation::operator() (const UnsignedInteger i,
    const UnsignedInteger j,
    const UnsignedInteger k)  const
{
  if ((i >= nbRows_) || (j >= nbColumns_) || (k >= nbSheets_)) throw InvalidDimensionException(HERE);

  return (*this)[this->convertPosition(i, j, k)];
}

/* getSheet returns the sheet specified by its sheet number k */
ComplexMatrix ComplexTensorImplementation::getSheet(const UnsignedInteger k) const
{
  if (k >= nbSheets_) throw InvalidDimensionException(HERE);

  ComplexMatrixImplementation sheet(nbRows_, nbColumns_);
  memcpy( &sheet[0], &(*this)[this->convertPosition(0, 0, k)], nbRows_ * nbColumns_ * sizeof(NumericalComplex) );
  return sheet;
}

/* setSheet sets matrix m as the sheet specified by its sheet number k  */
void ComplexTensorImplementation::setSheet(const UnsignedInteger k,
    const ComplexMatrix & m)
{
  if (k >= nbSheets_) throw InvalidDimensionException(HERE);
  if (m.getNbRows() != nbRows_) throw InvalidDimensionException(HERE);
  if (m.getNbColumns() != nbColumns_) throw InvalidDimensionException(HERE);
  memcpy( &(*this)[this->convertPosition(0, 0, k)], &m.getImplementation()->operator[](0), nbRows_ * nbColumns_ * sizeof(NumericalComplex) );
}

/* getSheetSym returns the hermitian sheet specified by its sheet number k */
HermitianMatrix ComplexTensorImplementation::getSheetSym(const UnsignedInteger k) const
{
  Pointer<ComplexMatrixImplementation> sheet(getSheet(k).getImplementation());
//   sheet->hermitianize();
  return sheet;
}

/* setSheetSym sets hermitian matrix m as the sheet specified by its sheet number k  */
void ComplexTensorImplementation::setSheetSym(const UnsignedInteger k,
    const HermitianMatrix & m)
{
  Pointer<ComplexMatrixImplementation> sheet(m.getImplementation());
//   sheet->hermitianize();
  setSheet(k, m);
}

/* Empty returns true if there is no element in the ComplexTensorImplementation */
Bool ComplexTensorImplementation::isEmpty() const
{
  return ((nbRows_ == 0)  || (nbColumns_ == 0) || (nbSheets_ == 0) || (PersistentCollection<NumericalComplex>::isEmpty())) ;
}

/* Check for symmetry */
Bool ComplexTensorImplementation::isSymmetric() const
{
  if (nbColumns_ != nbRows_) return false;
  for (UnsignedInteger k = 0; k < nbSheets_; ++k)
    for (UnsignedInteger j = 0; j < nbColumns_; ++j)
      for (UnsignedInteger i = 0; i < j; ++i)
        if((*this)[this->convertPosition(i, j, k)] != (*this)[this->convertPosition(j, i, k)]) return false;
  return true;
}

/* Symmetrize ComplexMatrixImplementation in case it is a hermitian matrix (stored as a triangular matrix) */
void ComplexTensorImplementation::hermitianize() const
{
  ComplexTensorImplementation & refThis = * const_cast<ComplexTensorImplementation *>(this);
  for (UnsignedInteger k = 0; k < nbSheets_; ++k)
    for (UnsignedInteger j = 0; j < nbColumns_; ++j)
      for (UnsignedInteger i = 0; i < j; ++i)
        refThis[this->convertPosition(i, j, k)] = std::conj( (*this)[this->convertPosition(j, i, k)] );
}

/* Comparison operator */
Bool ComplexTensorImplementation::operator == (const ComplexTensorImplementation & rhs) const
{
  const ComplexTensorImplementation &lhs(*this);
  Bool equality = true;

  if (&lhs != &rhs)   // Not the same object
  {
    const PersistentCollection<NumericalComplex> & refLhs = static_cast<const PersistentCollection<NumericalComplex> >(lhs);
    const PersistentCollection<NumericalComplex> & refRhs = static_cast<const PersistentCollection<NumericalComplex> >(rhs);

    equality = ( lhs.nbRows_ == rhs.nbRows_ && lhs.nbColumns_ == rhs.nbColumns_ && lhs.nbSheets_ == rhs.nbSheets_ && refLhs == refRhs);
  }

  return equality;
}

/* Method save() stores the object through the StorageManager */
void ComplexTensorImplementation::save(Advocate & adv) const
{
  PersistentCollection<NumericalComplex>::save(adv);
  adv.saveAttribute("nbRows_",    nbRows_);
  adv.saveAttribute("nbColumns_", nbColumns_);
  adv.saveAttribute("nbSheets_", nbSheets_);
}

/* Method load() reloads the object from the StorageManager */
void ComplexTensorImplementation::load(Advocate & adv)
{
  PersistentCollection<NumericalComplex>::load(adv);

  adv.loadAttribute("nbRows_",    nbRows_);
  adv.loadAttribute("nbColumns_", nbColumns_);
  adv.loadAttribute("nbSheets_", nbSheets_);
}


const NumericalComplex* ComplexTensorImplementation::__baseaddress__() const
{
  return &(*this)[0];
}


UnsignedInteger ComplexTensorImplementation::__elementsize__() const
{
  return sizeof(NumericalComplex);
}


UnsignedInteger ComplexTensorImplementation::__stride__(UnsignedInteger dim) const
{
  UnsignedInteger stride = __elementsize__();
  if (dim > 0)
    stride *= nbRows_;
  if (dim > 1)
    stride *= nbColumns_;
  if (dim > 2)
    stride *= nbSheets_;
  return stride;
}


END_NAMESPACE_OPENTURNS
