mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
templateLevmar.hpp
Go to the documentation of this file.
1 /** \file templateLevmar.hpp
2  * \author Jared R. Males (jaredmales@gmail.com)
3  * \brief Templatized wrappers to the levmar minimization routines..
4  * \ingroup fitting_files
5  *
6  */
7 
8 //***********************************************************************//
9 // Copyright 2015, 2016, 2017 Jared R. Males (jaredmales@gmail.com)
10 //
11 // This file is part of mxlib.
12 //
13 // mxlib is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation, either version 3 of the License, or
16 // (at your option) any later version.
17 //
18 // mxlib is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with mxlib. If not, see <http://www.gnu.org/licenses/>.
25 //***********************************************************************//
26 
27 #ifndef __templateLevmar_hpp__
28 #define __templateLevmar_hpp__
29 
30 
31 //*************************************************************************************************//
32 // copied from lm.h:
33 
34 /* work arrays size for ?levmar_der and ?levmar_dif functions.
35  * should be multiplied by sizeof(double) or sizeof(float) to be converted to bytes
36  */
37 #define LM_DER_WORKSZ(npar, nmeas) (2*(nmeas) + 4*(npar) + (nmeas)*(npar) + (npar)*(npar))
38 #define LM_DIF_WORKSZ(npar, nmeas) (4*(nmeas) + 4*(npar) + (nmeas)*(npar) + (npar)*(npar))
39 
40 /* work arrays size for ?levmar_bc_der and ?levmar_bc_dif functions.
41  * should be multiplied by sizeof(double) or sizeof(float) to be converted to bytes
42  */
43 #define LM_BC_DER_WORKSZ(npar, nmeas) (2*(nmeas) + 4*(npar) + (nmeas)*(npar) + (npar)*(npar))
44 #define LM_BC_DIF_WORKSZ(npar, nmeas) LM_BC_DER_WORKSZ((npar), (nmeas)) /* LEVMAR_BC_DIF currently implemented using LEVMAR_BC_DER()! */
45 
46 /* work arrays size for ?levmar_lec_der and ?levmar_lec_dif functions.
47  * should be multiplied by sizeof(double) or sizeof(float) to be converted to bytes
48  */
49 #define LM_LEC_DER_WORKSZ(npar, nmeas, nconstr) LM_DER_WORKSZ((npar)-(nconstr), (nmeas))
50 #define LM_LEC_DIF_WORKSZ(npar, nmeas, nconstr) LM_DIF_WORKSZ((npar)-(nconstr), (nmeas))
51 
52 /* work arrays size for ?levmar_blec_der and ?levmar_blec_dif functions.
53  * should be multiplied by sizeof(double) or sizeof(float) to be converted to bytes
54  */
55 #define LM_BLEC_DER_WORKSZ(npar, nmeas, nconstr) LM_LEC_DER_WORKSZ((npar), (nmeas)+(npar), (nconstr))
56 #define LM_BLEC_DIF_WORKSZ(npar, nmeas, nconstr) LM_LEC_DIF_WORKSZ((npar), (nmeas)+(npar), (nconstr))
57 
58 /* work arrays size for ?levmar_bleic_der and ?levmar_bleic_dif functions.
59  * should be multiplied by sizeof(double) or sizeof(float) to be converted to bytes
60  */
61 #define LM_BLEIC_DER_WORKSZ(npar, nmeas, nconstr1, nconstr2) LM_BLEC_DER_WORKSZ((npar)+(nconstr2), (nmeas)+(nconstr2), (nconstr1)+(nconstr2))
62 #define LM_BLEIC_DIF_WORKSZ(npar, nmeas, nconstr1, nconstr2) LM_BLEC_DIF_WORKSZ((npar)+(nconstr2), (nmeas)+(nconstr2), (nconstr1)+(nconstr2))
63 
64 #define LM_OPTS_SZ 5 /* max(4, 5) */
65 #define LM_INFO_SZ 10
66 #define LM_ERROR -1
67 #define LM_INIT_MU 1E-03
68 #define LM_STOP_THRESH 1E-17
69 #define LM_DIFF_DELTA 1E-06
70 #define LM_VERSION "2.6 (November 2011)"
71 
72 namespace mx
73 {
74 namespace math
75 {
76 namespace fit
77 {
78 
79 template<typename floatT>
80 int levmar_dif( void (*func)(floatT *p, floatT *hx, int m, int n, void *adata),
81  floatT *p,
82  floatT *x,
83  int m,
84  int n,
85  int itmax,
86  floatT *opts,
87  floatT *info,
88  floatT *work,
89  floatT *covar,
90  void *adata
91  );
92 
93 template<>
94 int levmar_dif<double>( void (*func)(double *p, double *hx, int m, int n, void *adata),
95  double *p,
96  double *x,
97  int m,
98  int n,
99  int itmax,
100  double *opts,
101  double *info,
102  double *work,
103  double *covar,
104  void *adata
105  );
106 
107 template<>
108 int levmar_dif<float>( void (*func)(float *p, float *hx, int m, int n, void *adata),
109  float *p,
110  float *x,
111  int m,
112  int n,
113  int itmax,
114  float *opts,
115  float *info,
116  float *work,
117  float *covar,
118  void *adata
119  );
120 
121 template<typename floatT>
122 int levmar_der( void (*func)(floatT *p, floatT *hx, int m, int n, void *adata),
123  void (*jacf)(floatT *p, floatT *j, int m, int n, void *adata),
124  floatT *p,
125  floatT *x,
126  int m,
127  int n,
128  int itmax,
129  floatT *opts,
130  floatT *info,
131  floatT *work,
132  floatT *covar,
133  void *adata
134  );
135 
136 template<>
137 int levmar_der<double>( void (*func)(double *p, double *hx, int m, int n, void *adata),
138  void (*jacf)(double *p, double *j, int m, int n, void *adata),
139  double *p,
140  double *x,
141  int m,
142  int n,
143  int itmax,
144  double *opts,
145  double *info,
146  double *work,
147  double *covar,
148  void *adata
149  );
150 
151 template<>
152 int levmar_der<float>( void (*func)(float *p, float *hx, int m, int n, void *adata),
153  void (*jacf)(float *p, float *j, int m, int n, void *adata),
154  float *p,
155  float *x,
156  int m,
157  int n,
158  int itmax,
159  float *opts,
160  float *info,
161  float *work,
162  float *covar,
163  void *adata
164  );
165 
166 } //namespace mx
167 } //namespace math
168 } //namespace fit
169 
170 #endif // __templateLevmar_hpp__
171 
The mxlib c++ namespace.
Definition: mxError.hpp:107