// file: $isip_ifc/class/algo/Lyapunov/Lyapunov.h
// version: $Id: Lyapunov.h 10636 2007-01-26 22:18:09Z tm334 $
//

// make sure definitions are only made once
//
#ifndef ISIP_LYAPUNOV
#define ISIP_LYAPUNOV

#ifndef ISIP_ALGORITHM_BASE
#include <AlgorithmBase.h>
#endif

// Lyapunov: a class that computes the Lyapunov spectra from an input
// reconsctructed (RPS)  matrix. from the RPS it computes the trajectory map,
// T, at every point and uses treppen-iteration to extract the average
// eigenvalues of the T matrices 
//
// Refer to :
// Michael Banbrook, "Nonlinear analysis of speech from a synthesis
// perspective", PhD thesis, The University of Edinburgh, 1996.
//
class Lyapunov : public AlgorithmBase {
  
  //---------------------------------------------------------------------------
  //
  // public constants
  //
  //---------------------------------------------------------------------------
public:
  
  // define the class name
  //
  static const String CLASS_NAME;

  //----------------------------------------
  //
  // other important constants
  //
  //----------------------------------------
  
  // define algorithm choices
  //
  enum ALGORITHM { LINEAR_TANGENT_MAP = 0, DEF_ALGORITHM = LINEAR_TANGENT_MAP };

  // define implementation choices
  //
  enum IMPLEMENTATION { TREPPEN_ITERATION = 0, DEF_IMPLEMENTATION = TREPPEN_ITERATION };

  // define static NameMap objects
  //
  static const NameMap ALGO_MAP;
  static const NameMap IMPL_MAP;
  
  //----------------------------------------
  //
  // i/o related constants
  //
  //----------------------------------------  

  static const String DEF_PARAM;
  static const String PARAM_ALGORITHM;  
  static const String PARAM_IMPLEMENTATION;
  static const String PARAM_NUM_NEIGHBORS;
  static const String PARAM_EVOLVE_STEP;
  static const String PARAM_LOCAL_DIM;
  static const String PARAM_NUM_STEPS;
  static const String PARAM_INNER_RADIUS;
  static const String PARAM_OUTER_RADIUS;
  static const String PARAM_NUM_NEIGHBOR_SUBGROUPS;
  static const String PARAM_REINIT_STEP;
  static const String PARAM_START_POS;


  //----------------------------------------
  //
  // default values and arguments
  //
  //----------------------------------------  

  // define the default value(s) of the class data
  //
  static const long DEF_NUM_NEIGHBORS = -1;
  static const long DEF_EVOLVE_STEP = -1;
  static const long DEF_LOCAL_DIM = -1;
  static const long DEF_NUM_STEPS = 1000;
  static const float DEF_INNER_RADIUS = 0.0;
  static const float DEF_OUTER_RADIUS = -1.0;
  static const long DEF_NUM_NEIGHBOR_SUBGROUPS = -1;
  static const long DEF_REINIT_STEP = -1;
  static const long DEF_START_POS = 1;

  // define default argument(s)
  //
  static const AlgorithmData::COEF_TYPE DEF_COEF_TYPE = AlgorithmData::RPS;
  
  //----------------------------------------
  //
  // error codes
  //
  //----------------------------------------  

  // MY: Needs to be addressed last
  //
  static const long ERR = 73500;
  static const long ERR_SUBGRPS = 73501;
  static const long ERR_NGH = 73502;
  static const long ERR_DIM = 73503;
  
    
  //---------------------------------------------------------------------------
  //
  // protected data
  //
  //---------------------------------------------------------------------------
protected:

  // algorithm name
  //
  ALGORITHM algorithm_d;

  // implementation type
  //
  IMPLEMENTATION implementation_d;

  // number of neighbors
  //
  Long num_neighbors_d;

  // evolve step for trajectory matrix computation
  //
  Long evolve_step_d;

  // local embedding dimension
  //
  Long local_dim_d;

