Skip to content

Commit

Permalink
update odr_long_c
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoMVale committed Jun 29, 2024
1 parent 05e787b commit 98c1e91
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 128 deletions.
50 changes: 30 additions & 20 deletions c/example/example5.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,28 +73,38 @@ int main()
int lunrpt = -1;
int info = 0;

// int ifixb[NP] = {1, 1};
// int ndigit = -1;
// double taufac = -1.0;
// double sstol = -1.0;
// double partol = -1.0;
// int maxint = -1;
// double *stpb = NULL;
// double *sclb = NULL;
// odr_short_c(fcn, &n, &m, &np, &nq, beta, (double *)y, (double *)x,
// (double *)we, &ldwe, &ld2we,
// (double *)wd, &ldwd, &ld2wd,
// lower, upper,
// &job, &iprint, &lunerr, &lunrpt,
// &info);

odr_short_c(fcn, &n, &m, &np, &nq, beta, (double *)y, (double *)x,
(double *)we, &ldwe, &ld2we,
(double *)wd, &ldwd, &ld2wd,
lower, upper,
&job, &iprint, &lunerr, &lunrpt,
&info);
int ifixb[NP] = {1, 1};
int ifixx[M][1] = {-1};
int ldifx = 1;
double stpb[NP] = {-1.0, -1.0};
double stpd[M][1] = {-1.0};
int ldstpd = 1;
double sclb[NP] = {-1.0, -1.0};
double scld[M][1] = {-1.0};
int ldscld = 1;
int ndigit = -1;
double taufac = -1.0;
double sstol = -1.0;
double partol = -1.0;
int maxit = -1;

// odr_basic_c(fcn, &n, &m, &np, &nq, beta, (double *)y, (double *)x,
// ifixb,
// &job, &ndigit, &taufac, &sstol, &partol, &maxint, &iprint, &lunerr, &lunrpt,
// stpb, sclb,
// &info,
// lower, upper);
odr_long_c(fcn, &n, &m, &np, &nq, beta, (double *)y, (double *)x,
(double *)we, &ldwe, &ld2we,
(double *)wd, &ldwd, &ld2wd,
ifixb, (int *)ifixx, &ldifx,
stpb, (double *)stpd, &ldstpd,
sclb, (double *)scld, &ldscld,
lower, upper,
&job, &ndigit, &taufac, &sstol, &partol,
&maxit, &iprint, &lunerr, &lunrpt,
&info);

return 0;
}
81 changes: 79 additions & 2 deletions include/odrpack/odrpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,86 @@ ODRPACK_EXTERN void odr_short_c(
const int *lunrpt,
int *info);

