Skip to content

Commit

Permalink
Update GSL to version 2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Nov 17, 2015
1 parent eeb32b4 commit 635f552
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 34 deletions.
6 changes: 3 additions & 3 deletions doc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ endif ()

set(BUILD_DOCS ${MP_SOURCE_DIR}/support/build-docs.py)

# Get .rst file names from amplgsl.c.
# Get .rst file names from amplgsl.cc.
set(amplgsl_docs )
set(amplgsl_c ${MP_SOURCE_DIR}/src/gsl/amplgsl.c)
file(READ ${amplgsl_c} content)
set(amplgsl_src ${MP_SOURCE_DIR}/src/gsl/amplgsl.cc)
file(READ ${amplgsl_src} content)
string(REGEX MATCHALL "@file[^\n]*" files ${content})
foreach (f ${files})
if (f MATCHES "@file +(.*)")
Expand Down
2 changes: 1 addition & 1 deletion src/gsl/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# CMake build script for the GSL wrapper for AMPL.

add_ampl_library(amplgsl amplgsl.c)
add_ampl_library(amplgsl amplgsl.cc)
target_link_libraries(amplgsl asl gsl gslcblas)
target_include_directories(amplgsl
PRIVATE ${PROJECT_BINARY_DIR}/thirdparty/build/gsl)
Expand Down
58 changes: 36 additions & 22 deletions src/gsl/amplgsl.c → src/gsl/amplgsl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@

#include "funcadd.h"

// Macros used for compatibility with GSL 1.x.
#if GSL_MAJOR_VERSION < 2
# define gsl_sf_mathieu_a_e gsl_sf_mathieu_a
# define gsl_sf_mathieu_b_e gsl_sf_mathieu_b
# define gsl_sf_mathieu_ce_e gsl_sf_mathieu_ce
# define gsl_sf_mathieu_se_e gsl_sf_mathieu_se
# define gsl_sf_mathieu_Mc_e gsl_sf_mathieu_Mc
# define gsl_sf_mathieu_Ms_e gsl_sf_mathieu_Ms
#endif

enum { MAX_ERROR_MESSAGE_SIZE = 100 };

static const char *const DERIVS_NOT_PROVIDED = "derivatives are not provided";
Expand All @@ -42,11 +52,15 @@ static double mul_by_sign(double x, double y) {
return y != 0 ? (x / fabs(x)) * y : 0;
}

static char* allocate_string(arglist *al, size_t size) {
return static_cast<char*>(al->AE->Tempmem(al->TMI, size));
}

