changeset 0:b4dac49484db

Separated gsl extension out from MonetDB.
author Sjoerd Mullender <sjoerd@acm.org>
date Tue, 14 Feb 2017 14:49:41 +0100 (2017-02-14)
parents
children 0eee04b5967f
files 46_gsl.sql 73_gsl.mal Makefile Tests/All Tests/gsl00.malC Tests/gsl00.stable.err Tests/gsl00.stable.out Tests/gsl01.sql Tests/gsl01.stable.err Tests/gsl01.stable.out gsl.c gsl.mal
diffstat 12 files changed, 625 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/46_gsl.sql	Tue Feb 14 14:49:41 2017 +0100
@@ -0,0 +1,12 @@
+-- This Source Code Form is subject to the terms of the Mozilla Public
+-- License, v. 2.0.  If a copy of the MPL was not distributed with this
+-- file, You can obtain one at http://mozilla.org/MPL/2.0/.
+--
+-- Copyright 1997 - July 2008 CWI, August 2008 - 2017 MonetDB B.V.
+
+-- (co) Arjen de Rijke, Bart Scheers
+-- Use statistical functions from gsl library
+
+-- Calculate Chi squared probability
+create function sys.chi2prob(chi2 double, datapoints double)
+returns double external name gsl."chi2prob";
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/73_gsl.mal	Tue Feb 14 14:49:41 2017 +0100
@@ -0,0 +1,7 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0.  If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+#
+# Copyright 1997 - July 2008 CWI, August 2008 - 2017 MonetDB B.V.
+
+include gsl;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Makefile	Tue Feb 14 14:49:41 2017 +0100
@@ -0,0 +1,22 @@
+LIBDIR = `pkg-config --variable=libdir monetdb5`
+
+CC = cc
+
+CFLAGS = `pkg-config --cflags monetdb5`
+LDFLAGS = `pkg-config --libs monetdb5`
+
+all: lib_gsl.so
+
+lib_gsl.so: gsl.o
+	$(CC) -fPIC -DPIC -o lib_gsl.so -shared gsl.o $(LDFLAGS) -Wl,-soname -Wl,lib_gsl.so
+
+gsl.o: gsl.c
+	$(CC) -fPIC -DPIC $(CFLAGS) -c gsl.c
+
+clean:
+	rm -f *.o *.so
+
+install: lib_gsl.so
+	cp gsl.mal lib_reverse.so $(DESTDIR)$(LIBDIR)/monetdb5
+	cp 46_gsl.sql $(DESTDIR)$(LIBDIR)/monetdb5/createdb
+	cp 73_gsl.mal $(DESTDIR)$(LIBDIR)/monetdb5/autoload
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Tests/All	Tue Feb 14 14:49:41 2017 +0100
@@ -0,0 +1,2 @@
+HAVE_GSL?gsl00
+HAVE_GSL?gsl01
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Tests/gsl00.malC	Tue Feb 14 14:49:41 2017 +0100
@@ -0,0 +1,4 @@
+a:=20:dbl;
+b:=5:dbl;
+c:=gsl.chi2prob(a,b);
+io.print(c);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Tests/gsl00.stable.err	Tue Feb 14 14:49:41 2017 +0100
@@ -0,0 +1,29 @@
+stderr of test 'gsl00` in directory 'sql/backends/monet5/gsl` itself:
+
+
+# 15:55:26 >  
+# 15:55:26 >  "mserver5" "--debug=10" "--set" "gdk_nr_threads=0" "--set" "mapi_open=true" "--set" "mapi_port=36073" "--set" "mapi_usock=/var/tmp/mtest-19131/.s.monetdb.36073" "--set" "monet_prompt=" "--forcemito" "--set" "mal_listing=2" "--dbpath=/export/scratch2/rijke/monetdb/default/var/MonetDB/mTests_sql_backends_monet5_gsl" "gsl00.mal"
+# 15:55:26 >  
+
+# builtin opt 	gdk_dbpath = /export/scratch2/rijke/monetdb/default/var/monetdb5/dbfarm/demo
+# builtin opt 	gdk_debug = 0
+# builtin opt 	gdk_vmtrim = yes
+# builtin opt 	monet_prompt = >
+# builtin opt 	monet_daemon = no
+# builtin opt 	mapi_port = 50000
+# builtin opt 	mapi_open = false
+# builtin opt 	mapi_autosense = false
+# builtin opt 	sql_optimizer = default_pipe
+# builtin opt 	sql_debug = 0
+# cmdline opt 	gdk_nr_threads = 0
+# cmdline opt 	mapi_open = true
+# cmdline opt 	mapi_port = 36073
+# cmdline opt 	mapi_usock = /var/tmp/mtest-19131/.s.monetdb.36073
+# cmdline opt 	monet_prompt = 
+# cmdline opt 	mal_listing = 2
+# cmdline opt 	gdk_dbpath = /export/scratch2/rijke/monetdb/default/var/MonetDB/mTests_sql_backends_monet5_gsl
+
+# 15:55:26 >  
+# 15:55:26 >  "Done."
+# 15:55:26 >  
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Tests/gsl00.stable.out	Tue Feb 14 14:49:41 2017 +0100
@@ -0,0 +1,35 @@
+stdout of test 'gsl00` in directory 'sql/backends/monet5/gsl` itself:
+
+
+# 15:55:26 >  
+# 15:55:26 >  "mserver5" "--debug=10" "--set" "gdk_nr_threads=0" "--set" "mapi_open=true" "--set" "mapi_port=36073" "--set" "mapi_usock=/var/tmp/mtest-19131/.s.monetdb.36073" "--set" "monet_prompt=" "--forcemito" "--set" "mal_listing=2" "--dbpath=/export/scratch2/rijke/monetdb/default/var/MonetDB/mTests_sql_backends_monet5_gsl" "gsl00.mal"
+# 15:55:26 >  
+
+# MonetDB 5 server v11.16.0
+# This is an unreleased version
+# Serving database 'mTests_sql_backends_monet5_gsl', using 8 threads
+# Compiled for x86_64-unknown-linux-gnu/64bit with 64bit OIDs dynamically linked
+# Found 15.592 GiB available main-memory.
+# Copyright (c) 1993-July 2008 CWI.
+# Copyright (c) August 2008-2015 MonetDB B.V., all rights reserved
+# Visit http://www.monetdb.org/ for further information
+# Listening for connection requests on mapi:monetdb://rotterdam.ins.cwi.nl:36073/
+# Listening for UNIX domain connection requests on mapi:monetdb:///var/tmp/mtest-19131/.s.monetdb.36073
+# MonetDB/GIS module loaded
+# MonetDB/JAQL module loaded
+# MonetDB/SQL module loaded
+# MonetDB/R   module loaded
+
+Ready.
+
+# 17:18:32 >  
+# 17:18:32 >  "mclient" "-lmal" "-ftest" "-Eutf-8" "-i" "-e" "--host=/var/tmp/mtest-13823" "--port=38735"
+# 17:18:32 >  
+
+#io.print(c);
+[ 0.001249730563	]
+
+# 17:18:32 >  
+# 17:18:32 >  "Done."
+# 17:18:32 >  
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Tests/gsl01.sql	Tue Feb 14 14:49:41 2017 +0100
@@ -0,0 +1,68 @@
+SELECT sys.chi2prob(20.0, 5.0);
+
+SELECT sys.chi2prob(20.0, NULL);
+
+SELECT sys.chi2prob(NULL, 5.0);
+
+SELECT sys.chi2prob(-1, 5.0);
+
+SELECT sys.chi2prob(20.0, 1.0);
+
+CREATE TABLE chi2(a double, b double);
+
+INSERT INTO chi2 VALUES (20.0, 5.0),
+       	    	 	(22.0, 4.0),
+			(20.0, 6.0);
+
+SELECT chi2prob(a, b) FROM chi2;
+
+SELECT chi2prob(a, 6.0) FROM chi2;
+
+SELECT chi2prob(19.0, b) FROM chi2;
+
+INSERT INTO chi2 VALUES (20.0, NULL);
+
+SELECT chi2prob(a, b) FROM chi2;
+
+SELECT chi2prob(a, 6.0) FROM chi2;
+
+SELECT chi2prob(19.0, b) FROM chi2;
+
+DELETE FROM chi2;
+
+INSERT INTO chi2 VALUES (20.0, 5.0),
+       	    	 	(22.0, 4.0),
+			(20.0, 6.0),
+                        (NULL, 5.0);
+
+SELECT chi2prob(a, b) FROM chi2;
+
+SELECT chi2prob(a, 6.0) FROM chi2;
+
+SELECT chi2prob(19.0, b) FROM chi2;
+
+DELETE FROM chi2;
+
+INSERT INTO chi2 VALUES (20.0, 5.0),
+       	    	 	(22.0, 4.0),
+			(20.0, 6.0),
+                        (-1, 5.0);
+
+SELECT chi2prob(a, b) FROM chi2;
+
+SELECT chi2prob(a, 6.0) FROM chi2;
+
+SELECT chi2prob(19.0, b) FROM chi2;
+
+DELETE FROM chi2;
+
+INSERT INTO chi2 VALUES (20.0, 5.0),
+       	    	 	(22.0, 4.0),
+			(20.0, 6.0),
+                        (20.0, 1.0);
+
+SELECT chi2prob(a, b) FROM chi2;
+
+SELECT chi2prob(a, 6.0) FROM chi2;
+
+SELECT chi2prob(19.0, b) FROM chi2;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Tests/gsl01.stable.err	Tue Feb 14 14:49:41 2017 +0100
@@ -0,0 +1,62 @@
+stderr of test 'gsl01` in directory 'sql/backends/monet5/gsl` itself:
+
+
+# 15:55:26 >  
+# 15:55:26 >  "mserver5" "--debug=10" "--set" "gdk_nr_threads=0" "--set" "mapi_open=true" "--set" "mapi_port=36073" "--set" "mapi_usock=/var/tmp/mtest-19131/.s.monetdb.36073" "--set" "monet_prompt=" "--forcemito" "--set" "mal_listing=2" "--dbpath=/export/scratch2/rijke/monetdb/default/var/MonetDB/mTests_sql_backends_monet5_gsl" "--set" "mal_listing=0"
+# 15:55:26 >  
+
+# builtin opt 	gdk_dbpath = /export/scratch2/rijke/monetdb/default/var/monetdb5/dbfarm/demo
+# builtin opt 	gdk_debug = 0
+# builtin opt 	gdk_vmtrim = yes
+# builtin opt 	monet_prompt = >
+# builtin opt 	monet_daemon = no
+# builtin opt 	mapi_port = 50000
+# builtin opt 	mapi_open = false
+# builtin opt 	mapi_autosense = false
+# builtin opt 	sql_optimizer = default_pipe
+# builtin opt 	sql_debug = 0
+# cmdline opt 	gdk_nr_threads = 0
+# cmdline opt 	mapi_open = true
+# cmdline opt 	mapi_port = 36073
+# cmdline opt 	mapi_usock = /var/tmp/mtest-19131/.s.monetdb.36073
+# cmdline opt 	monet_prompt = 
+# cmdline opt 	mal_listing = 2
+# cmdline opt 	gdk_dbpath = /export/scratch2/rijke/monetdb/default/var/MonetDB/mTests_sql_backends_monet5_gsl
+# cmdline opt 	mal_listing = 0
+
+# 15:57:56 >  
+# 15:57:56 >  "mclient" "-lsql" "-ftest" "-Eutf-8" "-i" "-e" "--host=/var/tmp/mtest-16688" "--port=34082"
+# 15:57:56 >  
+
+MAPI  = (monetdb) /var/tmp/mtest-28608/.s.monetdb.36472
+QUERY = SELECT sys.chi2prob(20.0, NULL);
+ERROR = !Wrong value for datapoints
+MAPI  = (monetdb) /var/tmp/mtest-28608/.s.monetdb.36472
+QUERY = SELECT sys.chi2prob(NULL, 5.0);
+ERROR = !Wrong value for chi2
+MAPI  = (monetdb) /var/tmp/mtest-28608/.s.monetdb.36472
+QUERY = SELECT sys.chi2prob(-1, 5.0);
+ERROR = !Wrong value for chi2
+MAPI  = (monetdb) /var/tmp/mtest-28608/.s.monetdb.36472
+QUERY = SELECT chi2prob(a, b) FROM chi2;
+ERROR = !Wrong value for datapoints
+MAPI  = (monetdb) /var/tmp/mtest-28608/.s.monetdb.36472
+QUERY = SELECT chi2prob(19.0, b) FROM chi2;
+ERROR = !Wrong value for datapoints
+MAPI  = (monetdb) /var/tmp/mtest-28608/.s.monetdb.36472
+QUERY = SELECT chi2prob(a, b) FROM chi2;
+ERROR = !Wrong value for chi2
+MAPI  = (monetdb) /var/tmp/mtest-28608/.s.monetdb.36472
+QUERY = SELECT chi2prob(a, 6.0) FROM chi2;
+ERROR = !Wrong value for chi2
+MAPI  = (monetdb) /var/tmp/mtest-28608/.s.monetdb.36472
+QUERY = SELECT chi2prob(a, b) FROM chi2;
+ERROR = !Wrong value for chi2
+MAPI  = (monetdb) /var/tmp/mtest-28608/.s.monetdb.36472
+QUERY = SELECT chi2prob(a, 6.0) FROM chi2;
+ERROR = !Wrong value for chi2
+
+# 15:57:57 >  
+# 15:57:57 >  "Done."
+# 15:57:57 >  
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Tests/gsl01.stable.out	Tue Feb 14 14:49:41 2017 +0100
@@ -0,0 +1,181 @@
+stdout of test 'gsl01` in directory 'sql/backends/monet5/gsl` itself:
+
+
+# 15:55:26 >  
+# 15:55:26 >  "mserver5" "--debug=10" "--set" "gdk_nr_threads=0" "--set" "mapi_open=true" "--set" "mapi_port=36073" "--set" "mapi_usock=/var/tmp/mtest-19131/.s.monetdb.36073" "--set" "monet_prompt=" "--forcemito" "--set" "mal_listing=2" "--dbpath=/export/scratch2/rijke/monetdb/default/var/MonetDB/mTests_sql_backends_monet5_gsl" "--set" "mal_listing=0"
+# 15:55:26 >  
+
+# MonetDB 5 server v11.16.0
+# This is an unreleased version
+# Serving database 'mTests_sql_backends_monet5_gsl', using 8 threads
+# Compiled for x86_64-unknown-linux-gnu/64bit with 64bit OIDs dynamically linked
+# Found 15.592 GiB available main-memory.
+# Copyright (c) 1993-July 2008 CWI.
+# Copyright (c) August 2008-2015 MonetDB B.V., all rights reserved
+# Visit http://www.monetdb.org/ for further information
+# Listening for connection requests on mapi:monetdb://rotterdam.ins.cwi.nl:36073/
+# Listening for UNIX domain connection requests on mapi:monetdb:///var/tmp/mtest-19131/.s.monetdb.36073
+# MonetDB/GIS module loaded
+# MonetDB/JAQL module loaded
+# MonetDB/SQL module loaded
+
+Ready.
+# SQL catalog created, loading sql scripts once
+# loading sql script: 09_like.sql
+# loading sql script: 10_math.sql
+# loading sql script: 11_times.sql
+# loading sql script: 12_url.sql
+# loading sql script: 13_date.sql
+# loading sql script: 14_inet.sql
+# loading sql script: 15_querylog.sql
+# loading sql script: 16_tracelog.sql
+# loading sql script: 17_compress.sql
+# loading sql script: 18_dictionary.sql
+# loading sql script: 19_cluster.sql
+# loading sql script: 20_vacuum.sql
+# loading sql script: 21_dependency_functions.sql
+# loading sql script: 22_clients.sql
+# loading sql script: 23_skyserver.sql
+# loading sql script: 24_zorder.sql
+# loading sql script: 25_debug.sql
+# loading sql script: 26_sysmon.sql
+# loading sql script: 39_analytics.sql
+# loading sql script: 40_geom.sql
+# loading sql script: 40_json.sql
+# loading sql script: 41_md5sum.sql
+# loading sql script: 45_uuid.sql
+# loading sql script: 46_gsl.sql
+# loading sql script: 75_storagemodel.sql
+# loading sql script: 80_statistics.sql
+# loading sql script: 80_udf.sql
+# loading sql script: 99_system.sql
+
+# 15:55:27 >  
+# 15:55:27 >  "mclient" "-lsql" "-ftest" "-Eutf-8" "-i" "-e" "--host=/var/tmp/mtest-19131" "--port=36073"
+# 15:55:27 >  
+
+#SELECT sys.chi2prob(20.0, 5.0);
+% .L1 # table_name
+% L1 # name
+% double # type
+% 24 # length
+[ 0.001249730563	]
+#SELECT sys.chi2prob(20.0, 1.0);
+% .L1 # table_name
+% L1 # name
+% double # type
+% 24 # length
+[ 7.744216431e-06	]
+#CREATE TABLE chi2(a double, b double);
+#INSERT INTO chi2 VALUES (20.0, 5.0),
+#       	    	 	(22.0, 4.0),
+#			(20.0, NULL),
+#			(NULL, 5.0),
+#			(20.0, 6.0);
+[ 3	]
+#SELECT chi2prob(a, b) FROM chi2;
+% sys.L1 # table_name
+% L1 # name
+% double # type
+% 24 # length
+[ 0.001249730563	]
+[ 0.0002004204095	]
+[ 0.002769395716	]
+#SELECT chi2prob(a, 6.0) FROM chi2;
+% sys.L1 # table_name
+% L1 # name
+% double # type
+% 24 # length
+[ 0.002769395716	]
+[ 0.001210873307	]
+[ 0.002769395716	]
+#SELECT chi2prob(19.0, b) FROM chi2;
+% .L1 # table_name
+% L1 # name
+% double # type
+% 24 # length
+[ 0.001922136821	]
+[ 0.0007859442138	]
+[ 0.004163633038	]
+#INSERT INTO chi2 VALUES (20.0, NULL);
+[ 1	]
+#SELECT chi2prob(a, 6.0) FROM chi2;
+% sys.L1 # table_name
+% L1 # name
+% double # type
+% 24 # length
+[ 0.002769395716	]
+[ 0.001210873307	]
+[ 0.002769395716	]
+[ 0.002769395716	]
+#DELETE FROM chi2;
+[ 4	]
+#INSERT INTO chi2 VALUES (20.0, 5.0),
+#       	    	 	(22.0, 4.0),
+#			(20.0, 6.0),
+#                        (NULL, 5.0);
+[ 4	]
+#SELECT chi2prob(19.0, b) FROM chi2;
+% .L1 # table_name
+% L1 # name
+% double # type
+% 24 # length
+[ 0.001922136821	]
+[ 0.0007859442138	]
+[ 0.004163633038	]
+[ 0.001922136821	]
+#DELETE FROM chi2;
+[ 4	]
+#INSERT INTO chi2 VALUES (20.0, 5.0),
+#       	    	 	(22.0, 4.0),
+#			(20.0, 6.0),
+#                        (-1, 5.0);
+[ 4	]
+#SELECT chi2prob(19.0, b) FROM chi2;
+% .L1 # table_name
+% L1 # name
+% double # type
+% 24 # length
+[ 0.001922136821	]
+[ 0.0007859442138	]
+[ 0.004163633038	]
+[ 0.001922136821	]
+#DELETE FROM chi2;
+[ 4	]
+#INSERT INTO chi2 VALUES (20.0, 5.0),
+#       	    	 	(22.0, 4.0),
+#			(20.0, 6.0),
+#                        (20.0, 1.0);
+[ 4	]
+#SELECT chi2prob(a, b) FROM chi2;
+% sys.L1 # table_name
+% L1 # name
+% double # type
+% 24 # length
+[ 0.001249730563	]
+[ 0.0002004204095	]
+[ 0.002769395716	]
+[ 7.744216431e-06	]
+#SELECT chi2prob(a, 6.0) FROM chi2;
+% sys.L1 # table_name
+% L1 # name
+% double # type
+% 24 # length
+[ 0.002769395716	]
+[ 0.001210873307	]
+[ 0.002769395716	]
+[ 0.002769395716	]
+#SELECT chi2prob(19.0, b) FROM chi2;
+% .L1 # table_name
+% L1 # name
+% double # type
+% 24 # length
+[ 0.001922136821	]
+[ 0.0007859442138	]
+[ 0.004163633038	]
+[ 1.307184537e-05	]
+
+# 15:55:27 >  
+# 15:55:27 >  "Done."
+# 15:55:27 >  
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gsl.c	Tue Feb 14 14:49:41 2017 +0100
@@ -0,0 +1,179 @@
+/*
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0.  If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * Copyright 1997 - July 2008 CWI, August 2008 - 2017 MonetDB B.V.
+ */
+
+/*
+ *  A. de Rijke, B. Scheers
+ * The gsl module
+ * The gsl module contains wrappers for functions in
+ * gsl.
+ */
+
+#ifndef AGSL
+#define AGSL
+
+#include "monetdb_config.h"
+#include "mal.h"
+#include "mal_exception.h"
+#include <gsl/gsl_cdf.h>
+
+#ifdef WIN32
+#define gsl_export extern __declspec(dllexport)
+#else
+#define gsl_export extern
+#endif
+
+gsl_export str GSLchisqProb(dbl *retval, dbl * chi2, dbl * datapoints);
+gsl_export str GSLbat_chisqProb_cst(bat *retval, bat * chi2, dbl * datapoints);
+gsl_export str GSLcst_chisqProb_bat(bat *retval, dbl * chi2, bat * datapoints);
+gsl_export str GSLbat_chisqProb_bat(bat *retval, bat * chi2, bat * datapoints);
+
+static str
+gsl_chisqprob(dbl * retval, dbl chi2, dbl datapoints) 
+{
+	*retval = dbl_nil;
+	if ((chi2 == dbl_nil) || (chi2 < 0))
+		throw(MAL, "gsl.chi2prob", "Wrong value for chi2");
+	if ((datapoints == dbl_nil) || (datapoints < 0))
+		throw(MAL, "gsl.chi2prob", "Wrong value for datapoints");
+	*retval = gsl_cdf_chisq_Q(chi2, datapoints);
+	return MAL_SUCCEED;
+}
+
+static str
+gsl_bat_chisqprob_cst(bat * retval, bat chi2, dbl datapoints) 
+{
+	BAT *b, *bn;
+	BATiter bi;
+	BUN p,q;
+	dbl r;
+	char *msg = NULL;
+
+	if (datapoints == dbl_nil) {
+		throw(MAL, "GSLbat_chisqprob_cst", "Parameter datapoints should not be nil");
+	}
+	if (datapoints < 0)
+		throw(MAL, "gsl.chi2prob", "Wrong value for datapoints");
+
+	if ((b = BATdescriptor(chi2)) == NULL) {
+		throw(MAL, "chisqprob", "Cannot access descriptor");
+	}
+	bi = bat_iterator(b);
+	bn = COLnew(b->hseqbase, TYPE_dbl, BATcount(b), TRANSIENT);
+	if (bn == NULL){
+		BBPunfix(b->batCacheid);
+		throw(MAL, "gsl.chisqprob", MAL_MALLOC_FAIL);
+	}
+	BATloop(b,p,q) {
+		dbl d = *(dbl*)BUNtail(bi,p);
+		if ((d == dbl_nil) || (d < 0))
+			throw(MAL, "gsl.chi2prob", "Wrong value for chi2");
+		r = gsl_cdf_chisq_Q(d, datapoints);
+		BUNappend(bn, &r, FALSE);
+	}
+	*retval = bn->batCacheid;
+	BBPkeepref(bn->batCacheid);
+	BBPunfix(b->batCacheid);
+	return msg;
+}
+
+static str
+gsl_cst_chisqprob_bat(bat * retval, dbl chi2, bat datapoints) 
+{
+	BAT *b, *bn;
+	BATiter bi;
+	BUN p,q;
+	dbl r;
+	char *msg = NULL;
+
+	if( (b = BATdescriptor(datapoints)) == NULL) {
+		throw(MAL, "chisqprob", "Cannot access descriptor");
+	}
+	if (chi2 == dbl_nil) {
+		throw(MAL, "GSLbat_chisqprob_cst", "Parameter chi2 should not be nil");
+	}
+	if (chi2 < 0)
+		throw(MAL, "gsl.chi2prob", "Wrong value for chi2");
+	bi = bat_iterator(b);
+	bn = COLnew(b->hseqbase, TYPE_dbl, BATcount(b), TRANSIENT);
+	if( bn == NULL) {
+		BBPunfix(b->batCacheid);
+		throw(MAL, "gsl.chisqprob", MAL_MALLOC_FAIL);
+	}
+	BATloop(b,p,q) {
+		dbl datapoints = *(dbl*)BUNtail(bi,p);
+
+		if ((datapoints == dbl_nil) || (datapoints < 0))
+			throw(MAL, "gsl.chi2prob", "Wrong value for datapoints");
+		r = gsl_cdf_chisq_Q(chi2, datapoints);
+		BUNappend(bn, &r, FALSE);
+	}
+	BBPkeepref( *retval = bn->batCacheid);
+	BBPunfix(b->batCacheid);
+	return msg;
+}
+
+static str
+gsl_bat_chisqprob_bat(bat * retval, bat chi2, bat datapoints) 
+{
+	BAT *b, *c, *bn;
+	dbl r, *chi2p, *datapointsp;
+	char *msg = NULL;
+	size_t cnt = 0, i;
+
+	if( (b = BATdescriptor(chi2)) == NULL) {
+		throw(MAL, "chisqprob", "Cannot access descriptor chi2");
+	}
+	if( (c = BATdescriptor(datapoints)) == NULL) {
+		throw(MAL, "chisqprob", "Cannot access descriptor datapoints");
+	}
+	bn = COLnew(b->hseqbase, TYPE_dbl, cnt = BATcount(b), TRANSIENT);
+	if( bn == NULL) {
+		BBPunfix(b->batCacheid);
+		throw(MAL, "gsl.chisqprob", MAL_MALLOC_FAIL);
+	}
+	chi2p = (dbl*)Tloc(b, 0);
+	datapointsp = (dbl*)Tloc(c, 0);
+	for(i = 0; i<cnt; i++) { 
+		if ((chi2p[i] == dbl_nil) || (chi2p[i] < 0))
+			throw(MAL, "gsl.chi2prob", "Wrong value for chi2");
+		if ((datapointsp[i] == dbl_nil) || (datapointsp[i] < 0))
+			throw(MAL, "gsl.chi2prob", "Wrong value for datapoints");
+		r = gsl_cdf_chisq_Q(chi2p[i], datapointsp[i]);
+		BUNappend(bn, &r, FALSE);
+	}
+	BBPkeepref( *retval = bn->batCacheid);
+	BBPunfix(b->batCacheid);
+	BBPunfix(c->batCacheid);
+	return msg;
+}
+
+str
+GSLchisqProb(dbl *retval, dbl * chi2, dbl * datapoints) 
+{
+	return gsl_chisqprob(retval, *chi2, *datapoints);
+}
+
+str
+GSLbat_chisqProb_cst(bat *retval, bat * chi2, dbl * datapoints) 
+{
+	return gsl_bat_chisqprob_cst(retval, *chi2, *datapoints);
+}
+
+str
+GSLcst_chisqProb_bat(bat *retval, dbl * chi2, bat * datapoints) 
+{
+	return gsl_cst_chisqprob_bat(retval, *chi2, *datapoints);
+}
+
+str
+GSLbat_chisqProb_bat(bat *retval, bat * chi2, bat * datapoints) 
+{
+	return gsl_bat_chisqprob_bat(retval, *chi2, *datapoints);
+}
+
+#endif /* AGSL */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gsl.mal	Tue Feb 14 14:49:41 2017 +0100
@@ -0,0 +1,24 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0.  If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+#
+# Copyright 1997 - July 2008 CWI, August 2008 - 2017 MonetDB B.V.
+
+module gsl;
+module batgsl;
+
+command gsl.chi2prob(d:dbl,i:dbl):dbl
+address GSLchisqProb
+comment "Chi Squared probability";
+
+command batgsl.chi2prob(d:bat[:dbl],i:dbl):bat[:dbl]
+address GSLbat_chisqProb_cst
+comment "Chi Squared probability";
+
+command batgsl.chi2prob(d:dbl,i:bat[:dbl]):bat[:dbl]
+address GSLcst_chisqProb_bat
+comment "Chi Squared probability";
+
+command batgsl.chi2prob(d:bat[:dbl],i:bat[:dbl]):bat[:dbl]
+address GSLbat_chisqProb_bat
+comment "Chi Squared probability";