// * @param stpb Input array [np] with relative step for computing finite difference derivatives with respect to `beta`.
// * @param sclb Input array [np] with scaling values for `beta`.
/**
* @brief Wrapper for the ODR routine including mandatory arguments and most commonly used
* optional arguments. Similar to the short-call statement of the original ODRPACK `DODR`.
*
* @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 `<=>` Array [np] of function parameters.
* @param y `==>` Array [nq][n] of dependent variable. Unused when the model is implicit.
* @param x `==>` Array [m][n] of explanatory variable.
* @param we `==>` Array [nq][ld2we][ldwe] with `epsilon` weights.
* @param ldwe `==>` Leading dimension of array `we`, `ldwe ∈ {1, n}`.
* @param ld2we `==>` Second dimension of array `we`, `ld2we ∈ {1, n}`.
* @param wd `==>` Array [m][ld2wd][ldwd] with `delta` weights.
* @param ldwd `==>` Leading dimension of array `wd`, `ldwd ∈ {1, n}`.
* @param ld2wd `==>` Second dimension of array `wd`, `ld2wd ∈ {1, m}`.
* @param ifixb `==>` Array [np] with values designating whether the elements of `beta` are fixed at their input values or not.
* @param ifixx `==>` Array [m][ldifx] with values designating whether the elements of `x` are fixed at their input values or not.
* @param ldifx `==>` Leading dimension of array `ifixx`, `ldifx ∈ {1, n}`.
* @param stpb `==>` Input array [np] with relative step for computing finite difference derivatives with respect to `beta`.
* @param stpd `==>` Input array [m][ldstpd] with relative step for computing finite difference derivatives with respect to `delta`.
* @param ldstpd `==>` Leading dimension of array `stpd`, `ldstpd ∈ {1, n}`.
* @param sclb `==>` Input array [np] with scaling values for `beta`.
* @param scld `==>` Input array [m][ldscld] with scaling values for `delta`.
* @param ldscld `==>` Leading dimension of array `scld`, `ldscld ∈ {1, n}`.
* @param lower `==>` Optional array [np] with lower bound on `beta`.
* @param upper `==>` Optional array [np] with upper bound on `beta`.
* @param delta `<=>` Optional array [m][n] with initial error in the `x` data.
* @param job `==>` Optional variable controlling initialization and computational method.
* @param ndigit `==>` Optional number of accurate digits in the function results, as supplied by the user.
* @param taufac `==>` Optional factor used to compute the initial trust region diameter.
* @param sstol `==>` Optional sum-of-squares convergence stopping tolerance.
* @param partol `==>` Optional parameter convergence stopping tolerance.
* @param maxit `==>` Optional maximum number of iterations allowed.
* @param iprint `==>` Optional print control variable.
* @param lunerr `==>` Optional logical unit number for error messages.
* @param lunrpt `==>` Optional logical unit number for computation reports.
* @param info `<==` Optional variable designating why the computations were stopped.
*/
ODRPACK_EXTERN void odr_long_c(
odrpack_fcn fcn,
const int *n,
const int *m,
const int *np,
const int *nq,
double *beta,
const double *y,
const double *x,
const double *we,
const int *ldwe,
const int *ld2we,
const double *wd,
const int *ldwd,
const int *ld2wd,
const int *ifixb,
const int *ifixx,
const int *ldifx,
const double *stpb,
const double *scpd,
const int *ldstpd,
const double *sclb,
const double *scld,
const int *ldscld,
const double *lower,
const double *upper,
// const double *delta,
const int *job,
const int *ndigit,
const double *taufac,
const double *sstol,
const double *partol,
const int *maxit,
const int *iprint,
const int *lunerr,
const int *lunrpt,
int *info);
/**
* @brief Set storage locations within real work space.
*
Expand Down
212 changes: 106 additions & 106 deletions src/odrpack_c.f90
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module odrpack_c
implicit none
private

public :: odr_basic_c, odr_short_c, dwinf_c, open_file, close_file
public :: odr_basic_c, odr_short_c, odr_long_c, dwinf_c, open_file, close_file

abstract interface
subroutine fcn_tc(n, m, np, nq, ldn, ldm, ldnp, beta, xplusd, ifixb, ifixx, ldifx, &
Expand Down Expand Up @@ -129,113 +129,111 @@ subroutine odr_short_c( &

end subroutine odr_short_c

! subroutine odr_c( &
! fcn, &
! n, m, np, nq, &
! beta, &
! y, x, &
! we, ldwe, ld2we, &
! wd, ldwd, ld2wd, &
! ifixb, ifixx, ldifx, &
! stpb, stpd, ldstpd, &
! sclb, scld, ldscld, &
! lower, upper, &
! delta, &
! job, ndigit, taufac, &
! sstol, partol, maxit, &
! iprint, lunerr, lunrpt, &

! info) bind(C)
! !! Wrapper for the ODR routine including mandatory arguments and all optional arguments.
! procedure(fcn_tc) :: fcn
! !! User-supplied subroutine for evaluating the model.
! integer(c_int), intent(in) :: n
! !! Number of observations.
! integer(c_int), intent(in) :: m
! !! Number of columns of data in the independent variable.
! integer(c_int), intent(in) :: np
! !! Number of function parameters.
! integer(c_int), intent(in) :: 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.
! real(c_double), intent(in) :: we(ldwe, ld2we, nq)
! !! `epsilon` weights.
! integer(c_int), intent(in) :: ldwe
! !! Leading dimension of array `we`, `ldwe ∈ {1, n}`.
! integer(c_int), intent(in) :: ld2we
! !! Second dimension of array `we`, `ld2we ∈ {1, n}`.
! real(c_double), intent(in) :: wd(ldwd, ld2wd, m)
! !! `delta` weights.
! integer(c_int), intent(in) :: ldwd
! !! Leading dimension of array `wd`, `ldwd ∈ {1, n}`.
! integer(c_int), intent(in) :: ld2wd
! !! Second dimension of array `wd`, `ld2wd ∈ {1, m}`.
! integer(c_int), intent(in) :: ifixb(np)
! !! Values designating whether the elements of `beta` are fixed at their input values or not.
! integer(c_int), intent(in) :: ifixx(ldifx, m)
! !! Values designating whether the elements of `x` are fixed at their input values or not.
! integer(c_int), intent(in) :: ldifx
! !! Leading dimension of array `ifixx`, `ldifx ∈ {1, n}`.
! real(c_double), intent(in) :: stpb(np)
! !! Relative step for computing finite difference derivatives with respect to `beta`.
! real(c_double), intent(in) :: stpd(ldstpd, m)
! !! Relative step for computing finite difference derivatives with respect to `delta`.
! integer(c_int), intent(in) :: ldstpd
! !! Leading dimension of array `stpd`, `ldstpd ∈ {1, n}`.
! real(c_double), intent(in) :: sclb(np)
! !! Scaling values for `beta`.
! real(c_double), intent(in) :: scld(ldscld, m)
! !! Scaling values for `delta`.
! integer(c_int), intent(in) :: ldscld
! !! Leading dimension of array `scld`, `ldstpd ∈ {1, n}`.
! real(c_double), intent(in), optional :: lower(np)
! !! Lower bound on `beta`.
! real(kind=wp), intent(in), optional :: upper(np)
! !! Upper bound on `beta`.
! real(c_double), intent(inout), pointer, optional :: delta(n, m)
! !! Initial error in the `x` data.
! integer(c_int), intent(in), optional :: job
! !! Variable controlling initialization and computational method.
! integer(c_int), intent(in), optional :: ndigit
! !! Number of accurate digits in the function results, as supplied by the user.
! real(c_double), intent(in), optional :: taufac
! !! Factor used to compute the initial trust region diameter.
! real(c_double), intent(in), optional :: sstol
! !! Sum-of-squares convergence stopping tolerance.
! real(c_double), intent(in), optional :: partol
! !! Parameter convergence stopping tolerance.
! integer(c_int), intent(in), optional :: maxit
! !! Maximum number of iterations allowed.
! integer(c_int), intent(in), optional :: iprint
! !! Print control variable.
! integer(c_int), intent(in), optional :: lunerr
! !! Logical unit number for error messages.
! integer(c_int), intent(in), optional :: lunrpt
! !! Logical unit number for computation reports.
! integer(c_int), intent(out), optional :: info
! !! Variable designating why the computations were stopped.
subroutine odr_long_c( &
fcn, &
n, m, np, nq, &
beta, y, x, &
we, ldwe, ld2we, &
wd, ldwd, ld2wd, &
ifixb, ifixx, ldifx, &
stpb, stpd, ldstpd, &
sclb, scld, ldscld, &
lower, upper, &
! delta, &
job, ndigit, taufac, &
sstol, partol, maxit, &
iprint, lunerr, lunrpt, &
info) bind(C)
!! Wrapper for the ODR routine including mandatory arguments and all optional arguments.
procedure(fcn_tc) :: fcn
!! User-supplied subroutine for evaluating the model.
integer(c_int), intent(in) :: n
!! Number of observations.
integer(c_int), intent(in) :: m
!! Number of columns of data in the independent variable.
integer(c_int), intent(in) :: np
!! Number of function parameters.
integer(c_int), intent(in) :: 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.
real(c_double), intent(in) :: we(ldwe, ld2we, nq)
!! `epsilon` weights.
integer(c_int), intent(in) :: ldwe
!! Leading dimension of array `we`, `ldwe ∈ {1, n}`.
integer(c_int), intent(in) :: ld2we
!! Second dimension of array `we`, `ld2we ∈ {1, n}`.
real(c_double), intent(in) :: wd(ldwd, ld2wd, m)
!! `delta` weights.
integer(c_int), intent(in) :: ldwd
!! Leading dimension of array `wd`, `ldwd ∈ {1, n}`.
integer(c_int), intent(in) :: ld2wd
!! Second dimension of array `wd`, `ld2wd ∈ {1, m}`.
integer(c_int), intent(in) :: ifixb(np)
!! Values designating whether the elements of `beta` are fixed at their input values or not.
integer(c_int), intent(in) :: ifixx(ldifx, m)
!! Values designating whether the elements of `x` are fixed at their input values or not.
integer(c_int), intent(in) :: ldifx
!! Leading dimension of array `ifixx`, `ldifx ∈ {1, n}`.
real(c_double), intent(in) :: stpb(np)
!! Relative step for computing finite difference derivatives with respect to `beta`.
real(c_double), intent(in) :: stpd(ldstpd, m)
!! Relative step for computing finite difference derivatives with respect to `delta`.
integer(c_int), intent(in) :: ldstpd
!! Leading dimension of array `stpd`, `ldstpd ∈ {1, n}`.
real(c_double), intent(in) :: sclb(np)
!! Scaling values for `beta`.
real(c_double), intent(in) :: scld(ldscld, m)
!! Scaling values for `delta`.
integer(c_int), intent(in) :: ldscld
!! Leading dimension of array `scld`, `ldscld ∈ {1, n}`.
real(c_double), intent(in), optional :: lower(np)
!! Lower bound on `beta`.
real(kind=wp), intent(in), optional :: upper(np)
!! Upper bound on `beta`.
!real(c_double), intent(inout), optional :: delta(n, m)
!! Initial error in the `x` data.
integer(c_int), intent(in), optional :: job
!! Variable controlling initialization and computational method.
integer(c_int), intent(in), optional :: ndigit
!! Number of accurate digits in the function results, as supplied by the user.
real(c_double), intent(in), optional :: taufac
!! Factor used to compute the initial trust region diameter.
real(c_double), intent(in), optional :: sstol
!! Sum-of-squares convergence stopping tolerance.
real(c_double), intent(in), optional :: partol
!! Parameter convergence stopping tolerance.
integer(c_int), intent(in), optional :: maxit
!! Maximum number of iterations allowed.
integer(c_int), intent(in), optional :: iprint
!! Print control variable.
integer(c_int), intent(in), optional :: lunerr
!! Logical unit number for error messages.
integer(c_int), intent(in), optional :: lunrpt
!! Logical unit number for computation reports.
integer(c_int), intent(out), optional :: info
!! Variable designating why the computations were stopped.

! call odr(fcn, n, m, np, nq, beta, y, x, &
! ! delta=delta, &
! we=we, wd=wd, &
! lower=lower, upper=upper, &
! ifixb=ifixb, &
! ! ifixx=ifixx, &
! job=job, ndigit=ndigit, taufac=taufac, &
! sstol=sstol, partol=partol, maxit=maxit, &
! iprint=iprint, lunerr=lunerr, lunrpt=lunrpt, &
! stpb=stpb, &
! !stpd=stpd, &
! sclb=sclb, &
! !scld=scld, &
! info=info)
call odr(fcn, n, m, np, nq, beta, y, x, &
!delta=delta, &
we=we, wd=wd, &
lower=lower, upper=upper, &
ifixb=ifixb, &
ifixx=ifixx, &
job=job, ndigit=ndigit, taufac=taufac, &
sstol=sstol, partol=partol, maxit=maxit, &
iprint=iprint, lunerr=lunerr, lunrpt=lunrpt, &
stpb=stpb, &
stpd=stpd, &
sclb=sclb, &
scld=scld, &
info=info)

! end subroutine odr_c
end subroutine odr_long_c

subroutine open_file(lun, fn, fnlen, ierr) bind(C)
!! Open a new file associated with a specified logical unit number.
Expand Down Expand Up @@ -270,7 +268,9 @@ subroutine close_file(lun, ierr) bind(C)
!! Logical unit number.
integer(c_int), intent(out) :: ierr
!! Error code returned by `iostat`.

close (unit=lun, iostat=ierr)

end subroutine close_file

subroutine dwinf_c( &
Expand Down

0 comments on commit 98c1e91

Please sign in to comment.