<?php $isip_window_title = "ISIP Homepage"; ?>
<?php include("/data/isip/www/templates/documentation/header/header.html"); ?>

<!--#set var="isip_page_title" value="NonLinearOptimization" -->
<?php include("/data/isip/www/templates/documentation/body/index_class.html"); ?>
<?php include("/data/isip/www/templates/documentation/body/bar.html"); ?>

<a name="name">
<b>
name:
<a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/NonlinearOptimization.h">NonLinearOptimization</a></b>

<br><br>

<a name="synopsis">
<b>synopsis:</b>
<br><br>

<blockquote>
<pre>
g++ [flags ...] file ... -l /isip/tools/lib/$ISIP_BINARY/lib_algo.a

#include &lt;NonLinearOptimization.h&gt;

NonLinearOptimization();
NonLinearOptimization(const NonLinearOptimization& arg);
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);
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);
</pre>
</blockquote>

<a name="quick_start">
<b>quick start:</b>
<br><br>

<blockquote>
<pre>
VectorFloat v_fl(L"1, 2, 3");
float chi_sq_fl = 0;
VectorFloat x_fl(L" 0, 1, 1");
VectorFloat stddev_fl(L"1, 1, 1");
VectorFloat y_fl;
NonlinearOptimization::levenbergMarquardt(v_fl, chi_sq_fl, x_fl,
					  y_fl, stddev_fl,
				          NonlinearOptimization::diagnoseSigmoidFl);
</pre>
</blockquote>

<a name="description"> 
<b>description:</b>
<br><br>

The NonLinearOptimization class is used to perform standard nonlinear
optimization techniques including parametric curve fitting and
nonparametric quadratic optimization. A good reference
to learn about such techniques is:

<ul>
  W. Press, S. Teukolsky, W. Vetterling, B. Flannery,
  <i>Numerical Recipes in C</i>, Second Edition, Cambridge University Press,
  New York, New York, USA, pp. 681-685, 1995.
</ul>

Our derivations follow this reference, but the code has been
completely rewritten to make use of the IFC environment,
and to better suit the needs of the IFC-based programmer.

<br><br>

<a name="dependencies">
<b>dependencies:</b>
<br><br>

<ul>
  <li> <a href="../../math/matrix/MatrixDouble/index.html">MatrixDouble</a>
       
  <li> <a href="../../math/matrix/MatrixFloat/index.html">MatrixFloat</a>
       
  <li> <a href="../../system/MemoryManager/index.html">MemoryManager</a>
</ul>

<a name="constants">
<b>public constants:</b>
<br><br>

<ul>
  <li> define the class name:
       
       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/nonlin_00.cc&constant=CLASS_NAME">static const String CLASS_NAME = L"NonLinearOptimization";</a></pre>

  <li> i/o related constants:
       
       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/nonlin_00.cc&constant=DEF_PARAM">static const String DEF_PARAM = L"";</pre></a>

  <li>  Levenberg-Marquardt computation constants:

       <pre>static const float LEVMARQ_INIT_LAMBDA = 0.001;</pre>

  <li> default values and arguments:

       <pre>static const float DEF_LEVMARQ_CONVERGE = 0.1;</pre>
</ul>

<a name="error">
<b>error codes:</b>
<br><br>

<ul>
  <li> error code indicating NonLinearOptimization class general error:

       <pre><a href="/perl/ifc_document.pl?err=00035200">static const long ERR = (long)35200;</a></pre>
       <pre><a href="/perl/ifc_document.pl?err=00035201">static const long ERR_LEV_MARQ = (long)35201;</a></pre>
</ul>

<a name="protected">
<b>protected data:</b>
<br><br>

<ul>

  <li> typedef for the functional form to use for Levenberg-Marquardt
       optimization:

       <pre>typedef boolean (*LEV_MARQ_FUNC_FLOAT)(float&, VectorFloat&, const float, const VectorFloat&);</pre>

       <pre>typedef boolean (*LEV_MARQ_FUNC_DOUBLE)(double&, VectorDouble&, const double, const VectorDouble&);</pre>

  <li> debug level:

       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/nonlin_00.cc&static=debug_level_d">static Integral::DEBUG debug_level_d;</a></pre>
       
  <li> a static memory manager:

       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/nonlin_00.cc&static=mgr_d">static MemoryManager mgr_d;</pre></a>
</ul>

<a name="public">
<b>required public methods:</b>
<br><br>

