Repository navigation
Expand file tree
/
Copy pathcminpackcpp.hpp
More file actions
301 lines (259 loc) · 10.3 KB
/
Copy pathcminpackcpp.hpp
File metadata and controls
301 lines (259 loc) · 10.3 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* cminpackcpp.hpp -- a thin, header-only C++ wrapper around cminpack.
The C API passes the user callback as a plain function pointer plus an
opaque `void *p` for user data. That makes it awkward to use a C++ lambda,
a functor, or a std::function, because none of those convert to a plain
function pointer once they capture state (issue #74).
This header adds overloads in namespace `cminpack` that accept ANY callable
whose signature matches the corresponding cminpack callback (minus the
leading `void *p`): a lambda, a functor, a std::function, or a plain
function passed by name or by address. The callable is forwarded through
the `void *p` slot and invoked by a small template trampoline, so no state
needs to be global.
Example:
#include <cminpackcpp.hpp>
...
std::vector<double> y = ...; // captured measurement data
auto residual = [&](int m, int n, const double *x, double *fvec, int iflag) {
for (int i = 0; i < m; ++i)
fvec[i] = model(x, i) - y[i];
return 0;
};
int info = cminpack::lmdif1(residual, m, n, x, fvec, tol, iwa, wa, lwa);
The wrapper is header-only and precision-agnostic: it uses the same
__cminpack_real__ / __cminpack_func__ selection as cminpack.h, so defining
__cminpack_float__ (etc.) before including this header targets that variant.
If the callable throws, the solver is stopped as if it returned -1, the
callable is not called again, and the exception is rethrown after the
solver returns.
The callable must outlive the solver call (which it always does when passed
as an argument). The wrapper adds no allocation and no virtual dispatch.
*/
#ifndef __CMINPACKCPP_HPP__
#define __CMINPACKCPP_HPP__
#include <cminpack.h>
#if !defined(__cplusplus)
#error "cminpackcpp.hpp is a C++ header; include cminpack.h from C code."
#endif
#include <exception> /* std::exception_ptr */
#include <memory> /* std::addressof */
#include <type_traits> /* std::remove_reference, std::is_function */
#if defined(__cpp_exceptions) || defined(_CPPUNWIND)
#define CMINPACKCPP_EXCEPTIONS
#endif
namespace cminpack {
typedef __cminpack_real__ real;
namespace detail {
/* callable<F> holds what is passed through the `void *p` slot, and `type` is
what the trampolines cast p back to.
For objects (lambdas, functors, std::function, function pointers) p points
to the caller's object itself.
cv-qualifiers are kept so a const callable is invoked as const. */
template <typename F,
bool = std::is_function<typename std::remove_reference<F>::type>::value>
struct callable {
typedef typename std::remove_reference<F>::type type;
void *ptr;
explicit callable(typename std::remove_reference<F>::type &fcn)
: ptr(const_cast<void *>(
static_cast<const void *>(std::addressof(fcn)))) {}
void *get() const { return ptr; }
};
/* A plain function passed by name has a function type, and a function's
address cannot be cast to void * (issue #92). Decay it to a function
pointer, store that pointer here, and pass the address of the pointer. */
template <typename F>
struct callable<F, true> {
typedef typename std::remove_reference<F>::type *type;
type fp;
explicit callable(type fcn) : fp(fcn) {}
void *get() { return &fp; }
};
/* what is passed through the `void *p` slot: the callable, and the
exception it threw, if any */
struct context {
void *fcn;
std::exception_ptr error;
explicit context(void *f) : fcn(f), error() {}
int rethrow(int info) const
{
#ifdef CMINPACKCPP_EXCEPTIONS
if (error) {
std::rethrow_exception(error);
}
#endif
return info;
}
};
/* call the callable; if it throws, store the exception and return -1 to stop
the solver, and don't call it again */
template <typename Fn, typename... Args>
int call(void *p, Args... args)
{
context *c = static_cast<context *>(p);
#ifdef CMINPACKCPP_EXCEPTIONS
if (c->error) {
return -1;
}
try {
return (*static_cast<Fn *>(c->fcn))(args...);
} catch (...) {
c->error = std::current_exception();
return -1;
}
#else
return (*static_cast<Fn *>(c->fcn))(args...);
#endif
}
/* trampolines: forward the arguments to the callable */
template <typename Fn>
int fcn_nn(void *p, int n, const real *x, real *fvec, int iflag)
{
return call<Fn>(p, n, x, fvec, iflag);
}
template <typename Fn>
int fcnder_nn(void *p, int n, const real *x, real *fvec, real *fjac,
int ldfjac, int iflag)
{
return call<Fn>(p, n, x, fvec, fjac, ldfjac, iflag);
}
template <typename Fn>
int fcn_mn(void *p, int m, int n, const real *x, real *fvec, int iflag)
{
return call<Fn>(p, m, n, x, fvec, iflag);
}
template <typename Fn>
int fcnder_mn(void *p, int m, int n, const real *x, real *fvec, real *fjac,
int ldfjac, int iflag)
{
return call<Fn>(p, m, n, x, fvec, fjac, ldfjac, iflag);
}
template <typename Fn>
int fcnderstr_mn(void *p, int m, int n, const real *x, real *fvec, real *fjrow,
int iflag)
{
return call<Fn>(p, m, n, x, fvec, fjrow, iflag);
}
} /* namespace detail */
/* --- nonlinear equations: hybrd / hybrd1 (finite-difference Jacobian) --- */
template <typename F>
inline int hybrd1(F &&fcn, int n, real *x, real *fvec, real tol,
real *wa, int lwa)
{
detail::callable<F> c(fcn);
typedef typename detail::callable<F>::type Fn;
detail::context ctx(c.get());
return ctx.rethrow(::__cminpack_func__(hybrd1)(
&detail::fcn_nn<Fn>, &ctx,
n, x, fvec, tol, wa, lwa));
}
template <typename F>
inline int hybrd(F &&fcn, int n, real *x, real *fvec, real xtol, int maxfev,
int ml, int mu, real epsfcn, real *diag, int mode,
real factor, int nprint, int *nfev, real *fjac, int ldfjac,
real *r, int lr, real *qtf,
real *wa1, real *wa2, real *wa3, real *wa4)
{
detail::callable<F> c(fcn);
typedef typename detail::callable<F>::type Fn;
detail::context ctx(c.get());
return ctx.rethrow(::__cminpack_func__(hybrd)(
&detail::fcn_nn<Fn>, &ctx,
n, x, fvec, xtol, maxfev, ml, mu, epsfcn, diag, mode, factor, nprint,
nfev, fjac, ldfjac, r, lr, qtf, wa1, wa2, wa3, wa4));
}
/* --- nonlinear equations: hybrj / hybrj1 (analytic Jacobian) --- */
template <typename F>
inline int hybrj1(F &&fcn, int n, real *x, real *fvec, real *fjac, int ldfjac,
real tol, real *wa, int lwa)
{
detail::callable<F> c(fcn);
typedef typename detail::callable<F>::type Fn;
detail::context ctx(c.get());
return ctx.rethrow(::__cminpack_func__(hybrj1)(
&detail::fcnder_nn<Fn>, &ctx,
n, x, fvec, fjac, ldfjac, tol, wa, lwa));
}
/* --- least squares, finite-difference Jacobian: lmdif / lmdif1 --- */
template <typename F>
inline int lmdif1(F &&fcn, int m, int n, real *x, real *fvec, real tol,
int *iwa, real *wa, int lwa)
{
detail::callable<F> c(fcn);
typedef typename detail::callable<F>::type Fn;
detail::context ctx(c.get());
return ctx.rethrow(::__cminpack_func__(lmdif1)(
&detail::fcn_mn<Fn>, &ctx,
m, n, x, fvec, tol, iwa, wa, lwa));
}
template <typename F>
inline int lmdif(F &&fcn, int m, int n, real *x, real *fvec, real ftol,
real xtol, real gtol, int maxfev, real epsfcn, real *diag,
int mode, real factor, int nprint, int *nfev, real *fjac,
int ldfjac, int *ipvt, real *qtf,
real *wa1, real *wa2, real *wa3, real *wa4)
{
detail::callable<F> c(fcn);
typedef typename detail::callable<F>::type Fn;
detail::context ctx(c.get());
return ctx.rethrow(::__cminpack_func__(lmdif)(
&detail::fcn_mn<Fn>, &ctx,
m, n, x, fvec, ftol, xtol, gtol, maxfev, epsfcn, diag, mode, factor,
nprint, nfev, fjac, ldfjac, ipvt, qtf, wa1, wa2, wa3, wa4));
}
/* --- least squares, analytic Jacobian: lmder / lmder1 --- */
template <typename F>
inline int lmder1(F &&fcn, int m, int n, real *x, real *fvec, real *fjac,
int ldfjac, real tol, int *ipvt, real *wa, int lwa)
{
detail::callable<F> c(fcn);
typedef typename detail::callable<F>::type Fn;
detail::context ctx(c.get());
return ctx.rethrow(::__cminpack_func__(lmder1)(
&detail::fcnder_mn<Fn>, &ctx,
m, n, x, fvec, fjac, ldfjac, tol, ipvt, wa, lwa));
}
template <typename F>
inline int lmder(F &&fcn, int m, int n, real *x, real *fvec, real *fjac,
int ldfjac, real ftol, real xtol, real gtol, int maxfev,
real *diag, int mode, real factor, int nprint, int *nfev,
int *njev, int *ipvt, real *qtf,
real *wa1, real *wa2, real *wa3, real *wa4)
{
detail::callable<F> c(fcn);
typedef typename detail::callable<F>::type Fn;
detail::context ctx(c.get());
return ctx.rethrow(::__cminpack_func__(lmder)(
&detail::fcnder_mn<Fn>, &ctx,
m, n, x, fvec, fjac, ldfjac, ftol, xtol, gtol, maxfev, diag, mode,
factor, nprint, nfev, njev, ipvt, qtf, wa1, wa2, wa3, wa4));
}
/* --- least squares, row-wise analytic Jacobian: lmstr / lmstr1 --- */
template <typename F>
inline int lmstr1(F &&fcn, int m, int n, real *x, real *fvec, real *fjac,
int ldfjac, real tol, int *ipvt, real *wa, int lwa)
{
detail::callable<F> c(fcn);
typedef typename detail::callable<F>::type Fn;
detail::context ctx(c.get());
return ctx.rethrow(::__cminpack_func__(lmstr1)(
&detail::fcnderstr_mn<Fn>, &ctx,
m, n, x, fvec, fjac, ldfjac, tol, ipvt, wa, lwa));
}
template <typename F>
inline int lmstr(F &&fcn, int m, int n, real *x, real *fvec, real *fjac,
int ldfjac, real ftol, real xtol, real gtol, int maxfev,
real *diag, int mode, real factor, int nprint, int *nfev,
int *njev, int *ipvt, real *qtf,
real *wa1, real *wa2, real *wa3, real *wa4)
{
detail::callable<F> c(fcn);
typedef typename detail::callable<F>::type Fn;
detail::context ctx(c.get());
return ctx.rethrow(::__cminpack_func__(lmstr)(
&detail::fcnderstr_mn<Fn>, &ctx,
m, n, x, fvec, fjac, ldfjac, ftol, xtol, gtol, maxfev, diag, mode,
factor, nprint, nfev, njev, ipvt, qtf, wa1, wa2, wa3, wa4));
}
} /* namespace cminpack */
#undef CMINPACKCPP_EXCEPTIONS
#endif /* !__CMINPACKCPP_HPP__ */