chiark / gitweb /
Merge branch 'master' of git://github.com/stevengj/nlopt
[nlopt.git] / src / api / f77funcs.h
1 /* Copyright (c) 2007-2014 Massachusetts Institute of Technology
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  * 
11  * The above copyright notice and this permission notice shall be
12  * included in all copies or substantial portions of the Software.
13  * 
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
21  */
22
23 /* Fortran API wrappers, using the F77 macro defined in f77api.c.
24    This header file is #included one or more times from f77api.c
25    in order to define verions of the Fortran API for various compilers. 
26
27    In homage to Fortran 77, we stick with 6-character subroutine names.
28    The return value of the function is converted into the first argument
29    of the subroutine. */
30
31 /* nlopt_minimize_constrained */
32 void F77(nloptc, NLOPTC) (int *info,
33                           const int *algorithm,
34                           const int *n, nlopt_f77_func f, void *f_data,
35                           const int *m, nlopt_f77_func fc,
36                           char *fc_data, char *fc_second_datum,
37                           const double *lb, const double *ub,
38                           double *x, double *minf,
39                           const double *minf_max,
40                           const double *ftol_rel, const double *ftol_abs, const double *xtol_rel, const double *xtol_abs, const int *have_xtol_abs, const int *maxeval, const double *maxtime) {
41     f77_func_data d, *dc;
42     int i;
43
44     d.f = f;
45     d.f_data = f_data;
46     if (*m < 0) {
47         *info = NLOPT_INVALID_ARGS;
48         return;
49     }
50     dc = (f77_func_data *) malloc(sizeof(f77_func_data) * *m);
51     if (*m > 0 && !dc) {
52         *info = NLOPT_OUT_OF_MEMORY;
53         return;
54     }
55     for (i = 0; i < *m; ++i) {
56         dc[i].f = fc;
57         dc[i].f_data = fc_data + i * (fc_second_datum - fc_data);
58     }
59
60     *info = nlopt_minimize_constrained((nlopt_algorithm) * algorithm,
61                                        *n, f77_func_wrap_old, &d,
62                                        *m, f77_func_wrap_old,
63                                        dc, sizeof(f77_func_data), lb, ub, x, minf, *minf_max, *ftol_rel, *ftol_abs, *xtol_rel, *have_xtol_abs ? xtol_abs : 0, *maxeval, *maxtime);
64
65     if (dc)
66         free(dc);
67 }
68
69 /* nlopt_minimize */
70 void F77(nloptm, NLOPTM) (int *info,
71                           const int *algorithm,
72                           const int *n, nlopt_f77_func f, void *f_data,
73                           const double *lb, const double *ub,
74                           double *x, double *minf,
75                           const double *minf_max,
76                           const double *ftol_rel, const double *ftol_abs, const double *xtol_rel, const double *xtol_abs, const int *have_xtol_abs, const int *maxeval, const double *maxtime) {
77     int m = 0;
78     F77CALL(nloptc, NLOPTC) (info, algorithm, n, f, f_data, &m, 0, 0, 0, lb, ub, x, minf, minf_max, ftol_rel, ftol_abs, xtol_rel, xtol_abs, have_xtol_abs, maxeval, maxtime);
79 }
80
81 void F77(nlosr, NLOSR) (const int *seed) {
82     nlopt_srand((unsigned long) *seed);
83 }
84
85 void F77(nlosrt, NLOSRT) (void) {
86     nlopt_srand_time();
87 }
88
89 void F77(nloptv, NLOPTV) (int *major, int *minor, int *bugfix) {
90     nlopt_version(major, minor, bugfix);
91 }
92
93 void F77(nlogls, NLOGLS) (int *ideriv, int *inonderiv, int *maxeval) {
94     nlopt_algorithm deriv, nonderiv;
95     nlopt_get_local_search_algorithm(&deriv, &nonderiv, maxeval);
96     *ideriv = deriv;
97     *inonderiv = nonderiv;
98 }
99
100 void F77(nlosls, NLOSLS) (int *ideriv, int *inonderiv, int *maxeval) {
101     nlopt_algorithm deriv = (nlopt_algorithm) * ideriv;
102     nlopt_algorithm nonderiv = (nlopt_algorithm) * inonderiv;
103     nlopt_set_local_search_algorithm(deriv, nonderiv, *maxeval);
104 }
105
106 void F77(nlogsp, NLOGSP) (int *pop) {
107     *pop = nlopt_get_stochastic_population();
108 }
109
110 void F77(nlossp, NLOSSP) (const int *pop) {
111     nlopt_set_stochastic_population(*pop);
112 }
113
114 #define F77_(name,NAME) F77(name,NAME)
115 # include "f77funcs_.h"
116 #undef F77_