twiecki / cythongsl Goto Github PK
View Code? Open in Web Editor NEWCython interface for the GNU Scientific Library (GSL).
License: Other
Cython interface for the GNU Scientific Library (GSL).
License: Other
the <gsl/gsl_integration.h> has a function called -gsl_integration_fixed- that isn't available at the integration module of CythonGSL
With Cython 0.25.2, GCC5.2, and Ubuntu, I'm getting the following unit test failure:
test_gsl_vector_view_array_with_stride (cython_gsl.test.test_vector.VectorTest) ... *** stack smashing detected ***: /usr/bin/python terminated
There seems to be a problem with the example provided in the multidimensional minimisation routine example: the compiler does not recognise any of the gsl_multimin_.... type identifiers, starting with the one below.
cdef const gsl_multimin_fdfminimizer_type* T
Is there a problem with the use of const? (I am using Cython 0.25.2)
...
def main_fdf():
cdef size_t iter = 0
cdef int max_iter = 100
cdef int status
cdef const gsl_multimin_fdfminimizer_type* T
^
/Users/idiosyncrasy58/.ipython/cython/_cython_magic_8bae89fb4fa0616d795da00f11d03362.pyx:32:15: 'gsl_multimin_fdfminimizer_type' is not a type identifier
From a recent Cython discussion with Robert Kern:
"https://github.com/twiecki/CythonGSL/blob/master/examples/integrate.pyx#L1
That's the wrong "from cython_gsl import gsl", not the correct "from cython_gsl cimport gsl".
I would love to help, but there are lots of other things that are broken in the CythonGSL repo at the moment (.pxd files aren't installed; you include "math.pxi" but provide the file "math.pxd"). I suggest starting much, much smaller and working out the details before doing the whole GSL.
I will note that I was wrong earlier to suggest that installing a package with the .pxd files will just work. Cython has its own include path separate from the PYTHONPATH. I don't know the best way to distribute .pxd files in this case."
CythonGSL builds and installs without issue, but trying to build the tests results in a raft of errors. Looks like I cannot comport the package for some reason. Here are the first few errors:
Ronning:~/Code/CythonGSL/test
[569] 10:47:37 fonnescj:master* $ python setup.py build_ext -i
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/extension.py:133: UserWarning: Unknown Extension options: 'cython_include_dirs'
warnings.warn(msg)
running build_ext
cythoning blas.pyx to blas.c
Error compiling Cython file:
------------------------------------------------------------
...
from cython_gsl cimport *
^
------------------------------------------------------------
blas.pyx:1:0: 'cython_gsl.pxd' not found
Error compiling Cython file:
------------------------------------------------------------
...
from cython_gsl cimport *
from numpy import array, dot, transpose, outer, conjugate
from numpy.linalg import inv
def t_gsl_blas_ddot():
cdef gsl_vector *v1, *v2
^
------------------------------------------------------------
blas.pyx:6:9: 'gsl_vector' is not a type identifier
Error compiling Cython file:
------------------------------------------------------------
...
gsl_vector_free(v1)
gsl_vector_free(v2)
return ary
def t_gsl_blas_zdotu():
cdef gsl_complex z
^
------------------------------------------------------------
blas.pyx:22:9: 'gsl_complex' is not a type identifier
Error compiling Cython file:
------------------------------------------------------------
...
gsl_vector_free(v2)
return ary
def t_gsl_blas_zdotu():
cdef gsl_complex z
cdef gsl_vector_complex *v1, *v2
^
------------------------------------------------------------
blas.pyx:23:9: 'gsl_vector_complex' is not a type identifier
I want to use a member function of a class as a global function for gsl_function and I don't know how it should be done. Since I now just very basics about C, I know that I have to send the class instance which is the integrand to a void parameter but technically I can not code it in cython, it is a cython example:
import cython
cimport cython
import numpy as np
cimport numpy as np
from cython_gsl cimport *
ctypedef double * double_ptr
ctypedef void * void_ptr
from math import *
cdef class Foo(object):
def __init__(self, double a=1.2, double b=0.6):
self.a = a
self.b = b
def _integrand(self,double x) nogil:
cdef double self.a = (<double_ptr> params)[0]
cdef double self.b = (<double_ptr> params)[1]
return self.a*log(x)+self.b/x**3
def whole(self, double upper_limit=10,double lower_limit=0):
cdef gsl_integration_workspace * w
cdef double result, error, expected, alpha
w = gsl_integration_workspace_alloc (1000)
expected = -4.0
params[0] = self.a
params[1] = self.b
cdef gsl_function F
F.function = &self._integrand
F.params = params
gsl_integration_qags (&F, lower_limit,upper_limit , 0, 1e-7, 1000, w, &result, &error)
return result,error
I got this error message:
Cannot assign type double (Foo, double, void *) nogil' to 'double (*)(double, void *) nogil
How should it be done properly?
Could you please add to the readme the recommended way to build the examples, other than with setuptools?
I am using the following
$ cython multinomial.pyx
$ gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing -I/usr/include/python2.7 -I/usr/include/gsl -lgsl -lblas -o multinomial.so multinomial.c
$ python -m multinomial.so
But I am getting an error when trying to import the example:
/usr/bin/python: ./multinomial.so: undefined symbol: gsl_rng_mt19937
The odeiv2.pyx example appears to have some error, I cannot for the life of me work out what it is though.
After running
$ python setup.py build_ext --inplace
it outputs an .so file okay but importing into python raises the following error
ImportError: /home/ga7g08/Temp/odeiv2.so: undefined symbol: gsl_odeiv2_step_rk8pd
The same does not occur for the odeiv.pyx example, I have tried multiple other steppers and the same problem is occurring.
I apologies profusely if this is the wrong medium to raise such an issue but I could see no other way to contact you.
EDIT: The same DOES occur for odeiv.pyx...I therefore there is something wrong with my compilation process rather than the code. I used the setup.py defined on the github home page though so I can't see what else I can do?
I installed cython_gsl in sage using
sage -python setup.py build
sage -python setup.py install
Then I started sage and tried
load("random1.pyx")
The code seems to work fine, but I get this warning
warning: /home/xing/Downloads/software/sage/8.7/local/lib/python2.7/site-packages/cython_gsl/gsl_integration.pxd:65:9: 'GSL_EMAXITER' redeclared
warning: /home/xing/Downloads/software/sage/8.7/local/lib/python2.7/site-packages/cython_gsl/gsl_integration.pxd:67:9: 'GSL_EROUND' redeclared
warning: /home/xing/Downloads/software/sage/8.7/local/lib/python2.7/site-packages/cython_gsl/gsl_integration.pxd:69:9: 'GSL_ESING' redeclared
warning: /home/xing/Downloads/software/sage/8.7/local/lib/python2.7/site-packages/cython_gsl/gsl_integration.pxd:71:9: 'GSL_EDIVERGE' redeclared
Is this normal?
Should this function not return just the first element of os.path.split(cython_gsl.path[0]), i.e. os.path.split(cython_gsl.path[0])[0]? Otherwise, it return the first element as a tuple, which doesn't match the beheavior of other get_include functions (i.e. numpy.get_include)
Would be nice if this package would also include the sparse matrix and sparse blas modules from GSL.
Hello its more a question.
Do you have an example script for the error handling which returns a user-defined value.
Thanx Sven
in README.srt,
in the setup file description,
should be libraries=cython_gsl.get_libraries(),
and not libraries=cython_gsl.get_library(),
and include_dirs
instead of cython_include_dirs
The attached script test2.pyx returns "segmentation fault: 11" when called from the python script test.py. The script is a modification of the nonlinear fit gsl example code, the only change being:
f.df=NULL
f.fdf=NULL
The question is, whether the problem is in the cython source or in my paths. The .pyx code:
from cython_gsl cimport *
from libc.math cimport exp, sqrt, pow
from libc.stdio cimport *
from libc.stdlib cimport malloc,free
from gsl_multifit_nlin cimport *
from gsl cimport GSL_CONTINUE
ctypedef struct Data:
size_t n
double * y
double * sigma
cdef int expb_f (const gsl_vector * x, void * data, gsl_vector * f) nogil:
cdef Data * d = <Data *> data
cdef size_t n = d.n
cdef double * y = d.y
cdef double * sigma = d.sigma
cdef double A = gsl_vector_get (x, 0)
cdef double lambd = gsl_vector_get (x, 1)
cdef double b = gsl_vector_get (x, 2)
cdef size_t i
cdef double t, Yi
for i from 0 <= i < n:
# Model Yi = A * exp(-lambd * i) + b
t = i
Yi = A * exp (-lambd * t) + b
gsl_vector_set (f, i, (Yi - y[i])/sigma[i])
return GSL_SUCCESS
cdef void print_state (size_t iter, gsl_multifit_fdfsolver * s) nogil:
printf ("iter: %3u x = % 15.8f % 15.8f % 15.8f |f(x)| = %g\n", iter, gsl_vector_get (s.x, 0), gsl_vector_get (s.x, 1), gsl_vector_get (s.x, 2), gsl_blas_dnrm2 (s.f))
def main_test2():
cdef const gsl_multifit_fdfsolver_type * T
cdef gsl_multifit_fdfsolver * s
cdef int status = GSL_CONTINUE
cdef unsigned int i
cdef unsigned int iter = 0
cdef size_t n = 40
cdef size_t p = 3
cdef gsl_matrix * covar = gsl_matrix_alloc (p, p)
cdef double y[40]
cdef double sigma[40]
cdef Data d
d.n = n
d.y = y
d.sigma = sigma
cdef gsl_multifit_function_fdf f
cdef double * x_init = [1.0, 0.0, 0.0]
cdef gsl_vector_view x = gsl_vector_view_array (x_init, p)
cdef const gsl_rng_type * type
cdef gsl_rng * r
gsl_rng_env_setup ()
type = gsl_rng_default
r = gsl_rng_alloc (type)
f.f = &expb_f
#f.df = &expb_df
#f.fdf = &expb_fdf
f.df=NULL
f.fdf=NULL
f.n = n
f.p = p
f.params = &d
# This is the data to be fitted
cdef double t
for i from 0 <= i < n:
t = i;
y[i] = 1.0 + 5 * exp (-0.1 * t) + gsl_ran_gaussian (r, 0.1)
sigma[i] = 0.1
printf ("data: %u %g %g\n", i, y[i], sigma[i])
T = gsl_multifit_fdfsolver_lmsder
s = gsl_multifit_fdfsolver_alloc (T, n, p)
gsl_multifit_fdfsolver_set (s, &f, &x.vector)
print_state (iter, s)
while (status == GSL_CONTINUE and iter < 500):
iter += 1
status = gsl_multifit_fdfsolver_iterate (s)
printf ("status = %d\n", status)
print_state (iter, s)
if status:
break
status = gsl_multifit_test_delta (s.dx, s.x, 1e-4, 1e-4)
gsl_multifit_covar (s.J, 0.0, covar)
cdef double chi = gsl_blas_dnrm2 (s.f)
cdef double dof = n - p
cdef double c = GSL_MAX_DBL(1, chi / sqrt (dof))
printf("chisq/dof = %g\n", pow (chi, 2.0) / dof)
cdef double fit, err
fit = gsl_vector_get (s.x, 0)
err = sqrt (gsl_matrix_get (covar, 0, 0))
printf ("A = %.5f +/- %.5f\n", fit, c*err)
fit = gsl_vector_get (s.x, 1)
err = sqrt (gsl_matrix_get (covar, 1, 1))
printf ("lambda = %.5f +/- %.5f\n", fit, c*err)
fit = gsl_vector_get (s.x, 2)
err = sqrt (gsl_matrix_get (covar, 2, 2))
printf ("b = %.5f +/- %.5f\n", fit, c*err)
printf ("status = %d\n", status)
gsl_multifit_fdfsolver_free (s)
gsl_matrix_free (covar)
gsl_rng_free (r)
The get_library_dir function should probably return just the string lib_gsl_dir, rather than a list with lib_gsl_dir as the sole element. This would make it consistent with numpy and cython_gsl get_include.
Traceback (most recent call last):
File "cython_gsl/test/test_blas.py", line 228, in test_gsl_blas_zherk
self.assertAlmostEqual(x,0, 14)
AssertionError: -1.6000000000000001 != 0 within 14 places
Traceback (most recent call last):
File "cython_gsl/test/test_linalg.py", line 24, in test_gsl_linalg_LU_refine
self.assertAlmostEqual(x,0, 15)
AssertionError: 5.820766091346741e-10 != 0 within 15 places
Traceback (most recent call last):
File "cython_gsl/test/test_math.py", line 118, in test_gsl_atanh
self.assertAlmostEqual(mathgsl.t_gsl_atanh(1.3),0, 16)
AssertionError: nan != 0 within 16 places
Traceback (most recent call last):
File "cython_gsl/test/test_math.py", line 125, in test_gsl_frexp
self.assertAlmostEqual(t[0],0.6, 16)
AssertionError: 0.0 != 0.6 within 16 places
Ran 328 tests in 3.849s
FAILED (failures=4)
Unfortunately, the gsl windows port does not provide the gsl-config binary. At the moment, the default windows path is hard coded which of course is ugly. Perhaps the install dir is stored in the registry? Anyone with expertise here would be very helpful.
Running pip install Cython CythonGSL
on a machine that doesn't already have Cython installed fails becuase setup.py
imports Cython.Distutils
.
During installation, pip
imports setup.py
from all packages (in order to determine dependencies) before actually installing any of them.
These (unused) imports in setup.py
cause it to blow up if Cython isn't already installed.
library=cython_gsl.get_library_dir() should say library=cython_gsl.get_library()
Thank you for this project is is really helpful!
Could you please post an example of how to call gsl_ran_multinomial?
I have tried without success passing a numpy array as the probability distribution, but Cython won't let me. I have tried as <double*> p.data, and as &p[0].
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.