/* Formats the error message and stores it in al->Errmsg. */
static void format_error(
arglist *al, const char *format, va_list args, char prefix) {
size_t size = MAX_ERROR_MESSAGE_SIZE + (prefix ? 1 : 0);
char *message = al->Errmsg = al->AE->Tempmem(al->TMI, size);
char *message = al->Errmsg = allocate_string(al, size);
if (prefix)
*message++ = prefix;
al->AE->VsnprintF(message, MAX_ERROR_MESSAGE_SIZE, format, args);
Expand Down Expand Up @@ -90,7 +104,7 @@ static int check_deriv_arg(arglist *al, int arg, int min, int max) {
static void format_eval_error(arglist *al, char prefix, const char *suffix) {
int n = 0, i = 0;
size_t size = MAX_ERROR_MESSAGE_SIZE + (prefix ? 1 : 0);
char *message = al->Errmsg = al->AE->Tempmem(al->TMI, size);
char *message = al->Errmsg = allocate_string(al, size);
if (prefix)
*message++ = prefix;
n += al->AE->SnprintF(message, MAX_ERROR_MESSAGE_SIZE,
Expand Down Expand Up @@ -1492,10 +1506,11 @@ static double amplgsl_sf_ellint_E(arglist *al) {
WRAP_CHECKED(gsl_sf_ellint_P, ARGS3_PREC)

#if GSL_MAJOR_VERSION >= 2
WRAP_CHECKED(gsl_sf_ellint_D, ARGS2_PREC)
# define GSL_ELLINT_D_ARGS ARGS2_PREC
#else
WRAP_CHECKED(gsl_sf_ellint_D, ARGS3_PREC)
# define GSL_ELLINT_D_ARGS ARGS2, 0, GSL_PREC_DOUBLE
#endif
WRAP_CHECKED(gsl_sf_ellint_D, GSL_ELLINT_D_ARGS)

WRAP_CHECKED(gsl_sf_ellint_RC, ARGS2_PREC)
WRAP_CHECKED(gsl_sf_ellint_RD, ARGS3_PREC)
Expand Down Expand Up @@ -2319,7 +2334,7 @@ static double amplgsl_sf_mathieu_a(arglist *al) {
if (al->derivs)
deriv_error(al, DERIVS_NOT_PROVIDED);
return check_result(al,
gsl_sf_mathieu_a(n, q, &result) ? GSL_NAN : result.val);
gsl_sf_mathieu_a_e(n, q, &result) ? GSL_NAN : result.val);
}

static double amplgsl_sf_mathieu_b(arglist *al) {
Expand All @@ -2333,7 +2348,7 @@ static double amplgsl_sf_mathieu_b(arglist *al) {
if (al->derivs)
deriv_error(al, DERIVS_NOT_PROVIDED);
return check_result(al,
gsl_sf_mathieu_b(n, q, &result) ? GSL_NAN : result.val);
gsl_sf_mathieu_b_e(n, q, &result) ? GSL_NAN : result.val);
}

static double amplgsl_sf_mathieu_ce(arglist *al) {
Expand All @@ -2348,7 +2363,7 @@ static double amplgsl_sf_mathieu_ce(arglist *al) {
if (al->derivs)
deriv_error(al, DERIVS_NOT_PROVIDED);
return check_result(al,
gsl_sf_mathieu_ce(n, q, x, &result) ? GSL_NAN : result.val);
gsl_sf_mathieu_ce_e(n, q, x, &result) ? GSL_NAN : result.val);
}

static double amplgsl_sf_mathieu_se(arglist *al) {
Expand All @@ -2363,7 +2378,7 @@ static double amplgsl_sf_mathieu_se(arglist *al) {
if (al->derivs)
deriv_error(al, DERIVS_NOT_PROVIDED);
return check_result(al,
gsl_sf_mathieu_se(n, q, x, &result) ? GSL_NAN : result.val);
gsl_sf_mathieu_se_e(n, q, x, &result) ? GSL_NAN : result.val);
}

static double amplgsl_sf_mathieu_Mc(arglist *al) {
Expand All @@ -2379,7 +2394,7 @@ static double amplgsl_sf_mathieu_Mc(arglist *al) {
if (al->derivs)
deriv_error(al, DERIVS_NOT_PROVIDED);
return check_result(al,
gsl_sf_mathieu_Mc(j, n, q, x, &result) ? GSL_NAN : result.val);
gsl_sf_mathieu_Mc_e(j, n, q, x, &result) ? GSL_NAN : result.val);
}

static double amplgsl_sf_mathieu_Ms(arglist *al) {
Expand All @@ -2395,7 +2410,7 @@ static double amplgsl_sf_mathieu_Ms(arglist *al) {
if (al->derivs)
deriv_error(al, DERIVS_NOT_PROVIDED);
return check_result(al,
gsl_sf_mathieu_Ms(j, n, q, x, &result) ? GSL_NAN : result.val);
gsl_sf_mathieu_Ms_e(j, n, q, x, &result) ? GSL_NAN : result.val);
}

static double amplgsl_sf_pow_int(arglist *al) {
Expand Down Expand Up @@ -2865,7 +2880,7 @@ WRAP(gsl_cdf_gumbel2_Qinv, ARGS3)
return check_result(al, func((unsigned)args)); \
}

const char *const *const DEFAULT_ARGS;
const char *const *const DEFAULT_ARGS = 0;

WRAP(gsl_ran_poisson, RNG_ARGS1)
WRAP_DISCRETE(gsl_ran_poisson_pdf, ARGS2, DEFAULT_ARGS)
Expand Down Expand Up @@ -2941,18 +2956,19 @@ WRAP(gsl_ran_logarithmic, RNG_ARGS1)
WRAP_DISCRETE(gsl_ran_logarithmic_pdf, ARGS2, DEFAULT_ARGS)

#define ADDFUNC(name, num_args) \
addfunc(#name, ampl##name, FUNCADD_REAL_VALUED, num_args, #name);
addfunc(#name, ampl##name, FUNCADD_REAL_VALUED, num_args, \
const_cast<char*>(#name));

#define ADDFUNC_RANDOM(name, num_args) \
addfunc(#name, ampl##name, FUNCADD_RANDOM_VALUED, num_args, #name);
addfunc(#name, ampl##name, FUNCADD_RANDOM_VALUED, num_args, \
const_cast<char*>(#name));

void funcadd_ASL(AmplExports *ae)
{
extern "C" void funcadd_ASL(AmplExports *ae) {
/* Don't call abort on error. */
gsl_set_error_handler_off();

addfunc("gsl_version", (rfunc)amplgsl_version,
FUNCADD_STRING_VALUED, 0, "gsl_version");
FUNCADD_STRING_VALUED, 0, const_cast<char*>("gsl_version"));

/**
* @file elementary
Expand Down Expand Up @@ -3964,19 +3980,17 @@ void funcadd_ASL(AmplExports *ae)
ADDFUNC(gsl_sf_ellint_P, 3);

/**
* .. function:: gsl_sf_ellint_D(phi, k, n)
* .. function:: gsl_sf_ellint_D(phi, k)
*
* This routine computes the incomplete elliptic integral $D(\phi,k,n)$
* This routine computes the incomplete elliptic integral $D(\phi,k)$
* which is defined through the Carlson form $RD(x,y,z)$ by the following
* relation,
*
* .. math::
* D(\phi,k,n) = (1/3)(\sin(\phi))^3
* D(\phi,k) = (1/3)(\sin(\phi))^3
* RD (1-\sin^2(\phi), 1-k^2 \sin^2(\phi), 1).
*
* The argument $n$ is not used and will be removed in a future release.
*/
ADDFUNC(gsl_sf_ellint_D, 3);
ADDFUNC(gsl_sf_ellint_D, 2);

/**
* Carlson Forms
Expand Down
2 changes: 1 addition & 1 deletion support/build-docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def build_docs(workdir, doxygen='doxygen'):
os.remove(path)
# Build docs.
dir = os.path.dirname(__file__)
extract_docs(os.path.join(dir, '../src/gsl/amplgsl.c'), build_dir)
extract_docs(os.path.join(dir, '../src/gsl/amplgsl.cc'), build_dir)
p = Popen([doxygen, '-'], stdin=PIPE, cwd=build_dir)
p.communicate(input=r'''
PROJECT_NAME = MP
Expand Down
18 changes: 12 additions & 6 deletions test/gsl-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <gsl/gsl_sf.h>
#include <gsl/gsl_cdf.h>
#include <gsl/gsl_randist.h>
#include <gsl/gsl_version.h>

#include <functional>
#include <map>
Expand Down Expand Up @@ -1034,12 +1035,17 @@ TEST_F(GSLTest, Log) {
}

TEST_F(GSLTest, Mathieu) {
TEST_FUNC2(gsl_sf_mathieu_a, NoDeriv("n"));
TEST_FUNC2(gsl_sf_mathieu_b, NoDeriv("n"));
TEST_FUNC2(gsl_sf_mathieu_ce, NoDeriv("n"));
TEST_FUNC2(gsl_sf_mathieu_se, NoDeriv("n"));
TEST_FUNC2(gsl_sf_mathieu_Mc, NoDeriv("j n"));
TEST_FUNC2(gsl_sf_mathieu_Ms, NoDeriv("j n"));
#if GSL_MAJOR_VERSION >= 2
# define TEST_MATHIEU TEST_EFUNC2
#else
# define TEST_MATHIEU TEST_FUNC2
#endif
TEST_MATHIEU(gsl_sf_mathieu_a, NoDeriv("n"));
TEST_MATHIEU(gsl_sf_mathieu_b, NoDeriv("n"));
TEST_MATHIEU(gsl_sf_mathieu_ce, NoDeriv("n"));
TEST_MATHIEU(gsl_sf_mathieu_se, NoDeriv("n"));
TEST_MATHIEU(gsl_sf_mathieu_Mc, NoDeriv("j n"));
TEST_MATHIEU(gsl_sf_mathieu_Ms, NoDeriv("j n"));
}

TEST_F(GSLTest, Power) {
Expand Down
2 changes: 1 addition & 1 deletion thirdparty/gsl
Submodule gsl updated 467 files

0 comments on commit 635f552

Please sign in to comment.