diff --git a/c/README.md b/c/README.md new file mode 100644 index 0000000..437d146 --- /dev/null +++ b/c/README.md @@ -0,0 +1 @@ +This is the C interface for calling the Fortran version of ODRPACK. \ No newline at end of file diff --git a/include/odrpack.h b/c/include/ordpack/odrpack.h similarity index 64% rename from include/odrpack.h rename to c/include/ordpack/odrpack.h index a12ca92..d5fbd15 100644 --- a/include/odrpack.h +++ b/c/include/ordpack/odrpack.h @@ -6,13 +6,6 @@ #define ODRPACK_EXTERN extern #endif -typedef void (*minpack_func)( - int /* n */, - const double * /* x */, - double * /* fvec */, - int * /* iflag */, - void * /* udata */); - /** * @brief User-supplied function for evaluating the model, computing predicted values and their Jacobians. * @@ -37,23 +30,43 @@ typedef void (*minpack_func)( * 1 - current `beta` and `x+delta` are not acceptable; ODRPACK95 should select values closer to most recently used values if possible, * -1 - current `beta` and `x+delta` are not acceptable; ODRPACK95 should stop. */ -typedef void (*odrpack_fcn)( - int /* n */, - int /* m */, - int /* np */, - int /* nq */, - int /* ldn */, - int /* ldm */, - int /* ldnp */, - const double * /* beta */, - const double * /* xplusd */, - const int * /* ifixb */, - const int * /* ifixx */, - int /* ldifx */, - int /* ideval */, - double * /* f */, - double * /* fjacb */, - double * /* fjacd */, - int * /* istop */ +ODRPACK_EXTERN typedef void (*odrpack_fcn)( + int n, + int m, + int np, + int nq, + int ldn, + int ldm, + int ldnp, + const double *beta, + const double *xplusd, + const int *ifixb, + const int *ifixx, + int ldifx, + int ideval, + double *f, + double *fjacb, + double *fjacd, + int *istop); -); \ No newline at end of file +/** + * @brief Wrapper for the ODR routine including only mandatory arguments. + * + * @param fcn User-supplied subroutine for evaluating the model. + * @param n Number of observations. + * @param m Number of columns of data in the independent variable. + * @param np Number of function parameters. + * @param nq Number of responses per observation. + * @param beta Function parameters. Array of size np. + * @param y Dependent variable. Unused when the model is implicit. 2D array of size n x nq. + * @param x Explanatory variable. 2D array of size n x m. + */ +ODRPACK_EXTERN void odr_c( + odrpack_fcn fcn, + int n, + int m, + int np, + int nq, + double *beta, + const double *y, + const double *x); diff --git a/src/odrpack_capi.f90 b/c/odrpack_c.f90 similarity index 97% rename from src/odrpack_capi.f90 rename to c/odrpack_c.f90 index 0486ead..b21b794 100644 --- a/src/odrpack_capi.f90 +++ b/c/odrpack_c.f90 @@ -1,10 +1,12 @@ -module odrpack_capi - use, intrinsic :: iso_c_binding, only : c_int, c_double, c_funptr, c_ptr, c_f_procpointer +module odrpack_c + use, intrinsic :: iso_c_binding, only : c_int, c_double use odrpack, only: odr use odrpack_kinds, only: wp implicit none private + public :: odr_c + abstract interface subroutine fcn_tc(n, m, np, nq, ldn, ldm, ldnp, beta, xplusd, ifixb, ifixx, ldifx, & ideval, f, fjacb, fjacd, istop) bind(c) @@ -27,11 +29,34 @@ subroutine fcn_tc(n, m, np, nq, ldn, ldm, ldnp, beta, xplusd, ifixb, ifixx, ldif real(c_double), intent(out) :: fjacb(ldn, ldnp, nq) real(c_double), intent(out) :: fjacd(ldn, ldm, nq) integer(c_int), intent(out) :: istop - end subroutine fcn_tc + end subroutine end interface contains + subroutine odr_c(fcn, n, m, np, nq, beta, y, x) bind(c) + !! "Short" wrapper for the ODR routine including only mandatory arguments. + procedure(fcn_tc) :: fcn + !! User-supplied subroutine for evaluating the model. + integer(c_int), intent(in), value :: n + !! Number of observations. + integer(c_int), intent(in), value :: m + !! Number of columns of data in the independent variable. + integer(c_int), intent(in), value :: np + !! Number of function parameters. + integer(c_int), intent(in), value :: nq + !! Number of responses per observation. + real(c_double), intent(inout) :: beta(np) + !! Function parameters. + real(c_double), intent(in) :: y(n, nq) + !! Dependent variable. Unused when the model is implicit. + real(c_double), intent(in) :: x(n, m) + !! Explanatory variable. + + call odr(fcn, n, m, np, nq, beta, y, x) + + end subroutine odr_c + ! subroutine odr_c( & ! fcn, & ! n, m, np, nq, & @@ -118,27 +143,4 @@ end subroutine fcn_tc ! end subroutine - subroutine odr_c(fcn, n, m, np, nq, beta, y, x) bind(c) - !! "Short" wrapper for the ODR routine including only mandatory arguments. - procedure(fcn_tc) :: fcn - !! User-supplied subroutine for evaluating the model. - integer(c_int), intent(in), value :: n - !! Number of observations. - integer(c_int), intent(in), value :: m - !! Number of columns of data in the independent variable. - integer(c_int), intent(in), value :: np - !! Number of function parameters. - integer(c_int), intent(in), value :: nq - !! Number of responses per observation. - real(c_double), intent(inout) :: beta(np) - !! Function parameters. - real(c_double), intent(in) :: y(n, nq) - !! Dependent variable. Unused when the model is implicit. - real(c_double), intent(in) :: x(n, m) - !! Explanatory variable. - - call odr(fcn, n, m, np, nq, beta, y, x) - - end subroutine - -end module odrpack_capi \ No newline at end of file +end module odrpack_c \ No newline at end of file