// file: $isip/class/numeric/NonlinearOptimization/NonlinearOptimization.h
// version: $Id: NonlinearOptimization.h 8338 2002-07-11 03:35:34Z picone $
//

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

#ifndef ISIP_LINEAR_ALGEBRA
#include <LinearAlgebra.h>
#endif

#ifndef ISIP_MATRIX_DOUBLE
#include <MatrixDouble.h>
#endif

#ifndef ISIP_MATRIX_FLOAT
#include <MatrixFloat.h>
#endif

#ifndef ISIP_MEMORY_MANAGER
#include <MemoryManager.h>
#endif

// NonlinearOptimization: a class to perform standard nonlinear optimization
// techniques including parametric curve fitting and nonparametric
// quadratic optimization
//
class NonlinearOptimization {
  
  //---------------------------------------------------------------------------
  //
  // public constants
  //
  //---------------------------------------------------------------------------
public:
  
  // define the class name
  //
  static const String CLASS_NAME;
  
  //----------------------------------------
  //
  // other important constants
  //
  //----------------------------------------
  
  // Levenberg-Marquardt computation constants
  //
  static const float LEVMARQ_INIT_LAMBDA = 0.001;
  
  //----------------------------------------
  //
  // i/o related constants
  //
  //----------------------------------------  
  
  static const String DEF_PARAM;
  
  //----------------------------------------
  //
  // default values and arguments
  //
  //----------------------------------------  
  
  static const float DEF_LEVMARQ_CONVERGE = 0.1;
  
  //----------------------------------------
  //
  // error codes
  //
  //----------------------------------------  
  
  static const long ERR = 35200;
  static const long ERR_LEV_MARQ = 35201;
  
  //---------------------------------------------------------------------------
  //
  // protected data
  //
  //---------------------------------------------------------------------------
protected:

  // typedef for the functional form to use for Levenberg-Marquardt
  // optimization in
  //
  typedef boolean (*LEV_MARQ_FUNC_FLOAT)(float&, VectorFloat&, const float,
					 const VectorFloat&);
  typedef boolean (*LEV_MARQ_FUNC_DOUBLE)(double&, VectorDouble&, const double,
					  const VectorDouble&);
  
  // a static debug level
  //
  static Integral::DEBUG debug_level_d;
  
  // a 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);
  
  // method: setDebug
  //
  static boolean setDebug(Integral::DEBUG level) {
    debug_level_d = level;
    return true;
  }
  
  // method: debug
  //
  boolean debug(const unichar* msg) const {
    return true;
  }
  
  // method: destructor
  //
  ~NonlinearOptimization() {}
  
  // method: default constructor
  //
  NonlinearOptimization() {}
  
  // method: copy constructor
  //
  NonlinearOptimization(const NonlinearOptimization& arg) {
    assign(arg);
  }
  
  // method: assign
  //
  boolean assign(const NonlinearOptimization& copy_node) {
    return true;
  }
  
  // method: operator=
  //
  NonlinearOptimization& operator= (const NonlinearOptimization& arg) {
    assign(arg);
    return *this;
  }
  
  // method: sofSize
  //
  long sofSize() const {
    return 0;
  }
  
  // method: read
  //
  boolean read(Sof& sof, long tag, const String& name = CLASS_NAME) {
    return true;
  }

  // method: write
  //
  boolean write(Sof& sof, long tag, const String& name = CLASS_NAME) const {
    return true;
  }

  // method: readData
  //
  boolean readData(Sof& sof, const String& pname = DEF_PARAM,
                   long size = SofParser::FULL_OBJECT,
                   boolean param = true,
                   boolean nested = false) {
    return true;
  }

  // method: writeData
  //
  boolean writeData(Sof& sof, const String& name = DEF_PARAM) const {
    return true;
  }

  // method: eq
  //
  boolean eq(const NonlinearOptimization& arg) const {
    return true;
  }
  
  // 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);
  }

  // method: clear
  //
  boolean clear(Integral::CMODE cmode = Integral::DEF_CMODE) {
    return true;
  }
  
  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  parametric curve-fitting methods
  //
  //---------------------------------------------------------------------------

  // method: levenbergMarquardt
  //  least-squares Levenberg-Marquardt optimization
  //
  static boolean levenbergMarquardt(VectorFloat& params, float& chi_square,
				    const VectorFloat& x, const VectorFloat& y,
				    const VectorFloat& stddev,
				    LEV_MARQ_FUNC_FLOAT func,
				    float convergence=DEF_LEVMARQ_CONVERGE) {
    return levMarqTemplate<MatrixFloat,VectorFloat,float>(params, chi_square,
							  x, y, stddev,
							  func,
							  convergence);
  }

  // method: levenbergMarquardt
  //  least-squares Levenberg-Marquardt optimization
  //
  static boolean levenbergMarquardt(VectorDouble& params, double& chi_square,
				    const VectorDouble& x,
				    const VectorDouble& y,
				    const VectorDouble& stddev,
				    LEV_MARQ_FUNC_DOUBLE func,
				    double convergence=DEF_LEVMARQ_CONVERGE) {
    return levMarqTemplate<MatrixDouble,VectorDouble,double>(params,
							     chi_square,
							     x, y, stddev,
							     func,
							     convergence);
  }
  
  //---------------------------------------------------------------------------
  //
  // private methods
  //
  //---------------------------------------------------------------------------
private:

  // templatized Levenberg-Marquardt optimization
  //
  template <class TMatrix, class TVector, class TIntegral>
  static boolean levMarqTemplate(TVector& params,
				 TIntegral& chi_square,
				 const TVector& x,
				 const TVector& y,
				 const TVector& stddev,
				 boolean (*)(TIntegral&, TVector&,
					     const TIntegral, const TVector&),
				 TIntegral convergence);

  // auxiliary methods:
  //  Levenberg-Marquardt routines
  //
  template <class TMatrix, class TVector, class TIntegral>
  static boolean levMarqChiSquare(TIntegral& chi_square, TMatrix& alpha,
				  TVector& beta, const TVector& x,
				  const TVector& y,
				  const TVector& inv_variance,
				  const TVector& params,
				  boolean (*)(TIntegral&, TVector&,
					      const TIntegral,
					      const TVector&));

  // auxiliary methods:
  //  general purpose
  //
  template <class TMatrix, class TIntegral>
  static boolean scaleDiagonal(TMatrix& mat, const TIntegral& scale);

  // auxiliary methods:
  //  diagnose
  //
  static boolean diagnoseSigmoidFl(float& y, VectorFloat& derivatives,
				   const float x,
				   const VectorFloat& params);
  static boolean diagnoseSigmoidDoub(double& y, VectorDouble& derivatives,
				     const double x,
				     const VectorDouble& params);
};

// end of include file
// 
#endif

