1 /*
2  * Copyright (C) 2017 Jelmer Ypma. All Rights Reserved.
3  * This code is published under the L-GPL.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published
7  * by the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * File:   nloptrAPI.h
19  * Author: Jelmer Ypma
20  * Date:   3 October 2017
21  *
22  * This file provides an API for calling internal NLopt code from C within
23  * R packages. The C functions that are registered in init_nloptr.c can be
24  * accessed by external R packages.
25  *
26  * 03/10/2017: Initial version exposing nlopt_version.
27  */
28 
29 
30 #ifndef __NLOPTRAPI_H__
31 #define __NLOPTRAPI_H__
32 
33 #include <R_ext/Rdynload.h>
34 #include <R.h>
35 #include <Rinternals.h>
36 
37 #include "nlopt.h"
38 
39 /*
40  * C functions can be exposed using the following template:
41  *
42  * RET_TYPE FUNCNAME(ARGTYPE_1 ARGNAME 1, ARGTYPE_2 ARGNAME_2)
43  * {
44  *     static RET_TYPE(*fun)(ARGTYPE_1, ARGTYPE_2) = NULL;
45  *     if (fun == NULL) fun = (RET_TYPE(*)(ARGTYPE_1, ARGTYPE_2)) R_GetCCallable("nloptr","FUNCNAME");
46  *     return fun(ARGNAME_1, ARGNAME_2);
47  * }
48  *
49  */
50 
nlopt_algorithm_name(nlopt_algorithm a)51 NLOPT_EXTERN(const char *) nlopt_algorithm_name(nlopt_algorithm a)
52 {
53     static const char *(*fun)(nlopt_algorithm) = NULL;
54     if (fun == NULL) fun = (const char *(*)(nlopt_algorithm)) R_GetCCallable("nloptr","nlopt_algorithm_name");
55     return fun(a);
56 }
57 
nlopt_srand(unsigned long seed)58 NLOPT_EXTERN(void) nlopt_srand(unsigned long seed)
59 {
60     static void(*fun)(unsigned long) = NULL;
61     if (fun == NULL) fun = (void(*)(unsigned long)) R_GetCCallable("nloptr","nlopt_srand");
62     return fun(seed);
63 }
64 
nlopt_srand_time(void)65 NLOPT_EXTERN(void) nlopt_srand_time(void)
66 {
67     static void(*fun)(void) = NULL;
68     if (fun == NULL) fun = (void(*)(void)) R_GetCCallable("nloptr","nlopt_srand_time");
69     return fun();
70 }
71 
nlopt_version(int * major,int * minor,int * bugfix)72 NLOPT_EXTERN(void) nlopt_version(int *major, int *minor, int *bugfix)
73 {
74     static void(*fun)(int *, int *, int *) = NULL;
75     if (fun == NULL) fun = (void(*)(int *, int *, int *)) R_GetCCallable("nloptr","nlopt_version");
76     return fun(major, minor, major);
77 }
78 
nlopt_create(nlopt_algorithm algorithm,unsigned n)79 NLOPT_EXTERN(nlopt_opt) nlopt_create(nlopt_algorithm algorithm, unsigned n)
80 {
81     static nlopt_opt(*fun)(nlopt_algorithm, unsigned) = NULL;
82     if (fun == NULL) fun = (nlopt_opt(*)(nlopt_algorithm, unsigned)) R_GetCCallable("nloptr","nlopt_create");
83     return fun(algorithm, n);
84 }
85 
nlopt_destroy(nlopt_opt opt)86 NLOPT_EXTERN(void) nlopt_destroy(nlopt_opt opt)
87 {
88     static void(*fun)(nlopt_opt) = NULL;
89     if (fun == NULL) fun = (void(*)(nlopt_opt)) R_GetCCallable("nloptr","nlopt_destroy");
90     return fun(opt);
91 }
92 
nlopt_copy(const nlopt_opt opt)93 NLOPT_EXTERN(nlopt_opt) nlopt_copy(const nlopt_opt opt)
94 {
95     static nlopt_opt(*fun)(const nlopt_opt) = NULL;
96     if (fun == NULL) fun = (nlopt_opt(*)(const nlopt_opt)) R_GetCCallable("nloptr","nlopt_copy");
97     return fun(opt);
98 }
99 
nlopt_optimize(nlopt_opt opt,double * x,double * opt_f)100 NLOPT_EXTERN(nlopt_result) nlopt_optimize(nlopt_opt opt, double *x, double *opt_f)
101 {
102     static nlopt_result(*fun)(nlopt_opt, double *, double *) = NULL;
103     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, double *, double *)) R_GetCCallable("nloptr","nlopt_optimize");
104     return fun(opt, x, opt_f);
105 }
106 
nlopt_set_min_objective(nlopt_opt opt,nlopt_func f,void * f_data)107 NLOPT_EXTERN(nlopt_result) nlopt_set_min_objective(nlopt_opt opt, nlopt_func f, void *f_data)
108 {
109     static nlopt_result(*fun)(nlopt_opt, nlopt_func, void *) = NULL;
110     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, nlopt_func, void *)) R_GetCCallable("nloptr","nlopt_set_min_objective");
111     return fun(opt, f, f_data);
112 }
113 
nlopt_set_max_objective(nlopt_opt opt,nlopt_func f,void * f_data)114 NLOPT_EXTERN(nlopt_result) nlopt_set_max_objective(nlopt_opt opt, nlopt_func f, void *f_data)
115 {
116     static nlopt_result(*fun)(nlopt_opt, nlopt_func, void *) = NULL;
117     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, nlopt_func, void *)) R_GetCCallable("nloptr","nlopt_set_max_objective");
118     return fun(opt, f, f_data);
119 }
120 
nlopt_set_precond_min_objective(nlopt_opt opt,nlopt_func f,nlopt_precond pre,void * f_data)121 NLOPT_EXTERN(nlopt_result) nlopt_set_precond_min_objective(nlopt_opt opt, nlopt_func f, nlopt_precond pre, void *f_data)
122 {
123     static nlopt_result(*fun)(nlopt_opt, nlopt_func, nlopt_precond, void *) = NULL;
124     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, nlopt_func, nlopt_precond, void *)) R_GetCCallable("nloptr","nlopt_set_precond_min_objective");
125     return fun(opt, f, pre, f_data);
126 }
127 
nlopt_set_precond_max_objective(nlopt_opt opt,nlopt_func f,nlopt_precond pre,void * f_data)128 NLOPT_EXTERN(nlopt_result) nlopt_set_precond_max_objective(nlopt_opt opt, nlopt_func f, nlopt_precond pre, void *f_data)
129 {
130     static nlopt_result(*fun)(nlopt_opt, nlopt_func, nlopt_precond, void *) = NULL;
131     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, nlopt_func, nlopt_precond, void *)) R_GetCCallable("nloptr","nlopt_set_precond_max_objective");
132     return fun(opt, f, pre, f_data);
133 }
134 
nlopt_get_algorithm(const nlopt_opt opt)135 NLOPT_EXTERN(nlopt_algorithm) nlopt_get_algorithm(const nlopt_opt opt)
136 {
137     static nlopt_algorithm(*fun)(const nlopt_opt) = NULL;
138     if (fun == NULL) fun = (nlopt_algorithm(*)(const nlopt_opt)) R_GetCCallable("nloptr","nlopt_get_algorithm");
139     return fun(opt);
140 }
141 
nlopt_get_dimension(const nlopt_opt opt)142 NLOPT_EXTERN(unsigned) nlopt_get_dimension(const nlopt_opt opt)
143 {
144     static unsigned(*fun)(const nlopt_opt) = NULL;
145     if (fun == NULL) fun = (unsigned(*)(const nlopt_opt)) R_GetCCallable("nloptr","nlopt_get_dimension");
146     return fun(opt);
147 }
148 
149 /* constraints: */
150 
nlopt_set_lower_bounds(nlopt_opt opt,const double * lb)151 NLOPT_EXTERN(nlopt_result) nlopt_set_lower_bounds(nlopt_opt opt, const double *lb)
152 {
153     static nlopt_result(*fun)(nlopt_opt, const double *) = NULL;
154     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, const double *)) R_GetCCallable("nloptr","nlopt_set_lower_bounds");
155     return fun(opt, lb);
156 }
157 
nlopt_set_lower_bounds1(nlopt_opt opt,double lb)158 NLOPT_EXTERN(nlopt_result) nlopt_set_lower_bounds1(nlopt_opt opt, double lb)
159 {
160     static nlopt_result(*fun)(nlopt_opt, double) = NULL;
161     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, double)) R_GetCCallable("nloptr","nlopt_set_lower_bounds1");
162     return fun(opt, lb);
163 }
164 
nlopt_get_lower_bounds(const nlopt_opt opt,double * lb)165 NLOPT_EXTERN(nlopt_result) nlopt_get_lower_bounds(const nlopt_opt opt, double *lb)
166 {
167     static nlopt_result(*fun)(const nlopt_opt, double *) = NULL;
168     if (fun == NULL) fun = (nlopt_result(*)(const nlopt_opt, double *)) R_GetCCallable("nloptr","nlopt_get_lower_bounds");
169     return fun(opt, lb);
170 }
171 
nlopt_set_upper_bounds(nlopt_opt opt,const double * ub)172 NLOPT_EXTERN(nlopt_result) nlopt_set_upper_bounds(nlopt_opt opt, const double *ub)
173 {
174     static nlopt_result(*fun)(nlopt_opt, const double *) = NULL;
175     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, const double *)) R_GetCCallable("nloptr","nlopt_set_upper_bounds");
176     return fun(opt, ub);
177 }
178 
nlopt_set_upper_bounds1(nlopt_opt opt,double ub)179 NLOPT_EXTERN(nlopt_result) nlopt_set_upper_bounds1(nlopt_opt opt, double ub)
180 {
181     static nlopt_result(*fun)(nlopt_opt, double) = NULL;
182     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, double)) R_GetCCallable("nloptr","nlopt_set_upper_bounds1");
183     return fun(opt, ub);
184 }
185 
nlopt_get_upper_bounds(const nlopt_opt opt,double * ub)186 NLOPT_EXTERN(nlopt_result) nlopt_get_upper_bounds(const nlopt_opt opt, double *ub)
187 {
188     static nlopt_result(*fun)(const nlopt_opt, double *) = NULL;
189     if (fun == NULL) fun = (nlopt_result(*)(const nlopt_opt, double *)) R_GetCCallable("nloptr","nlopt_get_upper_bounds");
190     return fun(opt, ub);
191 }
192 
nlopt_remove_inequality_constraints(nlopt_opt opt)193 NLOPT_EXTERN(nlopt_result) nlopt_remove_inequality_constraints(nlopt_opt opt)
194 {
195     static nlopt_result(*fun)(nlopt_opt) = NULL;
196     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt)) R_GetCCallable("nloptr","nlopt_remove_inequality_constraints");
197     return fun(opt);
198 }
199 
nlopt_add_inequality_constraint(nlopt_opt opt,nlopt_func fc,void * fc_data,double tol)200 NLOPT_EXTERN(nlopt_result) nlopt_add_inequality_constraint(nlopt_opt opt,
201              nlopt_func fc,
202              void *fc_data,
203              double tol)
204 {
205     static nlopt_result(*fun)(nlopt_opt, nlopt_func, void *, double) = NULL;
206     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, nlopt_func, void *, double)) R_GetCCallable("nloptr","nlopt_add_inequality_constraint");
207     return fun(opt, fc, fc_data, tol);
208 }
209 
nlopt_add_precond_inequality_constraint(nlopt_opt opt,nlopt_func fc,nlopt_precond pre,void * fc_data,double tol)210 NLOPT_EXTERN(nlopt_result) nlopt_add_precond_inequality_constraint(
211         nlopt_opt opt, nlopt_func fc, nlopt_precond pre, void *fc_data,
212         double tol)
213 {
214     static nlopt_result(*fun)(nlopt_opt, nlopt_func, nlopt_precond, void *, double) = NULL;
215     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, nlopt_func, nlopt_precond, void *, double)) R_GetCCallable("nloptr","nlopt_add_precond_inequality_constraint");
216     return fun(opt, fc, pre, fc_data, tol);
217 }
218 
nlopt_add_inequality_mconstraint(nlopt_opt opt,unsigned m,nlopt_mfunc fc,void * fc_data,const double * tol)219 NLOPT_EXTERN(nlopt_result) nlopt_add_inequality_mconstraint(nlopt_opt opt,
220              unsigned m,
221              nlopt_mfunc fc,
222              void *fc_data,
223              const double *tol)
224 {
225     static nlopt_result(*fun)(nlopt_opt, unsigned, nlopt_mfunc, void *, const double *) = NULL;
226     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, unsigned, nlopt_mfunc, void *, const double *)) R_GetCCallable("nloptr","nlopt_add_inequality_mconstraint");
227     return fun(opt, m, fc, fc_data, tol);
228 }
229 
nlopt_remove_equality_constraints(nlopt_opt opt)230 NLOPT_EXTERN(nlopt_result) nlopt_remove_equality_constraints(nlopt_opt opt)
231 {
232     static nlopt_result(*fun)(nlopt_opt) = NULL;
233     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt)) R_GetCCallable("nloptr","nlopt_remove_equality_constraints");
234     return fun(opt);
235 }
236 
nlopt_add_equality_constraint(nlopt_opt opt,nlopt_func h,void * h_data,double tol)237 NLOPT_EXTERN(nlopt_result) nlopt_add_equality_constraint(nlopt_opt opt,
238              nlopt_func h,
239              void *h_data,
240              double tol)
241 {
242     static nlopt_result(*fun)(nlopt_opt, nlopt_func, void *, double) = NULL;
243     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, nlopt_func, void *, double)) R_GetCCallable("nloptr","nlopt_add_equality_constraint");
244     return fun(opt, h, h_data, tol);
245 }
246 
nlopt_add_precond_equality_constraint(nlopt_opt opt,nlopt_func h,nlopt_precond pre,void * h_data,double tol)247 NLOPT_EXTERN(nlopt_result) nlopt_add_precond_equality_constraint(
248         nlopt_opt opt, nlopt_func h, nlopt_precond pre, void *h_data,
249         double tol)
250 {
251     static nlopt_result(*fun)(nlopt_opt, nlopt_func, nlopt_precond, void *, double) = NULL;
252     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, nlopt_func, nlopt_precond, void *, double)) R_GetCCallable("nloptr","nlopt_add_precond_equality_constraint");
253     return fun(opt, h, pre, h_data, tol);
254 }
255 
nlopt_add_equality_mconstraint(nlopt_opt opt,unsigned m,nlopt_mfunc h,void * h_data,const double * tol)256 NLOPT_EXTERN(nlopt_result) nlopt_add_equality_mconstraint(nlopt_opt opt,
257              unsigned m,
258              nlopt_mfunc h,
259              void *h_data,
260              const double *tol)
261 {
262     static nlopt_result(*fun)(nlopt_opt, unsigned, nlopt_mfunc, void *, const double *) = NULL;
263     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, unsigned, nlopt_mfunc, void *, const double *)) R_GetCCallable("nloptr","nlopt_add_equality_mconstraint");
264     return fun(opt, m, h, h_data, tol);
265 }
266 
267 /* stopping criteria: */
268 
nlopt_set_stopval(nlopt_opt opt,double stopval)269 NLOPT_EXTERN(nlopt_result) nlopt_set_stopval(nlopt_opt opt, double stopval)
270 {
271     static nlopt_result(*fun)(nlopt_opt, double) = NULL;
272     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, double)) R_GetCCallable("nloptr","nlopt_set_stopval");
273     return fun(opt, stopval);
274 }
275 
nlopt_get_stopval(const nlopt_opt opt)276 NLOPT_EXTERN(double) nlopt_get_stopval(const nlopt_opt opt)
277 {
278     static double(*fun)(const nlopt_opt) = NULL;
279     if (fun == NULL) fun = (double(*)(const nlopt_opt)) R_GetCCallable("nloptr","nlopt_get_stopval");
280     return fun(opt);
281 }
282 
nlopt_set_ftol_rel(nlopt_opt opt,double tol)283 NLOPT_EXTERN(nlopt_result) nlopt_set_ftol_rel(nlopt_opt opt, double tol)
284 {
285     static nlopt_result(*fun)(nlopt_opt, double) = NULL;
286     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, double)) R_GetCCallable("nloptr","nlopt_set_ftol_rel");
287     return fun(opt, tol);
288 }
289 
nlopt_get_ftol_rel(const nlopt_opt opt)290 NLOPT_EXTERN(double) nlopt_get_ftol_rel(const nlopt_opt opt)
291 {
292     static double(*fun)(const nlopt_opt) = NULL;
293     if (fun == NULL) fun = (double(*)(const nlopt_opt)) R_GetCCallable("nloptr","nlopt_get_ftol_rel");
294     return fun(opt);
295 }
296 
nlopt_set_ftol_abs(nlopt_opt opt,double tol)297 NLOPT_EXTERN(nlopt_result) nlopt_set_ftol_abs(nlopt_opt opt, double tol)
298 {
299     static nlopt_result(*fun)(nlopt_opt, double) = NULL;
300     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, double)) R_GetCCallable("nloptr","nlopt_set_ftol_abs");
301     return fun(opt, tol);
302 }
303 
nlopt_get_ftol_abs(const nlopt_opt opt)304 NLOPT_EXTERN(double) nlopt_get_ftol_abs(const nlopt_opt opt)
305 {
306     static double(*fun)(const nlopt_opt) = NULL;
307     if (fun == NULL) fun = (double(*)(const nlopt_opt)) R_GetCCallable("nloptr","nlopt_get_ftol_abs");
308     return fun(opt);
309 }
310 
nlopt_set_xtol_rel(nlopt_opt opt,double tol)311 NLOPT_EXTERN(nlopt_result) nlopt_set_xtol_rel(nlopt_opt opt, double tol)
312 {
313     static nlopt_result(*fun)(nlopt_opt, double) = NULL;
314     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, double)) R_GetCCallable("nloptr","nlopt_set_xtol_rel");
315     return fun(opt, tol);
316 }
317 
nlopt_get_xtol_rel(const nlopt_opt opt)318 NLOPT_EXTERN(double) nlopt_get_xtol_rel(const nlopt_opt opt)
319 {
320     static double(*fun)(const nlopt_opt) = NULL;
321     if (fun == NULL) fun = (double(*)(const nlopt_opt)) R_GetCCallable("nloptr","nlopt_get_xtol_rel");
322     return fun(opt);
323 }
324 
nlopt_set_xtol_abs1(nlopt_opt opt,double tol)325 NLOPT_EXTERN(nlopt_result) nlopt_set_xtol_abs1(nlopt_opt opt, double tol)
326 {
327     static nlopt_result(*fun)(nlopt_opt, double) = NULL;
328     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, double)) R_GetCCallable("nloptr","nlopt_set_xtol_abs1");
329     return fun(opt, tol);
330 }
331 
nlopt_set_xtol_abs(nlopt_opt opt,const double * tol)332 NLOPT_EXTERN(nlopt_result) nlopt_set_xtol_abs(nlopt_opt opt, const double *tol)
333 {
334     static nlopt_result(*fun)(nlopt_opt, const double *) = NULL;
335     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, const double *)) R_GetCCallable("nloptr","nlopt_set_xtol_abs");
336     return fun(opt, tol);
337 }
338 
nlopt_get_xtol_abs(const nlopt_opt opt,double * tol)339 NLOPT_EXTERN(nlopt_result) nlopt_get_xtol_abs(const nlopt_opt opt, double *tol)
340 {
341     static nlopt_result(*fun)(nlopt_opt, double *) = NULL;
342     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, double *)) R_GetCCallable("nloptr","nlopt_get_xtol_abs");
343     return fun(opt, tol);
344 }
345 
nlopt_set_maxeval(nlopt_opt opt,int maxeval)346 NLOPT_EXTERN(nlopt_result) nlopt_set_maxeval(nlopt_opt opt, int maxeval)
347 {
348     static nlopt_result(*fun)(nlopt_opt, int) = NULL;
349     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, int)) R_GetCCallable("nloptr","nlopt_set_maxeval");
350     return fun(opt, maxeval);
351 }
352 
nlopt_get_maxeval(const nlopt_opt opt)353 NLOPT_EXTERN(int) nlopt_get_maxeval(const nlopt_opt opt)
354 {
355     static int(*fun)(const nlopt_opt) = NULL;
356     if (fun == NULL) fun = (int(*)(const nlopt_opt)) R_GetCCallable("nloptr","nlopt_get_maxeval");
357     return fun(opt);
358 }
359 
nlopt_set_maxtime(nlopt_opt opt,double maxtime)360 NLOPT_EXTERN(nlopt_result) nlopt_set_maxtime(nlopt_opt opt, double maxtime)
361 {
362     static nlopt_result(*fun)(nlopt_opt, double) = NULL;
363     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, double)) R_GetCCallable("nloptr","nlopt_set_maxtime");
364     return fun(opt, maxtime);
365 }
366 
nlopt_get_maxtime(const nlopt_opt opt)367 NLOPT_EXTERN(double) nlopt_get_maxtime(const nlopt_opt opt)
368 {
369     static double(*fun)(nlopt_opt) = NULL;
370     if (fun == NULL) fun = (double(*)(nlopt_opt)) R_GetCCallable("nloptr","nlopt_get_maxtime");
371     return fun(opt);
372 }
373 
nlopt_force_stop(nlopt_opt opt)374 NLOPT_EXTERN(nlopt_result) nlopt_force_stop(nlopt_opt opt)
375 {
376     static nlopt_result(*fun)(nlopt_opt) = NULL;
377     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt)) R_GetCCallable("nloptr","nlopt_force_stop");
378     return fun(opt);
379 }
380 
nlopt_set_force_stop(nlopt_opt opt,int val)381 NLOPT_EXTERN(nlopt_result) nlopt_set_force_stop(nlopt_opt opt, int val)
382 {
383     static nlopt_result(*fun)(nlopt_opt, int) = NULL;
384     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, int)) R_GetCCallable("nloptr","nlopt_set_force_stop");
385     return fun(opt, val);
386 }
387 
nlopt_get_force_stop(const nlopt_opt opt)388 NLOPT_EXTERN(int) nlopt_get_force_stop(const nlopt_opt opt)
389 {
390     static int(*fun)(const nlopt_opt) = NULL;
391     if (fun == NULL) fun = (int(*)(const nlopt_opt)) R_GetCCallable("nloptr","nlopt_get_force_stop");
392     return fun(opt);
393 }
394 
395 /* more algorithm-specific parameters */
396 
nlopt_set_local_optimizer(nlopt_opt opt,const nlopt_opt local_opt)397 NLOPT_EXTERN(nlopt_result) nlopt_set_local_optimizer(nlopt_opt opt, const nlopt_opt local_opt)
398 {
399     static nlopt_result(*fun)(nlopt_opt, const nlopt_opt) = NULL;
400     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, const nlopt_opt)) R_GetCCallable("nloptr","nlopt_set_local_optimizer");
401     return fun(opt, local_opt);
402 }
403 
nlopt_set_population(nlopt_opt opt,unsigned pop)404 NLOPT_EXTERN(nlopt_result) nlopt_set_population(nlopt_opt opt, unsigned pop)
405 {
406     static nlopt_result(*fun)(nlopt_opt, unsigned) = NULL;
407     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, unsigned)) R_GetCCallable("nloptr","nlopt_set_population");
408     return fun(opt, pop);
409 }
410 
nlopt_get_population(const nlopt_opt opt)411 NLOPT_EXTERN(unsigned) nlopt_get_population(const nlopt_opt opt)
412 {
413     static unsigned(*fun)(const nlopt_opt) = NULL;
414     if (fun == NULL) fun = (unsigned(*)(const nlopt_opt)) R_GetCCallable("nloptr","nlopt_get_population");
415     return fun(opt);
416 }
417 
nlopt_set_vector_storage(nlopt_opt opt,unsigned dim)418 NLOPT_EXTERN(nlopt_result) nlopt_set_vector_storage(nlopt_opt opt, unsigned dim)
419 {
420     static nlopt_result(*fun)(nlopt_opt, unsigned) = NULL;
421     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, unsigned)) R_GetCCallable("nloptr","nlopt_set_vector_storage");
422     return fun(opt, dim);
423 }
424 
nlopt_get_vector_storage(const nlopt_opt opt)425 NLOPT_EXTERN(unsigned) nlopt_get_vector_storage(const nlopt_opt opt)
426 {
427     static unsigned(*fun)(const nlopt_opt) = NULL;
428     if (fun == NULL) fun = (unsigned(*)(const nlopt_opt)) R_GetCCallable("nloptr","nlopt_get_vector_storage");
429     return fun(opt);
430 }
431 
nlopt_set_default_initial_step(nlopt_opt opt,const double * x)432 NLOPT_EXTERN(nlopt_result) nlopt_set_default_initial_step(nlopt_opt opt, const double *x)
433 {
434     static nlopt_result(*fun)(nlopt_opt, const double *) = NULL;
435     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, const double *)) R_GetCCallable("nloptr","nlopt_set_default_initial_step");
436     return fun(opt, x);
437 }
438 
nlopt_set_initial_step(nlopt_opt opt,const double * dx)439 NLOPT_EXTERN(nlopt_result) nlopt_set_initial_step(nlopt_opt opt, const double *dx)
440 {
441     static nlopt_result(*fun)(nlopt_opt, const double *) = NULL;
442     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, const double *)) R_GetCCallable("nloptr","nlopt_set_initial_step");
443     return fun(opt, dx);
444 }
445 
nlopt_set_initial_step1(nlopt_opt opt,double dx)446 NLOPT_EXTERN(nlopt_result) nlopt_set_initial_step1(nlopt_opt opt, double dx)
447 {
448     static nlopt_result(*fun)(nlopt_opt, double) = NULL;
449     if (fun == NULL) fun = (nlopt_result(*)(nlopt_opt, double)) R_GetCCallable("nloptr","nlopt_set_initial_step1");
450     return fun(opt, dx);
451 }
452 
nlopt_get_initial_step(const nlopt_opt opt,const double * x,double * dx)453 NLOPT_EXTERN(nlopt_result) nlopt_get_initial_step(const nlopt_opt opt, const double *x, double *dx)
454 {
455     static nlopt_result(*fun)(const nlopt_opt, const double *, double *) = NULL;
456     if (fun == NULL) fun = (nlopt_result(*)(const nlopt_opt, const double *, double *)) R_GetCCallable("nloptr","nlopt_get_initial_step");
457     return fun(opt, x, dx);
458 }
459 
460 #endif /* __NLOPTRAPI_H__ */
461