  // number of points to evolve
  //
  Long num_steps_d;

  // inner and outer radius of neighborhood
  //
  Float inner_radius_d, outer_radius_d;

  // number of subgroups of neighbors (with averaging
  // in each subgroups done)
  //
  Long num_neighbor_subgroups_d;
    
  // number of points to jump before recalculating LE
  //
  Long reinit_step_d;

  // position to start computing
  //
  Long start_pos_d;

  // static memory manager
  //
  static MemoryManager mgr_d;

  //---------------------------------------------------------------------------
  //
  // required public methods
  //
  //---------------------------------------------------------------------------
public:

  // method: name
  //
  static const String& name() {
    return CLASS_NAME;
  }

  // other static methods
  //
  static boolean diagnose(Integral::DEBUG debug_level);
  
  // debug methods:
  //  setDebug is inherited from the AlgorithmBase class
  //
  boolean debug(const unichar* msg) const;
  
  // method: destructor
  //
  ~Lyapunov() {}

  // default constructor
  //
  Lyapunov(ALGORITHM algorithm = DEF_ALGORITHM,
	   IMPLEMENTATION implementation = DEF_IMPLEMENTATION,
	   long num_neighbors = DEF_NUM_NEIGHBORS,
	   long evolve_step = DEF_EVOLVE_STEP,
	   long local_dim = DEF_LOCAL_DIM,
	   long num_steps = DEF_NUM_STEPS,
	   float inner_radius = DEF_INNER_RADIUS,
	   float outer_radius = DEF_OUTER_RADIUS,
	   long num_neighbor_subgroups = DEF_NUM_NEIGHBOR_SUBGROUPS,
	   long reinit_step = DEF_REINIT_STEP,
	   long start_pos = DEF_START_POS) {
    algorithm_d = algorithm;
    implementation_d = implementation;
    num_neighbors_d = num_neighbors;
    evolve_step_d = evolve_step;
    local_dim_d = local_dim;
    num_steps_d = num_steps;
    inner_radius_d = inner_radius;
    outer_radius_d = outer_radius;
    num_neighbor_subgroups_d = num_neighbor_subgroups;
    reinit_step_d = reinit_step;
    start_pos_d = start_pos;
    is_valid_d = false;
  }

  // method: copy constructor
  //
  Lyapunov(const Lyapunov& arg) {
    assign(arg);
  }
  
  // assign methods
  //
  boolean assign(const Lyapunov& arg) {
       algorithm_d = arg.algorithm_d;
       implementation_d = arg.implementation_d;
       num_neighbors_d = arg.num_neighbors_d;
       evolve_step_d = arg.evolve_step_d;
       local_dim_d = arg.local_dim_d;
       num_steps_d = arg.num_steps_d;
       inner_radius_d = arg.inner_radius_d;
       outer_radius_d = arg.outer_radius_d;
       num_neighbor_subgroups_d = arg.num_neighbor_subgroups_d;
       reinit_step_d = arg.reinit_step_d;
       start_pos_d = arg.start_pos_d;
       return AlgorithmBase::assign(arg);
  }
  
  // method: operator=
  //
  Lyapunov& operator= (const Lyapunov& arg) {
    assign(arg);
    return *this;
  }

  // i/o methods
  //
  long sofSize() const;
  
  boolean read(Sof& sof, long tag, const String& name = CLASS_NAME);
  
  boolean write(Sof& sof, long tag, const String& name = CLASS_NAME) const;
  
  boolean readData(Sof& sof, const String& pname = DEF_PARAM,
                   long size = SofParser::FULL_OBJECT,
                   boolean param = true,
		   boolean nested = false);

  boolean writeData(Sof& sof, const String& pname = DEF_PARAM) const;
  