<ul>
  <li>  static methods:

       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/NonlinearOptimization.h&method=name">static const String& name();</a></pre>
       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/nonlin_02.cc&method=diagnose">static boolean diagnose(Integral::DEBUG debug_level);</a></pre>

  <li> debug methods:
       
       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/NonlinearOptimization.h&method=setDebug">boolean setDebug(Integral::DEBUG debug_level);</a></pre>
       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/NonlinearOptimization.h&method=debug">boolean debug(const unichar* message) const;</a></pre>

  <li> destructor/constructor(s):
       
       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/NonlinearOptimization.h&method=destructor">~NonLinearOptimization();</a></pre>
       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/NonlinearOptimization.h&method=default+constructor">NonLinearOptimization();</a></pre>
       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/NonlinearOptimization.h&method=copy+constructor">NonLinearOptimization(const NonLinearOptimization& arg);</a></pre>     

  <li> assign methods:
       
       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/NonlinearOptimization.h&method=assign">boolean assign(const NonLinearOptimization& arg)</a></pre>

  <li> operator= methods:
       
      <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/NonlinearOptimization.h&method=operator%3D">NonLinearOptimization& operator= (const NonLinearOptimization& arg);</a></pre>
                     
  <li> i/o methods:
       
       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/NonlinearOptimization.h&method=sofSize">long sofSize() const;</a></pre>
       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/NonlinearOptimization.h&method=read">boolean read(Sof& sof, long tag, const String& name = CLASS_NAME);</a></pre>
       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/NonlinearOptimization.h&method=write">boolean write(Sof& sof, long tag, const String& name = CLASS_NAME) const;</a></pre>
       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/NonlinearOptimization.h&method=readData">boolean readData(Sof& sof, const String& pname = DEF_PARAM,long size = SofParser::FULL_OBJECT, boolean param = true, boolean nested = false);</a></pre>
       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/NonlinearOptimization.h&method=writeData">boolean writeData(Sof& sof, const String& pname = DEF_PARAM) const;</a></pre>
       
  <li> equality methods:
       
       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/NonlinearOptimization.h&method=eq">boolean eq(const NonLinearOptimization& arg) const;</a></pre>

  <li> memory management methods:
       
       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/NonlinearOptimization.h&method=new">static void* operator new(size_t size);</a></pre>
       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/NonlinearOptimization.h&method=new[]">static void* operator new[](size_t size);</a></pre>
       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/NonlinearOptimization.h&method=delete">static void operator delete(void* ptr);</a></pre>
       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/NonlinearOptimization.h&method=delete[]">static void operator delete[](void* ptr);</a></pre>  
       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/NonlinearOptimization.h&method=setGrowSize">static boolean setGrowSize(long grow_size);</a></pre>  
       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/NonlinearOptimization.h&method=clear">boolean clear(Integral::CMODE ctype = Integral::DEF_CMODE);</a></pre>
</ul>

<a name="specific">
<b>class-specific public methods:</b>
<br><br>

<ul>
  <li> parametric curve-fitting methods:
       
       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/NonlinearOptimization.h&method=levenbergMarquardt&instance=0">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);</a></pre>
       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/NonlinearOptimization.h&method=levenbergMarquardt&instance=1">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);</a></pre>
</ul>
  
<a name="private">
<b>private methods:</b>
<br><br>

<ul>
  <li> templatized Levenberg-Marquardt optimization:

       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/nonlin_05.cc&method=levMarqTemplate">template &lt;class TMatrix, class TVector, class TIntegral&gt; 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);</a></pre>
       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/nonlin_05.cc&method=levMarqChiSquare">template &lt;class TMatrix, class TVector, class TIntegral&gt; 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&));</a></pre>
       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/nonlin_05.cc&method=scaleDiagonal">template &lt;class TMatrix, class TVector, class TIntegral&gt; static boolean scaleDiagonal(TMatrix& mat, const TIntegral& scale);</a></pre>
       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/nonlin_02.cc&method=diagnoseSigmoidFl">static boolean diagnoseSigmoidFl(float& y, VectorFloat& derivatives, const float x, const VectorFloat& params);</a></pre>
       <pre><a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/nonlin_02.cc&method=diagnoseSigmoidDoub">static boolean diagnoseSigmoidDoub(double& y, VectorDouble& derivatives, const double x, const VectorDouble& params);</a></pre>
</ul>

<a name="examples">
<b>examples:</b>
<br><br>

<ul>

  <li> This example shows how to use the Levenberg-Marquardt method of
       least-squares optimization of nonlinear functions:

<pre>
#include &lt;NonLinearOptimization.h&gt;
#include &lt;Console.h&gt;

int main() {

  // declare the measured data
  //
  VectorFloat params_fl;
  VectorFloat x_fl;
  VectorFloat y_fl;
  VectorFloat stddev_fl;
  VectorFloat final_params_fl;

  // open the test file
  //
  String test_file(L"<a href="/perl/ifc_document.pl?file=$isip/class/numeric/NonlinearOptimization/diagnose.sof">diagnose.sof</a>");
  Sof test_sof;
  test_sof.open(test_file);

  // read in the test data
  //
  String x_tag(L"x");
  String y_tag(L"y");
  String stddev_tag(L"stddev");
  String init_params_tag(L"initial_params");
  String final_params_tag(L"final_params");

  params_fl.read(test_sof, 0, init_params_tag);
  x_fl.read(test_sof, 0, x_tag);
  y_fl.read(test_sof, 0, y_tag);
  stddev_fl.read(test_sof, 0, stddev_tag);
  final_params_fl.read(test_sof, 0, final_params_tag);

  test_sof.close();
  
  // test levenbergMarquardt
  //
  float chi_sq_fl = 0;
  NonlinearOptimization::levenbergMarquardt(params_fl, chi_sq_fl, x_fl,
					    y_fl, stddev_fl,
				    NonlinearOptimization::diagnoseSigmoidFl);

  // verify that results match
  //
  if (!final_params_fl.almostEqual(params_fl)) {
    params_fl.debug(L"actual params");
    final_params_fl.debug(L"expected params");    
    Console::put(L"Error in levenbergMarquardt method");
  }       
       
  // exit gracefully
  //
  Integral::exit();
}</pre>       
</ul>

<a name="notes">
<b>notes:</b>
<br><br>

<ul>
  <li>None.
</ul>

<!--#include virtual="/hse/ies/templates/documentation/footer/footer.html" -->
