File: minimize.h

package info (click to toggle)
fslview 4.0.1-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,812 kB
  • ctags: 4,932
  • sloc: cpp: 28,276; ansic: 5,103; sh: 250; makefile: 125; python: 72; tcl: 43
file content (122 lines) | stat: -rw-r--r-- 2,970 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*  minimize
 
    Tim Behrens, FMRIB Image Analysis Group

    Copyright (C) 1999-2000 University of Oxford  */

/*  CCOPYRIGHT  */

#if !defined(minimize_h)
#define minimize_h

#include <string>
#include <iostream>
#include <fstream>
//#include <unistd.h>
#include <vector>
#include <algorithm>
#include "newmatap.h"
#include "newmatio.h"
#include "miscmaths.h"


#define WANT_STREAM
#define WANT_MATH

using namespace MISCMATHS;
using namespace NEWMAT;
using namespace std;
///////////////////////////////////////////////////////

//fminsearch.m

namespace MISCMATHS {

class pair_comparer
{
public:
  bool operator()(const pair<float,ColumnVector>& p1,const pair<float,ColumnVector>& p2) const
  {
    return p1.first < p2.first;
  }    
};

  class EvalFunction;
  class gEvalFunction;

float diff1(const ColumnVector& x, const EvalFunction& func, int i,float h,int errorord=4);// finite diff derivative

float diff2(const ColumnVector& x, const EvalFunction& func, int i,float h,int errorord=4);// finite diff 2nd derivative

float diff2(const ColumnVector& x, const EvalFunction& func, int i,int j,float h,int errorord=4);// finite diff cross derivative

ReturnMatrix gradient(const ColumnVector& x, const EvalFunction& func,float h,int errorord=4);// finite diff derivative vector 

ReturnMatrix hessian(const ColumnVector& x, const EvalFunction& func,float h,int errorord=4);// finite diff hessian

void minsearch(ColumnVector& x, const EvalFunction& func, ColumnVector& paramstovary);

void scg(ColumnVector& x, const gEvalFunction& func, ColumnVector& paramstovary, float tol = 0.0000001, float eps=1e-16, int niters=500);

class EvalFunction
{//Function where gradient is not analytic (or you are too lazy to work it out) (required for fminsearch)
public:
  EvalFunction(){}
  virtual float evaluate(const ColumnVector& x) const = 0; //evaluate the function
  virtual ~EvalFunction(){};

  virtual void minimize(ColumnVector& x)
  {
    ColumnVector paramstovary(x.Nrows());
    paramstovary = 1;
    minsearch(x,*this,paramstovary);
  }

  virtual void minimize(ColumnVector& x, ColumnVector& paramstovary)
  {
    minsearch(x,*this,paramstovary);
  }

private:
  const EvalFunction& operator=(EvalFunction& par);
  EvalFunction(const EvalFunction&);
};

class gEvalFunction : public EvalFunction
{//Function where gradient is analytic (required for scg)
public:
  gEvalFunction() : EvalFunction(){}
  // evaluate is inherited from EvalFunction
  
  virtual ReturnMatrix g_evaluate(const ColumnVector& x) const = 0; //evaluate the gradient
  virtual ~gEvalFunction(){};

  virtual void minimize(ColumnVector& x)
  {
    ColumnVector paramstovary(x.Nrows());
    paramstovary = 1;
    scg(x,*this,paramstovary);
  }

  virtual void minimize(ColumnVector& x, ColumnVector& paramstovary)
  {
    scg(x,*this,paramstovary);
  }

private:
    
  const gEvalFunction& operator=(gEvalFunction& par);
  gEvalFunction(const gEvalFunction&);
};

}
   

#endif