1 /////////////////////////////////////////////////////////////////////////////////
2 //
3 //  Levenberg - Marquardt non-linear minimization algorithm
4 //  Copyright (C) 2004-05  Manolis Lourakis (lourakis at ics forth gr)
5 //  Institute of Computer Science, Foundation for Research & Technology - Hellas
6 //  Heraklion, Crete, Greece.
7 //
8 //  This program is free software; you can redistribute it and/or modify
9 //  it under the terms of the GNU General Public License as published by
10 //  the Free Software Foundation; either version 2 of the License, or
11 //  (at your option) any later version.
12 //
13 //  This program is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 //  GNU General Public License for more details.
17 //
18 /////////////////////////////////////////////////////////////////////////////////
19 
20 /********************************************************************************
21  * Box-constrained Levenberg-Marquardt nonlinear minimization. The same core code
22  * is used with appropriate #defines to derive single and double precision versions,
23  * see also lmbc_core.c
24  ********************************************************************************/
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <math.h>
29 #include <float.h>
30 
31 #include "levmar.h"
32 #include "compiler.h"
33 #include "misc.h"
34 
35 #define EPSILON       1E-12
36 #define ONE_THIRD     0.3333333334 /* 1.0/3.0 */
37 #define _LSITMAX_     150 /* max #iterations for line search */
38 #define _POW_         2.1
39 
40 #if !defined(LM_DBL_PREC) && !defined(LM_SNGL_PREC)
41 #error At least one of LM_DBL_PREC, LM_SNGL_PREC should be defined!
42 #endif
43 
44 
45 #ifdef LM_SNGL_PREC
46 /* single precision (float) definitions */
47 #define LM_REAL float
48 #define LM_PREFIX s
49 
50 #define LM_REAL_MAX FLT_MAX
51 #define LM_REAL_MIN -FLT_MAX
52 
53 #define LM_REAL_EPSILON FLT_EPSILON
54 #define __SUBCNST(x) x##F
55 #define LM_CNST(x) __SUBCNST(x) // force substitution
56 
57 #include "lmbc_core.c" // read in core code
58 
59 #undef LM_REAL
60 #undef LM_PREFIX
61 #undef LM_REAL_MAX
62 #undef LM_REAL_MIN
63 #undef LM_REAL_EPSILON
64 #undef __SUBCNST
65 #undef LM_CNST
66 #endif /* LM_SNGL_PREC */
67 
68 #ifdef LM_DBL_PREC
69 /* double precision definitions */
70 #define LM_REAL double
71 #define LM_PREFIX d
72 
73 #define LM_REAL_MAX DBL_MAX
74 #define LM_REAL_MIN -DBL_MAX
75 
76 #define LM_REAL_EPSILON DBL_EPSILON
77 #define LM_CNST(x) (x)
78 
79 #include "lmbc_core.c" // read in core code
80 
81 #undef LM_REAL
82 #undef LM_PREFIX
83 #undef LM_REAL_MAX
84 #undef LM_REAL_MIN
85 #undef LM_REAL_EPSILON
86 #undef LM_CNST
87 #endif /* LM_DBL_PREC */
88