name: NonLinearOptimization

synopsis:

```g++ [flags ...] file ... -l /isip/tools/lib/\$ISIP_BINARY/lib_algo.a

#include <NonLinearOptimization.h>

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);
```
quick start:

```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);
```
description:

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:
W. Press, S. Teukolsky, W. Vetterling, B. Flannery, Numerical Recipes in C, Second Edition, Cambridge University Press, New York, New York, USA, pp. 681-685, 1995.
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.

dependencies:

public constants:

• define the class name:
`static const String CLASS_NAME = L"NonLinearOptimization";`
• i/o related constants:
`static const String DEF_PARAM = L"";`
• Levenberg-Marquardt computation constants:
`static const float LEVMARQ_INIT_LAMBDA = 0.001;`
• default values and arguments:
`static const float DEF_LEVMARQ_CONVERGE = 0.1;`
error codes:

protected data:

required public methods:

• static methods:
`static const String& name();`
`static boolean diagnose(Integral::DEBUG debug_level);`
• debug methods:
`boolean setDebug(Integral::DEBUG debug_level);`
`boolean debug(const unichar* message) const;`
• destructor/constructor(s):
`~NonLinearOptimization();`
`NonLinearOptimization();`
`NonLinearOptimization(const NonLinearOptimization& arg);`
• assign methods:
`boolean assign(const NonLinearOptimization& arg)`
• operator= methods:
`NonLinearOptimization& operator= (const NonLinearOptimization& arg);`
• 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 NonLinearOptimization& arg) const;`
• memory management methods:
`static void* operator new(size_t size);`
`static void* operator new[](size_t size);`
`static void operator delete(void* ptr);`
`static void operator delete[](void* ptr);`
`static boolean setGrowSize(long grow_size);`
`boolean clear(Integral::CMODE ctype = Integral::DEF_CMODE);`
class-specific public methods:

• parametric curve-fitting methods:
`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);`
`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);`
private methods:

• 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);`
`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&));`
`template <class TMatrix, class TVector, class TIntegral> static boolean scaleDiagonal(TMatrix& mat, const TIntegral& scale);`
`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);`
examples:

• This example shows how to use the Levenberg-Marquardt method of least-squares optimization of nonlinear functions:
```#include <NonLinearOptimization.h>
#include <Console.h>

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"diagnose.sof");
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");

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();
}```
notes:

• None.