## Adding a GSL integration function to Emacs with a dynamic module

| categories: | tags: | View Comments

Here we work out how to run this program: https://www.gnu.org/software/gsl/doc/html/integration.html#adaptive-integration-example in a dynamic module in emacs. The goal is to be able to evaluate $$\int_0^1 x^{-1/2} \log(x) dx$$. According to the example page the answer is -4. We will define an integration function that takes at least a function and integration bounds as arguments, and several optional arguments to specify tolerances and limits. In other words we want to evaluate integrals of the form:

$$\int_a^b f(x; params) dx$$

I want that to happen in an elisp function with a signature like:

(gsl-integration-qags (lambda (x params) body) a b &optional params epsabs epsrel limit)


And that function will return a list containing (result error-estimate). Here is the C-code that makes this happen. It is more complex that the last example, and only compiles with gcc that allows nested functions. I don't know how to write this without that feature. This is more complex also because you have to create a workspace to do the integration inside the function that does the integration. The C-module also has extra code in it to allow for optional arguments.

#include <gsl/gsl_integration.h>
#include "emacs-module.h"

int plugin_is_GPL_compatible;

static emacs_value F_gsl_integrate (emacs_env *env, ptrdiff_t nargs, emacs_value args[], void *data)
{
// nested function - only supported as an extension in gcc
double f (double x, void *params)
{
emacs_value fn = args[0];  // function we will integrate
emacs_value x2[] = { env->make_float(env, x), params };
emacs_value y = env->funcall(env, fn, 2, &x2);

return env->extract_float (env, y);
}

double a = env->extract_float (env, args[1]);
double b = env->extract_float (env, args[2]);

// default values for optional arguments
double epsabs = 0.0;
double epsrel = 1e-7;
size_t limit = 1000;
double result, error;

// Here is how I handle the optional arguments
// (gsl-integrate func a b params epsabs epsrel limit)
gsl_function F;
F.function = &f;
if (nargs >= 4) {F.params = args[3];}
if (nargs >= 5 && env->is_not_nil(env, args[4])) {epsabs = env->extract_float(env, args[4]);}
if (nargs >= 6 && env->is_not_nil(env, args[5])) {epsrel = env->extract_float(env, args[5]);}
if (nargs >= 7 && env->is_not_nil(env, args[6])) {limit = env->extract_integer(env, args[6]);}

gsl_integration_workspace * w = gsl_integration_workspace_alloc (limit);

gsl_integration_qags (&F, // gsl_function pointer
a, // lower integration bound
b, // upper integration bound
epsabs, // absolute error tolerance
epsrel, // relative error tolerance
limit, // max number of subintervals for integration
w, // the workspace
// pointers to put results and error in
&result, &error);

gsl_integration_workspace_free (w);

// make a list of (result error) to return
emacs_value Qlist = env->intern(env, "list");
emacs_value Qresult = env->make_float (env, result);
emacs_value Qerror = env->make_float (env, error);
emacs_value list_args[] = { Qresult, Qerror };
return env->funcall(env, Qlist, 2, list_args);
}

int emacs_module_init(struct emacs_runtime *ert)
{
emacs_env *env = ert->get_environment(ert);

// Here we create the function.
emacs_value fset = env->intern(env, "fset");
emacs_value args[2];
args[0] = env->intern(env, "gsl-integration-qags"); // symbol to create for function
// The function we set that symbol to.
args[1] = env->make_function(env,
3, // min nargs
7, // max nargs
F_gsl_integrate,
"(gsl-integration-qags F A B &optional PARAMS EPSABS EPSREL LIMIT)\n" \
"Integrate F(x; params) from A to B.\n" \
"F is a function of a single variable and parameters.\n" \
"A is the lower bound of integration\n"  \
"B is the upper bound of integration.\n" \
"Optional parameters:\n"\
"PARAMS is a list of params to pass to F.\n" \
"EPSABS is a float (default 0.0) and is the absolute error tolerance.\n" \
"EPSREL is a float (default 1e-7) and is the relative error tolerance.\n" \
"LIMIT is the maximum number of subintervals for the integration (default 1000).\n" \
"Returns (list result error-estimate).\n" \
0);
// This is basically (fset 'gsl-integration-qags (lambda func))
env->funcall(env, fset, 2, args);

// This is what allows the shared library to provide a feature
emacs_value provide = env->intern(env, "provide");
emacs_value provide_args[] = { env->intern(env, "gsl-integration") };
env->funcall(env, provide, 1, provide_args);

return 0;
}


Building this was moderately tricky. It appears the first gcc on my path uses clang which does not support nested functions in C. I don't know enough C to figure out how to do this without a nested function though, since the function has to be defined at run-time based on the emacs env and args. gcc does support inline functions, so the code below uses a gcc that does compile it.

rm -f gsl-integration.so gsl-integration.o
/usr/local/Cellar/gcc/6.1.0/bin/gcc-6 -Wall -I/usr/local/include -fPIC -c gsl-integration.c
/usr/local/Cellar/gcc/6.1.0/bin/gcc-6  -shared -L/usr/local/include -lgsl -o gsl-integration.so gsl-integration.o


Now we add this directory to our path since it is not on it and require our new module.

(add-to-list 'load-path "/Users/jkitchin/vc/blogofile-jkitchin.github.com/_blog/dynamic-module/")
(require 'gsl-integration)

gsl-integration



Let us see our new function in action. We evaluate $$\int_0^1 x^{-1/2} \log(x) dx$$. According to the example page the answer is -4. Here is an example where we ignore the parameters. You have to be careful; Emacs sometimes segfaults and crashes if you use an integer or float argument when it expects the other type.

(gsl-integration-qags (lambda (x params) (/ (log x) (sqrt x))) 0.0 1.0)

 -4 1.35447e-13

Here are some optional arguments.

(gsl-integration-qags (lambda (x params) (/ (log x) (sqrt x))) 0.0 1.0 nil nil 0.01)

 -4 0.0195266

Nice, with a larger epsrel argument we get a larger error. Note the arguments are positional, so we have to include them all just to set the epsrel argument. How about an easier example with parameters that we actually use. Here we integrate a constant, and set the value of the constant from the params arg. The integral should be the area of a rectangle of length 1 and width of the param we use.

(list
(gsl-integration-qags (lambda (x params) (first params)) 0.0 1.0 '(1.0))
(gsl-integration-qags (lambda (x params) (first params)) 0.0 1.0 '(0.5)))

 1 1.11022e-14 0.5 5.55112e-15

Wow! It actually works!!! That was harder won success than usual for me. I am claiming victory for now and leaving the following notes to future me:

1. It would be nice to have optional keyword arguments. This would take some handling of the arguments beyond what I know how to do for now, unless it is possible to pull in something like plist-get the way we pull in fset, provide and list in this example.
2. Error checking on types would be helpful. It is not good for Emacs to crash because 0 is not 0.0!
3. In numpy there is often a feature to get full_output. Here, the workspace created in the function has more information available in a struct that might be helpful to have access to at times. It seems like it might be possible to get that here too.