  // equality methods
  //
  boolean eq(const Lyapunov& arg) const {
    return ((algorithm_d == arg.algorithm_d) &&
	    (implementation_d == arg.implementation_d) &&
	    (num_neighbors_d == arg.num_neighbors_d) &&
	    (evolve_step_d == arg.evolve_step_d) &&
	    (local_dim_d == arg.local_dim_d) &&
	    (num_steps_d == arg.num_steps_d) &&
	    (inner_radius_d == arg.inner_radius_d) &&
      	    (outer_radius_d == arg.outer_radius_d) &&
  	    (num_neighbor_subgroups_d == arg.num_neighbor_subgroups_d) &&
	    (reinit_step_d == arg.reinit_step_d) &&
	    (start_pos_d == arg.start_pos_d));
  }
  
  // method: new
  //
  static void* operator new(size_t size) {
    return mgr_d.get();
  }

  // method: new[]
  //
  static void* operator new[](size_t size) {
    return mgr_d.getBlock(size);
  }

  // method: delete
  //
  static void operator delete(void* ptr) {
    mgr_d.release(ptr);
  }

  // method: delete[]
  //
  static void operator delete[](void* ptr) {
    mgr_d.releaseBlock(ptr);
  }

  // method: setGrowSize
  //
  static boolean setGrowSize(long grow_size) {
    return mgr_d.setGrow(grow_size);
  }

  // other memory management methods
  //
  boolean clear(Integral::CMODE ctype = Integral::DEF_CMODE);
  
  //---------------------------------------------------------------------------
  //
  // class-specific public methods
  //  set methods
  //
  //---------------------------------------------------------------------------

  // method: setAlgorithm
  //
  boolean setAlgorithm(ALGORITHM algorithm) {
    algorithm_d = algorithm;
    is_valid_d = false;
    return true;  
  }

  // method: setImplementation
  //
  boolean setImplementation(IMPLEMENTATION implementation) {
    implementation_d = implementation;
    is_valid_d = false;
    return true;  
  }  

  boolean setNumNeighbors(long num_neighbors) {
    num_neighbors_d = num_neighbors;
    is_valid_d = false;
    return true;  
  }

  boolean setEvolveStep(long evolve_step) {
    evolve_step_d = evolve_step;
    is_valid_d = false;
    return true;  
  }
  
  boolean setLocalDim(long local_dim) {
    local_dim_d = local_dim;
    is_valid_d = false;
    return true;  
  }

  boolean setNumSteps(long num_steps) {
    num_steps_d = num_steps;
    is_valid_d = false;
    return true;  
  }

  boolean setInnerRadius(float inner_radius) {
    inner_radius_d = inner_radius;
    is_valid_d = false;
    return true;  
  }

  boolean setOuterRadius(float outer_radius) {
    outer_radius_d = outer_radius;
    is_valid_d = false;
    return true;  
  }

  boolean setNumNeighborSubgroups(long num_neighbor_subgroups) {
    num_neighbor_subgroups_d = num_neighbor_subgroups;
    is_valid_d = false;
    return true;  
  }

  boolean setReinitStep(long reinit_step) {
    reinit_step_d = reinit_step;
    is_valid_d = false;
    return true;  
  }
  
  boolean setStartPos(long start_pos) {
    start_pos_d = start_pos;
    is_valid_d = false;
    return true;  
  }

  
  // method: set
  //
  boolean set(ALGORITHM algorithm = DEF_ALGORITHM,
	      IMPLEMENTATION implementation = DEF_IMPLEMENTATION,
	      long num_neighbors = DEF_NUM_NEIGHBORS,
	      long evolve_step = DEF_EVOLVE_STEP,
	      long local_dim = DEF_LOCAL_DIM,
	      long num_steps = DEF_NUM_STEPS,
	      float inner_radius = DEF_INNER_RADIUS,
	      float outer_radius = DEF_OUTER_RADIUS,
	      long num_neighbor_subgroups = DEF_NUM_NEIGHBOR_SUBGROUPS,
	      long reinit_step = DEF_REINIT_STEP,
      	      long start_pos = DEF_START_POS) {

    algorithm_d = algorithm;
    implementation_d = implementation;
    num_neighbors_d = num_neighbors;
    evolve_step_d = evolve_step;
    local_dim_d = local_dim;
    num_steps_d = num_steps;
    inner_radius_d = inner_radius;
    outer_radius_d = outer_radius;
    num_neighbor_subgroups_d = num_neighbor_subgroups;
    reinit_step_d = reinit_step;
    start_pos_d = start_pos;
    is_valid_d = false;
    return true;
  }

  //---------------------------------------------------------------------------
  //
  // class-specific public methods
  //  get methods
  //
  //---------------------------------------------------------------------------  
  // method: getAlgorithm
  //
  ALGORITHM getAlgorithm() const {
    return algorithm_d;
  }

  // method: getImplementation
  //
  IMPLEMENTATION getImplementation() const {
    return implementation_d;
  }  

  long getNumNeighbors() const {
    return num_neighbors_d;
  }

  long getEvolveStep() const {
    return evolve_step_d;
  }

  long getLocalDim() const {
    return local_dim_d;
  }

  long getNumSteps() const {
    return num_steps_d;
  }

  float getInnerRadius() const {
    return inner_radius_d;
  }

  float getOuterRadius() const {
    return outer_radius_d;
  }

  long getNumNeighborSubgroups() const {
    return num_neighbor_subgroups_d;
  }

  long getReinitStep() const {
    return reinit_step_d;
  }

  long getStartPos() const {
    return start_pos_d;
  }
  
  // method: get
  //
  boolean get(ALGORITHM& algorithm,
	      IMPLEMENTATION& implementation,
	      long& num_neighbors,
	      long& evolve_step,
	      long& local_dim, 
	      long& num_steps,
	      float& inner_radius,
	      float& outer_radius,
	      long& num_neighbor_subgroups,
	      long& reinit_step,
	      long& start_pos) const {

    algorithm = algorithm_d;
    implementation = implementation_d;
    num_neighbors = num_neighbors_d;
    evolve_step = evolve_step_d;
    local_dim = local_dim_d;
    num_steps = num_steps_d;
    inner_radius = inner_radius_d;
    outer_radius = outer_radius_d;
    num_neighbor_subgroups = num_neighbor_subgroups_d;
    reinit_step = reinit_step_d;
    start_pos = start_pos_d;
    return true;
  }

  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  computation methods
  //
  //---------------------------------------------------------------------------

  // compute method when input is given as matrix itself
  //
  boolean compute(VectorFloat& output, const MatrixFloat& input,
		  AlgorithmData::COEF_TYPE input_coef_type = DEF_COEF_TYPE,
		  long index = DEF_CHANNEL_INDEX);

  
  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  public methods required by the AlgorithmBase interface contract
  //
  //---------------------------------------------------------------------------

  // assign method
  //
  boolean assign(const AlgorithmBase& arg);

  // equality method
  //
  boolean eq(const AlgorithmBase& arg) const;

  // method: className
  //
  const String& className() const {
    return CLASS_NAME;
  }

  // no initialization method is needed since none of the
  // algorithms currently supported require a history

  // apply method
  //
  boolean apply(Vector<AlgorithmData>& output,
		const Vector< CircularBuffer<AlgorithmData> >& input);

  // method to set the parser
  //
  boolean setParser(SofParser* parser);

  //---------------------------------------------------------------------------
  //
  // private methods
  //
  //---------------------------------------------------------------------------
private:

  // common i/o methods
  //
  boolean readDataCommon(Sof& sof, const String& pname,
			 long size = SofParser::FULL_OBJECT,
			 boolean param = true, boolean nested = false);
  boolean writeDataCommon(Sof& sof, const String& pname) const;


  // compute Lyapunov exponents
  //
  boolean computeLyapunovLinearTangentMapMethod(VectorFloat& lyapunov,
						const MatrixFloat& rps);

};

// end of include file
// 
#endif

