├── cython_gsl ├── interface │ └── __init__.py ├── test │ ├── __init__.py │ ├── test_errno.py │ ├── gslerrno.pyx │ ├── test_spblas.py │ ├── test_eigen.py │ ├── test_multifit_nlin.py │ ├── test_combinations.py │ ├── test_poly.py │ ├── test_sort.py │ ├── poly.pyx │ ├── spblas.pyx │ ├── test_vector_complex.py │ ├── test_permutations.py │ ├── multifit_nlin.pyx │ ├── combinations.pyx │ ├── sort.pyx │ ├── test_vector.py │ ├── test_matrix.py │ ├── test_linalg.py │ ├── test_matrix_complex.py │ └── mathgsl.pyx ├── gsl_elljac.pxd ├── gsl_dawson.pxd ├── gsl_clausen.pxd ├── gsl_mode.pxd ├── gsl_pow_int.pxd ├── gsl_sf_result.pxd ├── gsl_elementary.pxd ├── gsl_dilog.pxd ├── gsl_errno.pxd ├── gsl_lambert.pxd ├── gsl_synchrotron.pxd ├── gsl_diff.pxd ├── gsl_debye.pxd ├── gsl_transport.pxd ├── gsl_spblas.pxd ├── gsl_log.pxd ├── gsl_zeta.pxd ├── gsl_psi.pxd ├── gsl_laguerre.pxd ├── gsl_qrng.pxd ├── gsl_erf.pxd ├── gsl_gegenbauer.pxd ├── gsl_blas_types.pxd ├── gsl_coupling.pxd ├── gsl_ntuple.pxd ├── gsl_splinalg.pxd ├── gsl_expint.pxd ├── gsl_chebyshev.pxd ├── gsl_fermi_dirac.pxd ├── gsl_exp.pxd ├── gsl_combination.pxd ├── gsl_coulomb.pxd ├── gsl_poly.pxd ├── gsl_rstat.pxd ├── gsl_sort.pxd ├── gsl_block.pxd ├── gsl_dht.pxd ├── gsl_eigen.pxd ├── gsl_trig.pxd ├── gsl_fit.pxd ├── gsl_ellint.pxd ├── gsl_hyperg.pxd ├── gsl_monte.pxd ├── gsl_sum.pxd ├── __init__.py ├── gsl_airy.pxd ├── gsl_roots.pxd ├── gsl_permutation.pxd ├── gsl_math.pxd ├── gsl_min.pxd ├── gsl_spmatrix.pxd ├── gsl_gamma.pxd ├── gsl_odeiv.pxd ├── gsl_legendre.pxd ├── gsl_vector_complex.pxd ├── gsl_rng.pxd ├── gsl_vector.pxd ├── gsl_integration.pxd ├── gsl.pxd ├── gsl_statistics.pxd └── gsl_interp.pxd ├── examples ├── erf.pyx ├── exp.pyx ├── log.pyx ├── trig.pyx ├── dawson.pyx ├── debye.pyx ├── dilog.pyx ├── gamma.pyx ├── psi.pyx ├── zeta.pyx ├── expint.pyx ├── lambert.pyx ├── pow_int.pyx ├── hyperg.pyx ├── legendre.pyx ├── transport.pyx ├── fermi_dirac.pyx ├── gegenbauer.pyx ├── laguerre.pyx ├── synchrotron.pyx ├── airy.pyx ├── clausen.pyx ├── normal.pyx ├── math.pyx ├── ellint.pyx ├── complex.pyx ├── elljac.pyx ├── coupling.pyx ├── element.pyx ├── qrng.pyx ├── bessel.pyx ├── coulomb.pyx ├── rng.pyx ├── blas1.pyx ├── permutation2.pyx ├── random1.pyx ├── random2.pyx ├── poly.pyx ├── fft1.pyx ├── combination.pyx ├── multinomial.pyx ├── diff.pyx ├── chebyshev.pyx ├── statistics1.pyx ├── interp.pyx ├── sort.pyx ├── integration.pyx ├── permutation1.pyx ├── example_Sconscript ├── blas2.pyx ├── integrate.pyx ├── statistics2.pyx ├── fft3.pyx ├── fft2.pyx ├── gibbs.pyx ├── sum.pyx ├── linalg.pyx ├── fit.pyx ├── eigen.pyx ├── setup_integrate.py ├── ntuple.pyx ├── setup.py ├── min.pyx ├── odeiv2.pyx ├── sparse.pyx ├── odeiv.pyx ├── monte.pyx └── multimin.pyx ├── MANIFEST.in ├── CHANGES.rst ├── LICENSE ├── setup.py ├── setup_test.py ├── setup_interface.py └── README.rst /cython_gsl/interface/__init__.py: -------------------------------------------------------------------------------- 1 | #empty 2 | -------------------------------------------------------------------------------- /cython_gsl/test/__init__.py: -------------------------------------------------------------------------------- 1 | #__init__.py 2 | -------------------------------------------------------------------------------- /examples/erf.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | print gsl_sf_erf(0.5) 5 | -------------------------------------------------------------------------------- /examples/exp.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | print gsl_sf_exp(0.5) 5 | -------------------------------------------------------------------------------- /examples/log.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | print gsl_sf_log(0.5) 5 | -------------------------------------------------------------------------------- /examples/trig.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | print gsl_sf_sin(0.5) 5 | -------------------------------------------------------------------------------- /examples/dawson.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | print gsl_sf_dawson(0.1) 5 | -------------------------------------------------------------------------------- /examples/debye.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | print gsl_sf_debye_1(0.1) 5 | -------------------------------------------------------------------------------- /examples/dilog.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | print gsl_sf_dilog(0.1) 5 | -------------------------------------------------------------------------------- /examples/gamma.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | print gsl_sf_gamma(0.5) 5 | -------------------------------------------------------------------------------- /examples/psi.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | print gsl_sf_psi_int(4) 5 | -------------------------------------------------------------------------------- /examples/zeta.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | print gsl_sf_zeta_int(3) 5 | -------------------------------------------------------------------------------- /examples/expint.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | print gsl_sf_expint_E1(0.5) 5 | -------------------------------------------------------------------------------- /examples/lambert.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | print gsl_sf_lambert_W0(0.5) 5 | -------------------------------------------------------------------------------- /examples/pow_int.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | print gsl_sf_pow_int(0.5,3) 5 | -------------------------------------------------------------------------------- /examples/hyperg.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | print gsl_sf_hyperg_0F1(0.5, 0.6) 5 | -------------------------------------------------------------------------------- /examples/legendre.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | print gsl_sf_legendre_P1(0.5) 5 | -------------------------------------------------------------------------------- /examples/transport.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | print gsl_sf_transport_2(0.5) 5 | -------------------------------------------------------------------------------- /examples/fermi_dirac.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | print gsl_sf_fermi_dirac_m1(0.5) 5 | -------------------------------------------------------------------------------- /examples/gegenbauer.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | print gsl_sf_gegenpoly_1(0.5, 0.3) 5 | -------------------------------------------------------------------------------- /examples/laguerre.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | print gsl_sf_laguerre_1(0.5, 0.6) 5 | -------------------------------------------------------------------------------- /examples/synchrotron.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | print gsl_sf_synchrotron_1(0.5) 5 | -------------------------------------------------------------------------------- /examples/airy.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | print "%.15f\n" % gsl_sf_airy_Ai(0, GSL_PREC_DOUBLE) 5 | -------------------------------------------------------------------------------- /examples/clausen.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | cdef double x 5 | x = 1.7 6 | print gsl_sf_clausen(x) 7 | -------------------------------------------------------------------------------- /examples/normal.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl import gsl 2 | 3 | cpdef Normal(double x, double sigma): 4 | return gsl.gsl_ran_gaussian_pdf(x, sigma) 5 | -------------------------------------------------------------------------------- /examples/math.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | x = 1.0e-12 5 | cdef int e 6 | print gsl_frexp(x, &e) 7 | print e 8 | -------------------------------------------------------------------------------- /examples/ellint.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | x = 0.6 5 | r = gsl_sf_ellint_Kcomp(x, GSL_PREC_DOUBLE) 6 | print "%.18f\n" % r 7 | -------------------------------------------------------------------------------- /cython_gsl/gsl_elljac.pxd: -------------------------------------------------------------------------------- 1 | cdef extern from "gsl/gsl_sf_elljac.h": 2 | 3 | int gsl_sf_elljac_e(double u, double m, double * sn, double * cn, double * dn) nogil 4 | 5 | -------------------------------------------------------------------------------- /examples/complex.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | cdef gsl_complex c2 5 | GSL_SET_COMPLEX(&c2, 2.2, 1.5) 6 | print 'c2', GSL_REAL(c2) 7 | -------------------------------------------------------------------------------- /examples/elljac.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | cdef double sn, cn, dn 5 | gsl_sf_elljac_e(10.2, 0.2, &sn, &cn, &dn) 6 | print sn, cn, dn 7 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include GPLv3.txt README.rst CHANGES.rst LICENSE examples/*.py examples/*.pyx cython_gsl/test/*.pyx cython_gsl/test/*.py setup.py setup_test.py cython_gsl/interface/* 2 | -------------------------------------------------------------------------------- /examples/coupling.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | x = [2, 2, 4, 0,0,0] 5 | r = gsl_sf_coupling_3j(x[0], x[1],x[2],x[3],x[4],x[5]) 6 | print r 7 | -------------------------------------------------------------------------------- /examples/element.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | x = 0.5; y = 0.9 5 | cdef gsl_sf_result res 6 | print gsl_sf_multiply_e(x,y, &res) 7 | print res.val, res.err 8 | -------------------------------------------------------------------------------- /cython_gsl/gsl_dawson.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_sf_dawson.h": 4 | 5 | double gsl_sf_dawson(double x) nogil 6 | 7 | int gsl_sf_dawson_e(double x, gsl_sf_result * result) nogil 8 | 9 | -------------------------------------------------------------------------------- /cython_gsl/gsl_clausen.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_sf_clausen.h": 4 | 5 | double gsl_sf_clausen(double x) nogil 6 | 7 | int gsl_sf_clausen_e(double x, gsl_sf_result * result) nogil 8 | 9 | -------------------------------------------------------------------------------- /cython_gsl/gsl_mode.pxd: -------------------------------------------------------------------------------- 1 | cdef extern from "gsl/gsl_mode.h": 2 | ctypedef unsigned int gsl_mode_t 3 | gsl_mode_t GSL_PREC_DOUBLE 4 | 5 | gsl_mode_t GSL_PREC_SINGLE 6 | 7 | gsl_mode_t GSL_PREC_APPROX 8 | 9 | 10 | -------------------------------------------------------------------------------- /cython_gsl/gsl_pow_int.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_sf_pow_int.h": 4 | 5 | double gsl_sf_pow_int(double x, int n) nogil 6 | 7 | int gsl_sf_pow_int_e(double x, int n, gsl_sf_result * result) nogil 8 | 9 | -------------------------------------------------------------------------------- /cython_gsl/gsl_sf_result.pxd: -------------------------------------------------------------------------------- 1 | cdef extern from "gsl/gsl_sf_result.h": 2 | ctypedef struct gsl_sf_result: 3 | double val 4 | double err 5 | 6 | 7 | ctypedef struct gsl_sf_result_e10: 8 | double val 9 | double err 10 | int e10 11 | 12 | -------------------------------------------------------------------------------- /cython_gsl/test/test_errno.py: -------------------------------------------------------------------------------- 1 | import unittest, gslerrno 2 | 3 | class ErrnoTest(unittest.TestCase): 4 | 5 | def test_gsl_matrix_alloc_fail(self): 6 | t = gslerrno.t_gsl_matrix_alloc_fail() 7 | 8 | if __name__ == '__main__': 9 | unittest.main() 10 | -------------------------------------------------------------------------------- /cython_gsl/gsl_elementary.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_sf_elementary.h": 4 | 5 | int gsl_sf_multiply_e(double x, double y, gsl_sf_result * result) nogil 6 | 7 | int gsl_sf_multiply_err_e(double x, double dx, double y, double dy, gsl_sf_result * result) nogil 8 | 9 | -------------------------------------------------------------------------------- /examples/qrng.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | cdef int i 5 | cdef gsl_qrng * q 6 | q = gsl_qrng_alloc (gsl_qrng_sobol, 2) 7 | cdef double v[2] 8 | for i from 0 <= i < 1024: 9 | gsl_qrng_get (q, v) 10 | print "%.5f %.5f\n" % (v[0], v[1]) 11 | 12 | gsl_qrng_free (q) 13 | -------------------------------------------------------------------------------- /cython_gsl/test/gslerrno.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | from cython_gsl.gsl_errno cimport * 3 | 4 | 5 | def t_gsl_matrix_alloc_fail(): 6 | cdef gsl_matrix * m 7 | h = gsl_set_error_handler_off() 8 | #This will fail b/c cannot alloc dimension of 0 9 | m = gsl_matrix_alloc (0, 3) 10 | gsl_set_error_handler(h) 11 | 12 | -------------------------------------------------------------------------------- /examples/bessel.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | cdef double x 5 | x = 5.0 6 | cdef gsl_sf_result result 7 | cdef int status 8 | status = gsl_sf_bessel_J0_e (x, &result) 9 | print "J0(5.0) = %.18f\n +/- % .18f\n" % (result.val, result.err) 10 | print "%.18f\n" % gsl_sf_bessel_J0 (5.0) 11 | -------------------------------------------------------------------------------- /cython_gsl/gsl_dilog.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_sf_dilog.h": 4 | 5 | double gsl_sf_dilog(double x) nogil 6 | 7 | int gsl_sf_dilog_e(double x, gsl_sf_result * result) nogil 8 | 9 | int gsl_sf_complex_dilog_e(double r, double theta, gsl_sf_result * result_re, gsl_sf_result * result_im) nogil 10 | 11 | -------------------------------------------------------------------------------- /cython_gsl/gsl_errno.pxd: -------------------------------------------------------------------------------- 1 | cdef extern from "gsl/gsl_errno.h": 2 | ctypedef void gsl_error_handler_t(const char * reason, const char * file, 3 | int line, int gsl_errno) 4 | gsl_error_handler_t * gsl_set_error_handler (gsl_error_handler_t * new_handler) nogil 5 | gsl_error_handler_t * gsl_set_error_handler_off () nogil 6 | -------------------------------------------------------------------------------- /cython_gsl/gsl_lambert.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_sf_lambert.h": 4 | 5 | double gsl_sf_lambert_W0(double x) nogil 6 | 7 | int gsl_sf_lambert_W0_e(double x, gsl_sf_result * result) nogil 8 | 9 | double gsl_sf_lambert_Wm1(double x) nogil 10 | 11 | int gsl_sf_lambert_Wm1_e(double x, gsl_sf_result * result) nogil 12 | 13 | -------------------------------------------------------------------------------- /examples/coulomb.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | x = [2.2, 0.4] 5 | Z = x[0]; r = x[1] 6 | res = gsl_sf_hydrogenicR_1(Z, r) 7 | print res 8 | 9 | ''' 10 | puts "Normalized Hydrogenic Bound States" 11 | x = [2.2, 0.4]; Z = x[0]; r = x[1] 12 | res = Coulomb::hydrogenicR_1(*x) 13 | assert res =~ (2 * Z * sqrt(Z) * exp(-Z*r)) 14 | ''' 15 | -------------------------------------------------------------------------------- /cython_gsl/gsl_synchrotron.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_sf_synchrotron.h": 4 | 5 | double gsl_sf_synchrotron_1(double x) nogil 6 | 7 | int gsl_sf_synchrotron_1_e(double x, gsl_sf_result * result) nogil 8 | 9 | double gsl_sf_synchrotron_2(double x) nogil 10 | 11 | int gsl_sf_synchrotron_2_e(double x, gsl_sf_result * result) nogil 12 | 13 | -------------------------------------------------------------------------------- /examples/rng.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | cdef gsl_rng_type * T 5 | cdef gsl_rng * r 6 | 7 | cdef int i, n 8 | n = 10 9 | 10 | gsl_rng_env_setup() 11 | T = gsl_rng_default 12 | r = gsl_rng_alloc (T) 13 | 14 | cdef double u 15 | for i from 0 <= i < n: 16 | u = gsl_rng_uniform (r) 17 | print "%.5f\n" % u 18 | 19 | gsl_rng_free (r) 20 | -------------------------------------------------------------------------------- /examples/blas1.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | cdef int i 5 | cdef double res 6 | cdef gsl_vector *v1, *v2 7 | v1 = gsl_vector_alloc(10) 8 | v2 = gsl_vector_alloc(10) 9 | 10 | for i from 0 <= i < 10: 11 | gsl_vector_set (v1, i, 1) 12 | 13 | for i from 0 <= i < 10: 14 | gsl_vector_set (v2, i, 2) 15 | 16 | gsl_blas_ddot(v1, v2, &res) 17 | print res 18 | -------------------------------------------------------------------------------- /examples/permutation2.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | cdef gsl_permutation * p 5 | p = gsl_permutation_alloc (3) 6 | gsl_permutation_init (p) 7 | gsl_permutation_fprintf (stdout, p, " %u") 8 | print 9 | # GSL_SUCCESS = 0 10 | while (gsl_permutation_next(p) == GSL_SUCCESS): 11 | gsl_permutation_fprintf (stdout, p, " %u") 12 | print 13 | gsl_permutation_fprintf (stdout, p, " %u") 14 | print 15 | -------------------------------------------------------------------------------- /examples/random1.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | cdef gsl_rng_type * T 5 | cdef gsl_rng * r 6 | cdef int i, n 7 | cdef double mu 8 | n = 10 9 | mu = 3.0 10 | 11 | 12 | gsl_rng_env_setup() 13 | 14 | T = gsl_rng_default 15 | r = gsl_rng_alloc (T) 16 | 17 | cdef unsigned int k 18 | for i from 0 <= i < n: 19 | k = gsl_ran_poisson (r, mu) 20 | print " %u" % k, 21 | 22 | print "\n" 23 | -------------------------------------------------------------------------------- /cython_gsl/gsl_diff.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_diff.h": 4 | int gsl_diff_central ( gsl_function *f, double x, 5 | double *result, double *abserr) nogil 6 | 7 | int gsl_diff_backward ( gsl_function *f, double x, 8 | double *result, double *abserr) nogil 9 | 10 | int gsl_diff_forward ( gsl_function *f, double x, 11 | double *result, double *abserr) nogil 12 | 13 | -------------------------------------------------------------------------------- /examples/random2.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | cdef double P, Q 5 | cdef double x 6 | x = 2.0 7 | 8 | P = gsl_cdf_ugaussian_P (x) 9 | print "prob(x < %f) = %f\n" % (x, P) 10 | 11 | Q = gsl_cdf_ugaussian_Q (x) 12 | print "prob(x < %f) = %f\n" % (x, Q) 13 | 14 | x = gsl_cdf_ugaussian_Pinv (P) 15 | print "Pinv(%f) = %f\n" % (P, x) 16 | 17 | x = gsl_cdf_ugaussian_Qinv (Q) 18 | print "Qinv(%f) = %f\n" % (Q, x) 19 | -------------------------------------------------------------------------------- /examples/poly.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | cdef int i 5 | cdef double a[6] 6 | cdef double z[10] 7 | cdef gsl_poly_complex_workspace * w 8 | a[0] = -1 9 | a[1] = a[2] = a[3] = a[4] = 0 10 | a[5] = 1 11 | w = gsl_poly_complex_workspace_alloc (6) 12 | gsl_poly_complex_solve (a, 6, w, z) 13 | gsl_poly_complex_workspace_free (w) 14 | 15 | for i from 0 <= i < 5: 16 | print "z%d = %+.18f %+.18f\n" % (i, z[2*i], z[2*i+1]) 17 | -------------------------------------------------------------------------------- /cython_gsl/test/test_spblas.py: -------------------------------------------------------------------------------- 1 | import unittest, spblas 2 | 3 | class SpblasTest(unittest.TestCase): 4 | def test_gsl_spblas_dgemm(self): 5 | t = spblas.t_gsl_spblas_dgemm() 6 | expected = [[0.0, 0.0, 0.0, 4.0], [0.0, 0.0, 15.0, 18.0]] 7 | self.assertListEqual(t[0], expected[0]) 8 | self.assertListEqual(t[1], expected[1]) 9 | 10 | def test_gsl_spblas_dgemv(self): 11 | t = spblas.t_gsl_spblas_dgemv() 12 | expected = [16.0, 15.0] 13 | self.assertListEqual(t, expected) 14 | 15 | if __name__ == '__main__': 16 | unittest.main() 17 | -------------------------------------------------------------------------------- /cython_gsl/gsl_debye.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_sf_debye.h": 4 | 5 | double gsl_sf_debye_1(double x) nogil 6 | 7 | int gsl_sf_debye_1_e(double x, gsl_sf_result * result) nogil 8 | 9 | double gsl_sf_debye_2(double x) nogil 10 | 11 | int gsl_sf_debye_2_e(double x, gsl_sf_result * result) nogil 12 | 13 | double gsl_sf_debye_3(double x) nogil 14 | 15 | int gsl_sf_debye_3_e(double x, gsl_sf_result * result) nogil 16 | 17 | double gsl_sf_debye_4(double x) nogil 18 | 19 | int gsl_sf_debye_4_e(double x, gsl_sf_result * result) nogil 20 | 21 | -------------------------------------------------------------------------------- /examples/fft1.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | 4 | def main(): 5 | cdef int i 6 | cdef double data[2*128] 7 | 8 | for i from 0 <= i < 128: 9 | data[2*i] = 0.0 10 | data[2*i+1] = 0.0 11 | 12 | data[0] = 1.0 13 | 14 | for i from 1 <= i <= 10: 15 | data[2*i] = data[2*(128-i)] = 1.0 16 | 17 | for i from 0 <= i < 128: 18 | print "%d %e %e\n" %(i, data[2*i], data[2*i+1]) 19 | 20 | gsl_fft_complex_radix2_forward (data, 1, 128) 21 | 22 | for i from 0 <= i < 128: 23 | print "%d %e %e\n" %(i, data[2*i]/sqrt(128), data[2*i+1]/sqrt(128)) 24 | -------------------------------------------------------------------------------- /CHANGES.rst: -------------------------------------------------------------------------------- 1 | Version history 2 | =============== 3 | 4 | 0.2.1 (Nov 12 2012) 5 | =================== 6 | 7 | * New example Gibbs sampler (Flavio Coelho) 8 | * Ported sage interface to probability distributions 9 | * Added odeiv2 support (Benni Hepp) 10 | 11 | 0.2 (May 13 2012) 12 | ================= 13 | 14 | * Added Unittests (integrated from pyrexgsl) 15 | * Added more examples (integrated from pyrexgsl) 16 | 17 | 0.1.2 18 | ===== 19 | 20 | * Better setup.py interface 21 | 22 | 0.1.1 23 | ===== 24 | 25 | * Install CythonGSL so that it can be cimported reliably. 26 | 27 | 0.1 28 | === 29 | 30 | * First public release 31 | -------------------------------------------------------------------------------- /cython_gsl/gsl_transport.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_sf_transport.h": 4 | 5 | double gsl_sf_transport_2(double x) nogil 6 | 7 | int gsl_sf_transport_2_e(double x, gsl_sf_result * result) nogil 8 | 9 | double gsl_sf_transport_3(double x) nogil 10 | 11 | int gsl_sf_transport_3_e(double x, gsl_sf_result * result) nogil 12 | 13 | double gsl_sf_transport_4(double x) nogil 14 | 15 | int gsl_sf_transport_4_e(double x, gsl_sf_result * result) nogil 16 | 17 | double gsl_sf_transport_5(double x) nogil 18 | 19 | int gsl_sf_transport_5_e(double x, gsl_sf_result * result) nogil 20 | 21 | -------------------------------------------------------------------------------- /examples/combination.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | cdef gsl_combination * c 5 | cdef size_t i 6 | print "All subsets of {0,1,2,3} by size:\n" 7 | for i from 0 <= i <= 4: 8 | c = gsl_combination_calloc (4, i) 9 | print "{", 10 | gsl_combination_fprintf (stdout, c, " %u") 11 | print "}" 12 | while(gsl_combination_next (c) == 0): # GSL_SUCCESS = 0 13 | print "{", 14 | gsl_combination_fprintf (stdout, c, " %u") 15 | print "}" 16 | print "{", 17 | gsl_combination_fprintf (stdout, c, " %u") 18 | print "}" 19 | gsl_combination_free (c) 20 | -------------------------------------------------------------------------------- /cython_gsl/gsl_spblas.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_spblas.h": 4 | int gsl_spblas_dgemv(const CBLAS_TRANSPOSE_t TransA, const double alpha, 5 | const gsl_spmatrix *A, const gsl_vector *x, 6 | const double beta, gsl_vector *y) nogil 7 | 8 | int gsl_spblas_dgemm(const double alpha, const gsl_spmatrix *A, 9 | const gsl_spmatrix *B, gsl_spmatrix *C) nogil 10 | 11 | size_t gsl_spblas_scatter(const gsl_spmatrix *A, const size_t j, 12 | const double alpha, size_t *w, double *x, 13 | const size_t mark, gsl_spmatrix *C, size_t nz) nogil 14 | -------------------------------------------------------------------------------- /examples/multinomial.pyx: -------------------------------------------------------------------------------- 1 | cimport cython 2 | from cython_gsl cimport * 3 | 4 | import numpy as np 5 | from numpy cimport * 6 | 7 | cdef gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937) 8 | 9 | def multinomial(ndarray[double, ndim=1] p, unsigned int N): 10 | cdef: 11 | size_t K = p.shape[0] 12 | ndarray[uint32_t, ndim=1] n = np.empty_like(p, dtype='uint32') 13 | 14 | # void gsl_ran_multinomial (const gsl_rng * r, size_t K, unsigned int N, const double p[], unsigned int n[]) 15 | gsl_ran_multinomial(r, K, N, p.data, n.data) 16 | 17 | return n 18 | 19 | print multinomial(np.array([.2, .2, .2, .2, .2], dtype='double'), 500) 20 | -------------------------------------------------------------------------------- /examples/diff.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef double f (double x, void * params) nogil: 4 | return pow (x, 1.5) 5 | 6 | def main (): 7 | cdef gsl_function F 8 | cdef double result, abserr 9 | 10 | F.function = &f 11 | F.params = NULL 12 | 13 | print "f(x) = x^(3/2)\n" 14 | 15 | gsl_diff_central (&F, 2.0, &result, &abserr) 16 | print "x = 2.0\n" 17 | print "f'(x) = %.10f +/- %.5f\n" % (result, abserr) 18 | print "exact = %.10f\n\n" % (1.5 * sqrt(2.0)) 19 | 20 | gsl_diff_forward (&F, 0.0, &result, &abserr) 21 | print "x = 0.0\n" 22 | print "f'(x) = %.10f +/- %.5f\n" % (result, abserr) 23 | print "exact = %.10f\n" % 0.0 24 | -------------------------------------------------------------------------------- /cython_gsl/gsl_log.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_sf_log.h": 4 | 5 | double gsl_sf_log(double x) nogil 6 | 7 | int gsl_sf_log_e(double x, gsl_sf_result * result) nogil 8 | 9 | double gsl_sf_log_abs(double x) nogil 10 | 11 | int gsl_sf_log_abs_e(double x, gsl_sf_result * result) nogil 12 | 13 | int gsl_sf_complex_log_e(double zr, double zi, gsl_sf_result * lnr, gsl_sf_result * theta) nogil 14 | 15 | double gsl_sf_log_1plusx(double x) nogil 16 | 17 | int gsl_sf_log_1plusx_e(double x, gsl_sf_result * result) nogil 18 | 19 | double gsl_sf_log_1plusx_mx(double x) nogil 20 | 21 | int gsl_sf_log_1plusx_mx_e(double x, gsl_sf_result * result) nogil 22 | 23 | -------------------------------------------------------------------------------- /cython_gsl/gsl_zeta.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_sf_zeta.h": 4 | 5 | double gsl_sf_zeta_int(int n) nogil 6 | 7 | int gsl_sf_zeta_int_e(int n, gsl_sf_result * result) nogil 8 | 9 | double gsl_sf_zeta(double s) nogil 10 | 11 | int gsl_sf_zeta_e(double s, gsl_sf_result * result) nogil 12 | 13 | double gsl_sf_hzeta(double s, double q) nogil 14 | 15 | int gsl_sf_hzeta_e(double s, double q, gsl_sf_result * result) nogil 16 | 17 | double gsl_sf_eta_int(int n) nogil 18 | 19 | int gsl_sf_eta_int_e(int n, gsl_sf_result * result) nogil 20 | 21 | double gsl_sf_eta(double s) nogil 22 | 23 | int gsl_sf_eta_e(double s, gsl_sf_result * result) nogil 24 | 25 | -------------------------------------------------------------------------------- /cython_gsl/gsl_psi.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_sf_psi.h": 4 | 5 | double gsl_sf_psi_int(int n) nogil 6 | 7 | int gsl_sf_psi_int_e(int n, gsl_sf_result * result) nogil 8 | 9 | double gsl_sf_psi(double x) nogil 10 | 11 | int gsl_sf_psi_e(double x, gsl_sf_result * result) nogil 12 | 13 | double gsl_sf_psi_1piy(double y) nogil 14 | 15 | int gsl_sf_psi_1piy_e(double y, gsl_sf_result * result) nogil 16 | 17 | double gsl_sf_psi_1_int(int n) nogil 18 | 19 | int gsl_sf_psi_1_int_e(int n, gsl_sf_result * result) nogil 20 | 21 | double gsl_sf_psi_n(int m, double x) nogil 22 | 23 | int gsl_sf_psi_n_e(int m, double x, gsl_sf_result * result) nogil 24 | 25 | -------------------------------------------------------------------------------- /cython_gsl/gsl_laguerre.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_sf_laguerre.h": 4 | 5 | double gsl_sf_laguerre_1(double a, double x) nogil 6 | 7 | double gsl_sf_laguerre_2(double a, double x) nogil 8 | 9 | double gsl_sf_laguerre_3(double a, double x) nogil 10 | 11 | int gsl_sf_laguerre_1_e(double a, double x, gsl_sf_result * result) nogil 12 | 13 | int gsl_sf_laguerre_2_e(double a, double x, gsl_sf_result * result) nogil 14 | 15 | int gsl_sf_laguerre_3_e(double a, double x, gsl_sf_result * result) nogil 16 | 17 | double gsl_sf_laguerre_n(int n, double a, double x) nogil 18 | 19 | int gsl_sf_laguerre_n_e(int n, double a, double x, gsl_sf_result * result) nogil 20 | 21 | -------------------------------------------------------------------------------- /examples/chebyshev.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef double f (double x, void *p) nogil: 4 | if (x < 0.5): 5 | return 0.25 6 | else: 7 | return 0.75 8 | 9 | 10 | def main (): 11 | cdef int i, n 12 | n = 10000 13 | 14 | cdef gsl_cheb_series *cs 15 | cs = gsl_cheb_alloc (40) 16 | 17 | cdef gsl_function F 18 | 19 | F.function = f 20 | F.params = NULL 21 | 22 | gsl_cheb_init (cs, &F, 0.0, 1.0) 23 | 24 | cdef double x, r10, r40 25 | for i from 0 <= i < n: 26 | x = i / n 27 | r10 = gsl_cheb_eval_n (cs, 10, x) 28 | r40 = gsl_cheb_eval (cs, x) 29 | print "%g %g %g %g\n" % \ 30 | (x, GSL_FN_EVAL (&F, x), r10, r40) 31 | 32 | gsl_cheb_free (cs) 33 | -------------------------------------------------------------------------------- /cython_gsl/gsl_qrng.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_qrng.h": 4 | 5 | ctypedef struct gsl_qrng 6 | ctypedef struct gsl_qrng_type 7 | gsl_qrng_type * gsl_qrng_niederreiter_2 8 | gsl_qrng_type * gsl_qrng_sobol 9 | 10 | gsl_qrng * gsl_qrng_alloc(gsl_qrng_type * T, unsigned int d) nogil 11 | 12 | void gsl_qrng_free(gsl_qrng * q) nogil 13 | 14 | void gsl_qrng_init(gsl_qrng * q) nogil 15 | 16 | int gsl_qrng_get(gsl_qrng * q, double x[]) nogil 17 | 18 | char * gsl_qrng_name(gsl_qrng * q) nogil 19 | 20 | size_t gsl_qrng_size(gsl_qrng * q) nogil 21 | 22 | void * gsl_qrng_state(gsl_qrng * q) nogil 23 | 24 | int gsl_qrng_memcpy(gsl_qrng * dest, gsl_qrng * src) nogil 25 | 26 | gsl_qrng * gsl_qrng_clone(gsl_qrng * q) nogil 27 | 28 | -------------------------------------------------------------------------------- /examples/statistics1.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | cdef double data[5] 5 | data[0] = 17.2 6 | data[1] = 18.1 7 | data[2] = 16.5 8 | data[3] = 18.3 9 | data[4] = 12.6 10 | cdef double mean, variance, largest, smallest 11 | 12 | mean = gsl_stats_mean(data, 1, 5) 13 | variance = gsl_stats_variance(data, 1, 5) 14 | largest = gsl_stats_max(data, 1, 5) 15 | smallest = gsl_stats_min(data, 1, 5) 16 | 17 | print "The dataset is %g, %g, %g, %g, %g\n" % \ 18 | (data[0], data[1], data[2], data[3], data[4]) 19 | 20 | print "The sample mean is %g\n" % mean 21 | print"The estimated variance is %g\n" % variance 22 | print"The largest value is %g\n" % largest 23 | print"The smallest value is %g\n" % smallest 24 | -------------------------------------------------------------------------------- /cython_gsl/test/test_eigen.py: -------------------------------------------------------------------------------- 1 | import unittest, eigen, os 2 | 3 | class EigenTest(unittest.TestCase): 4 | 5 | def test_gsl_eigen_symm(self): 6 | t = eigen.t_gsl_eigen_symm() 7 | for x in t: 8 | self.assertAlmostEqual(x,0, 15) 9 | 10 | def test_gsl_eigen_symmv(self): 11 | t = eigen.t_gsl_eigen_symmv() 12 | for x in t: 13 | self.assertAlmostEqual(x,0, 15) 14 | 15 | def test_gsl_eigen_herm(self): 16 | t = eigen.t_gsl_eigen_herm() 17 | for x in t: 18 | self.assertAlmostEqual(x,0, 15) 19 | 20 | def test_gsl_eigen_hermv(self): 21 | t = eigen.t_gsl_eigen_hermv() 22 | for x in t: 23 | self.assertAlmostEqual(x,0, 15) 24 | 25 | if __name__ == '__main__': 26 | unittest.main() 27 | -------------------------------------------------------------------------------- /examples/interp.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main (): 4 | cdef int i 5 | cdef double xi, yi, x[10], y[10] 6 | 7 | print "#m=0,S=2\n" 8 | 9 | for i from 0 <= i < 10: 10 | x[i] = i + 0.5 * sin (i) 11 | y[i] = i + cos (i * i) 12 | print "%g %g\n" %(x[i], y[i]) 13 | 14 | print "#m=1,S=0\n" 15 | 16 | cdef gsl_interp_accel *acc 17 | acc = gsl_interp_accel_alloc () 18 | cdef gsl_spline *spline 19 | spline = gsl_spline_alloc (gsl_interp_cspline, 10) 20 | 21 | gsl_spline_init (spline, x, y, 10) 22 | 23 | xi = x[0] 24 | while (xi < x[9]): 25 | yi = gsl_spline_eval (spline, xi, acc) 26 | print "%g %g\n" %(xi, yi) 27 | xi = xi + 0.01 28 | 29 | gsl_spline_free (spline) 30 | gsl_interp_accel_free (acc) 31 | -------------------------------------------------------------------------------- /examples/sort.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "stdlib.h": 4 | void *malloc(size_t size) 5 | int free(void*) 6 | int sizeof() 7 | 8 | def main(): 9 | cdef gsl_rng_type * T 10 | cdef gsl_rng * r 11 | 12 | cdef size_t i, k, N 13 | k = 5 14 | N= 100000 15 | cdef double * x, * small 16 | x = malloc (N * sizeof(double)) 17 | small = malloc (k * sizeof(double)) 18 | 19 | gsl_rng_env_setup() 20 | 21 | T = gsl_rng_default 22 | r = gsl_rng_alloc (T) 23 | 24 | for i from 0 <= i < N: 25 | x[i] = gsl_rng_uniform(r) 26 | 27 | gsl_sort_smallest (small, k, x, 1, N) 28 | 29 | print "%d smallest values from %d" %(k, N) 30 | 31 | for i from 0 <= i < k: 32 | print "%d: %.18f\n" %(i, small[i]), 33 | -------------------------------------------------------------------------------- /cython_gsl/gsl_erf.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_sf_erf.h": 4 | 5 | double gsl_sf_erf(double x) nogil 6 | 7 | int gsl_sf_erf_e(double x, gsl_sf_result * result) nogil 8 | 9 | double gsl_sf_erfc(double x) nogil 10 | 11 | int gsl_sf_erfc_e(double x, gsl_sf_result * result) nogil 12 | 13 | double gsl_sf_log_erfc(double x) nogil 14 | 15 | int gsl_sf_log_erfc_e(double x, gsl_sf_result * result) nogil 16 | 17 | double gsl_sf_erf_Z(double x) nogil 18 | 19 | int gsl_sf_erf_Z_e(double x, gsl_sf_result * result) nogil 20 | 21 | double gsl_sf_erf_Q(double x) nogil 22 | 23 | int gsl_sf_erf_Q_e(double x, gsl_sf_result * result) nogil 24 | 25 | double gsl_sf_hazard(double x) nogil 26 | 27 | int gsl_sf_hazard_e(double x, gsl_sf_result * result) nogil 28 | 29 | -------------------------------------------------------------------------------- /examples/integration.pyx: -------------------------------------------------------------------------------- 1 | #cython: cdivision=True 2 | 3 | from cython_gsl cimport * 4 | 5 | ctypedef double * double_ptr 6 | ctypedef void * void_ptr 7 | 8 | cdef double foo(double x, void * params) nogil: 9 | cdef double alpha, f 10 | alpha = ( params)[0] 11 | f = log(alpha*x) / sqrt(x) 12 | return f 13 | 14 | 15 | def main(): 16 | cdef gsl_integration_workspace * w 17 | cdef double result, error, expected, alpha 18 | w = gsl_integration_workspace_alloc (1000) 19 | 20 | expected = -4.0 21 | alpha = 1 22 | 23 | cdef gsl_function F 24 | F.function = &foo 25 | F.params = &alpha 26 | 27 | gsl_integration_qags (&F, 0, 1, 0, 1e-7, 1000, w, &result, &error) 28 | print "result = % .18f\n" % result 29 | print "estimated error = % .18f\n" % error 30 | -------------------------------------------------------------------------------- /examples/permutation1.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | 5 | N = 10 6 | cdef gsl_rng_type * T 7 | cdef gsl_rng * r 8 | 9 | cdef gsl_permutation * p, * q 10 | p = gsl_permutation_alloc (N) 11 | q = gsl_permutation_alloc (N) 12 | 13 | gsl_rng_env_setup() 14 | T = gsl_rng_default 15 | r = gsl_rng_alloc (T) 16 | 17 | print "initial permutation:" 18 | gsl_permutation_init (p) 19 | gsl_permutation_fprintf (stdout, p, " %u") 20 | print "\n" 21 | 22 | print " random permutation:" 23 | gsl_ran_shuffle (r, p.data, N, sizeof(size_t)) 24 | gsl_permutation_fprintf (stdout, p, " %u") 25 | print "\n" 26 | 27 | print "inverse permutation:" 28 | gsl_permutation_inverse (q, p) 29 | gsl_permutation_fprintf (stdout, q, " %u") 30 | print "\n" 31 | -------------------------------------------------------------------------------- /cython_gsl/test/test_multifit_nlin.py: -------------------------------------------------------------------------------- 1 | import unittest, multifit_nlin 2 | 3 | 4 | class MultifitNlinTest(unittest.TestCase): 5 | 6 | def test_multifit_nlin_example(self): 7 | """Tests implementation of nonlinear least-squares fitting 8 | against an example provided by GSL docs. 9 | 10 | The docs is available at: 11 | http://www.gnu.org/software/gsl/manual/html_node/Example-programs-for-Nonlinear-Least_002dSquares-Fitting.html#Example-programs-for-Nonlinear-Least_002dSquares-Fitting 12 | """ 13 | 14 | A, lambd, b = multifit_nlin.t_gsl_multifit_nlin_example() 15 | self.assertAlmostEqual(A, 5.04536, 5) 16 | self.assertAlmostEqual(lambd, 0.10405, 5) 17 | self.assertAlmostEqual(b, 1.01925, 5) 18 | 19 | 20 | if __name__ == '__main__': 21 | unittest.main() 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | CythonGSL provides a set of Cython declarations for the GNU Scientific Library (GSL). 2 | Copyright (C) 2012 Thomas V. Wiecki 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | -------------------------------------------------------------------------------- /examples/example_Sconscript: -------------------------------------------------------------------------------- 1 | # This example was provided by Flávio Codeço Coelho 2 | 3 | env = Environment() 4 | env.Decider('MD5') 5 | 6 | cybld = Builder(action='cython -v -a -o $TARGET $SOURCE') 7 | env.Append(BUILDERS={'Cython':cybld}) 8 | env.Append(CCFLAGS='-pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv \ 9 | -Wall -Wstrict-prototypes -Wstrict-prototypes -O2 -fPIC \ 10 | -I/usr/local/lib/python2.7/dist-packages/numpy/core/include -I/usr/include \ 11 | -I/usr/include/python2.7/ -I/usr/include/gsl/') 12 | 13 | env.Cython('cgillespie.c','cgillespie.pyx') 14 | env.SharedLibrary('cgillespie',['cgillespie.c'], LIBPREFIX='', 15 | LIBS=['gsl','gslcblas'], 16 | LIBPATH=['/usr/local/lib/python2.7/dist-packages/numpy/core/include', 17 | '/usr/include','/usr/include/python2.7/','/usr/include/gsl/']) 18 | -------------------------------------------------------------------------------- /cython_gsl/gsl_gegenbauer.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_sf_gegenbauer.h": 4 | 5 | double gsl_sf_gegenpoly_1(double lambd, double x) nogil 6 | 7 | double gsl_sf_gegenpoly_2(double lambd, double x) nogil 8 | 9 | double gsl_sf_gegenpoly_3(double lambd, double x) nogil 10 | 11 | int gsl_sf_gegenpoly_1_e(double lambd, double x, gsl_sf_result * result) nogil 12 | 13 | int gsl_sf_gegenpoly_2_e(double lambd, double x, gsl_sf_result * result) nogil 14 | 15 | int gsl_sf_gegenpoly_3_e(double lambd, double x, gsl_sf_result * result) nogil 16 | 17 | double gsl_sf_gegenpoly_n(int n, double lambd, double x) nogil 18 | 19 | int gsl_sf_gegenpoly_n_e(int n, double lambd, double x, gsl_sf_result * result) nogil 20 | 21 | int gsl_sf_gegenpoly_array(int nmax, double lambd, double x, double result_array[]) nogil 22 | 23 | -------------------------------------------------------------------------------- /examples/blas2.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | cdef double a[6] 5 | 6 | a[0] = 0.11 7 | a[1] = 0.12 8 | a[2] = 0.13 9 | a[3] = 0.21 10 | a[4] = 0.22 11 | a[5] = 0.23 12 | 13 | cdef double b[6] 14 | b[0] = 1011 15 | b[1] = 1012 16 | b[2] = 1021 17 | b[3] = 1022 18 | b[4] = 1031 19 | b[5] = 1032 20 | 21 | cdef double c[4] 22 | c[0] = 0 23 | c[1] = 0 24 | c[3] = 0 25 | c[4] = 0 26 | 27 | cdef gsl_matrix_view A, B, C 28 | A = gsl_matrix_view_array(a, 2, 3) 29 | B = gsl_matrix_view_array(b, 3, 2) 30 | C = gsl_matrix_view_array(c, 2, 2) 31 | 32 | gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 33 | 1.0, &A.matrix, &B.matrix, 0.0, &C.matrix) 34 | 35 | print "[ %g, %g\n" %(c[0], c[1]) 36 | print " %g, %g ]\n" %(c[2], c[3]) 37 | -------------------------------------------------------------------------------- /cython_gsl/gsl_blas_types.pxd: -------------------------------------------------------------------------------- 1 | cdef extern from "gsl/gsl_cblas.h": 2 | cdef enum CBLAS_ORDER: 3 | CblasRowMajor=101 4 | CblasColMajor=102 5 | 6 | cdef enum CBLAS_TRANSPOSE: 7 | CblasNoTrans=111 8 | CblasTrans=112 9 | CblasConjTrans=113 10 | 11 | cdef enum CBLAS_UPLO: 12 | CblasUpper=121 13 | CblasLower=122 14 | 15 | cdef enum CBLAS_DIAG: 16 | CblasNonUnit=131 17 | CblasUnit=132 18 | 19 | cdef enum CBLAS_SIDE: 20 | CblasLeft=141 21 | CblasRight=142 22 | 23 | ctypedef size_t CBLAS_INDEX 24 | 25 | cdef extern from "gsl/gsl_blas_types.h": 26 | ctypedef CBLAS_INDEX CBLAS_INDEX_t 27 | ctypedef CBLAS_ORDER CBLAS_ORDER_t 28 | ctypedef CBLAS_TRANSPOSE CBLAS_TRANSPOSE_t 29 | ctypedef CBLAS_UPLO CBLAS_UPLO_t 30 | ctypedef CBLAS_DIAG CBLAS_DIAG_t 31 | ctypedef CBLAS_SIDE CBLAS_SIDE_t 32 | 33 | -------------------------------------------------------------------------------- /examples/integrate.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | ctypedef double * double_ptr 4 | ctypedef void * void_ptr 5 | 6 | cdef double normal(double x, void * params) nogil: 7 | cdef double mu = ( params)[0] 8 | cdef double sigma = ( params)[1] 9 | 10 | return gsl_ran_gaussian_pdf(x - mu, sigma) 11 | 12 | def cdf_numerical(double x, double mu, double sigma): 13 | cdef double alpha, result, error, expected 14 | cdef gsl_integration_workspace * W 15 | W = gsl_integration_workspace_alloc(1000) 16 | cdef gsl_function F 17 | cdef double params[1] 18 | cdef size_t neval 19 | 20 | params[0] = mu 21 | params[1] = sigma 22 | 23 | F.function = &normal 24 | F.params = params 25 | 26 | gsl_integration_qag(&F, -10, x, 1e-2, 1e-2, 1000, GSL_INTEG_GAUSS15, W, &result, &error) 27 | gsl_integration_workspace_free(W) 28 | 29 | return result 30 | -------------------------------------------------------------------------------- /cython_gsl/gsl_coupling.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_sf_coupling.h": 4 | 5 | double gsl_sf_coupling_3j(int two_ja, int two_jb, int two_jc, int two_ma, int two_mb, int two_mc) nogil 6 | 7 | int gsl_sf_coupling_3j_e(int two_ja, int two_jb, int two_jc, int two_ma, int two_mb, int two_mc, gsl_sf_result * result) nogil 8 | 9 | double gsl_sf_coupling_6j(int two_ja, int two_jb, int two_jc, int two_jd, int two_je, int two_jf) nogil 10 | 11 | int gsl_sf_coupling_6j_e(int two_ja, int two_jb, int two_jc, int two_jd, int two_je, int two_jf, gsl_sf_result * result) nogil 12 | 13 | double gsl_sf_coupling_9j(int two_ja, int two_jb, int two_jc, int two_jd, int two_je, int two_jf, int two_jg, int two_jh, int two_ji) nogil 14 | 15 | int gsl_sf_coupling_9j_e(int two_ja, int two_jb, int two_jc, int two_jd, int two_je, int two_jf, int two_jg, int two_jh, int two_ji, gsl_sf_result * result) nogil 16 | 17 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | from distutils.core import setup 3 | 4 | setup( 5 | name="CythonGSL", 6 | version="0.2.3", 7 | author="Thomas V. Wiecki", 8 | author_email="thomas.wiecki@gmail.com", 9 | url="http://github.com/twiecki/CythonGSL", 10 | packages=["cython_gsl"], 11 | package_data={"cython_gsl": ["*.pxd"]}, 12 | description="""Cython declarations for the Gnu Scientific Library.""", 13 | setup_requires=['Cython >= 0.16'], 14 | install_requires=['Cython >= 0.16'], 15 | classifiers=[ 16 | 'Development Status :: 4 - Beta', 17 | 'Environment :: Console', 18 | 'Operating System :: OS Independent', 19 | 'Intended Audience :: Science/Research', 20 | 'License :: OSI Approved :: GNU General Public License (GPL)', 21 | 'Programming Language :: Python', 22 | 'Topic :: Scientific/Engineering'] 23 | ) 24 | -------------------------------------------------------------------------------- /examples/statistics2.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | cdef double data[5] 5 | data[0] = 17.2 6 | data[1] = 18.1 7 | data[2] = 16.5 8 | data[3] = 18.3 9 | data[4] = 12.6 10 | cdef double median, upperq, lowerq 11 | 12 | print "Original dataset: %g, %g, %g, %g, %g\n" % \ 13 | (data[0], data[1], data[2], data[3], data[4]) 14 | 15 | #gsl_sort (data, 1, 5) 16 | data[0] = 12.6 17 | data[1] = 16.5 18 | data[2] = 17.2 19 | data[3] = 18.1 20 | data[4] = 18.3 21 | 22 | print "Sorted dataset: %g, %g, %g, %g, %g\n" % \ 23 | (data[0], data[1], data[2], data[3], data[4]) 24 | 25 | median = gsl_stats_median_from_sorted_data (data, 1, 5) 26 | 27 | upperq = gsl_stats_quantile_from_sorted_data (data, 1, 5, 0.75) 28 | lowerq = gsl_stats_quantile_from_sorted_data (data, 1, 5, 0.25) 29 | 30 | print "The median is %g\n" % median 31 | print "The upper quartile is %g\n" % upperq 32 | print "The lower quartile is %g\n" % lowerq 33 | -------------------------------------------------------------------------------- /cython_gsl/gsl_ntuple.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_ntuple.h": 4 | ctypedef struct gsl_ntuple 5 | 6 | ctypedef struct gsl_ntuple_select_fn: 7 | int (* function) (void * ntuple_data, void * params) nogil 8 | void * params 9 | 10 | ctypedef struct gsl_ntuple_value_fn: 11 | double (* function) (void * ntuple_data, void * params) nogil 12 | void * params 13 | 14 | gsl_ntuple * gsl_ntuple_open (char * filename, void * ntuple_data, size_t size) nogil 15 | 16 | gsl_ntuple * gsl_ntuple_create (char * filename, void * ntuple_data, size_t size) nogil 17 | 18 | int gsl_ntuple_write (gsl_ntuple * ntuple) nogil 19 | int gsl_ntuple_read (gsl_ntuple * ntuple) nogil 20 | 21 | int gsl_ntuple_bookdata (gsl_ntuple * ntuple) nogil 22 | 23 | int gsl_ntuple_project (gsl_histogram * h, gsl_ntuple * ntuple, 24 | gsl_ntuple_value_fn *value_func, 25 | gsl_ntuple_select_fn *select_func) nogil 26 | 27 | int gsl_ntuple_close (gsl_ntuple * ntuple) nogil 28 | 29 | -------------------------------------------------------------------------------- /examples/fft3.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | cdef int i, n 5 | n = 100 6 | cdef double data[100] 7 | 8 | cdef gsl_fft_real_wavetable * real 9 | cdef gsl_fft_halfcomplex_wavetable * hc 10 | cdef gsl_fft_real_workspace * work 11 | 12 | for i from 0 <= i < n: 13 | data[i] = 0.0 14 | 15 | for i from n/3 <= i < 2*n/3: 16 | data[i] = 1.0 17 | 18 | for i from 0 <= i < n: 19 | print "%d: %e\n" %(i, data[i]), 20 | print "\n" 21 | 22 | work = gsl_fft_real_workspace_alloc (n) 23 | real = gsl_fft_real_wavetable_alloc (n) 24 | 25 | gsl_fft_real_transform (data, 1, n, real, work) 26 | 27 | gsl_fft_real_wavetable_free (real) 28 | 29 | for i from 11 <= i < n: 30 | data[i] = 0 31 | 32 | hc = gsl_fft_halfcomplex_wavetable_alloc (n) 33 | 34 | gsl_fft_halfcomplex_inverse (data, 1, n, hc, work) 35 | gsl_fft_halfcomplex_wavetable_free (hc) 36 | 37 | for i from 0 <= i < n: 38 | print "%d: %e\n" %(i, data[i]), 39 | 40 | gsl_fft_real_workspace_free (work) 41 | -------------------------------------------------------------------------------- /examples/fft2.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | 4 | def main(): 5 | cdef int i, n 6 | n = 630 7 | cdef double data[2*630] 8 | 9 | cdef gsl_fft_complex_wavetable * wavetable 10 | cdef gsl_fft_complex_workspace * workspace 11 | 12 | for i from 0 <= i < n: 13 | data[2*i] = 0.0 14 | data[2*i+1] = 0.0 15 | 16 | data[0] = 1.0 17 | for i from 1 <= i <= 10: 18 | data[2*i] = data[2*(n-i)] = 1.0 19 | 20 | for i from 0 <= i < n: 21 | print "%d: %e %e\n" %(i, data[2*i], data[2*i+1]), 22 | print "\n" 23 | 24 | wavetable = gsl_fft_complex_wavetable_alloc (n) 25 | workspace = gsl_fft_complex_workspace_alloc (n) 26 | 27 | for i from 0 <= i < wavetable.nf: 28 | print "# factor %d: %d\n" %(i, wavetable.factor[i]), 29 | print "\n" 30 | 31 | gsl_fft_complex_forward (data, 1, n, wavetable, workspace) 32 | 33 | for i from 0 <= i < n: 34 | print "%d: %e %e\n" %(i, data[2*i], data[2*i+1]), 35 | print "\n" 36 | gsl_fft_complex_wavetable_free (wavetable) 37 | gsl_fft_complex_workspace_free (workspace) 38 | -------------------------------------------------------------------------------- /examples/gibbs.pyx: -------------------------------------------------------------------------------- 1 | ''' 2 | Gibbs sampler for function: 3 | 4 | f(x,y) = x x^2 \exp(-xy^2 - y^2 + 2y - 4x) 5 | 6 | using conditional distributions: 7 | 8 | x|y \sim Gamma(3, y^2 +4) 9 | y|x \sim Normal(\frac{1}{1+x}, \frac{1}{2(1+x)}) 10 | 11 | Original version written by Flavio Coelho. 12 | Tweaked by Chris Fonnesbeck. 13 | Ported to CythonGSL Thomas V. Wiecki. 14 | ''' 15 | cimport cython 16 | from cython_gsl cimport * 17 | 18 | import numpy as np 19 | from numpy cimport * 20 | 21 | cdef gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937) 22 | 23 | @cython.cdivision(True) 24 | @cython.wraparound(False) 25 | @cython.boundscheck(False) 26 | def gibbs(int N=20000, int thin=500): 27 | cdef: 28 | double x=0 29 | double y=0 30 | Py_ssize_t i, j 31 | ndarray[float64_t, ndim=2] samples 32 | 33 | samples = np.empty((N,thin)) 34 | 35 | for i in range(N): 36 | for j in range(thin): 37 | x = gsl_ran_gamma(r,3,1.0/(y*y+4)) 38 | y = gsl_ran_gaussian(r,1.0/sqrt(x+1)) 39 | samples[i,0] = x 40 | samples[i,1] = y 41 | return samples 42 | -------------------------------------------------------------------------------- /cython_gsl/gsl_splinalg.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_splinalg.h": 4 | ctypedef struct gsl_splinalg_itersolve_type: 5 | const char *name 6 | void * (*alloc) (const size_t n, const size_t m) 7 | int (*iterate) (const gsl_spmatrix *A, const gsl_vector *b, const double tol, gsl_vector *x, void *) 8 | double (*normr)(const void *) 9 | void (*free) (void *) 10 | 11 | ctypedef struct gsl_splinalg_itersolve: 12 | const gsl_splinalg_itersolve_type * type 13 | double normr 14 | void * state 15 | 16 | gsl_splinalg_itersolve_type * gsl_splinalg_itersolve_gmres 17 | 18 | gsl_splinalg_itersolve * gsl_splinalg_itersolve_alloc(const gsl_splinalg_itersolve_type *T, const size_t n, const size_t m) nogil 19 | 20 | void gsl_splinalg_itersolve_free(gsl_splinalg_itersolve *w) nogil 21 | 22 | const char *gsl_splinalg_itersolve_name(const gsl_splinalg_itersolve *w) nogil 23 | 24 | int gsl_splinalg_itersolve_iterate(const gsl_spmatrix *A, const gsl_vector *b, const double tol, gsl_vector *x, gsl_splinalg_itersolve *w) nogil 25 | 26 | double gsl_splinalg_itersolve_normr(const gsl_splinalg_itersolve *w) nogil 27 | -------------------------------------------------------------------------------- /examples/sum.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef enum: 4 | N = 20 5 | 6 | def main (): 7 | cdef double t[N], sum_accel, err, sum 8 | sum = 0 9 | cdef int n 10 | 11 | cdef gsl_sum_levin_u_workspace * w 12 | w = gsl_sum_levin_u_alloc (N) 13 | 14 | cdef double zeta_2 15 | zeta_2 = M_PI * M_PI / 6.0 16 | 17 | cdef double np1 18 | for n from 0 <= n < N: 19 | np1 = n + 1.0 20 | t[n] = 1.0 / (np1 * np1) 21 | sum = sum + t[n] 22 | 23 | gsl_sum_levin_u_accel (t, N, w, &sum_accel, &err) 24 | 25 | print "term-by-term sum = % .16f using %d terms\n" %(sum, N) 26 | 27 | print "term-by-term sum = % .16f using %d terms\n" % \ 28 | (w.sum_plain, w.terms_used) 29 | 30 | print "exact value = % .16f\n" % zeta_2 31 | print "accelerated sum = % .16f using %d terms\n" % \ 32 | (sum_accel, w.terms_used) 33 | 34 | print "estimated error = % .16f\n" % err 35 | cdef double boh 36 | boh = sum_accel - zeta_2 37 | print "actual error = % .16f\n" % boh 38 | #print "actual error = % .16f\n" % sum_accel - zeta_2 39 | 40 | gsl_sum_levin_u_free (w) 41 | -------------------------------------------------------------------------------- /cython_gsl/gsl_expint.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_sf_expint.h": 4 | 5 | double gsl_sf_expint_E1(double x) nogil 6 | 7 | int gsl_sf_expint_E1_e(double x, gsl_sf_result * result) nogil 8 | 9 | double gsl_sf_expint_E2(double x) nogil 10 | 11 | int gsl_sf_expint_E2_e(double x, gsl_sf_result * result) nogil 12 | 13 | double gsl_sf_expint_Ei(double x) nogil 14 | 15 | int gsl_sf_expint_Ei_e(double x, gsl_sf_result * result) nogil 16 | 17 | double gsl_sf_Shi(double x) nogil 18 | 19 | int gsl_sf_Shi_e(double x, gsl_sf_result * result) nogil 20 | 21 | double gsl_sf_Chi(double x) nogil 22 | 23 | int gsl_sf_Chi_e(double x, gsl_sf_result * result) nogil 24 | 25 | double gsl_sf_expint_3(double x) nogil 26 | 27 | int gsl_sf_expint_3_e(double x, gsl_sf_result * result) nogil 28 | 29 | double gsl_sf_Si(double x) nogil 30 | 31 | int gsl_sf_Si_e(double x, gsl_sf_result * result) nogil 32 | 33 | double gsl_sf_Ci(double x) nogil 34 | 35 | int gsl_sf_Ci_e(double x, gsl_sf_result * result) nogil 36 | 37 | double gsl_sf_atanint(double x) nogil 38 | 39 | int gsl_sf_atanint_e(double x, gsl_sf_result * result) nogil 40 | 41 | -------------------------------------------------------------------------------- /examples/linalg.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | cdef double a_data[16], b_data[4] 5 | cdef gsl_matrix_view m 6 | cdef gsl_vector_view b 7 | cdef gsl_vector *x 8 | cdef int s 9 | cdef gsl_permutation * p 10 | 11 | a_data[0] = 0.18 12 | a_data[1] = 0.60 13 | a_data[2] = 0.57 14 | a_data[3] = 0.96 15 | a_data[4] = 0.41 16 | a_data[5] = 0.24 17 | a_data[6] = 0.99 18 | a_data[7] = 0.58 19 | a_data[8] = 0.14 20 | a_data[9] = 0.30 21 | a_data[10] = 0.97 22 | a_data[11] = 0.66 23 | a_data[12] = 0.51 24 | a_data[13] = 0.13 25 | a_data[14] = 0.19 26 | a_data[15] = 0.85 27 | 28 | b_data[0] = 1 29 | b_data[1] = 2 30 | b_data[2] = 3 31 | b_data[3] = 4 32 | 33 | 34 | m = gsl_matrix_view_array (a_data, 4, 4) 35 | b = gsl_vector_view_array (b_data, 4) 36 | x = gsl_vector_alloc (4) 37 | p = gsl_permutation_alloc (4) 38 | 39 | gsl_vector_fprintf (stdout,&b.vector, "%g") 40 | 41 | gsl_linalg_LU_decomp (&m.matrix, p, &s) 42 | gsl_linalg_LU_solve (&m.matrix, p, &b.vector, x) 43 | 44 | print "x = \n" 45 | gsl_vector_fprintf (stdout, x, "%g") 46 | 47 | gsl_permutation_free (p) 48 | -------------------------------------------------------------------------------- /cython_gsl/gsl_chebyshev.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_chebyshev.h": 4 | 5 | ctypedef struct gsl_cheb_series 6 | 7 | 8 | gsl_cheb_series * gsl_cheb_alloc( size_t order) nogil 9 | 10 | void gsl_cheb_free(gsl_cheb_series * cs) nogil 11 | 12 | int gsl_cheb_init(gsl_cheb_series * cs, gsl_function * func, 13 | double a, double b) nogil 14 | 15 | 16 | double gsl_cheb_eval( gsl_cheb_series * cs, double x) nogil 17 | int gsl_cheb_eval_err( gsl_cheb_series * cs, double x, 18 | double * result, double * abserr) nogil 19 | 20 | 21 | double gsl_cheb_eval_n( gsl_cheb_series * cs, size_t order, double x) nogil 22 | int gsl_cheb_eval_n_err( gsl_cheb_series * cs, size_t order, 23 | double x, double * result, double * abserr) nogil 24 | 25 | 26 | double gsl_cheb_eval_mode( gsl_cheb_series * cs, double x, gsl_mode_t mode) nogil 27 | int gsl_cheb_eval_mode_e( gsl_cheb_series * cs, double x, gsl_mode_t mode, double * result, double * abserr) nogil 28 | 29 | 30 | 31 | int gsl_cheb_calc_deriv(gsl_cheb_series * deriv, gsl_cheb_series * cs) nogil 32 | 33 | int gsl_cheb_calc_integ(gsl_cheb_series * integ, gsl_cheb_series * cs) nogil 34 | 35 | -------------------------------------------------------------------------------- /examples/fit.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main (): 4 | cdef int i, n 5 | n = 4 6 | cdef double x[4], y[4], w[4] 7 | x[0] = 1970 8 | x[1] = 1980 9 | x[2] = 1990 10 | x[3] = 2000 11 | y[0] = 12 12 | y[1] = 11 13 | y[2] = 14 14 | y[3] = 13 15 | w[0] = 0.1 16 | w[1] = 0.2 17 | w[2] = 0.3 18 | w[3] = 0.4 19 | 20 | cdef double c0, c1, cov00, cov01, cov11, chisq 21 | 22 | gsl_fit_wlinear (x, 1, w, 1, y, 1, n, 23 | &c0, &c1, &cov00, &cov01, &cov11, 24 | &chisq) 25 | 26 | print "# best fit: Y = %g + %g X\n" % (c0, c1) 27 | print "# covariance matrix:\n" 28 | print "# [ %g, %g\n# %g, %g]\n" % (cov00, cov01, cov01, cov11) 29 | print "# chisq = %g\n", chisq 30 | 31 | for i from 0 <= i < n: 32 | print "data: %g %g %g\n" % ( x[i], y[i], 1/sqrt(w[i])) 33 | 34 | print "\n" 35 | 36 | cdef double xf, yf, yf_err 37 | for i from -30 <= i < 130: 38 | xf = x[0] + (i/100.0) * (x[n-1] - x[0]) 39 | 40 | gsl_fit_linear_est (xf, c0, c1, cov00, cov01, cov11, &yf, &yf_err) 41 | 42 | print "fit: %g %g\n" %(xf, yf) 43 | print "hi : %g %g\n" %(xf, yf + yf_err) 44 | print "lo : %g %g\n" %(xf, yf - yf_err) 45 | -------------------------------------------------------------------------------- /examples/eigen.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def main(): 4 | cdef double data[16] 5 | cdef gsl_matrix_view m 6 | cdef gsl_vector *eval 7 | cdef gsl_matrix *evec 8 | cdef gsl_eigen_symmv_workspace *w 9 | 10 | data[0] = 1.0 11 | data[1] = 1/2.0 12 | data[2] = 1/3.0 13 | data[3] = 1/4.0 14 | 15 | data[4] = 1/2.0 16 | data[5] = 1/3.0 17 | data[6] = 1/4.0 18 | data[7] = 1/5.0 19 | 20 | data[8] = 1/3.0 21 | data[9] = 1/4.0 22 | data[10] = 1/5.0 23 | data[11] = 1/6.0 24 | 25 | data[12] = 1/4.0 26 | data[13] = 1/5.0 27 | data[14] = 1/6.0 28 | data[15] = 1/7.0 29 | 30 | m = gsl_matrix_view_array (data, 4, 4) 31 | eval = gsl_vector_alloc (4) 32 | evec = gsl_matrix_alloc (4, 4) 33 | w = gsl_eigen_symmv_alloc (4) 34 | gsl_eigen_symmv (&(m.matrix), eval, evec, w) 35 | gsl_eigen_symmv_free (w) 36 | gsl_eigen_symmv_sort (eval, evec, GSL_EIGEN_SORT_ABS_ASC) 37 | 38 | cdef int i 39 | cdef double eval_i 40 | cdef gsl_vector_view evec_i 41 | for i from 0 <= i < 4: 42 | eval_i = gsl_vector_get (eval, i) 43 | evec_i = gsl_matrix_column (evec, i) 44 | print "eigenvalue = %g\n" % eval_i 45 | print "eigenvector = \n" 46 | gsl_vector_fprintf (stdout, &evec_i.vector, "%g") 47 | -------------------------------------------------------------------------------- /examples/setup_integrate.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup 2 | from Cython.Distutils import Extension 3 | from Cython.Distutils import build_ext 4 | import os 5 | import cython_gsl 6 | 7 | setup( 8 | name="integrate", 9 | version="0.1", 10 | author="Thomas V. Wiecki", 11 | author_email="thomas_wiecki@brown.edu", 12 | url="http://github.com/twiecki/CythonGSL", 13 | description="CythonGSL example integrate.", 14 | include_dirs = [cython_gsl.get_include()], 15 | cmdclass = {'build_ext': build_ext}, 16 | classifiers=[ 17 | 'Development Status :: 3 - Alpha', 18 | 'Environment :: Console', 19 | 'Operating System :: OS Independent', 20 | 'Intended Audience :: Science/Research', 21 | 'License :: OSI Approved :: GNU General Public License (GPL)', 22 | 'Programming Language :: Python', 23 | 'Topic :: Scientific/Engineering', 24 | ], 25 | 26 | ext_modules = [Extension("integrate", 27 | ["integrate.pyx"], 28 | libraries=cython_gsl.get_libraries(), 29 | library_dirs=[cython_gsl.get_library_dir()], 30 | cython_include_dirs=[cython_gsl.get_cython_include_dir()])] 31 | ) 32 | -------------------------------------------------------------------------------- /cython_gsl/gsl_fermi_dirac.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_sf_fermi_dirac.h": 4 | 5 | double gsl_sf_fermi_dirac_m1(double x) nogil 6 | 7 | int gsl_sf_fermi_dirac_m1_e(double x, gsl_sf_result * result) nogil 8 | 9 | double gsl_sf_fermi_dirac_0(double x) nogil 10 | 11 | int gsl_sf_fermi_dirac_0_e(double x, gsl_sf_result * result) nogil 12 | 13 | double gsl_sf_fermi_dirac_1(double x) nogil 14 | 15 | int gsl_sf_fermi_dirac_1_e(double x, gsl_sf_result * result) nogil 16 | 17 | double gsl_sf_fermi_dirac_2(double x) nogil 18 | 19 | int gsl_sf_fermi_dirac_2_e(double x, gsl_sf_result * result) nogil 20 | 21 | double gsl_sf_fermi_dirac_int(int j, double x) nogil 22 | 23 | int gsl_sf_fermi_dirac_int_e(int j, double x, gsl_sf_result * result) nogil 24 | 25 | double gsl_sf_fermi_dirac_mhalf(double x) nogil 26 | 27 | int gsl_sf_fermi_dirac_mhalf_e(double x, gsl_sf_result * result) nogil 28 | 29 | double gsl_sf_fermi_dirac_half(double x) nogil 30 | 31 | int gsl_sf_fermi_dirac_half_e(double x, gsl_sf_result * result) nogil 32 | 33 | double gsl_sf_fermi_dirac_3half(double x) nogil 34 | 35 | int gsl_sf_fermi_dirac_3half_e(double x, gsl_sf_result * result) nogil 36 | 37 | double gsl_sf_fermi_dirac_inc_0(double x, double b) nogil 38 | 39 | int gsl_sf_fermi_dirac_inc_0_e(double x, double b, gsl_sf_result * result) nogil 40 | 41 | -------------------------------------------------------------------------------- /cython_gsl/gsl_exp.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_sf_exp.h": 4 | 5 | double gsl_sf_exp(double x) nogil 6 | 7 | int gsl_sf_exp_e(double x, gsl_sf_result * result) nogil 8 | 9 | int gsl_sf_exp_e10_e(double x, gsl_sf_result_e10 * result) nogil 10 | 11 | double gsl_sf_exp_mult(double x, double y) nogil 12 | 13 | int gsl_sf_exp_mult_e(double x, double y, gsl_sf_result * result) nogil 14 | 15 | int gsl_sf_exp_mult_e10_e(double x, double y, gsl_sf_result_e10 * result) nogil 16 | 17 | double gsl_sf_expm1(double x) nogil 18 | 19 | int gsl_sf_expm1_e(double x, gsl_sf_result * result) nogil 20 | 21 | double gsl_sf_exprel(double x) nogil 22 | 23 | int gsl_sf_exprel_e(double x, gsl_sf_result * result) nogil 24 | 25 | double gsl_sf_exprel_2(double x) nogil 26 | 27 | int gsl_sf_exprel_2_e(double x, gsl_sf_result * result) nogil 28 | 29 | double gsl_sf_exprel_n(int n, double x) nogil 30 | 31 | int gsl_sf_exprel_n_e(int n, double x, gsl_sf_result * result) nogil 32 | 33 | int gsl_sf_exp_err_e(double x, double dx, gsl_sf_result * result) nogil 34 | 35 | int gsl_sf_exp_err_e10_e(double x, double dx, gsl_sf_result_e10 * result) nogil 36 | 37 | int gsl_sf_exp_mult_err_e(double x, double dx, double y, double dy, gsl_sf_result * result) nogil 38 | 39 | int gsl_sf_exp_mult_err_e10_e(double x, double dx, double y, double dy, gsl_sf_result_e10 * result) nogil 40 | 41 | -------------------------------------------------------------------------------- /cython_gsl/gsl_combination.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_combination.h": 4 | 5 | ctypedef struct gsl_combination: 6 | size_t n 7 | size_t k 8 | size_t *data 9 | 10 | gsl_combination * gsl_combination_alloc(size_t n, size_t k) nogil 11 | 12 | gsl_combination * gsl_combination_calloc(size_t n, size_t k) nogil 13 | 14 | void gsl_combination_init_first(gsl_combination * c) nogil 15 | 16 | void gsl_combination_init_last(gsl_combination * c) nogil 17 | 18 | void gsl_combination_free(gsl_combination * c) nogil 19 | 20 | int gsl_combination_memcpy(gsl_combination * dest, gsl_combination * src) nogil 21 | 22 | int gsl_combination_fread(FILE * stream, gsl_combination * c) nogil 23 | 24 | int gsl_combination_fwrite(FILE * stream, gsl_combination * c) nogil 25 | 26 | int gsl_combination_fscanf(FILE * stream, gsl_combination * c) nogil 27 | 28 | int gsl_combination_fprintf(FILE * stream, gsl_combination * c, char *format) nogil 29 | 30 | size_t gsl_combination_n(gsl_combination * c) nogil 31 | 32 | size_t gsl_combination_k(gsl_combination * c) nogil 33 | 34 | size_t * gsl_combination_data(gsl_combination * c) nogil 35 | 36 | size_t gsl_combination_get(gsl_combination * c, size_t i) nogil 37 | 38 | int gsl_combination_valid(gsl_combination * c) nogil 39 | 40 | int gsl_combination_next(gsl_combination * c) nogil 41 | 42 | int gsl_combination_prev(gsl_combination * c) nogil 43 | 44 | -------------------------------------------------------------------------------- /cython_gsl/test/test_combinations.py: -------------------------------------------------------------------------------- 1 | import unittest, combinations, os 2 | 3 | class CombinationTest(unittest.TestCase): 4 | 5 | def test_gsl_combination_calloc(self): 6 | t = combinations.t_gsl_combination_calloc() 7 | for x in t: 8 | self.assertAlmostEqual(x,0, 15) 9 | 10 | def test_gsl_combination_init_first(self): 11 | t = combinations.t_gsl_combination_init_first() 12 | for x in t: 13 | self.assertAlmostEqual(x,0, 15) 14 | 15 | def test_gsl_combination_memcpy(self): 16 | t = combinations.t_gsl_combination_memcpy() 17 | for x in t: 18 | self.assertAlmostEqual(x,0, 15) 19 | 20 | def test_gsl_combination_n(self): 21 | t = combinations.t_gsl_combination_n() 22 | for x in t: 23 | self.assertAlmostEqual(x,0, 15) 24 | 25 | def test_gsl_combination_get(self): 26 | t = combinations.t_gsl_combination_get() 27 | for x in t: 28 | self.assertAlmostEqual(x,0, 15) 29 | 30 | def test_gsl_combination_valid(self): 31 | t = combinations.t_gsl_combination_valid() 32 | for x in t: 33 | self.assertAlmostEqual(x,0, 15) 34 | 35 | def test_gsl_combination_fprintf(self): 36 | t = combinations.t_gsl_combination_fprintf() 37 | os.unlink('test.dat') 38 | for x in t: 39 | self.assertAlmostEqual(x,0, 15) 40 | 41 | if __name__ == '__main__': 42 | unittest.main() 43 | -------------------------------------------------------------------------------- /cython_gsl/gsl_coulomb.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_sf_coulomb.h": 4 | 5 | double gsl_sf_hydrogenicR_1(double Z, double r) nogil 6 | 7 | int gsl_sf_hydrogenicR_1_e(double Z, double r, gsl_sf_result * result) nogil 8 | 9 | double gsl_sf_hydrogenicR(int n, int l, double Z, double r) nogil 10 | 11 | int gsl_sf_hydrogenicR_e(int n, int l, double Z, double r, gsl_sf_result * result) nogil 12 | 13 | int gsl_sf_coulomb_wave_FG_e(double eta, double x, double L_F, int k, gsl_sf_result * F, gsl_sf_result * Fp, gsl_sf_result * G, gsl_sf_result * Gp, double * exp_F, double * exp_G) nogil 14 | 15 | int gsl_sf_coulomb_wave_F_array(double L_min, int kmax, double eta, double x, double fc_array[], double * F_exponent) nogil 16 | 17 | int gsl_sf_coulomb_wave_FG_array(double L_min, int kmax, double eta, double x, double fc_array[], double gc_array[], double * F_exponent, double * G_exponent) nogil 18 | 19 | int gsl_sf_coulomb_wave_FGp_array(double L_min, int kmax, double eta, double x, double fc_array[], double fcp_array[], double gc_array[], double gcp_array[], double * F_exponent, double * G_exponent) nogil 20 | 21 | int gsl_sf_coulomb_wave_sphF_array(double L_min, int kmax, double eta, double x, double fc_array[], double F_exponent[]) nogil 22 | 23 | int gsl_sf_coulomb_CL_e(double L, double eta, gsl_sf_result * result) nogil 24 | 25 | int gsl_sf_coulomb_CL_array(double Lmin, int kmax, double eta, double cl[]) nogil 26 | 27 | -------------------------------------------------------------------------------- /cython_gsl/gsl_poly.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_poly.h": 4 | 5 | # Evaluate polynomial 6 | double gsl_poly_eval(double c[], int len, double x) nogil 7 | 8 | # divided-difference polynomials 9 | int gsl_poly_dd_init(double dd[], double xa[], double ya[], size_t size) nogil 10 | 11 | double gsl_poly_dd_eval(double dd[], double xa[], size_t size, double x) nogil 12 | 13 | int gsl_poly_dd_taylor(double c[], double xp, double dd[], double xa[], size_t size, double w[]) nogil 14 | 15 | # quadratic equation 16 | int gsl_poly_solve_quadratic(double a, double b, double c, double *x0, double *x1) nogil 17 | 18 | int gsl_poly_complex_solve_quadratic(double a, double b, double c, gsl_complex *z0, gsl_complex *z1) nogil 19 | 20 | # cubic equation 21 | int gsl_poly_solve_cubic(double a, double b, double c, double *x0, double *x1, double *x2) nogil 22 | 23 | int gsl_poly_complex_solve_cubic(double a, double b, double c, gsl_complex *z0, gsl_complex *z1, gsl_complex *z2) nogil 24 | 25 | # General Polynomial Equations 26 | ctypedef struct gsl_poly_complex_workspace: 27 | size_t nc 28 | double * matrix 29 | 30 | gsl_poly_complex_workspace * gsl_poly_complex_workspace_alloc(size_t n) nogil 31 | 32 | void gsl_poly_complex_workspace_free(gsl_poly_complex_workspace * w) nogil 33 | 34 | int gsl_poly_complex_solve(double * a, size_t n, gsl_poly_complex_workspace * w, gsl_complex_packed_ptr z) nogil 35 | 36 | 37 | -------------------------------------------------------------------------------- /examples/ntuple.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef struct data: 4 | double x 5 | double y 6 | double z 7 | 8 | 9 | 10 | cdef int sel_func (void *ntuple_data, void *params) nogil: 11 | cdef data * data1 12 | data1 = ntuple_data 13 | cdef double x, y, z, E2, scale 14 | scale = ( params)[0] 15 | 16 | x = data1.x 17 | y = data1.y 18 | z = data1.z 19 | 20 | E2 = x * x + y * y + z * z 21 | 22 | return E2 > scale 23 | 24 | cdef double val_func (void *ntuple_data, void *params) nogil: 25 | cdef data * data1 26 | data1 = ntuple_data 27 | cdef double x, y, z 28 | 29 | x = data1.x 30 | y = data1.y 31 | z = data1.z 32 | 33 | return x * x + y * y + z * z 34 | 35 | def main( ): 36 | cdef data ntuple_row 37 | 38 | cdef gsl_ntuple *ntuple 39 | ntuple = gsl_ntuple_open ("test_dat/ntuple.dat", &ntuple_row, sizeof (ntuple_row)) 40 | cdef double lower 41 | lower = 1.5 42 | 43 | cdef gsl_ntuple_select_fn S 44 | cdef gsl_ntuple_value_fn V 45 | 46 | cdef gsl_histogram *h 47 | h = gsl_histogram_alloc (100) 48 | gsl_histogram_set_ranges_uniform(h, 0.0, 10.0) 49 | 50 | S.function = &sel_func 51 | S.params = &lower 52 | 53 | V.function = &val_func 54 | V.params = NULL 55 | 56 | gsl_ntuple_project (h, ntuple, &V, &S) 57 | gsl_histogram_fprintf (stdout, h, "%f", "%f") 58 | gsl_histogram_free (h) 59 | gsl_ntuple_close (ntuple) 60 | -------------------------------------------------------------------------------- /examples/setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup 2 | from Cython.Distutils import Extension 3 | from Cython.Distutils import build_ext 4 | import os 5 | import cython_gsl 6 | import numpy as np 7 | from glob import glob 8 | from os.path import splitext 9 | 10 | ext_modules = [] 11 | for pyxfile in glob('*.pyx'): 12 | ext = Extension(splitext(pyxfile)[0], 13 | [pyxfile], 14 | libraries=cython_gsl.get_libraries(), 15 | library_dirs=[cython_gsl.get_library_dir()], 16 | cython_include_dirs=[cython_gsl.get_cython_include_dir()]) 17 | 18 | ext_modules.append(ext) 19 | 20 | setup( 21 | name="CythonGSL_examples", 22 | version="0.2", 23 | author="Thomas V. Wiecki", 24 | author_email="thomas.wiecki@gmail.com", 25 | url="http://github.com/twiecki/CythonGSL", 26 | description="CythonGSL examples.", 27 | include_dirs = [np.get_include(), cython_gsl.get_include()], 28 | cmdclass = {'build_ext': build_ext}, 29 | classifiers=[ 30 | 'Development Status :: 3 - Alpha', 31 | 'Environment :: Console', 32 | 'Operating System :: OS Independent', 33 | 'Intended Audience :: Science/Research', 34 | 'License :: OSI Approved :: GNU General Public License (GPL)', 35 | 'Programming Language :: Python', 36 | 'Topic :: Scientific/Engineering', 37 | ], 38 | ext_modules = ext_modules 39 | ) 40 | -------------------------------------------------------------------------------- /cython_gsl/gsl_rstat.pxd: -------------------------------------------------------------------------------- 1 | cdef extern from "gsl/gsl_rstat.h": 2 | 3 | ctypedef struct gsl_rstat_workspace 4 | ctypedef struct gsl_rstat_quantile_workspace 5 | 6 | gsl_rstat_workspace * gsl_rstat_alloc () nogil 7 | gsl_rstat_quantile_workspace * gsl_rstat_quantile_alloc (double p) nogil 8 | 9 | void gsl_rstat_free (gsl_rstat_workspace * w) nogil 10 | void gsl_rstat_quantile_free (gsl_rstat_quantile_workspace * w) nogil 11 | 12 | int gsl_rstat_reset (gsl_rstat_workspace * w) nogil 13 | int gsl_rstat_quantile_reset (gsl_rstat_quantile_workspace * w) nogil 14 | 15 | int gsl_rstat_add (double x, gsl_rstat_workspace * w) nogil 16 | int gsl_rstat_quantile_add (double x, gsl_rstat_quantile_workspace * w) nogil 17 | 18 | size_t gsl_rstat_n (gsl_rstat_workspace * w) nogil 19 | 20 | double gsl_rstat_min (gsl_rstat_workspace * w) nogil 21 | double gsl_rstat_max (gsl_rstat_workspace * w) nogil 22 | double gsl_rstat_mean (gsl_rstat_workspace * w) nogil 23 | double gsl_rstat_variance (gsl_rstat_workspace * w) nogil 24 | double gsl_rstat_sd (gsl_rstat_workspace * w) nogil 25 | double gsl_rstat_sd_mean (gsl_rstat_workspace * w) nogil 26 | double gsl_rstat_rms (gsl_rstat_workspace * w) nogil 27 | double gsl_rstat_skew (gsl_rstat_workspace * w) nogil 28 | double gsl_rstat_kurtosis (gsl_rstat_workspace * w) nogil 29 | double gsl_rstat_median (gsl_rstat_workspace * w) nogil 30 | 31 | double gsl_rstat_quantile_get (gsl_rstat_quantile_workspace * w) nogil 32 | -------------------------------------------------------------------------------- /cython_gsl/gsl_sort.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_heapsort.h": 4 | 5 | ctypedef int (*gsl_comparison_fn_t) ( void * , void * ) nogil 6 | 7 | void gsl_heapsort (void * array, size_t count, size_t size, gsl_comparison_fn_t compare) nogil 8 | int gsl_heapsort_index (size_t * p, void * array, size_t count, size_t size, gsl_comparison_fn_t compare) nogil 9 | 10 | 11 | cdef extern from "gsl/gsl_sort_double.h": 12 | void gsl_sort (double * data, size_t stride, size_t n) nogil 13 | void gsl_sort_index (size_t * p, double * data, size_t stride, size_t n) nogil 14 | 15 | int gsl_sort_smallest (double * dest, size_t k, double * src, size_t stride, size_t n) nogil 16 | int gsl_sort_smallest_index (size_t * p, size_t k, double * src, size_t stride, size_t n) nogil 17 | 18 | int gsl_sort_largest (double * dest, size_t k, double * src, size_t stride, size_t n) nogil 19 | int gsl_sort_largest_index (size_t * p, size_t k, double * src, size_t stride, size_t n) nogil 20 | 21 | 22 | cdef extern from "gsl/gsl_sort_vector_double.h": 23 | 24 | void gsl_sort_vector (gsl_vector * v) nogil 25 | int gsl_sort_vector_index (gsl_permutation * p, gsl_vector * v) nogil 26 | 27 | int gsl_sort_vector_smallest (double * dest, size_t k, gsl_vector * v) nogil 28 | int gsl_sort_vector_largest (double * dest, size_t k, gsl_vector * v) nogil 29 | 30 | int gsl_sort_vector_smallest_index (size_t * p, size_t k, gsl_vector * v) nogil 31 | int gsl_sort_vector_largest_index (size_t * p, size_t k, gsl_vector * v) nogil 32 | 33 | -------------------------------------------------------------------------------- /cython_gsl/gsl_block.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_block_double.h": 4 | 5 | ctypedef struct gsl_block: 6 | size_t size 7 | double * data 8 | 9 | gsl_block * gsl_block_alloc(size_t n) nogil 10 | 11 | gsl_block * gsl_block_calloc(size_t n) nogil 12 | 13 | void gsl_block_free(gsl_block * b) nogil 14 | 15 | int gsl_block_fread(FILE * stream, gsl_block * b) nogil 16 | 17 | int gsl_block_fwrite(FILE * stream, gsl_block * b) nogil 18 | 19 | int gsl_block_fscanf(FILE * stream, gsl_block * b) nogil 20 | 21 | int gsl_block_fprintf(FILE * stream, gsl_block * b, char * format) nogil 22 | 23 | size_t gsl_block_size (gsl_block * b) nogil 24 | double * gsl_block_data (gsl_block * b) nogil 25 | 26 | 27 | cdef extern from "gsl/gsl_block_complex_double.h": 28 | 29 | ctypedef struct gsl_block_complex: 30 | size_t size 31 | double * data 32 | 33 | gsl_block_complex * gsl_block_complex_alloc(size_t n) nogil 34 | 35 | gsl_block_complex * gsl_block_complex_calloc(size_t n) nogil 36 | 37 | void gsl_block_complex_free(gsl_block_complex * b) nogil 38 | 39 | int gsl_block_complex_fread(FILE * stream, gsl_block_complex * b) nogil 40 | 41 | int gsl_block_complex_fwrite(FILE * stream, gsl_block_complex * b) nogil 42 | 43 | int gsl_block_complex_fscanf(FILE * stream, gsl_block_complex * b) nogil 44 | 45 | int gsl_block_complex_fprintf(FILE * stream, gsl_block_complex * b, char * format) nogil 46 | 47 | size_t gsl_block_complex_size (gsl_block_complex * b) nogil 48 | double * gsl_block_complex_data (gsl_block_complex * b) nogil 49 | -------------------------------------------------------------------------------- /examples/min.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef double fn1(double x, void * params) nogil: 4 | return cos(x) + 1.0 5 | 6 | def main(): 7 | cdef int status, iter, max_iter 8 | iter = 0 9 | max_iter = 100 10 | cdef gsl_min_fminimizer_type *T 11 | cdef gsl_min_fminimizer *s 12 | cdef double m, m_expected, a, b 13 | m = 2.0 14 | m_expected = M_PI 15 | a = 0.0 16 | b = 6.0 17 | cdef gsl_function F 18 | 19 | F.function = &fn1 20 | F.params = NULL 21 | 22 | T = gsl_min_fminimizer_brent 23 | s = gsl_min_fminimizer_alloc(T) 24 | gsl_min_fminimizer_set(s, &F, m, a, b) 25 | 26 | print "using %s method\n" % gsl_min_fminimizer_name(s) 27 | 28 | print "%5s [%9s, %9s] %9s %10s %9s\n" % \ 29 | ("iter", "lower", "upper", "min", "err", "err(est)") 30 | 31 | print "%5d [%.7f, %.7f] %.7f %+.7f %.7f\n" % \ 32 | (iter, a, b, m, m - m_expected, b - a) 33 | 34 | cdef double GSL_CONTINUE 35 | GSL_CONTINUE = -2 # to go... 36 | status = -2 37 | 38 | while (status == GSL_CONTINUE and iter < max_iter): 39 | iter = iter + 1 40 | status = gsl_min_fminimizer_iterate(s) 41 | 42 | m = gsl_min_fminimizer_x_minimum(s) 43 | a = gsl_min_fminimizer_x_lower(s) 44 | b = gsl_min_fminimizer_x_upper(s) 45 | 46 | status = gsl_min_test_interval(a, b, 0.001, 0.0) 47 | 48 | if (status == GSL_SUCCESS): 49 | print ("Converged:\n") 50 | 51 | print "%5d [%.7f, %.7f] %.7f %.7f %+.7f %.7f\n" % \ 52 | (iter, a, b, m, m_expected, m - m_expected, b - a) 53 | 54 | return status 55 | 56 | -------------------------------------------------------------------------------- /cython_gsl/gsl_dht.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_dht.h": 4 | ctypedef struct gsl_dht_struct: 5 | size_t size # size of the sample arrays to be transformed 6 | double nu # Bessel function order 7 | double xmax # the upper limit to the x-sampling domain 8 | double kmax # the upper limit to the k-sampling domain 9 | double * j # array of computed J_nu zeros, j_{nu,s} = j[s] 10 | double * Jjj # transform numerator, J_nu(j_i j_m / j_N) 11 | double * J2 # transform denominator, J_{nu+1}^2(j_m) 12 | ctypedef gsl_dht_struct gsl_dht 13 | 14 | # Create a new transform object for a given size 15 | # sampling array on the domain [0, xmax]. 16 | gsl_dht * gsl_dht_alloc(size_t size) nogil 17 | gsl_dht * gsl_dht_new(size_t size, double nu, double xmax) nogil 18 | 19 | # Recalculate a transform object for given values of nu, xmax. 20 | # You cannot change the size of the object since the internal 21 | # allocation is reused. 22 | int gsl_dht_init(gsl_dht * t, double nu, double xmax) nogil 23 | 24 | # The n'th computed x sample point for a given transform. 25 | # 0 <= n <= size-1 26 | double gsl_dht_x_sample(gsl_dht * t, int n) nogil 27 | 28 | 29 | # The n'th computed k sample point for a given transform. 30 | # 0 <= n <= size-1 31 | double gsl_dht_k_sample(gsl_dht * t, int n) nogil 32 | 33 | 34 | # Free a transform object. 35 | void gsl_dht_free(gsl_dht * t) nogil 36 | 37 | 38 | # Perform a transform on a sampled array. 39 | # f_in[0] ... f_in[size-1] and similarly for f_out[] 40 | int gsl_dht_apply(gsl_dht * t, double * f_in, double * f_out) nogil 41 | 42 | -------------------------------------------------------------------------------- /cython_gsl/test/test_poly.py: -------------------------------------------------------------------------------- 1 | import unittest, poly 2 | 3 | 4 | class PolyTest(unittest.TestCase): 5 | 6 | 7 | def test_gsl_poly_eval(self): 8 | t = poly.t_gsl_poly_eval() 9 | self.assertAlmostEqual(t,0, 15) 10 | 11 | def test_gsl_poly_dd_init(self): 12 | t = poly.t_gsl_poly_dd_init() 13 | for x in t: 14 | self.assertAlmostEqual(x,0, 15) 15 | 16 | def test_gsl_poly_solve_quadratic(self): 17 | t = poly.t_gsl_poly_solve_quadratic() 18 | for x in t: 19 | self.assertAlmostEqual(x, 0, 15) 20 | 21 | def test_gsl_poly_complex_solve_quadratic(self): 22 | t = poly.t_gsl_poly_complex_solve_quadratic() 23 | for x in t: 24 | self.assertAlmostEqual(x, 0, 15) 25 | 26 | def test_gsl_poly_solve_cubic1(self): 27 | t = poly.t_gsl_poly_solve_cubic1() 28 | for x in t: 29 | self.assertAlmostEqual(x, 0, 15) 30 | 31 | def test_gsl_poly_solve_cubic2(self): 32 | t = poly.t_gsl_poly_solve_cubic2() 33 | for x in t: 34 | self.assertAlmostEqual(x, 0, 15) 35 | 36 | def test_gsl_poly_complex_solve_cubic1(self): 37 | t = poly.t_gsl_poly_complex_solve_cubic1() 38 | for x in t: 39 | self.assertAlmostEqual(x, 0, 15) 40 | 41 | def test_gsl_poly_complex_solve_cubic2(self): 42 | t = poly.t_gsl_poly_complex_solve_cubic2() 43 | for x in t: 44 | self.assertAlmostEqual(x, 0, 15) 45 | 46 | def test_gsl_poly_complex_solve(self): 47 | t = poly.t_gsl_poly_complex_solve() 48 | for x in t: 49 | self.assertAlmostEqual(x, 0, 14) 50 | 51 | if __name__ == '__main__': 52 | unittest.main() 53 | -------------------------------------------------------------------------------- /setup_test.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | from distutils.core import setup 3 | from Cython.Distutils import Extension 4 | from Cython.Distutils import build_ext 5 | from glob import glob 6 | import os.path 7 | import cython_gsl 8 | from os.path import splitext 9 | 10 | ext_modules = [] 11 | for pyxfile in glob(os.path.join('cython_gsl', 'test', '*.pyx')): 12 | ext_name = splitext(os.path.split(pyxfile)[-1])[0] 13 | ext = Extension(ext_name, 14 | [pyxfile], 15 | libraries=cython_gsl.get_libraries(), 16 | library_dirs=[cython_gsl.get_library_dir()], 17 | cython_include_dirs=[cython_gsl.get_cython_include_dir()]) 18 | 19 | ext_modules.append(ext) 20 | 21 | setup( 22 | name="CythonGSL_test", 23 | version="0.2.1", 24 | author="Thomas V. Wiecki", 25 | author_email="thomas.wiecki@gmail.com", 26 | url="http://github.com/twiecki/CythonGSL", 27 | packages=["cython_gsl.test"], 28 | package_data={"cython_gsl.test": ["*.py"]}, 29 | description="""Cython declarations for the Gnu Scientific Library.""", 30 | setup_requires=['Cython >= 0.16', 'CythonGSL'], 31 | install_requires=['Cython >= 0.16', 'CythonGSL'], 32 | classifiers=[ 33 | 'Development Status :: 4 - Beta', 34 | 'Environment :: Console', 35 | 'Operating System :: OS Independent', 36 | 'Intended Audience :: Science/Research', 37 | 'License :: OSI Approved :: GNU General Public License (GPL)', 38 | 'Programming Language :: Python', 39 | 'Topic :: Scientific/Engineering'], 40 | cmdclass = {'build_ext': build_ext}, 41 | ext_modules = ext_modules 42 | ) 43 | -------------------------------------------------------------------------------- /examples/odeiv2.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | 4 | cdef int func (double t, double y[], double f[], void *params) nogil: 5 | cdef double mu 6 | mu = ( params)[0] 7 | f[0] = y[1] 8 | f[1] = -y[0] - mu*y[1]*(y[0]*y[0] - 1) 9 | return GSL_SUCCESS 10 | 11 | cdef int jac (double t, double y[], double *dfdy, double dfdt[], void *params) nogil: 12 | cdef double mu 13 | mu = (params)[0] 14 | cdef gsl_matrix_view dfdy_mat 15 | dfdy_mat = gsl_matrix_view_array (dfdy, 2, 2) 16 | cdef gsl_matrix * m 17 | m = &dfdy_mat.matrix 18 | gsl_matrix_set (m, 0, 0, 0.0) 19 | gsl_matrix_set (m, 0, 1, 1.0) 20 | gsl_matrix_set (m, 1, 0, -2.0*mu*y[0]*y[1] - 1.0) 21 | gsl_matrix_set (m, 1, 1, -mu*(y[0]*y[0] - 1.0)) 22 | dfdt[0] = 0.0 23 | dfdt[1] = 0.0 24 | return GSL_SUCCESS 25 | 26 | def main ( ): 27 | cdef double mu 28 | mu = 10 29 | cdef gsl_odeiv2_system sys 30 | sys.function = func 31 | sys.jacobian = jac 32 | sys.dimension = 2 33 | sys.params = &mu 34 | 35 | cdef gsl_odeiv2_driver * d 36 | d = gsl_odeiv2_driver_alloc_y_new( 37 | &sys, gsl_odeiv2_step_rk8pd, 38 | 1e-6, 1e-6, 0.0) 39 | 40 | cdef int i 41 | cdef double t, t1, y[2] 42 | t = 0.0 43 | t1 = 100.0 44 | y[0] = 1.0 45 | y[1] = 0.0 46 | 47 | cdef int status 48 | cdef double ti 49 | for i from 1 <= i <= 100: 50 | ti = i * t1 / 100.0 51 | status = gsl_odeiv2_driver_apply (d, &t, ti, y) 52 | 53 | if (status != GSL_SUCCESS): 54 | print("error, return value=%d\n" % status) 55 | break 56 | 57 | print("%.5e %.5e %.5e\n" %(t, y[0], y[1])) 58 | 59 | gsl_odeiv2_driver_free(d) 60 | 61 | -------------------------------------------------------------------------------- /cython_gsl/gsl_eigen.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_eigen.h": 4 | 5 | ctypedef enum gsl_eigen_sort_t: 6 | GSL_EIGEN_SORT_VAL_ASC, 7 | GSL_EIGEN_SORT_VAL_DESC, 8 | GSL_EIGEN_SORT_ABS_ASC, 9 | GSL_EIGEN_SORT_ABS_DESC 10 | ctypedef struct gsl_eigen_symm_workspace 11 | ctypedef struct gsl_eigen_symmv_workspace 12 | ctypedef struct gsl_eigen_herm_workspace 13 | ctypedef struct gsl_eigen_hermv_workspace 14 | 15 | gsl_eigen_symm_workspace * gsl_eigen_symm_alloc(size_t n) nogil 16 | 17 | void gsl_eigen_symm_free(gsl_eigen_symm_workspace * w) nogil 18 | 19 | int gsl_eigen_symm(gsl_matrix * A, gsl_vector * eval, gsl_eigen_symm_workspace * w) nogil 20 | 21 | gsl_eigen_symmv_workspace * gsl_eigen_symmv_alloc(size_t n) nogil 22 | 23 | void gsl_eigen_symmv_free(gsl_eigen_symmv_workspace * w) nogil 24 | 25 | int gsl_eigen_symmv(gsl_matrix * A, gsl_vector * eval, gsl_matrix * evec, gsl_eigen_symmv_workspace * w) nogil 26 | 27 | gsl_eigen_herm_workspace * gsl_eigen_herm_alloc(size_t n) nogil 28 | 29 | void gsl_eigen_herm_free(gsl_eigen_herm_workspace * w) nogil 30 | 31 | int gsl_eigen_herm(gsl_matrix_complex * A, gsl_vector * eval, gsl_eigen_herm_workspace * w) nogil 32 | 33 | gsl_eigen_hermv_workspace * gsl_eigen_hermv_alloc(size_t n) nogil 34 | 35 | void gsl_eigen_hermv_free(gsl_eigen_hermv_workspace * w) nogil 36 | 37 | int gsl_eigen_hermv(gsl_matrix_complex * A, gsl_vector * eval, gsl_matrix_complex * evec, gsl_eigen_hermv_workspace * w) nogil 38 | 39 | int gsl_eigen_symmv_sort(gsl_vector * eval, gsl_matrix * evec, gsl_eigen_sort_t sort_type) nogil 40 | 41 | int gsl_eigen_hermv_sort(gsl_vector * eval, gsl_matrix_complex * evec, gsl_eigen_sort_t sort_type) nogil 42 | 43 | -------------------------------------------------------------------------------- /examples/sparse.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | from libc.stdio cimport printf 3 | 4 | def main(): 5 | cdef gsl_spmatrix *A = gsl_spmatrix_alloc(5, 4) 6 | cdef gsl_spmatrix *B, *C 7 | cdef size_t i, j 8 | 9 | # build the sparse matrix 10 | gsl_spmatrix_set(A, 0, 2, 3.1) 11 | gsl_spmatrix_set(A, 0, 3, 4.6) 12 | gsl_spmatrix_set(A, 1, 0, 1.0) 13 | gsl_spmatrix_set(A, 1, 2, 7.2) 14 | gsl_spmatrix_set(A, 3, 0, 2.1) 15 | gsl_spmatrix_set(A, 3, 1, 2.9) 16 | gsl_spmatrix_set(A, 3, 3, 8.5) 17 | gsl_spmatrix_set(A, 4, 0, 4.1) 18 | 19 | print "printing all matrix elements:" 20 | for i in range(5): 21 | for j in range(4): 22 | print "A(%d,%d) = %g" % (i, j, gsl_spmatrix_get(A, i, j)) 23 | 24 | printf("matrix in triplet format (i,j,Aij):\n") 25 | gsl_spmatrix_fprintf(stdout, A, "%.1f") 26 | 27 | # convert to compressed column format 28 | B = gsl_spmatrix_ccs(A) 29 | 30 | print("\nmatrix in compressed column format:") 31 | print("i = [ ") 32 | for i in range(B.nz): 33 | print "%d, " % B.i[i] 34 | print " ]" 35 | 36 | print("p = [ ") 37 | for i in range(B.size2): 38 | print "%d, " % B.p[i] 39 | print " ]" 40 | 41 | print("d = [ ") 42 | for i in range(B.nz): 43 | print "%g, " % B.data[i] 44 | print " ]" 45 | 46 | # convert to compressed row format 47 | C = gsl_spmatrix_crs(A) 48 | 49 | print "\nmatrix in compressed row format:" 50 | print "i = [ " 51 | for i in range(C.nz): 52 | print "%d, " % C.i[i] 53 | print " ]" 54 | 55 | print("p = [ ") 56 | for i in range(C.size1): 57 | print "%d, " % C.p[i] 58 | print " ]" 59 | 60 | print("d = [ ") 61 | for i in range(C.nz): 62 | print "%g, " % C.data[i] 63 | print " ]" 64 | 65 | gsl_spmatrix_free(A) 66 | gsl_spmatrix_free(B) 67 | gsl_spmatrix_free(C) 68 | -------------------------------------------------------------------------------- /setup_interface.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | from distutils.core import setup 3 | from Cython.Distutils import Extension 4 | from Cython.Distutils import build_ext 5 | from glob import glob 6 | import os.path 7 | import cython_gsl 8 | from os.path import splitext 9 | 10 | ext_modules = [] 11 | for pyxfile in glob(os.path.join('cython_gsl', 'interface', '*.pyx')): 12 | ext_name = splitext(os.path.split(pyxfile)[-1])[0] 13 | ext = Extension(ext_name, 14 | [pyxfile], 15 | libraries=cython_gsl.get_libraries(), 16 | library_dirs=[cython_gsl.get_library_dir()], 17 | cython_include_dirs=[cython_gsl.get_cython_include_dir()]) 18 | 19 | ext_modules.append(ext) 20 | 21 | setup( 22 | name="CythonGSL_interface", 23 | version="0.2.1", 24 | author="Thomas V. Wiecki", 25 | author_email="thomas.wiecki@gmail.com", 26 | url="http://github.com/twiecki/CythonGSL", 27 | packages=["cython_gsl.interface"], 28 | package_data={"cython_gsl.interface": ["*.pyx"]}, 29 | description="""Cython declarations for the Gnu Scientific Library.""", 30 | setup_requires=['Cython >= 0.16', 'CythonGSL'], 31 | install_requires=['Cython >= 0.16', 'CythonGSL'], 32 | classifiers=[ 33 | 'Development Status :: 4 - Beta', 34 | 'Environment :: Console', 35 | 'Operating System :: OS Independent', 36 | 'Intended Audience :: Science/Research', 37 | 'License :: OSI Approved :: GNU General Public License (GPL)', 38 | 'Programming Language :: Python', 39 | 'Topic :: Scientific/Engineering'], 40 | cmdclass = {'build_ext': build_ext}, 41 | ext_modules = ext_modules 42 | ) 43 | -------------------------------------------------------------------------------- /cython_gsl/test/test_sort.py: -------------------------------------------------------------------------------- 1 | import unittest, sort, os 2 | 3 | class SortTest(unittest.TestCase): 4 | 5 | def test_gsl_sort_smallest(self): 6 | t = sort.t_gsl_sort_smallest() 7 | for x in t: 8 | self.assertAlmostEqual(x,0, 15) 9 | 10 | def test_gsl_sort_smallest_index(self): 11 | t = sort.t_gsl_sort_smallest_index() 12 | for x in t: 13 | self.assertAlmostEqual(x,0, 15) 14 | 15 | def test_gsl_sort_vector_smallest(self): 16 | t = sort.t_gsl_sort_vector_smallest() 17 | for x in t: 18 | self.assertAlmostEqual(x,0, 15) 19 | 20 | def test_gsl_sort_vector_smallest_index(self): 21 | t = sort.t_gsl_sort_vector_smallest_index() 22 | for x in t: 23 | self.assertAlmostEqual(x,0, 15) 24 | 25 | def test_gsl_sort(self): 26 | t = sort.t_gsl_sort() 27 | for x in t: 28 | self.assertAlmostEqual(x,0, 15) 29 | 30 | def test_gsl_sort_index(self): 31 | t = sort.t_gsl_sort_index() 32 | for x in t: 33 | self.assertAlmostEqual(x,0, 15) 34 | 35 | def test_gsl_sort_vector(self): 36 | t = sort.t_gsl_sort_vector() 37 | for x in t: 38 | self.assertAlmostEqual(x,0, 15) 39 | 40 | def test_gsl_sort_vector_index(self): 41 | t = sort.t_gsl_sort_vector_index() 42 | for x in t: 43 | self.assertAlmostEqual(x,0, 15) 44 | 45 | def test_gsl_heapsort(self): 46 | t = sort.t_gsl_heapsort() 47 | for x in t: 48 | self.assertAlmostEqual(x,0, 15) 49 | 50 | def test_gsl_heapsort_index(self): 51 | t = sort.t_gsl_heapsort_index() 52 | for x in t: 53 | self.assertAlmostEqual(x,0, 15) 54 | 55 | if __name__ == '__main__': 56 | unittest.main() 57 | -------------------------------------------------------------------------------- /examples/odeiv.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | 4 | cdef int func (double t, double y[], double f[], void *params) nogil: 5 | cdef double mu 6 | mu = ( params)[0] 7 | f[0] = y[1] 8 | f[1] = -y[0] - mu*y[1]*(y[0]*y[0] - 1) 9 | return GSL_SUCCESS 10 | 11 | cdef int jac (double t, double y[], double *dfdy, double dfdt[], void *params) nogil: 12 | cdef double mu 13 | mu = (params)[0] 14 | cdef gsl_matrix_view dfdy_mat 15 | dfdy_mat = gsl_matrix_view_array (dfdy, 2, 2) 16 | cdef gsl_matrix * m 17 | m = &dfdy_mat.matrix 18 | gsl_matrix_set (m, 0, 0, 0.0) 19 | gsl_matrix_set (m, 0, 1, 1.0) 20 | gsl_matrix_set (m, 1, 0, -2.0*mu*y[0]*y[1] - 1.0) 21 | gsl_matrix_set (m, 1, 1, -mu*(y[0]*y[0] - 1.0)) 22 | dfdt[0] = 0.0 23 | dfdt[1] = 0.0 24 | return GSL_SUCCESS 25 | 26 | def main ( ): 27 | cdef gsl_odeiv_step_type * T 28 | T = gsl_odeiv_step_rk8pd 29 | 30 | cdef gsl_odeiv_step * s 31 | s = gsl_odeiv_step_alloc (T, 2) 32 | cdef gsl_odeiv_control * c 33 | c = gsl_odeiv_control_y_new (1e-6, 0.0) 34 | cdef gsl_odeiv_evolve * e 35 | e = gsl_odeiv_evolve_alloc (2) 36 | 37 | cdef double mu 38 | mu = 10 39 | cdef gsl_odeiv_system sys 40 | sys.function = func 41 | sys.jacobian = jac 42 | sys.dimension = 2 43 | sys.params = &mu 44 | 45 | cdef double t, t1, h, y[2] 46 | t = 0.0 47 | t1 = 100.0 48 | h = 1e-6 49 | y[0] = 1.0 50 | y[1] = 0.0 51 | 52 | cdef int status 53 | while (t < t1): 54 | status = gsl_odeiv_evolve_apply (e, c, s, &sys, &t, t1, &h, y) 55 | 56 | if (status != GSL_SUCCESS): 57 | break 58 | 59 | print "%.5e %.5e %.5e\n" %(t, y[0], y[1]) 60 | 61 | gsl_odeiv_evolve_free (e) 62 | gsl_odeiv_control_free (c) 63 | gsl_odeiv_step_free (s) 64 | -------------------------------------------------------------------------------- /cython_gsl/gsl_trig.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_sf_trig.h": 4 | 5 | double gsl_sf_sin(double x) nogil 6 | 7 | int gsl_sf_sin_e(double x, gsl_sf_result * result) nogil 8 | 9 | double gsl_sf_cos(double x) nogil 10 | 11 | int gsl_sf_cos_e(double x, gsl_sf_result * result) nogil 12 | 13 | double gsl_sf_hypot(double x, double y) nogil 14 | 15 | int gsl_sf_hypot_e(double x, double y, gsl_sf_result * result) nogil 16 | 17 | double gsl_sf_sinc(double x) nogil 18 | 19 | int gsl_sf_sinc_e(double x, gsl_sf_result * result) nogil 20 | 21 | int gsl_sf_complex_sin_e(double zr, double zi, gsl_sf_result * szr, gsl_sf_result * szi) nogil 22 | 23 | int gsl_sf_complex_cos_e(double zr, double zi, gsl_sf_result * czr, gsl_sf_result * czi) nogil 24 | 25 | int gsl_sf_complex_logsin_e(double zr, double zi, gsl_sf_result * lszr, gsl_sf_result * lszi) nogil 26 | 27 | double gsl_sf_lnsinh(double x) nogil 28 | 29 | int gsl_sf_lnsinh_e(double x, gsl_sf_result * result) nogil 30 | 31 | double gsl_sf_lncosh(double x) nogil 32 | 33 | int gsl_sf_lncosh_e(double x, gsl_sf_result * result) nogil 34 | 35 | int gsl_sf_polar_to_rect(double r, double theta, gsl_sf_result * x, gsl_sf_result * y) nogil 36 | 37 | int gsl_sf_rect_to_polar(double x, double y, gsl_sf_result * r, gsl_sf_result * theta) nogil 38 | 39 | double gsl_sf_angle_restrict_symm(double theta) nogil 40 | 41 | int gsl_sf_angle_restrict_symm_e(double * theta) nogil 42 | 43 | double gsl_sf_angle_restrict_pos(double theta) nogil 44 | 45 | int gsl_sf_angle_restrict_pos_e(double * theta) nogil 46 | 47 | double gsl_sf_sin_err(double x, double dx) nogil 48 | 49 | int gsl_sf_sin_err_e(double x, double dx, gsl_sf_result * result) nogil 50 | 51 | double gsl_sf_cos_err(double x, double dx) nogil 52 | 53 | int gsl_sf_cos_err_e(double x, double dx, gsl_sf_result * result) nogil 54 | 55 | -------------------------------------------------------------------------------- /cython_gsl/gsl_fit.pxd: -------------------------------------------------------------------------------- 1 | cdef extern from "gsl/gsl_fit.h": 2 | 3 | 4 | int gsl_fit_linear ( double * x, size_t xstride, 5 | double * y, size_t ystride, 6 | size_t n, 7 | double * c0, double * c1, 8 | double * cov00, double * cov01, double * cov11, 9 | double * sumsq) nogil 10 | 11 | 12 | int gsl_fit_wlinear ( double * x, size_t xstride, 13 | double * w, size_t wstride, 14 | double * y, size_t ystride, 15 | size_t n, double * c0, double * c1, 16 | double * cov00, double * cov01, double * cov11, 17 | double * chisq) nogil 18 | 19 | int gsl_fit_linear_est ( double x, double c0, double c1, 20 | double c00, double c01, double c11, 21 | double *y, double *y_err) nogil 22 | 23 | 24 | int gsl_fit_mul ( double * x, size_t xstride, 25 | double * y, size_t ystride, 26 | size_t n, double * c1, double * cov11, double * sumsq) nogil 27 | 28 | int gsl_fit_wmul ( double * x, size_t xstride, 29 | double * w, size_t wstride, 30 | double * y, size_t ystride, 31 | size_t n, double * c1, double * cov11, double * sumsq) nogil 32 | 33 | 34 | int gsl_fit_mul_est ( double x, double c1, double c11, 35 | double *y, double *y_err) nogil 36 | 37 | 38 | 39 | int gsl_fit_poly ( double * x, double * w, double * y, size_t n, 40 | double * c, size_t m, double * chisq) nogil 41 | 42 | int gsl_fit_fns ( double * A, double * w, double * y, 43 | size_t n, double * c, size_t m, double * chisq) nogil 44 | 45 | int gsl_fit_linear_nd (double * m, double * y, double * w) nogil 46 | 47 | -------------------------------------------------------------------------------- /cython_gsl/gsl_ellint.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_sf_ellint.h": 4 | 5 | double gsl_sf_ellint_Kcomp(double k, gsl_mode_t mode) nogil 6 | 7 | int gsl_sf_ellint_Kcomp_e(double k, gsl_mode_t mode, gsl_sf_result * result) nogil 8 | 9 | double gsl_sf_ellint_Ecomp(double k, gsl_mode_t mode) nogil 10 | 11 | int gsl_sf_ellint_Ecomp_e(double k, gsl_mode_t mode, gsl_sf_result * result) nogil 12 | 13 | double gsl_sf_ellint_F(double phi, double k, gsl_mode_t mode) nogil 14 | 15 | int gsl_sf_ellint_F_e(double phi, double k, gsl_mode_t mode, gsl_sf_result * result) nogil 16 | 17 | double gsl_sf_ellint_E(double phi, double k, gsl_mode_t mode) nogil 18 | 19 | int gsl_sf_ellint_E_e(double phi, double k, gsl_mode_t mode, gsl_sf_result * result) nogil 20 | 21 | double gsl_sf_ellint_P(double phi, double k, double n, gsl_mode_t mode) nogil 22 | 23 | int gsl_sf_ellint_P_e(double phi, double k, double n, gsl_mode_t mode, gsl_sf_result * result) nogil 24 | 25 | double gsl_sf_ellint_D(double phi, double k, double n, gsl_mode_t mode) nogil 26 | 27 | int gsl_sf_ellint_D_e(double phi, double k, double n, gsl_mode_t mode, gsl_sf_result * result) nogil 28 | 29 | double gsl_sf_ellint_RC(double x, double y, gsl_mode_t mode) nogil 30 | 31 | int gsl_sf_ellint_RC_e(double x, double y, gsl_mode_t mode, gsl_sf_result * result) nogil 32 | 33 | double gsl_sf_ellint_RD(double x, double y, double z, gsl_mode_t mode) nogil 34 | 35 | int gsl_sf_ellint_RD_e(double x, double y, double z, gsl_mode_t mode, gsl_sf_result * result) nogil 36 | 37 | double gsl_sf_ellint_RF(double x, double y, double z, gsl_mode_t mode) nogil 38 | 39 | int gsl_sf_ellint_RF_e(double x, double y, double z, gsl_mode_t mode, gsl_sf_result * result) nogil 40 | 41 | double gsl_sf_ellint_RJ(double x, double y, double z, double p, gsl_mode_t mode) nogil 42 | 43 | int gsl_sf_ellint_RJ_e(double x, double y, double z, double p, gsl_mode_t mode, gsl_sf_result * result) nogil 44 | 45 | -------------------------------------------------------------------------------- /cython_gsl/gsl_hyperg.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_sf_hyperg.h": 4 | 5 | double gsl_sf_hyperg_0F1(double c, double x) nogil 6 | 7 | int gsl_sf_hyperg_0F1_e(double c, double x, gsl_sf_result * result) nogil 8 | 9 | double gsl_sf_hyperg_1F1_int(int m, int n, double x) nogil 10 | 11 | int gsl_sf_hyperg_1F1_int_e(int m, int n, double x, gsl_sf_result * result) nogil 12 | 13 | double gsl_sf_hyperg_1F1(double a, double b, double x) nogil 14 | 15 | int gsl_sf_hyperg_1F1_e(double a, double b, double x, gsl_sf_result * result) nogil 16 | 17 | double gsl_sf_hyperg_U_int(int m, int n, double x) nogil 18 | 19 | int gsl_sf_hyperg_U_int_e(int m, int n, double x, gsl_sf_result * result) nogil 20 | 21 | int gsl_sf_hyperg_U_int_e10_e(int m, int n, double x, gsl_sf_result_e10 * result) nogil 22 | 23 | double gsl_sf_hyperg_U(double a, double b, double x) nogil 24 | 25 | int gsl_sf_hyperg_U_e(double a, double b, double x) nogil 26 | 27 | int gsl_sf_hyperg_U_e10_e(double a, double b, double x, gsl_sf_result_e10 * result) nogil 28 | 29 | double gsl_sf_hyperg_2F1(double a, double b, double c, double x) nogil 30 | 31 | int gsl_sf_hyperg_2F1_e(double a, double b, double c, double x, gsl_sf_result * result) nogil 32 | 33 | double gsl_sf_hyperg_2F1_conj(double aR, double aI, double c, double x) nogil 34 | 35 | int gsl_sf_hyperg_2F1_conj_e(double aR, double aI, double c, double x, gsl_sf_result * result) nogil 36 | 37 | double gsl_sf_hyperg_2F1_renorm(double a, double b, double c, double x) nogil 38 | 39 | int gsl_sf_hyperg_2F1_renorm_e(double a, double b, double c, double x, gsl_sf_result * result) nogil 40 | 41 | double gsl_sf_hyperg_2F1_conj_renorm(double aR, double aI, double c, double x) nogil 42 | 43 | int gsl_sf_hyperg_2F1_conj_renorm_e(double aR, double aI, double c, double x, gsl_sf_result * result) nogil 44 | 45 | double gsl_sf_hyperg_2F0(double a, double b, double x) nogil 46 | 47 | int gsl_sf_hyperg_2F0_e(double a, double b, double x, gsl_sf_result * result) nogil 48 | 49 | -------------------------------------------------------------------------------- /cython_gsl/gsl_monte.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_monte.h": 4 | ctypedef struct gsl_monte_function: 5 | double (*f) (double * x_array, size_t dim, void * params) nogil 6 | size_t dim 7 | void * params 8 | 9 | cdef extern from "gsl/gsl_monte_plain.h": 10 | ctypedef struct gsl_monte_plain_state 11 | 12 | int gsl_monte_plain_integrate ( gsl_monte_function * f, \ 13 | double xl[], double xu[], \ 14 | size_t dim, size_t calls, gsl_rng * r, \ 15 | gsl_monte_plain_state * state, \ 16 | double *result, double *abserr) nogil 17 | 18 | gsl_monte_plain_state* gsl_monte_plain_alloc(size_t dim) nogil 19 | 20 | int gsl_monte_plain_init(gsl_monte_plain_state* state) nogil 21 | 22 | void gsl_monte_plain_free (gsl_monte_plain_state* state) nogil 23 | 24 | cdef extern from "gsl/gsl_monte_miser.h": 25 | 26 | ctypedef struct gsl_monte_miser_state 27 | 28 | int gsl_monte_miser_integrate(gsl_monte_function * f, double xl[], double xh[], size_t dim, size_t calls, gsl_rng *r, gsl_monte_miser_state* state, double *result, double *abserr) nogil 29 | 30 | gsl_monte_miser_state* gsl_monte_miser_alloc(size_t dim) nogil 31 | 32 | int gsl_monte_miser_init(gsl_monte_miser_state* state) nogil 33 | 34 | void gsl_monte_miser_free(gsl_monte_miser_state* state) nogil 35 | 36 | # vegas 37 | cdef extern from "gsl/gsl_monte_vegas.h": 38 | cdef enum: 39 | GSL_VEGAS_MODE_IMPORTANCE = 1 40 | GSL_VEGAS_MODE_IMPORTANCE_ONLY = 0 41 | GSL_VEGAS_MODE_STRATIFIED = -1 42 | 43 | ctypedef struct gsl_monte_vegas_state: 44 | double chisq 45 | 46 | int gsl_monte_vegas_integrate(gsl_monte_function * f, double xl[], double xu[], size_t dim, size_t calls, gsl_rng * r, gsl_monte_vegas_state *state, double* result, double* abserr) nogil 47 | 48 | gsl_monte_vegas_state* gsl_monte_vegas_alloc(size_t dim) nogil 49 | 50 | int gsl_monte_vegas_init(gsl_monte_vegas_state* state) nogil 51 | 52 | void gsl_monte_vegas_free (gsl_monte_vegas_state* state) nogil 53 | -------------------------------------------------------------------------------- /cython_gsl/gsl_sum.pxd: -------------------------------------------------------------------------------- 1 | cdef extern from "gsl/gsl_sum.h": 2 | 3 | ctypedef struct gsl_sum_levin_u_workspace: 4 | size_t terms_used 5 | double sum_plain 6 | 7 | gsl_sum_levin_u_workspace *gsl_sum_levin_u_alloc (size_t n) nogil 8 | void gsl_sum_levin_u_free (gsl_sum_levin_u_workspace * w) nogil 9 | 10 | 11 | int gsl_sum_levin_u_accel ( double *array, 12 | size_t n, 13 | gsl_sum_levin_u_workspace * w, 14 | double *sum_accel, double *abserr) nogil 15 | 16 | 17 | int gsl_sum_levin_u_minmax ( double *array, 18 | size_t n, 19 | size_t min_terms, 20 | size_t max_terms, 21 | gsl_sum_levin_u_workspace * w, 22 | double *sum_accel, double *abserr) nogil 23 | 24 | 25 | int gsl_sum_levin_u_step ( double term, 26 | size_t n, 27 | size_t nmax, 28 | gsl_sum_levin_u_workspace * w, 29 | double *sum_accel) nogil 30 | 31 | 32 | ctypedef struct gsl_sum_levin_utrunc_workspace 33 | 34 | gsl_sum_levin_utrunc_workspace *gsl_sum_levin_utrunc_alloc (size_t n) nogil 35 | void gsl_sum_levin_utrunc_free (gsl_sum_levin_utrunc_workspace * w) nogil 36 | 37 | int gsl_sum_levin_utrunc_accel ( double *array, size_t n, 38 | gsl_sum_levin_utrunc_workspace * w, 39 | double *sum_accel, double *abserr_trunc) nogil 40 | 41 | int gsl_sum_levin_utrunc_minmax ( double *array, size_t n, 42 | size_t min_terms, 43 | size_t max_terms, 44 | gsl_sum_levin_utrunc_workspace * w, 45 | double *sum_accel, double *abserr_trunc) nogil 46 | 47 | int gsl_sum_levin_utrunc_step ( double term, size_t n, 48 | gsl_sum_levin_utrunc_workspace * w, 49 | double *sum_accel) nogil 50 | 51 | -------------------------------------------------------------------------------- /cython_gsl/__init__.py: -------------------------------------------------------------------------------- 1 | # CythonGSL provides a set of Cython declarations for the GNU Scientific Library (GSL). 2 | # Copyright (C) 2012 Thomas V. Wiecki 3 | # 4 | # This program is free software: you can redistribute it and/or modify 5 | # it under the terms of the GNU General Public License as published by 6 | # the Free Software Foundation, either version 3 of the License, or 7 | # (at your option) any later version. 8 | # 9 | # This program is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU General Public License 15 | # along with this program. If not, see . 16 | 17 | import os 18 | import subprocess 19 | 20 | 21 | def get_include(): 22 | try: 23 | gsl_include = subprocess.check_output('gsl-config --cflags', shell=True).decode('utf-8')[2:-1] 24 | except subprocess.CalledProcessError: 25 | gsl_include = os.getenv('LIB_GSL') 26 | if gsl_include is None: 27 | # Environmental variable LIB_GSL not set, use hardcoded path. 28 | gsl_include = r"c:\Program Files\GnuWin32\include" 29 | else: 30 | gsl_include += "/include" 31 | 32 | assert gsl_include != '', "Couldn't find gsl. Make sure it's installed and in the path." 33 | 34 | return gsl_include 35 | 36 | 37 | def get_library_dir(): 38 | try: 39 | lib_gsl_dir = subprocess.check_output('gsl-config --libs', shell=True).decode('utf-8').split()[0][2:] 40 | except subprocess.CalledProcessError: 41 | lib_gsl_dir = os.getenv('LIB_GSL') 42 | if lib_gsl_dir is None: 43 | # Environmental variable LIB_GSL not set, use hardcoded path. 44 | lib_gsl_dir = r"c:\Program Files\GnuWin32\lib" 45 | else: 46 | lib_gsl_dir += "/lib" 47 | 48 | return lib_gsl_dir 49 | 50 | 51 | def get_libraries(): 52 | return ['gsl', 'gslcblas'] 53 | 54 | 55 | def get_cython_include_dir(): 56 | import cython_gsl 57 | return os.path.split(cython_gsl.__path__[0])[0] 58 | -------------------------------------------------------------------------------- /cython_gsl/gsl_airy.pxd: -------------------------------------------------------------------------------- 1 | # CythonGSL Copyright (C) 2012 Thomas V. Wiecki 2 | # This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 3 | # This is free software, and you are welcome to redistribute it 4 | # under certain conditions; type `show c' for details. 5 | 6 | from cython_gsl cimport * 7 | 8 | cdef extern from "gsl/gsl_sf_airy.h": 9 | 10 | double gsl_sf_airy_Ai(double x, gsl_mode_t mode) nogil 11 | 12 | int gsl_sf_airy_Ai_e(double x, gsl_mode_t mode, gsl_sf_result * result) nogil 13 | 14 | double gsl_sf_airy_Bi(double x, gsl_mode_t mode) nogil 15 | 16 | int gsl_sf_airy_Bi_e(double x, gsl_mode_t mode, gsl_sf_result * result) nogil 17 | 18 | double gsl_sf_airy_Ai_scaled(double x, gsl_mode_t mode) nogil 19 | 20 | int gsl_sf_airy_Ai_scaled_e(double x, gsl_mode_t mode, gsl_sf_result * result) nogil 21 | 22 | double gsl_sf_airy_Bi_scaled(double x, gsl_mode_t mode) nogil 23 | 24 | int gsl_sf_airy_Bi_scaled_e(double x, gsl_mode_t mode, gsl_sf_result * result) nogil 25 | 26 | double gsl_sf_airy_Ai_deriv(double x, gsl_mode_t mode) nogil 27 | 28 | int gsl_sf_airy_Ai_deriv_e(double x, gsl_mode_t mode, gsl_sf_result * result) nogil 29 | 30 | double gsl_sf_airy_Bi_deriv(double x, gsl_mode_t mode) nogil 31 | 32 | int gsl_sf_airy_Bi_deriv_e(double x, gsl_mode_t mode, gsl_sf_result * result) nogil 33 | 34 | double gsl_sf_airy_Ai_deriv_scaled(double x, gsl_mode_t mode) nogil 35 | 36 | int gsl_sf_airy_Ai_deriv_scaled_e(double x, gsl_mode_t mode, gsl_sf_result * result) nogil 37 | 38 | double gsl_sf_airy_Bi_deriv_scaled(double x, gsl_mode_t mode) nogil 39 | 40 | int gsl_sf_airy_Bi_deriv_scaled_e(double x, gsl_mode_t mode, gsl_sf_result * result) nogil 41 | 42 | double gsl_sf_airy_zero_Ai(unsigned int s) nogil 43 | 44 | int gsl_sf_airy_zero_Ai_e(unsigned int s, gsl_sf_result * result) nogil 45 | 46 | double gsl_sf_airy_zero_Bi(unsigned int s) nogil 47 | 48 | int gsl_sf_airy_zero_Bi_e(unsigned int s, gsl_sf_result * result) nogil 49 | 50 | double gsl_sf_airy_zero_Ai_deriv(unsigned int s) nogil 51 | 52 | int gsl_sf_airy_zero_Ai_deriv_e(unsigned int s, gsl_sf_result * result) nogil 53 | 54 | double gsl_sf_airy_zero_Bi_deriv(unsigned int s) nogil 55 | 56 | int gsl_sf_airy_zero_Bi_deriv_e(unsigned int s, gsl_sf_result * result) nogil 57 | 58 | -------------------------------------------------------------------------------- /cython_gsl/test/poly.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | 4 | def t_gsl_poly_eval(): 5 | cdef double c[2] 6 | c[0] = 1; c[1] = 2 7 | # c[0] + c[1] x for x = 3 8 | return gsl_poly_eval(c, 2, 3) - 7 9 | 10 | def t_gsl_poly_dd_init(): 11 | cdef double xa[4], ya[4], dd[4] 12 | xa[0]=0; xa[1]=2; xa[2]=4; xa[3]=6 13 | ya[0]=0; ya[1]=10; ya[2]=0; ya[3]=-10 14 | cdef double x01, x12, x23, x012 15 | x01 = (ya[0] - ya[1])/(xa[0] - xa[1]) 16 | x12 = (ya[1] - ya[2])/(xa[1] - xa[2]) 17 | x23 = (ya[2] - ya[3])/(xa[2] - xa[3]) 18 | x012 = (x01 - x12)/(xa[0] - xa[2]) 19 | # dd[3] not checked 20 | gsl_poly_dd_init(dd, xa, ya, 4) 21 | return (dd[0] -ya[0], dd[1] -x01, dd[2] - x012) 22 | 23 | 24 | def t_gsl_poly_solve_quadratic(): 25 | cdef double x0, x1 26 | gsl_poly_solve_quadratic(1, -3, 2, &x0, &x1) 27 | return (x0 - 1, x1 - 2) 28 | 29 | def t_gsl_poly_complex_solve_quadratic(): 30 | cdef gsl_complex z0, z1 31 | gsl_poly_complex_solve_quadratic(1, 0, 1, &z0, &z1) 32 | return(GSL_REAL(z0), GSL_IMAG(z0) +1, GSL_REAL(z1), GSL_IMAG(z1) -1) 33 | 34 | 35 | def t_gsl_poly_solve_cubic1(): 36 | cdef int roots 37 | cdef double x0, x1, x2 38 | roots = gsl_poly_solve_cubic(-5, 8, -4, &x0, &x1, &x2) 39 | return (roots - 3, x0 - 1, x1 - 2, x2 - 2) 40 | 41 | def t_gsl_poly_solve_cubic2(): 42 | cdef int roots 43 | cdef double x0, x1, x2 44 | roots = gsl_poly_solve_cubic(-2, 1, -2, &x0, &x1, &x2) 45 | return (roots - 1, x0 - 2) 46 | 47 | 48 | def t_gsl_poly_complex_solve_cubic1(): 49 | cdef gsl_complex z0, z1, z2 50 | gsl_poly_complex_solve_cubic(-5, 8, -4, &z0, &z1, &z2) 51 | return(GSL_REAL(z0) - 1, GSL_IMAG(z0), GSL_REAL(z1) -2 , GSL_IMAG(z1), 52 | GSL_REAL(z2) - 2, GSL_IMAG(z2)) 53 | 54 | 55 | def t_gsl_poly_complex_solve_cubic2(): 56 | cdef gsl_complex z0, z1, z2 57 | gsl_poly_complex_solve_cubic(-2, 1, -2, &z0, &z1, &z2) 58 | return(GSL_REAL(z0), GSL_IMAG(z0) + 1, GSL_REAL(z1), GSL_IMAG(z1) - 1, 59 | GSL_REAL(z2) - 2, GSL_IMAG(z2) ) 60 | 61 | def t_gsl_poly_complex_solve(): 62 | cdef double a[5], z[10] 63 | a[0] = 6; a[1] = -5; a[2] = 7; a[3] = -5; a[4] = 1 64 | cdef gsl_poly_complex_workspace * w 65 | w = gsl_poly_complex_workspace_alloc(5) 66 | gsl_poly_complex_solve(a, 5, w, z) 67 | gsl_poly_complex_workspace_free (w) 68 | return (z[0], z[1] - 1, z[2], z[3] + 1, z[4] -3, z[5] - 0, z[6] -2, z[7]) 69 | -------------------------------------------------------------------------------- /examples/monte.pyx: -------------------------------------------------------------------------------- 1 | #cython: cdivision=True 2 | 3 | from cython_gsl cimport * 4 | 5 | cdef double exact 6 | exact = 1.3932039296856768591842462603255 7 | 8 | cdef double g (double *k, size_t dim, void *params) nogil: 9 | cdef double A 10 | A = 1.0 / (M_PI * M_PI * M_PI) 11 | return A / (1.0 - cos (k[0]) * cos (k[1]) * cos (k[2])) 12 | 13 | 14 | cdef void display_results (char *title, double result, double error): 15 | print "%s ==================\n" % title 16 | print "result = % .6f\n" % result 17 | print "sigma = % .6f\n" % error 18 | print "exact = % .6f\n" % exact 19 | print "error = % .6f = %.1g sigma\n" % (result - exact, 20 | float(result - exact) / error) 21 | 22 | 23 | def main (): 24 | cdef double res, err 25 | 26 | cdef double xl[3] 27 | xl[0] = 0 28 | xl[1] = 0 29 | xl[2] = 0 30 | cdef double xu[3] 31 | xu[0] = M_PI 32 | xu[1] = M_PI 33 | xu[2] = M_PI 34 | 35 | cdef gsl_rng_type *T 36 | cdef gsl_rng *r 37 | 38 | cdef gsl_monte_function G 39 | G.f = &g 40 | G.dim = 3 41 | G.params = NULL 42 | 43 | cdef size_t calls 44 | calls = 500000 45 | 46 | gsl_rng_env_setup () 47 | 48 | T = gsl_rng_default 49 | r = gsl_rng_alloc (T) 50 | 51 | cdef gsl_monte_plain_state *s 52 | s = gsl_monte_plain_alloc (3) 53 | gsl_monte_plain_integrate (&G, xl, xu, 3, calls, r, s, 54 | &res, &err) 55 | gsl_monte_plain_free (s) 56 | 57 | display_results ("plain", res, err) 58 | 59 | 60 | 61 | 62 | cdef gsl_monte_miser_state *sm 63 | sm = gsl_monte_miser_alloc (3) 64 | gsl_monte_miser_integrate (&G, xl, xu, 3, calls, r, sm, 65 | &res, &err) 66 | gsl_monte_miser_free (sm) 67 | 68 | display_results ("miser", res, err) 69 | 70 | print "DB: start vegas" 71 | 72 | cdef gsl_monte_vegas_state *sv 73 | sv = gsl_monte_vegas_alloc (3) 74 | 75 | gsl_monte_vegas_integrate (&G, xl, xu, 3, 10000, r, sv, 76 | &res, &err) 77 | display_results ("vegas warm-up", res, err) 78 | 79 | print "converging...\n" 80 | 81 | while (float(sv.chisq - 1.0) > 0.5): 82 | gsl_monte_vegas_integrate (&G, xl, xu, 3, calls/5, r, sv, 83 | &res, &err) 84 | print "result = % .6f sigma = % .6f chisq/dof = %.1f\n" % \ 85 | (res, err, sv.chisq) 86 | 87 | display_results ("vegas final", res, err) 88 | 89 | gsl_monte_vegas_free (sv) 90 | -------------------------------------------------------------------------------- /cython_gsl/test/spblas.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | cimport numpy as np 3 | import numpy as np 4 | from scipy.sparse import csc_matrix 5 | 6 | def t_gsl_spblas_dgemm(): 7 | # A = [[ 1, 0, 2 ], 8 | # [ 0, 3, 0]] 9 | cdef size_t a_m = 2 10 | cdef size_t a_n = 3 11 | cdef size_t a_nnz = 3 12 | cdef double[3] a 13 | a[0] = 1.0 14 | a[1] = 3.0 15 | a[2] = 2.0 16 | cdef size_t[4] ja 17 | ja[0] = 0 18 | ja[1] = 1 19 | ja[2] = 2 20 | ja[3] = 3 21 | cdef size_t[3] ia 22 | ia[0] = 0 23 | ia[1] = 1 24 | ia[2] = 0 25 | cdef gsl_spmatrix *A = gsl_spmatrix_alloc_nzmax(a_m, a_n, a_nnz, GSL_SPMATRIX_CCS) 26 | A.i = &ia[0] 27 | A.p = &ja[0] 28 | A.data = &a[0] 29 | A.nz = a_nnz 30 | A.nzmax = a_nnz 31 | 32 | # B = [[0, 0, 0, 4], 33 | # [0, 0, 5, 6], 34 | # [0, 0, 0, 0]] 35 | cdef size_t b_m = 3 36 | cdef size_t b_n = 4 37 | cdef size_t b_nnz = 3 38 | cdef double[3] b 39 | b[0] = 5.0 40 | b[1] = 4.0 41 | b[2] = 6.0 42 | cdef size_t[5] jb 43 | jb[0] = 0 44 | jb[1] = 0 45 | jb[2] = 0 46 | jb[3] = 1 47 | jb[4] = 3 48 | cdef size_t[3] ib 49 | ib[0] = 1 50 | ib[1] = 0 51 | ib[2] = 1 52 | cdef gsl_spmatrix *B = gsl_spmatrix_alloc_nzmax(b_m, b_n, b_nnz, GSL_SPMATRIX_CCS) 53 | B.i = &ib[0] 54 | B.p = &jb[0] 55 | B.data = &b[0] 56 | B.nz = b_nnz 57 | B.nzmax = b_nnz 58 | 59 | # Expected : C = A * B 60 | # C = [[0, 0, 0, 4], 61 | # [0, 0, 15, 18]] 62 | cdef gsl_spmatrix * C = gsl_spmatrix_alloc_nzmax(a_m, b_n, 3, GSL_SPMATRIX_CCS) 63 | gsl_spblas_dgemm(1.0, A, B, C) 64 | 65 | c_nnz = C.nz 66 | c = np.asarray( C.data) 67 | ic = np.asarray( C.i) 68 | jc = np.asarray( C.p) 69 | C_dense = csc_matrix((c, ic, jc), shape=(a_m, b_n)).todense().tolist() 70 | return C_dense 71 | 72 | def t_gsl_spblas_dgemv(): 73 | # A = [[ 1, 0, 2 ], 74 | # [ 0, 3, 0]] 75 | cdef size_t a_m = 2 76 | cdef size_t a_n = 3 77 | cdef size_t a_nnz = 3 78 | cdef double[3] a 79 | a[0] = 1.0 80 | a[1] = 3.0 81 | a[2] = 2.0 82 | cdef size_t[4] ja 83 | ja[0] = 0 84 | ja[1] = 1 85 | ja[2] = 2 86 | ja[3] = 3 87 | cdef size_t[3] ia 88 | ia[0] = 0 89 | ia[1] = 1 90 | ia[2] = 0 91 | cdef gsl_spmatrix *A = gsl_spmatrix_alloc_nzmax(a_m, a_n, a_nnz, GSL_SPMATRIX_CCS) 92 | A.i = &ia[0] 93 | A.p = &ja[0] 94 | A.data = &a[0] 95 | A.nz = a_nnz 96 | A.nzmax = a_nnz 97 | 98 | # x = [4, 5, 6] 99 | cdef gsl_vector * x = gsl_vector_calloc(a_n) 100 | x.data[0] = 4.0 101 | x.data[1] = 5.0 102 | x.data[2] = 6.0 103 | 104 | # Expected : y = A * x 105 | # y = [16, 15] 106 | cdef gsl_vector * y = gsl_vector_calloc(a_m) 107 | gsl_spblas_dgemv(CblasNoTrans, 1.0, A, x, 1.0, y) 108 | 109 | return np.asarray( y.data).tolist() 110 | -------------------------------------------------------------------------------- /cython_gsl/gsl_roots.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_roots.h": 4 | 5 | ctypedef struct gsl_root_fsolver_type: 6 | char *name 7 | size_t size 8 | int (*set) (void *state, gsl_function * f, double * root, double x_lower, double x_upper) nogil 9 | int (*iterate) (void *state, gsl_function * f, double * root, double * x_lower, double * x_upper) nogil 10 | 11 | ctypedef struct gsl_root_fsolver: 12 | gsl_root_fsolver_type * type 13 | gsl_function * function 14 | double root 15 | double x_lower 16 | double x_upper 17 | void *state 18 | 19 | ctypedef struct gsl_root_fdfsolver_type: 20 | char *name 21 | size_t size 22 | int (*set) (void *state, gsl_function_fdf * f, double * root) nogil 23 | int (*iterate) (void *state, gsl_function_fdf * f, double * root) nogil 24 | 25 | ctypedef struct gsl_root_fdfsolver: 26 | gsl_root_fdfsolver_type * type 27 | gsl_function_fdf * fdf 28 | double root 29 | void *state 30 | 31 | gsl_root_fsolver * gsl_root_fsolver_alloc ( gsl_root_fsolver_type * T) nogil 32 | void gsl_root_fsolver_free (gsl_root_fsolver * s) nogil 33 | 34 | int gsl_root_fsolver_set (gsl_root_fsolver * s, gsl_function * f, 35 | double x_lower, double x_upper) nogil 36 | 37 | int gsl_root_fsolver_iterate (gsl_root_fsolver * s) nogil 38 | 39 | char * gsl_root_fsolver_name ( gsl_root_fsolver * s) nogil 40 | double gsl_root_fsolver_root ( gsl_root_fsolver * s) nogil 41 | double gsl_root_fsolver_x_lower ( gsl_root_fsolver * s) nogil 42 | double gsl_root_fsolver_x_upper ( gsl_root_fsolver * s) nogil 43 | 44 | 45 | gsl_root_fdfsolver * gsl_root_fdfsolver_alloc ( gsl_root_fdfsolver_type * T) nogil 46 | 47 | int gsl_root_fdfsolver_set (gsl_root_fdfsolver * s, 48 | gsl_function_fdf * fdf, double root) nogil 49 | 50 | int gsl_root_fdfsolver_iterate (gsl_root_fdfsolver * s) nogil 51 | 52 | void gsl_root_fdfsolver_free (gsl_root_fdfsolver * s) nogil 53 | 54 | char * gsl_root_fdfsolver_name ( gsl_root_fdfsolver * s) nogil 55 | double gsl_root_fdfsolver_root ( gsl_root_fdfsolver * s) nogil 56 | 57 | int gsl_root_test_interval (double x_lower, double x_upper, double epsabs, double epsrel) nogil 58 | 59 | int gsl_root_test_residual (double f, double epsabs) nogil 60 | 61 | int gsl_root_test_delta (double x1, double x0, double epsabs, double epsrel) nogil 62 | 63 | gsl_root_fsolver_type * gsl_root_fsolver_bisection 64 | gsl_root_fsolver_type * gsl_root_fsolver_brent 65 | gsl_root_fsolver_type * gsl_root_fsolver_falsepos 66 | gsl_root_fdfsolver_type * gsl_root_fdfsolver_newton 67 | gsl_root_fdfsolver_type * gsl_root_fdfsolver_secant 68 | gsl_root_fdfsolver_type * gsl_root_fdfsolver_steffenson 69 | -------------------------------------------------------------------------------- /cython_gsl/gsl_permutation.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_permutation.h": 4 | 5 | ctypedef struct gsl_permutation: 6 | size_t size 7 | size_t *data 8 | 9 | # Allocation 10 | gsl_permutation * gsl_permutation_alloc(size_t n) nogil 11 | 12 | gsl_permutation * gsl_permutation_calloc(size_t n) nogil 13 | 14 | void gsl_permutation_init(gsl_permutation * p) nogil 15 | 16 | void gsl_permutation_free(gsl_permutation * p) nogil 17 | 18 | # Reading and writing permutations 19 | int gsl_permutation_fread(FILE * stream, gsl_permutation * p) nogil 20 | 21 | int gsl_permutation_fwrite(FILE * stream, gsl_permutation * p) nogil 22 | 23 | int gsl_permutation_fscanf(FILE * stream, gsl_permutation * p) nogil 24 | 25 | int gsl_permutation_fprintf(FILE * stream, gsl_permutation * p, char *format) nogil 26 | 27 | # Permutation properties 28 | size_t gsl_permutation_size(gsl_permutation * p) nogil 29 | 30 | size_t * gsl_permutation_data(gsl_permutation * p) nogil 31 | 32 | int gsl_permutation_memcpy(gsl_permutation * dest, gsl_permutation * src) nogil 33 | 34 | size_t gsl_permutation_get(gsl_permutation * p, size_t i) nogil 35 | 36 | int gsl_permutation_swap(gsl_permutation * p, size_t i, size_t j) nogil 37 | 38 | int gsl_permutation_valid(gsl_permutation * p) nogil 39 | 40 | # Permutation functions 41 | void gsl_permutation_reverse(gsl_permutation * p) nogil 42 | 43 | int gsl_permutation_inverse(gsl_permutation * inv, gsl_permutation * p) nogil 44 | 45 | int gsl_permutation_next(gsl_permutation * p) nogil 46 | 47 | int gsl_permutation_prev(gsl_permutation * p) nogil 48 | 49 | int gsl_permutation_mul(gsl_permutation * p, gsl_permutation * pa, gsl_permutation * pb) nogil 50 | 51 | # Permutations in Cyclic Form 52 | int gsl_permutation_linear_to_canonical(gsl_permutation * q, gsl_permutation * p) nogil 53 | 54 | int gsl_permutation_canonical_to_linear(gsl_permutation * p, gsl_permutation * q) nogil 55 | 56 | size_t gsl_permutation_inversions(gsl_permutation * p) nogil 57 | 58 | size_t gsl_permutation_linear_cycles(gsl_permutation * p) nogil 59 | 60 | size_t gsl_permutation_canonical_cycles(gsl_permutation * q) nogil 61 | 62 | 63 | 64 | # Applying Permutations 65 | cdef extern from "gsl/gsl_permute_double.h": 66 | int gsl_permute(size_t * p, double * data, size_t stride, size_t n) nogil 67 | 68 | int gsl_permute_inverse(size_t * p, double * data, size_t stride, size_t n) nogil 69 | 70 | 71 | cdef extern from "gsl/gsl_permute_vector_double.h": 72 | int gsl_permute_vector(gsl_permutation * p, gsl_vector * v) nogil 73 | 74 | int gsl_permute_vector_inverse(gsl_permutation * p, gsl_vector * v) nogil 75 | 76 | cdef extern from "gsl/gsl_permute_vector_complex_double.h": 77 | int gsl_permute_vector_complex ( gsl_permutation * p, gsl_vector_complex * v) nogil 78 | 79 | int gsl_permute_vector_complex_inverse ( gsl_permutation * p, gsl_vector_complex * v) nogil 80 | 81 | -------------------------------------------------------------------------------- /cython_gsl/gsl_math.pxd: -------------------------------------------------------------------------------- 1 | cdef extern from "gsl/gsl_math.h": 2 | 3 | double M_E 4 | 5 | double M_LOG2E 6 | 7 | double M_LOG10E 8 | 9 | double M_SQRT2 10 | 11 | double M_SQRT1_2 12 | 13 | double M_SQRT3 14 | 15 | double M_PI 16 | 17 | double M_PI_2 18 | 19 | double M_PI_4 20 | 21 | double M_SQRTPI 22 | 23 | double M_2_SQRTPI 24 | 25 | double M_1_PI 26 | 27 | double M_2_PI 28 | 29 | double M_LN10 30 | 31 | double M_LN2 32 | 33 | double M_LNPI 34 | 35 | double M_EULER 36 | 37 | int gsl_isnan(double x) nogil 38 | 39 | int gsl_isinf(double x) nogil 40 | 41 | int gsl_finite(double x) nogil 42 | 43 | double gsl_log1p(double x) nogil 44 | 45 | double gsl_expm1(double x) nogil 46 | 47 | double gsl_hypot(double x, double y) nogil 48 | 49 | double gsl_acosh(double x) nogil 50 | 51 | double gsl_asinh(double x) nogil 52 | 53 | double gsl_atanh(double x) nogil 54 | 55 | double gsl_ldexp(double x, int e) nogil 56 | 57 | double gsl_frexp(double x, int * e) nogil 58 | 59 | double gsl_pow_int(double x, int n) nogil 60 | 61 | double gsl_pow_2(double x) nogil 62 | 63 | double gsl_pow_3(double x) nogil 64 | 65 | double gsl_pow_4(double x) nogil 66 | 67 | double gsl_pow_5(double x) nogil 68 | 69 | double gsl_pow_6(double x) nogil 70 | 71 | double gsl_pow_7(double x) nogil 72 | 73 | double gsl_pow_8(double x) nogil 74 | 75 | double gsl_pow_9(double x) nogil 76 | 77 | int GSL_SIGN(double x) nogil 78 | 79 | int GSL_IS_ODD(int n) nogil 80 | 81 | int GSL_IS_EVEN(int n) nogil 82 | 83 | double GSL_MAX(double a, double b) nogil 84 | 85 | double GSL_MIN(double a, double b) nogil 86 | 87 | double GSL_MAX_DBL(double a, double b) nogil 88 | 89 | double GSL_MIN_DBL(double a, double b) nogil 90 | 91 | int GSL_MAX_INT(int a, int b) nogil 92 | 93 | int GSL_MIN_INT(int a, int b) nogil 94 | 95 | long double GSL_MAX_LDBL(long double a, long double b) nogil 96 | 97 | long double GSL_MIN_LDBL(long double a, long double b) nogil 98 | 99 | int gsl_fcmp(double x, double y, double epsilon) nogil 100 | 101 | # Definition of an arbitrary function with parameters 102 | ctypedef struct gsl_function: 103 | double (* function) (double x, void * params) nogil 104 | void * params 105 | 106 | double GSL_FN_EVAL(gsl_function * F, double x) nogil 107 | 108 | # Definition of an arbitrary function returning two values, r1, r2 109 | ctypedef struct gsl_function_fdf: 110 | double (* f) (double x, void * params) nogil 111 | double (* df) (double x, void * params) nogil 112 | void (* fdf) (double x, void * params, double * f, double * df) nogil 113 | void * params 114 | 115 | double GSL_FN_FDF_EVAL_F(gsl_function_fdf * FDF, double x) nogil 116 | 117 | GSL_FN_FDF_EVAL_DF(gsl_function_fdf * FDF,double x) nogil 118 | 119 | GSL_FN_FDF_EVAL_F_DF(gsl_function_fdf * FDF,double x, double y,double dy) nogil 120 | 121 | -------------------------------------------------------------------------------- /cython_gsl/test/test_vector_complex.py: -------------------------------------------------------------------------------- 1 | import unittest, vector_complex, os 2 | 3 | class Block_complexTest(unittest.TestCase): 4 | 5 | def test_gsl_block_complex(self): 6 | t = vector_complex.t_gsl_block_complex() 7 | for x in t: 8 | self.assertAlmostEqual(x,0, 15) 9 | 10 | def test_gsl_vector_complex_set(self): 11 | t = vector_complex.t_gsl_vector_complex_set() 12 | for x in t: 13 | self.assertAlmostEqual(x,0, 15) 14 | 15 | def test_gsl_vector_complex_fprintf(self): 16 | t = vector_complex.t_gsl_vector_complex_fprintf() 17 | self.assertEqual(t,True) 18 | os.unlink('test.dat') 19 | 20 | def test_gsl_vector_complex_set_zero(self): 21 | t = vector_complex.t_gsl_vector_complex_set_zero() 22 | for x in t: 23 | self.assertAlmostEqual(x,0, 15) 24 | 25 | def test_gsl_vector_complex_set_all(self): 26 | t = vector_complex.t_gsl_vector_complex_set_all() 27 | for x in t: 28 | self.assertAlmostEqual(x,0, 15) 29 | 30 | def test_gsl_vector_complex_set_basis(self): 31 | t = vector_complex.t_gsl_vector_complex_set_basis() 32 | for x in t: 33 | self.assertAlmostEqual(x,0, 15) 34 | 35 | def test_gsl_vector_complex_memcpy(self): 36 | t = vector_complex.t_gsl_vector_complex_memcpy() 37 | for x in t: 38 | self.assertAlmostEqual(x,0, 15) 39 | 40 | def test_gsl_vector_complex_reverse(self): 41 | t = vector_complex.t_gsl_vector_complex_reverse() 42 | for x in t: 43 | self.assertAlmostEqual(x,0, 15) 44 | 45 | def test_gsl_vector_complex_swap(self): 46 | t = vector_complex.t_gsl_vector_complex_swap() 47 | for x in t: 48 | self.assertAlmostEqual(x,0, 15) 49 | 50 | def test_gsl_vector_complex_swap_elements(self): 51 | t = vector_complex.t_gsl_vector_complex_swap_elements() 52 | for x in t: 53 | self.assertAlmostEqual(x,0, 15) 54 | 55 | def test_gsl_vector_complex_real(self): 56 | t = vector_complex.t_gsl_vector_complex_real() 57 | for x in t: 58 | self.assertAlmostEqual(x,0, 15) 59 | 60 | def test_gsl_vector_complex_isnull(self): 61 | t = vector_complex.t_gsl_vector_complex_isnull() 62 | for x in t: 63 | self.assertAlmostEqual(x,0, 15) 64 | 65 | def test_gsl_vector_complex_subvector(self): 66 | t = vector_complex.t_gsl_vector_complex_subvector() 67 | for x in t: 68 | self.assertAlmostEqual(x,0, 15) 69 | 70 | def test_gsl_vector_complex_subvector_with_stride(self): 71 | t = vector_complex.t_gsl_vector_complex_subvector_with_stride() 72 | for x in t: 73 | self.assertAlmostEqual(x,0, 14) 74 | 75 | def test_gsl_vector_complex_view_array(self): 76 | t = vector_complex.t_gsl_vector_complex_view_array() 77 | for x in t: 78 | self.assertAlmostEqual(x,0, 15) 79 | 80 | if __name__ == '__main__': 81 | unittest.main() 82 | -------------------------------------------------------------------------------- /cython_gsl/gsl_min.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_min.h": 4 | 5 | ctypedef struct gsl_min_fminimizer_type: 6 | char *name 7 | size_t size 8 | int (*set) (void *state, gsl_function * f, double x_minimum, double f_minimum, double x_lower, double f_lower, double x_upper, double f_upper) nogil 9 | int (*iterate) (void *state, gsl_function * f, double * x_minimum, double * f_minimum, double * x_lower, double * f_lower, double * x_upper, double * f_upper) nogil 10 | 11 | ctypedef struct gsl_min_fminimizer: 12 | gsl_min_fminimizer_type * type 13 | gsl_function * function 14 | double x_minimum 15 | double x_lower 16 | double x_upper 17 | double f_minimum, f_lower, f_upper 18 | void *state 19 | 20 | gsl_min_fminimizer * gsl_min_fminimizer_alloc ( gsl_min_fminimizer_type * T) nogil 21 | 22 | void gsl_min_fminimizer_free (gsl_min_fminimizer * s) nogil 23 | 24 | int gsl_min_fminimizer_set (gsl_min_fminimizer * s, 25 | gsl_function * f, double x_minimum, 26 | double x_lower, double x_upper) nogil 27 | 28 | int gsl_min_fminimizer_set_with_values (gsl_min_fminimizer * s, 29 | gsl_function * f, 30 | double x_minimum, double f_minimum, 31 | double x_lower, double f_lower, 32 | double x_upper, double f_upper) nogil 33 | 34 | int gsl_min_fminimizer_iterate (gsl_min_fminimizer * s) nogil 35 | 36 | char * gsl_min_fminimizer_name ( gsl_min_fminimizer * s) nogil 37 | 38 | double gsl_min_fminimizer_x_minimum ( gsl_min_fminimizer * s) nogil 39 | double gsl_min_fminimizer_x_lower ( gsl_min_fminimizer * s) nogil 40 | double gsl_min_fminimizer_x_upper ( gsl_min_fminimizer * s) nogil 41 | double gsl_min_fminimizer_f_minimum ( gsl_min_fminimizer * s) nogil 42 | double gsl_min_fminimizer_f_lower ( gsl_min_fminimizer * s) nogil 43 | double gsl_min_fminimizer_f_upper ( gsl_min_fminimizer * s) nogil 44 | 45 | #Deprecated, use x_minimum instead 46 | #double gsl_min_fminimizer_minimum ( gsl_min_fminimizer * s) nogil 47 | 48 | int gsl_min_test_interval (double x_lower, double x_upper, double epsabs, double epsrel) nogil 49 | 50 | gsl_min_fminimizer_type * gsl_min_fminimizer_goldensection 51 | gsl_min_fminimizer_type * gsl_min_fminimizer_brent 52 | 53 | ctypedef int (*gsl_min_bracketing_function)(gsl_function *f, 54 | double *x_minimum,double * f_minimum, 55 | double *x_lower, double * f_lower, 56 | double *x_upper, double * f_upper, 57 | size_t eval_max) nogil 58 | 59 | int gsl_min_find_bracket(gsl_function *f,double *x_minimum,double * f_minimum, 60 | double *x_lower, double * f_lower, 61 | double *x_upper, double * f_upper, 62 | size_t eval_max) nogil 63 | 64 | -------------------------------------------------------------------------------- /cython_gsl/gsl_spmatrix.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | from libc.stdlib cimport * 3 | 4 | cdef extern from "gsl/gsl_spmatrix.h": 5 | ctypedef struct gsl_spmatrix_tree: 6 | void *tree 7 | void *node_array 8 | size_t n 9 | 10 | ctypedef struct gsl_spmatrix: 11 | size_t size1 12 | size_t size2 13 | size_t *i 14 | double *data 15 | size_t *p 16 | size_t nzmax 17 | size_t nz 18 | gsl_spmatrix_tree *tree_data 19 | void *work 20 | size_t sptype 21 | 22 | size_t GSL_SPMATRIX_TRIPLET 23 | size_t GSL_SPMATRIX_CCS 24 | size_t GSL_SPMATRIX_CRS 25 | 26 | int GSL_SPMATRIX_ISTRIPLET(size_t *m) nogil 27 | int GSL_SPMATRIX_ISCCS(size_t *m) nogil 28 | int GSL_SPMATRIX_ISCRS(size_t *m) nogil 29 | 30 | gsl_spmatrix *gsl_spmatrix_alloc(const size_t n1, const size_t n2) nogil 31 | 32 | gsl_spmatrix *gsl_spmatrix_alloc_nzmax(const size_t n1, const size_t n2, const size_t nzmax, const size_t flags) nogil 33 | 34 | void gsl_spmatrix_free(gsl_spmatrix *m) nogil 35 | 36 | int gsl_spmatrix_realloc(const size_t nzmax, gsl_spmatrix *m) nogil 37 | 38 | int gsl_spmatrix_set_zero(gsl_spmatrix *m) nogil 39 | 40 | size_t gsl_spmatrix_nnz(const gsl_spmatrix *m) nogil 41 | 42 | int gsl_spmatrix_compare_idx(const size_t ia, const size_t ja, const size_t ib, const size_t jb) nogil 43 | 44 | int gsl_spmatrix_tree_rebuild(gsl_spmatrix * m) nogil 45 | 46 | # /* spcopy.c */ 47 | int gsl_spmatrix_memcpy(gsl_spmatrix *dest, const gsl_spmatrix *src) nogil 48 | 49 | # /* spgetset.c */ 50 | double gsl_spmatrix_get(const gsl_spmatrix *m, const size_t i, const size_t j) nogil 51 | 52 | int gsl_spmatrix_set(gsl_spmatrix *m, const size_t i, const size_t j, const double x) nogil 53 | 54 | double *gsl_spmatrix_ptr(gsl_spmatrix *m, const size_t i, const size_t j) nogil 55 | 56 | # /* spcompress.c */ 57 | gsl_spmatrix *gsl_spmatrix_compcol(const gsl_spmatrix *T) nogil 58 | 59 | gsl_spmatrix *gsl_spmatrix_ccs(const gsl_spmatrix *T) nogil 60 | 61 | gsl_spmatrix *gsl_spmatrix_crs(const gsl_spmatrix *T) nogil 62 | 63 | void gsl_spmatrix_cumsum(const size_t n, size_t *c) nogil 64 | 65 | # /* spio.c */ 66 | int gsl_spmatrix_fprintf(FILE *stream, const gsl_spmatrix *m, const char *format) nogil 67 | 68 | gsl_spmatrix * gsl_spmatrix_fscanf(FILE *stream) nogil 69 | 70 | int gsl_spmatrix_fwrite(FILE *stream, const gsl_spmatrix *m) nogil 71 | 72 | int gsl_spmatrix_fread(FILE *stream, gsl_spmatrix *m) nogil 73 | 74 | # /* spoper.c */ 75 | int gsl_spmatrix_scale(gsl_spmatrix *m, const double x) nogil 76 | 77 | int gsl_spmatrix_minmax(const gsl_spmatrix *m, double *min_out, double *max_out) nogil 78 | 79 | int gsl_spmatrix_add(gsl_spmatrix *c, const gsl_spmatrix *a, const gsl_spmatrix *b) nogil 80 | 81 | int gsl_spmatrix_d2sp(gsl_spmatrix *S, const gsl_matrix *A) nogil 82 | 83 | int gsl_spmatrix_sp2d(gsl_matrix *A, const gsl_spmatrix *S) nogil 84 | 85 | # /* spprop.c */ 86 | int gsl_spmatrix_equal(const gsl_spmatrix *a, const gsl_spmatrix *b) nogil 87 | 88 | # /* spswap.c */ 89 | int gsl_spmatrix_transpose(gsl_spmatrix * m) nogil 90 | 91 | int gsl_spmatrix_transpose2(gsl_spmatrix * m) nogil 92 | 93 | int gsl_spmatrix_transpose_memcpy(gsl_spmatrix *dest, const gsl_spmatrix *src) nogil 94 | -------------------------------------------------------------------------------- /cython_gsl/gsl_gamma.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_sf_gamma.h": 4 | 5 | double gsl_sf_gamma(double x) nogil 6 | 7 | int gsl_sf_gamma_e(double x, gsl_sf_result * result) nogil 8 | 9 | double gsl_sf_lngamma(double x) nogil 10 | 11 | int gsl_sf_lngamma_e(double x, gsl_sf_result * result) nogil 12 | 13 | int gsl_sf_lngamma_sgn_e(double x, gsl_sf_result * result_lg, double * sgn) nogil 14 | 15 | double gsl_sf_gammastar(double x) nogil 16 | 17 | int gsl_sf_gammastar_e(double x, gsl_sf_result * result) nogil 18 | 19 | double gsl_sf_gammainv(double x) nogil 20 | 21 | int gsl_sf_gammainv_e(double x, gsl_sf_result * result) nogil 22 | 23 | int gsl_sf_lngamma_complex_e(double zr, double zi, gsl_sf_result * lnr, gsl_sf_result * arg) nogil 24 | 25 | double gsl_sf_taylorcoeff(int n, double x) nogil 26 | 27 | int gsl_sf_taylorcoeff_e(int n, double x, gsl_sf_result * result) nogil 28 | 29 | double gsl_sf_fact(unsigned int n) nogil 30 | 31 | int gsl_sf_fact_e(unsigned int n, gsl_sf_result * result) nogil 32 | 33 | double gsl_sf_doublefact(unsigned int n) nogil 34 | 35 | int gsl_sf_doublefact_e(unsigned int n, gsl_sf_result * result) nogil 36 | 37 | double gsl_sf_lnfact(unsigned int n) nogil 38 | 39 | int gsl_sf_lnfact_e(unsigned int n, gsl_sf_result * result) nogil 40 | 41 | double gsl_sf_lndoublefact(unsigned int n) nogil 42 | 43 | int gsl_sf_lndoublefact_e(unsigned int n, gsl_sf_result * result) nogil 44 | 45 | double gsl_sf_choose(unsigned int n, unsigned int m) nogil 46 | 47 | int gsl_sf_choose_e(unsigned int n, unsigned int m, gsl_sf_result * result) nogil 48 | 49 | double gsl_sf_lnchoose(unsigned int n, unsigned int m) nogil 50 | 51 | int gsl_sf_lnchoose_e(unsigned int n, unsigned int m, gsl_sf_result * result) nogil 52 | 53 | double gsl_sf_poch(double a, double x) nogil 54 | 55 | int gsl_sf_poch_e(double a, double x, gsl_sf_result * result) nogil 56 | 57 | double gsl_sf_lnpoch(double a, double x) nogil 58 | 59 | int gsl_sf_lnpoch_e(double a, double x, gsl_sf_result * result) nogil 60 | 61 | int gsl_sf_lnpoch_sgn_e(double a, double x, gsl_sf_result * result, double * sgn) nogil 62 | 63 | double gsl_sf_pochrel(double a, double x) nogil 64 | 65 | int gsl_sf_pochrel_e(double a, double x, gsl_sf_result * result) nogil 66 | 67 | double gsl_sf_gamma_inc_Q(double a, double x) nogil 68 | 69 | int gsl_sf_gamma_inc_Q_e(double a, double x, gsl_sf_result * result) nogil 70 | 71 | double gsl_sf_gamma_inc_P(double a, double x) nogil 72 | 73 | int gsl_sf_gamma_inc_P_e(double a, double x, gsl_sf_result * result) nogil 74 | 75 | double gsl_sf_gamma_inc(double a, double x) nogil 76 | 77 | int gsl_sf_gamma_inc_e(double a, double x, gsl_sf_result * result) nogil 78 | 79 | double gsl_sf_beta(double a, double b) nogil 80 | 81 | int gsl_sf_beta_e(double a, double b, gsl_sf_result * result) nogil 82 | 83 | double gsl_sf_lnbeta(double a, double b) nogil 84 | 85 | int gsl_sf_lnbeta_e(double a, double b, gsl_sf_result * result) nogil 86 | 87 | double gsl_sf_beta_inc(double a, double b, double x) nogil 88 | 89 | int gsl_sf_beta_inc_e(double a, double b, double x, gsl_sf_result * result) nogil 90 | 91 | -------------------------------------------------------------------------------- /cython_gsl/gsl_odeiv.pxd: -------------------------------------------------------------------------------- 1 | cdef extern from "gsl/gsl_odeiv.h": 2 | 3 | ctypedef struct gsl_odeiv_system: 4 | int (* function) (double t, double y[], double dydt[], void * params) nogil 5 | int (* jacobian) (double t, double y[], double * dfdy, double dfdt[], void * params) nogil 6 | size_t dimension 7 | void * params 8 | 9 | # no: 10 | #define GSL_ODEIV_FN_EVAL(S,t,y,f) (*((S)->function))(t,y,f,(S) nogil->params) nogil 11 | #define GSL_ODEIV_JA_EVAL(S,t,y,dfdy,dfdt)(*((S)->jacobian))(t,y,dfdy,dfdt,(S)->params) nogil 12 | 13 | 14 | 15 | 16 | ctypedef struct gsl_odeiv_step_type 17 | 18 | ctypedef struct gsl_odeiv_step 19 | 20 | gsl_odeiv_step_type *gsl_odeiv_step_rk2 21 | gsl_odeiv_step_type *gsl_odeiv_step_rk4 22 | gsl_odeiv_step_type *gsl_odeiv_step_rkf45 23 | gsl_odeiv_step_type *gsl_odeiv_step_rkck 24 | gsl_odeiv_step_type *gsl_odeiv_step_rk8pd 25 | gsl_odeiv_step_type *gsl_odeiv_step_rk2imp 26 | gsl_odeiv_step_type *gsl_odeiv_step_rk4imp 27 | gsl_odeiv_step_type *gsl_odeiv_step_bsimp 28 | gsl_odeiv_step_type *gsl_odeiv_step_gear1 29 | gsl_odeiv_step_type *gsl_odeiv_step_gear2 30 | 31 | 32 | gsl_odeiv_step * gsl_odeiv_step_alloc( gsl_odeiv_step_type * T, size_t dim) nogil 33 | int gsl_odeiv_step_reset(gsl_odeiv_step * s) nogil 34 | void gsl_odeiv_step_free(gsl_odeiv_step * s) nogil 35 | 36 | char * gsl_odeiv_step_name( gsl_odeiv_step *) nogil 37 | unsigned int gsl_odeiv_step_order( gsl_odeiv_step * s) nogil 38 | 39 | int gsl_odeiv_step_apply(gsl_odeiv_step *, double t, double h, double y[], double yerr[], double dydt_in[], double dydt_out[], gsl_odeiv_system * dydt) nogil 40 | 41 | 42 | ctypedef struct gsl_odeiv_control_type 43 | 44 | ctypedef struct gsl_odeiv_control 45 | cdef enum: 46 | GSL_ODEIV_HADJ_DEC = -1 47 | GSL_ODEIV_HADJ_NIL = 0 48 | GSL_ODEIV_HADJ_INC = 1 49 | 50 | gsl_odeiv_control * gsl_odeiv_control_alloc( gsl_odeiv_control_type * T) nogil 51 | int gsl_odeiv_control_init(gsl_odeiv_control * c, double eps_abs, double eps_rel, double a_y, double a_dydt) nogil 52 | void gsl_odeiv_control_free(gsl_odeiv_control * c) nogil 53 | int gsl_odeiv_control_hadjust (gsl_odeiv_control * c, gsl_odeiv_step * s, double y0[], double yerr[], double dydt[], double * h) nogil 54 | char * gsl_odeiv_control_name( gsl_odeiv_control * c) nogil 55 | 56 | 57 | gsl_odeiv_control * gsl_odeiv_control_standard_new(double eps_abs, double eps_rel, double a_y, double a_dydt) nogil 58 | gsl_odeiv_control * gsl_odeiv_control_y_new(double eps_abs, double eps_rel) nogil 59 | gsl_odeiv_control * gsl_odeiv_control_yp_new(double eps_abs, double eps_rel) nogil 60 | 61 | gsl_odeiv_control * gsl_odeiv_control_scaled_new(double eps_abs, double eps_rel, double a_y, double a_dydt, double scale_abs[], size_t dim) nogil 62 | 63 | ctypedef struct gsl_odeiv_evolve 64 | 65 | gsl_odeiv_evolve * gsl_odeiv_evolve_alloc(size_t dim) nogil 66 | int gsl_odeiv_evolve_apply(gsl_odeiv_evolve *, gsl_odeiv_control * con, gsl_odeiv_step * step, gsl_odeiv_system * dydt, double * t, double t1, double * h, double y[]) nogil 67 | int gsl_odeiv_evolve_reset(gsl_odeiv_evolve *) nogil 68 | void gsl_odeiv_evolve_free(gsl_odeiv_evolve *) nogil 69 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | ************ 2 | Introduction 3 | ************ 4 | 5 | :Date: May 13, 2012 6 | :Version: 0.2.1 7 | :Authors: Thomas Wiecki, thomas.wiecki[at]gmail.com 8 | :Web site: https://github.com/twiecki/CythonGSL 9 | :Copyright: This document has been placed in the public domain. 10 | :License: CythonGSL is released under the GPLv3. 11 | 12 | 13 | Purpose 14 | ======= 15 | 16 | CythonGSL provides a Cython interface for the GNU Scientific 17 | Library (GSL). 18 | 19 | Cython is the ideal tool to speed up numerical computations by 20 | converting typed Python code to C and generating Python wrappers so 21 | that these compiled functions can be called from Python. Scientific 22 | programming often requires use of various numerical routines 23 | (e.g. numerical integration, optimization). While SciPy provides many 24 | of those tools, there is an overhead associated with using these 25 | functions within your Cython code. CythonGSL allows you to shave off 26 | that last layer by providing Cython declarations for the GSL which 27 | allow you to use this high-quality library from within Cython without 28 | any Python overhead. 29 | 30 | Fork of PyrexGsl by Mario Pernici 31 | (http://wwwteor.mi.infn.it/~pernici/pyrexgsl/pyrexgsl.html). 32 | 33 | ***** 34 | Usage 35 | ***** 36 | 37 | For a quick demo, see: 38 | http://nbviewer.ipython.org/urls/raw.github.com/twiecki/CythonGSL/master/examples/cython_gsl_ipythonnb.ipynb 39 | 40 | Import CythonGSL in your pyx file 41 | ================================= 42 | 43 | In your cython file from which you want to call gsl routines, import 44 | gsl like this: 45 | 46 | :: 47 | 48 | from cython_gsl cimport * 49 | 50 | From there you should be able to call any gsl function, see 51 | http://www.gnu.org/software/gsl/manual/gsl-ref.html for the GSL 52 | reference. 53 | 54 | For more examples, check out the examples directory. 55 | 56 | Compile your module 57 | =================== 58 | 59 | Here is what your setup.py could look like: 60 | 61 | :: 62 | 63 | from distutils.core import setup 64 | from Cython.Distutils import Extension 65 | from Cython.Distutils import build_ext 66 | import cython_gsl 67 | 68 | setup( 69 | [...] 70 | include_dirs = [cython_gsl.get_include()], 71 | cmdclass = {'build_ext': build_ext}, 72 | ext_modules = [Extension("my_cython_script", 73 | ["src/my_cython_script.pyx"], 74 | libraries=cython_gsl.get_libraries(), 75 | library_dirs=[cython_gsl.get_library_dir()], 76 | include_dirs=[cython_gsl.get_cython_include_dir()])] 77 | ) 78 | 79 | 80 | ************ 81 | Installation 82 | ************ 83 | 84 | Dependencies 85 | ============ 86 | 87 | * Python 88 | * Cython (http://cython.org) 89 | * GSL (for a Windows port see 90 | https://code.google.com/p/oscats/downloads/list) 91 | 92 | Installing CythonGSL 93 | ==================== 94 | 95 | :: 96 | 97 | python setup.py build 98 | python setup.py install 99 | 100 | Installing CythonGSL Unittests 101 | ============================== 102 | 103 | :: 104 | 105 | python setup_test.py build 106 | python setup_test.py install 107 | nosetest cython_gsl 108 | 109 | -------------------------------------------------------------------------------- /cython_gsl/test/test_permutations.py: -------------------------------------------------------------------------------- 1 | import unittest, permutations, os 2 | 3 | class PermutationTest(unittest.TestCase): 4 | 5 | def test_gsl_permutation_alloc(self): 6 | t = permutations.t_gsl_permutation_alloc() 7 | for x in t: 8 | self.assertAlmostEqual(x,0, 15) 9 | 10 | def test_gsl_permutation_calloc(self): 11 | t = permutations.t_gsl_permutation_calloc() 12 | for x in t: 13 | self.assertAlmostEqual(x,0, 15) 14 | 15 | def test_gsl_permutation_get(self): 16 | t = permutations.t_gsl_permutation_get() 17 | for x in t: 18 | self.assertAlmostEqual(x,0, 15) 19 | 20 | def test_gsl_permutation_next(self): 21 | t = permutations.t_gsl_permutation_next() 22 | for x in t: 23 | self.assertAlmostEqual(x,0, 15) 24 | 25 | def test_gsl_permutation_prev(self): 26 | t = permutations.t_gsl_permutation_prev() 27 | for x in t: 28 | self.assertAlmostEqual(x,0, 15) 29 | 30 | def test_gsl_permutation_valid(self): 31 | t = permutations.t_gsl_permutation_valid() 32 | for x in t: 33 | self.assertAlmostEqual(x,0, 15) 34 | 35 | def test_gsl_permutation_swap(self): 36 | t = permutations.t_gsl_permutation_swap() 37 | for x in t: 38 | self.assertAlmostEqual(x,0, 15) 39 | 40 | def test_gsl_permutation_memcpy(self): 41 | t = permutations.t_gsl_permutation_memcpy() 42 | for x in t: 43 | self.assertAlmostEqual(x,0, 15) 44 | 45 | def test_gsl_permutation_reverse(self): 46 | t = permutations.t_gsl_permutation_reverse() 47 | for x in t: 48 | self.assertAlmostEqual(x,0, 15) 49 | 50 | def test_gsl_permutation_inverse(self): 51 | t = permutations.t_gsl_permutation_inverse() 52 | for x in t: 53 | self.assertAlmostEqual(x,0, 15) 54 | 55 | def test_gsl_permute_vector(self): 56 | t = permutations.t_gsl_permute_vector() 57 | for x in t: 58 | self.assertAlmostEqual(x,0, 15) 59 | 60 | def test_gsl_permute_vector_inverse(self): 61 | t = permutations.t_gsl_permute_vector_inverse() 62 | for x in t: 63 | self.assertAlmostEqual(x,0, 15) 64 | 65 | def test_gsl_permute_vector_complex(self): 66 | t = permutations.t_gsl_permute_vector_complex() 67 | for x in t: 68 | self.assertAlmostEqual(x,0, 15) 69 | 70 | def test_gsl_permute_vector_complex_inverse(self): 71 | t = permutations.t_gsl_permute_vector_complex_inverse() 72 | for x in t: 73 | self.assertAlmostEqual(x,0, 15) 74 | 75 | def test_gsl_permutation_linear_to_canonical(self): 76 | t = permutations.t_gsl_permutation_linear_to_canonical() 77 | for x in t: 78 | self.assertAlmostEqual(x,0, 15) 79 | 80 | def test_gsl_permutation_fprintf(self): 81 | t = permutations.t_gsl_permutation_fprintf() 82 | os.unlink('test.dat') 83 | for x in t: 84 | self.assertAlmostEqual(x,0, 15) 85 | 86 | def test_gsl_permutation_linear_cycles(self): 87 | t = permutations.t_gsl_permutation_linear_cycles() 88 | for x in t: 89 | self.assertAlmostEqual(x,0, 15) 90 | 91 | if __name__ == '__main__': 92 | unittest.main() 93 | -------------------------------------------------------------------------------- /cython_gsl/gsl_legendre.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_sf_legendre.h": 4 | 5 | double gsl_sf_legendre_P1(double x) nogil 6 | 7 | double gsl_sf_legendre_P2(double x) nogil 8 | 9 | double gsl_sf_legendre_P3(double x) nogil 10 | 11 | int gsl_sf_legendre_P1_e(double x, gsl_sf_result * result) nogil 12 | 13 | int gsl_sf_legendre_P2_e(double x, gsl_sf_result * result) nogil 14 | 15 | int gsl_sf_legendre_P3_e(double x, gsl_sf_result * result) nogil 16 | 17 | double gsl_sf_legendre_Pl(int l, double x) nogil 18 | 19 | int gsl_sf_legendre_Pl_e(int l, double x, gsl_sf_result * result) nogil 20 | 21 | int gsl_sf_legendre_Pl_array(int lmax, double x, double result_array[]) nogil 22 | 23 | double gsl_sf_legendre_Q0(double x) nogil 24 | 25 | int gsl_sf_legendre_Q0_e(double x, gsl_sf_result * result) nogil 26 | 27 | double gsl_sf_legendre_Q1(double x) nogil 28 | 29 | int gsl_sf_legendre_Q1_e(double x, gsl_sf_result * result) nogil 30 | 31 | double gsl_sf_legendre_Ql(int l, double x) nogil 32 | 33 | int gsl_sf_legendre_Ql_e(int l, double x, gsl_sf_result * result) nogil 34 | 35 | double gsl_sf_legendre_Plm(int l, int m, double x) nogil 36 | 37 | int gsl_sf_legendre_Plm_e(int l, int m, double x, gsl_sf_result * result) nogil 38 | 39 | int gsl_sf_legendre_Plm_array(int lmax, int m, double x, double result_array[]) nogil 40 | 41 | double gsl_sf_legendre_sphPlm(int l, int m, double x) nogil 42 | 43 | int gsl_sf_legendre_sphPlm_e(int l, int m, double x, gsl_sf_result * result) nogil 44 | 45 | int gsl_sf_legendre_sphPlm_array(int lmax, int m, double x, double result_array[]) nogil 46 | 47 | int gsl_sf_legendre_array_size(int lmax, int m) nogil 48 | 49 | double gsl_sf_conicalP_half(double lambd, double x) nogil 50 | 51 | int gsl_sf_conicalP_half_e(double lambd, double x, gsl_sf_result * result) nogil 52 | 53 | double gsl_sf_conicalP_mhalf(double lambd, double x) nogil 54 | 55 | int gsl_sf_conicalP_mhalf_e(double lambd, double x, gsl_sf_result * result) nogil 56 | 57 | double gsl_sf_conicalP_0(double lambd, double x) nogil 58 | 59 | int gsl_sf_conicalP_0_e(double lambd, double x, gsl_sf_result * result) nogil 60 | 61 | double gsl_sf_conicalP_1(double lambd, double x) nogil 62 | 63 | int gsl_sf_conicalP_1_e(double lambd, double x, gsl_sf_result * result) nogil 64 | 65 | double gsl_sf_conicalP_sph_reg(int l, double lambd, double x) nogil 66 | 67 | int gsl_sf_conicalP_sph_reg_e(int l, double lambd, double x, gsl_sf_result * result) nogil 68 | 69 | double gsl_sf_conicalP_cyl_reg(int m, double lambd, double x) nogil 70 | 71 | int gsl_sf_conicalP_cyl_reg_e(int m, double lambd, double x, gsl_sf_result * result) nogil 72 | 73 | double gsl_sf_legendre_H3d_0(double lambd, double eta) nogil 74 | 75 | int gsl_sf_legendre_H3d_0_e(double lambd, double eta, gsl_sf_result * result) nogil 76 | 77 | double gsl_sf_legendre_H3d_1(double lambd, double eta) nogil 78 | 79 | int gsl_sf_legendre_H3d_1_e(double lambd, double eta, gsl_sf_result * result) nogil 80 | 81 | double gsl_sf_legendre_H3d(int l, double lambd, double eta) nogil 82 | 83 | int gsl_sf_legendre_H3d_e(int l, double lambd, double eta, gsl_sf_result * result) nogil 84 | 85 | int gsl_sf_legendre_H3d_array(int lmax, double lambd, double eta, double result_array[]) nogil 86 | 87 | -------------------------------------------------------------------------------- /cython_gsl/test/multifit_nlin.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | from libc.math cimport exp, sqrt 4 | 5 | ctypedef struct Data: 6 | size_t n 7 | double * y 8 | double * sigma 9 | 10 | cdef int expb_f (const gsl_vector * x, void * data, gsl_vector * f) nogil: 11 | cdef Data * d = data 12 | cdef size_t n = d.n 13 | cdef double * y = d.y 14 | cdef double * sigma = d.sigma 15 | 16 | cdef double A = gsl_vector_get (x, 0) 17 | cdef double lambd = gsl_vector_get (x, 1) 18 | cdef double b = gsl_vector_get (x, 2) 19 | 20 | cdef size_t i 21 | cdef double t, Yi 22 | 23 | for i from 0 <= i < n: 24 | t = i 25 | Yi = A * exp (-lambd * t) + b 26 | gsl_vector_set (f, i, (Yi - y[i])/sigma[i]) 27 | 28 | return GSL_SUCCESS 29 | 30 | cdef int expb_df (const gsl_vector * x, void * data, gsl_matrix * J) nogil: 31 | cdef Data * d = data 32 | cdef size_t n = d.n 33 | cdef double * y = d.y 34 | cdef double * sigma = d.sigma 35 | 36 | cdef double A = gsl_vector_get (x, 0) 37 | cdef double lambd = gsl_vector_get (x, 1) 38 | 39 | cdef size_t i 40 | cdef double t, s, e 41 | 42 | for i from 0 <= i < n: 43 | t = i 44 | s = sigma[i] 45 | e = exp (-lambd * t) 46 | gsl_matrix_set (J, i, 0, e/s) 47 | gsl_matrix_set (J, i, 1, -t * A * e/s) 48 | gsl_matrix_set (J, i, 2, 1/s) 49 | 50 | return GSL_SUCCESS 51 | 52 | cdef int expb_fdf (const gsl_vector * x, void * data, gsl_vector * f, 53 | gsl_matrix * J) nogil: 54 | 55 | expb_f (x, data, f) 56 | expb_df (x, data, J) 57 | 58 | return GSL_SUCCESS 59 | 60 | 61 | def t_gsl_multifit_nlin_example(): 62 | cdef const gsl_multifit_fdfsolver_type * T 63 | cdef gsl_multifit_fdfsolver * s 64 | cdef int status = GSL_CONTINUE 65 | cdef unsigned int i 66 | cdef unsigned int iter = 0 67 | cdef size_t n = 40 68 | cdef size_t p = 3 69 | 70 | cdef gsl_matrix * covar = gsl_matrix_alloc (p, p) 71 | cdef double y[40] 72 | cdef double sigma[40] 73 | cdef Data d 74 | d.n = n 75 | d.y = y 76 | d.sigma = sigma 77 | 78 | cdef gsl_multifit_function_fdf f 79 | cdef double * x_init = [1.0, 0.0, 0.0] 80 | cdef gsl_vector_view x = gsl_vector_view_array (x_init, p) 81 | cdef const gsl_rng_type * type 82 | cdef gsl_rng * r 83 | 84 | gsl_rng_env_setup () 85 | 86 | type = gsl_rng_default 87 | r = gsl_rng_alloc (type) 88 | 89 | f.f = &expb_f 90 | f.df = &expb_df 91 | f.fdf = &expb_fdf 92 | f.n = n 93 | f.p = p 94 | f.params = &d 95 | 96 | cdef double t 97 | 98 | for i from 0 <= i < n: 99 | t = i; 100 | y[i] = 1.0 + 5 * exp (-0.1 * t) + gsl_ran_gaussian (r, 0.1) 101 | sigma[i] = 0.1 102 | 103 | T = gsl_multifit_fdfsolver_lmsder 104 | s = gsl_multifit_fdfsolver_alloc (T, n, p) 105 | gsl_multifit_fdfsolver_set (s, &f, &x.vector) 106 | 107 | while (status == GSL_CONTINUE and iter < 500): 108 | 109 | iter += 1 110 | status = gsl_multifit_fdfsolver_iterate (s) 111 | 112 | if status: 113 | break 114 | 115 | status = gsl_multifit_test_delta (s.dx, s.x, 1e-4, 1e-4) 116 | 117 | A = gsl_vector_get (s.x, 0) 118 | lambd = gsl_vector_get (s.x, 1) 119 | b = gsl_vector_get (s.x, 2) 120 | 121 | gsl_multifit_fdfsolver_free (s) 122 | gsl_matrix_free (covar) 123 | gsl_rng_free (r) 124 | 125 | return (A, lambd, b) 126 | -------------------------------------------------------------------------------- /cython_gsl/test/combinations.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def t_gsl_combination_calloc(): 4 | cdef gsl_combination * c 5 | cdef size_t i 6 | cdef size_t * n 7 | cdef int status1 8 | c = gsl_combination_calloc (4, 2) 9 | ary = [] 10 | n = gsl_combination_data(c) 11 | ary.extend([n[0], n[1] - 1]) 12 | gsl_combination_next (c) 13 | ary.extend([n[0], n[1] - 2]) 14 | gsl_combination_next (c) 15 | ary.extend([n[0], n[1] - 3]) 16 | gsl_combination_next (c) 17 | ary.extend([n[0] - 1, n[1] - 2]) 18 | status = gsl_combination_next (c) 19 | # status == 0 GSL_SUCCESS 20 | ary.extend([status, n[0] - 1, n[1] - 3]) 21 | gsl_combination_next (c) 22 | ary.extend([n[0] - 2, n[1] - 3]) 23 | status = gsl_combination_next (c) 24 | ary.extend([(status != 0) - 1, n[0] - 2, n[1] - 3]) 25 | ary.extend([n[0] - 2, n[1] - 3]) 26 | gsl_combination_prev(c) 27 | ary.extend([n[0] - 1, n[1] - 3]) 28 | gsl_combination_free (c) 29 | return ary 30 | 31 | def t_gsl_combination_init_first(): 32 | cdef gsl_combination * c 33 | cdef size_t * n 34 | c = gsl_combination_calloc (4, 2) 35 | ary = [] 36 | gsl_combination_next (c) 37 | gsl_combination_init_first(c) 38 | n = gsl_combination_data(c) 39 | ary.extend([n[0], n[1] - 1]) 40 | gsl_combination_init_last(c) 41 | ary.extend([n[0] - 2, n[1] - 3]) 42 | gsl_combination_free (c) 43 | return ary 44 | 45 | 46 | def t_gsl_combination_memcpy(): 47 | cdef gsl_combination * c1, * c2 48 | cdef size_t * n 49 | c1 = gsl_combination_calloc (4, 2) 50 | c2 = gsl_combination_calloc (4, 2) 51 | ary = [] 52 | gsl_combination_next(c1) 53 | gsl_combination_memcpy(c2, c1) 54 | n = gsl_combination_data(c2) 55 | ary.extend([n[0], n[1] - 2]) 56 | gsl_combination_free (c1) 57 | gsl_combination_free (c2) 58 | return ary 59 | 60 | 61 | def t_gsl_combination_n(): 62 | cdef gsl_combination * c 63 | c = gsl_combination_calloc (4, 2) 64 | ary = [] 65 | gsl_combination_next (c) 66 | ary.append(gsl_combination_n(c) - 4) 67 | ary.append(gsl_combination_k(c) - 2) 68 | gsl_combination_free (c) 69 | return ary 70 | 71 | 72 | def t_gsl_combination_get(): 73 | cdef gsl_combination * c 74 | c = gsl_combination_calloc (4, 2) 75 | ary = [] 76 | gsl_combination_next (c) 77 | ary.append(gsl_combination_get(c, 0)) 78 | ary.append(gsl_combination_get(c, 1) - 2) 79 | gsl_combination_free (c) 80 | return ary 81 | 82 | def t_gsl_combination_valid(): 83 | cdef gsl_combination * c 84 | c = gsl_combination_calloc (4, 2) 85 | ary = [] 86 | gsl_combination_next (c) 87 | ary.append(gsl_combination_valid(c)) 88 | gsl_combination_free (c) 89 | cdef gsl_combination * c2 90 | c2 = gsl_combination_alloc (4, 2) 91 | if 0: 92 | c2.data[0] = 2 93 | c2.data[1] = 0 94 | else: 95 | c2.data[0] = 0 96 | c2.data[1] = 2 97 | ary.append(gsl_combination_valid(c2)) 98 | gsl_combination_free (c2) 99 | return ary 100 | 101 | def t_gsl_combination_fprintf(): 102 | cdef gsl_combination * c 103 | c = gsl_combination_calloc (4, 2) 104 | ary = [] 105 | gsl_combination_next (c) 106 | cdef FILE * f 107 | f = fopen ("test.dat", "w") 108 | gsl_combination_fprintf(f, c, "%u ") 109 | fclose(f) 110 | gsl_combination_free (c) 111 | cdef gsl_combination * c2 112 | c2 = gsl_combination_calloc (4, 2) 113 | cdef FILE * f2 114 | f2 = fopen ("test.dat", "r") 115 | gsl_combination_fscanf(f2, c2) 116 | fclose(f2) 117 | ary.append(gsl_combination_get(c2,0)) 118 | ary.append(gsl_combination_get(c2,1) - 2) 119 | gsl_combination_free (c2) 120 | return ary 121 | -------------------------------------------------------------------------------- /cython_gsl/gsl_vector_complex.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_vector_complex_double.h": 4 | 5 | ctypedef struct gsl_vector_complex: 6 | size_t size 7 | size_t stride 8 | double *data 9 | gsl_block_complex *block 10 | int owner 11 | 12 | ctypedef struct gsl_vector_complex_view: 13 | gsl_vector_complex vector 14 | 15 | ctypedef struct gsl_vector_complex_const_view: 16 | gsl_vector_complex vector_complex 17 | 18 | 19 | # Allocation 20 | gsl_vector_complex * gsl_vector_complex_alloc(size_t n) nogil 21 | 22 | gsl_vector_complex * gsl_vector_complex_calloc(size_t n) nogil 23 | 24 | gsl_vector_complex_alloc_from_block(gsl_block_complex * b, size_t offset, 25 | size_t n, size_t stride) nogil 26 | 27 | gsl_vector_complex *gsl_vector_complex_alloc_from_vector(gsl_vector_complex * v, 28 | size_t offset, size_t n, size_t stride) nogil 29 | 30 | void gsl_vector_complex_free(gsl_vector_complex * v) nogil 31 | 32 | # Views 33 | gsl_vector_complex_view gsl_vector_complex_view_array(double *base, size_t n) nogil 34 | 35 | gsl_vector_complex_view gsl_vector_complex_view_array_with_stride(double * base, size_t stride, size_t n) nogil 36 | 37 | gsl_vector_complex_const_view gsl_vector_complex_const_view_array(double *base, size_t n) nogil 38 | 39 | gsl_vector_complex_const_view gsl_vector_complex_const_view_array_with_stride(double * base, size_t stride, size_t n) nogil 40 | 41 | gsl_vector_complex_view gsl_vector_complex_subvector(gsl_vector_complex *v, size_t offset, size_t n) nogil 42 | 43 | gsl_vector_complex_const_view gsl_vector_complex_const_subvector(gsl_vector_complex * v, size_t offset, size_t n) nogil 44 | 45 | gsl_vector_complex_view gsl_vector_complex_subvector_with_stride(gsl_vector_complex *v, size_t offset, size_t stride, size_t n) nogil 46 | 47 | gsl_vector_complex_const_view gsl_vector_complex_const_subvector_with_stride(gsl_vector_complex * v, size_t offset, size_t stride, size_t n) nogil 48 | 49 | 50 | gsl_vector_view gsl_vector_complex_real(gsl_vector_complex *v) nogil 51 | 52 | gsl_vector_view gsl_vector_complex_imag(gsl_vector_complex *v) nogil 53 | 54 | gsl_vector_const_view gsl_vector_complex_const_real(gsl_vector_complex *v) nogil 55 | 56 | gsl_vector_const_view gsl_vector_complex_const_imag(gsl_vector_complex *v) nogil 57 | 58 | 59 | # Operations 60 | gsl_complex gsl_vector_complex_get(gsl_vector_complex * v, size_t i) nogil 61 | 62 | void gsl_vector_complex_set(gsl_vector_complex * v, size_t i, gsl_complex x) nogil 63 | 64 | gsl_complex * gsl_vector_complex_ptr(gsl_vector_complex * v, size_t i) nogil 65 | 66 | gsl_complex * gsl_vector_complex_const_ptr(gsl_vector_complex * v, size_t i) nogil 67 | 68 | void gsl_vector_complex_set_zero(gsl_vector_complex * v) nogil 69 | 70 | void gsl_vector_complex_set_all(gsl_vector_complex * v, gsl_complex x) nogil 71 | 72 | int gsl_vector_complex_set_basis(gsl_vector_complex * v, size_t i) nogil 73 | 74 | # Reading and writing vector_complexs 75 | int gsl_vector_complex_fread(FILE * stream, gsl_vector_complex * v) nogil 76 | 77 | int gsl_vector_complex_fwrite(FILE * stream, gsl_vector_complex * v) nogil 78 | 79 | int gsl_vector_complex_fscanf(FILE * stream, gsl_vector_complex * v) nogil 80 | 81 | int gsl_vector_complex_fprintf(FILE * stream, gsl_vector_complex * v, char * format) nogil 82 | 83 | # Copying or exchanging elements 84 | int gsl_vector_complex_memcpy(gsl_vector_complex * dest, gsl_vector_complex * src) nogil 85 | 86 | int gsl_vector_complex_reverse(gsl_vector_complex * v) nogil 87 | 88 | int gsl_vector_complex_swap(gsl_vector_complex * v, gsl_vector_complex * w) nogil 89 | 90 | int gsl_vector_complex_swap_elements(gsl_vector_complex * v, size_t i, size_t j) nogil 91 | 92 | int gsl_vector_complex_isnull(gsl_vector_complex * v) nogil 93 | 94 | 95 | -------------------------------------------------------------------------------- /cython_gsl/gsl_rng.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_rng.h": 4 | 5 | ctypedef struct gsl_rng_type 6 | ctypedef struct gsl_rng 7 | 8 | cdef gsl_rng_type *gsl_rng_borosh13 9 | cdef gsl_rng_type *gsl_rng_coveyou 10 | cdef gsl_rng_type *gsl_rng_cmrg 11 | cdef gsl_rng_type *gsl_rng_fishman18 12 | cdef gsl_rng_type *gsl_rng_fishman20 13 | cdef gsl_rng_type *gsl_rng_fishman2x 14 | cdef gsl_rng_type *gsl_rng_gfsr4 15 | cdef gsl_rng_type *gsl_rng_knuthran 16 | cdef gsl_rng_type *gsl_rng_knuthran2 17 | cdef gsl_rng_type *gsl_rng_lecuyer21 18 | cdef gsl_rng_type *gsl_rng_minstd 19 | cdef gsl_rng_type *gsl_rng_mrg 20 | cdef gsl_rng_type *gsl_rng_mt19937 21 | cdef gsl_rng_type *gsl_rng_mt19937_1999 22 | cdef gsl_rng_type *gsl_rng_mt19937_1998 23 | cdef gsl_rng_type *gsl_rng_r250 24 | cdef gsl_rng_type *gsl_rng_ran0 25 | cdef gsl_rng_type *gsl_rng_ran1 26 | cdef gsl_rng_type *gsl_rng_ran2 27 | cdef gsl_rng_type *gsl_rng_ran3 28 | cdef gsl_rng_type *gsl_rng_rand 29 | cdef gsl_rng_type *gsl_rng_rand48 30 | cdef gsl_rng_type *gsl_rng_random128_bsd 31 | cdef gsl_rng_type *gsl_rng_random128_glibc2 32 | cdef gsl_rng_type *gsl_rng_random128_libc5 33 | cdef gsl_rng_type *gsl_rng_random256_bsd 34 | cdef gsl_rng_type *gsl_rng_random256_glibc2 35 | cdef gsl_rng_type *gsl_rng_random256_libc5 36 | cdef gsl_rng_type *gsl_rng_random32_bsd 37 | cdef gsl_rng_type *gsl_rng_random32_glibc2 38 | cdef gsl_rng_type *gsl_rng_random32_libc5 39 | cdef gsl_rng_type *gsl_rng_random64_bsd 40 | cdef gsl_rng_type *gsl_rng_random64_glibc2 41 | cdef gsl_rng_type *gsl_rng_random64_libc5 42 | cdef gsl_rng_type *gsl_rng_random8_bsd 43 | cdef gsl_rng_type *gsl_rng_random8_glibc2 44 | cdef gsl_rng_type *gsl_rng_random8_libc5 45 | cdef gsl_rng_type *gsl_rng_random_bsd 46 | cdef gsl_rng_type *gsl_rng_random_glibc2 47 | cdef gsl_rng_type *gsl_rng_random_libc5 48 | cdef gsl_rng_type *gsl_rng_randu 49 | cdef gsl_rng_type *gsl_rng_ranf 50 | cdef gsl_rng_type *gsl_rng_ranlux 51 | cdef gsl_rng_type *gsl_rng_ranlux389 52 | cdef gsl_rng_type *gsl_rng_ranlxd1 53 | cdef gsl_rng_type *gsl_rng_ranlxd2 54 | cdef gsl_rng_type *gsl_rng_ranlxs0 55 | cdef gsl_rng_type *gsl_rng_ranlxs1 56 | cdef gsl_rng_type *gsl_rng_ranlxs2 57 | cdef gsl_rng_type *gsl_rng_ranmar 58 | cdef gsl_rng_type *gsl_rng_slatec 59 | cdef gsl_rng_type *gsl_rng_taus 60 | cdef gsl_rng_type *gsl_rng_taus2 61 | cdef gsl_rng_type *gsl_rng_taus113 62 | cdef gsl_rng_type *gsl_rng_transputer 63 | cdef gsl_rng_type *gsl_rng_tt800 64 | cdef gsl_rng_type *gsl_rng_uni 65 | cdef gsl_rng_type *gsl_rng_uni32 66 | cdef gsl_rng_type *gsl_rng_vax 67 | cdef gsl_rng_type *gsl_rng_waterman14 68 | cdef gsl_rng_type *gsl_rng_zuf 69 | 70 | 71 | cdef gsl_rng_type *gsl_rng_default 72 | unsigned long int gsl_rng_default_seed 73 | 74 | gsl_rng *gsl_rng_alloc ( gsl_rng_type * T) nogil 75 | int gsl_rng_memcpy (gsl_rng * dest, gsl_rng * src) nogil 76 | gsl_rng *gsl_rng_clone ( gsl_rng * r) nogil 77 | 78 | void gsl_rng_free (gsl_rng * r) nogil 79 | 80 | void gsl_rng_set ( gsl_rng * r, unsigned long int seed) nogil 81 | unsigned long int gsl_rng_max ( gsl_rng * r) nogil 82 | unsigned long int gsl_rng_min ( gsl_rng * r) nogil 83 | char *gsl_rng_name ( gsl_rng * r) nogil 84 | 85 | int gsl_rng_fread (FILE * stream, gsl_rng * r) nogil 86 | int gsl_rng_fwrite (FILE * stream, gsl_rng * r) nogil 87 | 88 | size_t gsl_rng_size ( gsl_rng * r) nogil 89 | void * gsl_rng_state ( gsl_rng * r) nogil 90 | 91 | void gsl_rng_print_state ( gsl_rng * r) nogil 92 | 93 | gsl_rng_type * gsl_rng_env_setup () nogil 94 | 95 | unsigned long int gsl_rng_get ( gsl_rng * r) nogil 96 | double gsl_rng_uniform ( gsl_rng * r) nogil 97 | double gsl_rng_uniform_pos ( gsl_rng * r) nogil 98 | unsigned long int gsl_rng_uniform_int ( gsl_rng * r, unsigned long int n) nogil 99 | 100 | 101 | -------------------------------------------------------------------------------- /cython_gsl/gsl_vector.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_vector.h": 4 | 5 | ctypedef struct gsl_vector: 6 | size_t size 7 | size_t stride 8 | double *data 9 | gsl_block *block 10 | int owner 11 | 12 | ctypedef struct gsl_vector_view: 13 | gsl_vector vector 14 | 15 | ctypedef struct gsl_vector_const_view: 16 | gsl_vector vector 17 | 18 | 19 | # Allocation 20 | gsl_vector * gsl_vector_alloc(size_t n) nogil 21 | 22 | gsl_vector * gsl_vector_calloc(size_t n) nogil 23 | 24 | gsl_vector_alloc_from_block(gsl_block * b, size_t offset, 25 | size_t n, size_t stride) nogil 26 | 27 | gsl_vector *gsl_vector_alloc_from_vector(gsl_vector * v, 28 | size_t offset, size_t n, size_t stride) nogil 29 | 30 | void gsl_vector_free(gsl_vector * v) nogil 31 | 32 | # Views 33 | gsl_vector_view gsl_vector_view_array(double *base, size_t n) nogil 34 | 35 | gsl_vector_view gsl_vector_subvector(gsl_vector *v, size_t offset, size_t n) nogil 36 | 37 | gsl_vector_view gsl_vector_view_array_with_stride(double * base, size_t stride, size_t n) nogil 38 | 39 | gsl_vector_const_view gsl_vector_const_view_array(double *base, size_t n) nogil 40 | 41 | gsl_vector_const_view gsl_vector_const_view_array_with_stride(double * base, size_t stride, size_t n) nogil 42 | 43 | gsl_vector_const_view gsl_vector_const_subvector(gsl_vector * v, size_t offset, size_t n) nogil 44 | 45 | gsl_vector_view gsl_vector_subvector_with_stride(gsl_vector *v, size_t offset, size_t stride, size_t n) nogil 46 | 47 | gsl_vector_const_view gsl_vector_const_subvector_with_stride(gsl_vector * v, size_t offset, size_t stride, size_t n) nogil 48 | 49 | 50 | # Operations 51 | double gsl_vector_get(gsl_vector * v, size_t i) nogil 52 | 53 | void gsl_vector_set(gsl_vector * v, size_t i, double x) nogil 54 | 55 | double * gsl_vector_ptr(gsl_vector * v, size_t i) nogil 56 | 57 | double * gsl_vector_const_ptr(gsl_vector * v, size_t i) nogil 58 | 59 | void gsl_vector_set_zero(gsl_vector * v) nogil 60 | 61 | void gsl_vector_set_all(gsl_vector * v, double x) nogil 62 | 63 | int gsl_vector_set_basis(gsl_vector * v, size_t i) nogil 64 | 65 | # Reading and writing vectors 66 | int gsl_vector_fread(FILE * stream, gsl_vector * v) nogil 67 | 68 | int gsl_vector_fwrite(FILE * stream, gsl_vector * v) nogil 69 | 70 | int gsl_vector_fscanf(FILE * stream, gsl_vector * v) nogil 71 | 72 | int gsl_vector_fprintf(FILE * stream, gsl_vector * v, char * format) nogil 73 | 74 | # Copying or exchanging elements 75 | int gsl_vector_memcpy(gsl_vector * dest, gsl_vector * src) nogil 76 | 77 | int gsl_vector_reverse(gsl_vector * v) nogil 78 | 79 | int gsl_vector_swap(gsl_vector * v, gsl_vector * w) nogil 80 | 81 | int gsl_vector_swap_elements(gsl_vector * v, size_t i, size_t j) nogil 82 | 83 | # Finding maximum and minimum elements of vectors 84 | 85 | double gsl_vector_max(gsl_vector * v) nogil 86 | 87 | double gsl_vector_min(gsl_vector * v) nogil 88 | 89 | void gsl_vector_minmax(gsl_vector * v, double * min_out, double * max_out) nogil 90 | 91 | size_t gsl_vector_max_index(gsl_vector * v) nogil 92 | 93 | size_t gsl_vector_min_index(gsl_vector * v) nogil 94 | 95 | void gsl_vector_minmax_index(gsl_vector * v, size_t * imin, size_t * imax) nogil 96 | 97 | # Vector operations 98 | int gsl_vector_add(gsl_vector * a, gsl_vector * b) nogil 99 | 100 | int gsl_vector_sub(gsl_vector * a, gsl_vector * b) nogil 101 | 102 | int gsl_vector_mul(gsl_vector * a, gsl_vector * b) nogil 103 | 104 | int gsl_vector_div(gsl_vector * a, gsl_vector * b) nogil 105 | 106 | int gsl_vector_scale(gsl_vector * a, double x) nogil 107 | 108 | int gsl_vector_add_constant(gsl_vector * a, double x) nogil 109 | 110 | int gsl_vector_isnull(gsl_vector * v) nogil 111 | 112 | 113 | -------------------------------------------------------------------------------- /cython_gsl/gsl_integration.pxd: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef extern from "gsl/gsl_integration.h": 4 | 5 | ctypedef struct gsl_integration_workspace 6 | ctypedef struct gsl_integration_qaws_table 7 | ctypedef struct gsl_integration_qawo_table 8 | ctypedef struct gsl_integration_cquad_workspace 9 | cdef enum: 10 | GSL_INTEG_GAUSS15 = 1 11 | GSL_INTEG_GAUSS21 = 2 12 | GSL_INTEG_GAUSS31 = 3 13 | GSL_INTEG_GAUSS41 = 4 14 | GSL_INTEG_GAUSS51 = 5 15 | GSL_INTEG_GAUSS61 = 6 16 | cdef enum gsl_integration_qawo_enum: 17 | GSL_INTEG_COSINE, GSL_INTEG_SINE 18 | 19 | gsl_integration_cquad_workspace * gsl_integration_cquad_workspace_alloc (size_t n) nogil 20 | 21 | void gsl_integration_cquad_workspace_free (gsl_integration_cquad_workspace * w) nogil 22 | 23 | int gsl_integration_cquad (gsl_function * f, double a, double b, double epsabs, double epsrel, gsl_integration_cquad_workspace * workspace, double * result, double * abserr, size_t * nevals) nogil 24 | 25 | int gsl_integration_qng(gsl_function *f, double a, double b, double epsabs, double epsrel, double * result, double * abserr, size_t * neval) nogil 26 | 27 | gsl_integration_workspace * gsl_integration_workspace_alloc(size_t n) nogil 28 | 29 | void gsl_integration_workspace_free(gsl_integration_workspace * w) nogil 30 | 31 | int gsl_integration_qag(gsl_function *f, double a, double b, double epsabs, double epsrel, size_t limit, int key, gsl_integration_workspace * workspace, double * result, double * abserr) nogil 32 | 33 | int gsl_integration_qags(gsl_function * f, double a, double b, double epsabs, double epsrel, size_t limit, gsl_integration_workspace * workspace, double *result, double *abserr) nogil 34 | 35 | int gsl_integration_qagp(gsl_function * f, double *pts, size_t npts, double epsabs, double epsrel, size_t limit, gsl_integration_workspace * workspace, double *result, double *abserr) nogil 36 | 37 | int gsl_integration_qagi(gsl_function * f, double epsabs, double epsrel, size_t limit, gsl_integration_workspace * workspace, double *result, double *abserr) nogil 38 | 39 | int gsl_integration_qagiu(gsl_function * f, double a, double epsabs, double epsrel, size_t limit, gsl_integration_workspace * workspace, double *result, double *abserr) nogil 40 | 41 | int gsl_integration_qagil(gsl_function * f, double b, double epsabs, double epsrel, size_t limit, gsl_integration_workspace * workspace, double *result, double *abserr) nogil 42 | 43 | int gsl_integration_qawc(gsl_function *f, double a, double b, double c, double epsabs, double epsrel, size_t limit, gsl_integration_workspace * workspace, double * result, double * abserr) nogil 44 | 45 | gsl_integration_qaws_table * gsl_integration_qaws_table_alloc(double alpha, double beta, int mu, int nu) nogil 46 | 47 | int gsl_integration_qaws_table_set(gsl_integration_qaws_table * t, double alpha, double beta, int mu, int nu) nogil 48 | 49 | void gsl_integration_qaws_table_free(gsl_integration_qaws_table * t) nogil 50 | 51 | int gsl_integration_qaws(gsl_function * f, double a, double b, gsl_integration_qaws_table * t, double epsabs, double epsrel, size_t limit, gsl_integration_workspace * workspace, double *result, double *abserr) nogil 52 | 53 | gsl_integration_qawo_table * gsl_integration_qawo_table_alloc(double omega, double L, gsl_integration_qawo_enum sine, size_t n) nogil 54 | 55 | int gsl_integration_qawo_table_set(gsl_integration_qawo_table * t, double omega, double L, gsl_integration_qawo_enum sine) nogil 56 | 57 | int gsl_integration_qawo_table_set_length(gsl_integration_qawo_table * t, double L) nogil 58 | 59 | void gsl_integration_qawo_table_free(gsl_integration_qawo_table * t) nogil 60 | 61 | int gsl_integration_qawo(gsl_function * f, double a, double epsabs, double epsrel, size_t limit, gsl_integration_workspace * workspace, gsl_integration_qawo_table * wf, double *result, double *abserr) nogil 62 | 63 | int gsl_integration_qawf(gsl_function * f, double a, double epsabs, size_t limit, gsl_integration_workspace * workspace, gsl_integration_workspace * cycle_workspace, gsl_integration_qawo_table * wf, double *result, double *abserr) nogil 64 | -------------------------------------------------------------------------------- /examples/multimin.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | cdef double my_f (const gsl_vector * v, void * params) nogil: 4 | cdef double x, y 5 | cdef double * p = params 6 | 7 | x = gsl_vector_get(v, 0) 8 | y = gsl_vector_get(v, 1) 9 | 10 | return p[2] * (x - p[0]) * (x - p[0]) + p[3] * (y - p[1]) * (y - p[1]) + p[4] 11 | 12 | cdef void my_df (const gsl_vector * v, void * params, gsl_vector * df) nogil: 13 | cdef double x, y 14 | cdef double * p = params 15 | 16 | x = gsl_vector_get(v, 0) 17 | y = gsl_vector_get(v, 1) 18 | 19 | gsl_vector_set(df, 0, 2.0 * p[2] * (x - p[0])) 20 | gsl_vector_set(df, 1, 2.0 * p[3] * (y - p[1])) 21 | 22 | cdef void my_fdf (const gsl_vector * x, void * params, double * f, gsl_vector * df) nogil: 23 | f[0] = my_f (x, params) 24 | my_df (x, params, df) 25 | 26 | def main_fdf(): 27 | cdef size_t iter = 0 28 | cdef int max_iter = 100 29 | cdef int status 30 | 31 | cdef const gsl_multimin_fdfminimizer_type * T 32 | cdef gsl_multimin_fdfminimizer * s 33 | cdef gsl_vector * x 34 | 35 | cdef gsl_multimin_function_fdf my_func 36 | cdef double p[5] 37 | p[0] = 1.0 38 | p[1] = 2.0 39 | p[2] = 10.0 40 | p[3] = 20.0 41 | p[4] = 30.0 42 | my_func.n = 2 43 | my_func.f = &my_f 44 | my_func.df = &my_df 45 | my_func.fdf = &my_fdf 46 | my_func.params = p 47 | 48 | # Starting point, x = (5, 7) 49 | x = gsl_vector_alloc (2) 50 | gsl_vector_set (x, 0, 5.0) 51 | gsl_vector_set (x, 1, 7.0) 52 | 53 | T = gsl_multimin_fdfminimizer_conjugate_fr 54 | s = gsl_multimin_fdfminimizer_alloc (T, 2) 55 | 56 | gsl_multimin_fdfminimizer_set (s, &my_func, x, 0.01, 1e-4) 57 | 58 | status = GSL_CONTINUE 59 | 60 | while (status == GSL_CONTINUE and iter <= max_iter): 61 | iter += 1 62 | status = gsl_multimin_fdfminimizer_iterate (s) 63 | 64 | if status: 65 | break 66 | 67 | status = gsl_multimin_test_gradient (s.gradient, 1e-3) 68 | 69 | if status == GSL_SUCCESS: 70 | print("Minimum found at:\n") 71 | 72 | print("%5d %.5f %.5f %10.5f\n" %\ 73 | (iter, gsl_vector_get (s.x, 0), gsl_vector_get (s.x, 1), s.f)) 74 | 75 | gsl_multimin_fdfminimizer_free (s) 76 | gsl_vector_free (x) 77 | 78 | return status 79 | 80 | def main_f(): 81 | cdef size_t iter = 0 82 | cdef int max_iter = 100 83 | cdef int status 84 | 85 | cdef const gsl_multimin_fminimizer_type * T 86 | cdef gsl_multimin_fminimizer * s 87 | cdef gsl_vector * ss 88 | cdef gsl_vector * x 89 | 90 | cdef gsl_multimin_function my_func 91 | cdef double p[5] 92 | p[0] = 1.0 93 | p[1] = 2.0 94 | p[2] = 10.0 95 | p[3] = 20.0 96 | p[4] = 30.0 97 | my_func.n = 2 98 | my_func.f = &my_f 99 | my_func.params = p 100 | 101 | # Starting point, x = (5, 7) 102 | x = gsl_vector_alloc (2) 103 | gsl_vector_set (x, 0, 5.0) 104 | gsl_vector_set (x, 1, 7.0) 105 | 106 | # Set initial step sizes to 1 107 | ss = gsl_vector_alloc (2) 108 | gsl_vector_set_all (ss, 1.0) 109 | 110 | T = gsl_multimin_fminimizer_nmsimplex2 111 | s = gsl_multimin_fminimizer_alloc (T, 2) 112 | 113 | gsl_multimin_fminimizer_set (s, &my_func, x, ss) 114 | 115 | status = GSL_CONTINUE 116 | 117 | while (status == GSL_CONTINUE and iter <= max_iter): 118 | iter += 1 119 | status = gsl_multimin_fminimizer_iterate (s) 120 | 121 | if status: 122 | print(status) 123 | break 124 | 125 | size = gsl_multimin_fminimizer_size (s) 126 | status = gsl_multimin_test_size (size, 1e-2) 127 | 128 | if status == GSL_SUCCESS: 129 | print("Minimum found at:\n") 130 | 131 | print("%5d %10.3e %10.3e f() = %7.3f size = %.3f\n" %\ 132 | (iter, gsl_vector_get (s.x, 0), gsl_vector_get (s.x, 1), s.fval, size)) 133 | 134 | gsl_multimin_fminimizer_free (s) 135 | gsl_vector_free (x) 136 | gsl_vector_free (ss) 137 | 138 | return status 139 | -------------------------------------------------------------------------------- /cython_gsl/test/sort.pyx: -------------------------------------------------------------------------------- 1 | from cython_gsl cimport * 2 | 3 | def t_gsl_sort_smallest(): 4 | cdef double a[5], b[2] 5 | a1 = [3, 4, 1, 5, 2] 6 | for i from 0 <= i < 5: 7 | a[i] = a1[i] + 0.1 8 | gsl_sort_smallest(b, 2, a, 1, 5) 9 | ary = [b[0] - 1.1, b[1] - 2.1] 10 | gsl_sort_largest(b, 2, a, 1, 5) 11 | ary.extend([b[0] - 5.1, b[1] - 4.1]) 12 | return ary 13 | 14 | def t_gsl_sort_smallest_index(): 15 | cdef double a[5] 16 | cdef size_t b[2] 17 | a1 = [3, 4, 1, 5, 2] 18 | for i from 0 <= i < 5: 19 | a[i] = a1[i] + 0.1 20 | gsl_sort_smallest_index(b, 2, a, 1, 5) 21 | ary = [b[0] - 2, b[1] - 4] 22 | gsl_sort_largest_index(b, 2, a, 1, 5) 23 | ary.extend([b[0] - 3, b[1] - 1]) 24 | return ary 25 | 26 | def t_gsl_sort_vector_smallest(): 27 | cdef gsl_vector * a 28 | a = gsl_vector_alloc(5) 29 | cdef double b[2] 30 | a1 = [3, 4, 1, 5, 2] 31 | for i from 0 <= i < 5: 32 | gsl_vector_set(a, i, a1[i] + 0.1) 33 | gsl_sort_vector_smallest(b, 2, a) 34 | ary = [b[0] - 1.1, b[1] - 2.1] 35 | gsl_sort_vector_largest(b, 2, a) 36 | ary.extend([b[0] - 5.1, b[1] - 4.1]) 37 | return ary 38 | 39 | def t_gsl_sort_vector_smallest_index(): 40 | cdef gsl_vector * a 41 | a = gsl_vector_alloc(5) 42 | cdef size_t b[2] 43 | a1 = [3, 4, 1, 5, 2] 44 | for i from 0 <= i < 5: 45 | gsl_vector_set(a, i, a1[i] + 0.1) 46 | gsl_sort_vector_smallest_index(b, 2, a) 47 | ary = [b[0] - 2, b[1] - 4] 48 | gsl_sort_vector_largest_index(b, 2, a) 49 | ary.extend([b[0] - 3, b[1] - 1]) 50 | return ary 51 | 52 | def t_gsl_sort(): 53 | cdef double a[5] 54 | a1 = [3, 4, 1, 5, 2] 55 | for i from 0 <= i < 5: 56 | a[i] = a1[i] + 0.1 57 | gsl_sort(a, 1, 5) 58 | ary = [a[0] - 1.1, a[1] - 2.1, a[2] - 3.1, a[3] - 4.1, a[4] - 5.1] 59 | return ary 60 | 61 | def t_gsl_sort_index(): 62 | cdef double a[5] 63 | cdef size_t b[5] 64 | a1 = [3, 4, 1, 5, 2] 65 | for i from 0 <= i < 5: 66 | a[i] = a1[i] + 0.1 67 | gsl_sort_index(b, a, 1, 5) 68 | ary = [b[0] -2, b[1] - 4, b[2], b[3] - 1, b[4] - 3] 69 | return ary 70 | 71 | def t_gsl_sort_vector(): 72 | cdef gsl_vector * a 73 | a = gsl_vector_alloc(5) 74 | a1 = [3, 4, 1, 5, 2] 75 | for i from 0 <= i < 5: 76 | gsl_vector_set(a, i, a1[i] + 0.1) 77 | gsl_sort_vector(a) 78 | ary = [] 79 | for i from 0 <= i < 5: 80 | ary.append(gsl_vector_get(a,i) - i - 1.1) 81 | return ary 82 | 83 | def t_gsl_sort_vector_index(): 84 | cdef gsl_vector * a 85 | a = gsl_vector_alloc(5) 86 | cdef gsl_permutation * p 87 | p = gsl_permutation_alloc(5) 88 | a1 = [3, 4, 1, 5, 2] 89 | for i from 0 <= i < 5: 90 | gsl_vector_set(a, i, a1[i] + 0.1) 91 | gsl_sort_vector_index(p, a) 92 | ary = [] 93 | cdef int j 94 | for i from 0 <= i < 5: 95 | j = gsl_permutation_get(p, i) 96 | ary.append(gsl_vector_get(a,j) - i - 1.1) 97 | return ary 98 | 99 | 100 | cdef int cmp1(void * a, void * b): 101 | cdef int a1, b1 102 | a1 = ( a)[0] 103 | a1 = (a1 * a1)%10 104 | b1 = ( b)[0] 105 | b1 = (b1 * b1)%10 106 | if (a1 < b1): 107 | return -1 108 | elif (a1 > b1): 109 | return 1 110 | else: 111 | return 0 112 | 113 | def t_gsl_heapsort(): 114 | cdef int a[5], b[2] 115 | cdef sizeof_int 116 | sizeof_int = 4 117 | a1 = [3, 4, 1, 5, 2] 118 | for i from 0 <= i < 5: 119 | a[i] = a1[i] 120 | gsl_heapsort( a, 5, sizeof_int, cmp1) 121 | r = [1,2,5,4,3] 122 | ary = [] 123 | for i from 0 <= i < 5: 124 | ary.append(a[i] - r[i]) 125 | return ary 126 | 127 | def t_gsl_heapsort_index(): 128 | cdef size_t b[5] 129 | cdef int a[5] 130 | cdef sizeof_int 131 | sizeof_int = 4 132 | a1 = [3, 4, 1, 5, 2] 133 | for i from 0 <= i < 5: 134 | a[i] = a1[i] 135 | gsl_heapsort_index(b, a, 5, sizeof_int, cmp1) 136 | ary = [b[0] -2, b[1] - 4, b[2] - 3, b[3] - 1, b[4]] 137 | return ary 138 | -------------------------------------------------------------------------------- /cython_gsl/gsl.pxd: -------------------------------------------------------------------------------- 1 | cimport cython_gsl.math 2 | cimport cython_gsl.stdio 3 | cdef enum: 4 | GSL_SUCCESS = 0 5 | GSL_FAILURE = -1 6 | GSL_CONTINUE = -2 # iteration has not converged 7 | GSL_EDOM = 1 # input domain error, e.g sqrt(-1) 8 | GSL_ERANGE = 2 # output range error, e.g. exp(1e100) 9 | GSL_EFAULT = 3 # invalid pointer 10 | GSL_EINVAL = 4 # invalid argument supplied by user 11 | GSL_EFAILED = 5 # generic failure 12 | GSL_EFACTOR = 6 # factorization failed 13 | GSL_ESANITY = 7 # sanity check failed - shouldn't happen 14 | GSL_ENOMEM = 8 # malloc failed 15 | GSL_EBADFUNC = 9 # problem with user-supplied function 16 | GSL_ERUNAWAY = 10 # iterative process is out of control 17 | GSL_EMAXITER = 11 # exceeded max number of iterations 18 | GSL_EZERODIV = 12 # tried to divide by zero 19 | GSL_EBADTOL = 13 # user specified an invalid tolerance 20 | GSL_ETOL = 14 # failed to reach the specified tolerance 21 | GSL_EUNDRFLW = 15 # underflow 22 | GSL_EOVRFLW = 16 # overflow 23 | GSL_ELOSS = 17 # loss of accuracy 24 | GSL_EROUND = 18 # failed because of roundoff error 25 | GSL_EBADLEN = 19 # matrix, vector lengths are not conformant 26 | GSL_ENOTSQR = 20 # matrix not square 27 | GSL_ESING = 21 # apparent singularity detected 28 | GSL_EDIVERGE = 22 # integral or series is divergent 29 | GSL_EUNSUP = 23 # requested feature is not supported by the hardware 30 | GSL_EUNIMPL = 24 # requested feature not (yet) implemented 31 | GSL_ECACHE = 25 # cache limit exceeded 32 | GSL_ETABLE = 26 # table limit exceeded 33 | GSL_ENOPROG = 27 # iteration is not making progress towards solution 34 | GSL_ENOPROGJ = 28 # jacobian evaluations are not improving the solution 35 | GSL_ETOLF = 29 # cannot reach the specified tolerance in F 36 | GSL_ETOLX = 30 # cannot reach the specified tolerance in X 37 | GSL_ETOLG = 31 # cannot reach the specified tolerance in gradient 38 | GSL_EOF = 32 # end of file 39 | 40 | ctypedef int size_t 41 | cimport cython_gsl.gsl_mode 42 | cimport cython_gsl.gsl_math 43 | cimport cython_gsl.gsl_complex 44 | cimport cython_gsl.gsl_poly 45 | cimport cython_gsl.gsl_sf_result 46 | cimport cython_gsl.gsl_airy 47 | cimport cython_gsl.gsl_bessel 48 | cimport cython_gsl.gsl_clausen 49 | cimport cython_gsl.gsl_coulomb 50 | cimport cython_gsl.gsl_coupling 51 | cimport cython_gsl.gsl_dawson 52 | cimport cython_gsl.gsl_debye 53 | cimport cython_gsl.gsl_dilog 54 | cimport cython_gsl.gsl_elementary 55 | cimport cython_gsl.gsl_ellint 56 | cimport cython_gsl.gsl_elljac 57 | cimport cython_gsl.gsl_erf 58 | cimport cython_gsl.gsl_exp 59 | cimport cython_gsl.gsl_expint 60 | cimport cython_gsl.gsl_fermi_dirac 61 | cimport cython_gsl.gsl_gamma 62 | cimport cython_gsl.gsl_gegenbauer 63 | cimport cython_gsl.gsl_hyperg 64 | cimport cython_gsl.gsl_laguerre 65 | cimport cython_gsl.gsl_lambert 66 | cimport cython_gsl.gsl_legendre 67 | cimport cython_gsl.gsl_log 68 | cimport cython_gsl.gsl_pow_int 69 | cimport cython_gsl.gsl_psi 70 | cimport cython_gsl.gsl_synchrotron 71 | cimport cython_gsl.gsl_transport 72 | cimport cython_gsl.gsl_trig 73 | cimport cython_gsl.gsl_zeta 74 | 75 | cimport cython_gsl.gsl_block 76 | cimport cython_gsl.gsl_vector 77 | cimport cython_gsl.gsl_vector_complex 78 | cimport cython_gsl.gsl_matrix 79 | cimport cython_gsl.gsl_matrix_complex 80 | 81 | cimport cython_gsl.gsl_permutation 82 | cimport cython_gsl.gsl_combination 83 | cimport cython_gsl.gsl_sort 84 | 85 | cimport cython_gsl.gsl_blas 86 | cimport cython_gsl.gsl_linalg 87 | cimport cython_gsl.gsl_eigen 88 | cimport cython_gsl.gsl_fft 89 | cimport cython_gsl.gsl_integration 90 | cimport cython_gsl.gsl_rng 91 | cimport cython_gsl.gsl_qrng 92 | cimport cython_gsl.gsl_random 93 | cimport cython_gsl.gsl_statistics 94 | cimport cython_gsl.gsl_histogram 95 | cimport cython_gsl.gsl_ntuple 96 | cimport cython_gsl.gsl_monte 97 | cimport cython_gsl.gsl_odeiv 98 | cimport cython_gsl.gsl_odeiv2 99 | cimport cython_gsl.gsl_interp 100 | cimport cython_gsl.gsl_diff 101 | cimport cython_gsl.gsl_chebyshev 102 | cimport cython_gsl.gsl_sum 103 | cimport cython_gsl.gsl_roots 104 | cimport cython_gsl.gsl_min 105 | cimport cython_gsl.gsl_fit 106 | cimport cython_gsl.gsl_dht 107 | -------------------------------------------------------------------------------- /cython_gsl/gsl_statistics.pxd: -------------------------------------------------------------------------------- 1 | cdef extern from "gsl/gsl_statistics_double.h": 2 | double gsl_stats_mean ( double data[], size_t stride, size_t n) nogil 3 | double gsl_stats_variance ( double data[], size_t stride, size_t n) nogil 4 | double gsl_stats_sd ( double data[], size_t stride, size_t n) nogil 5 | double gsl_stats_variance_with_fixed_mean ( double data[], size_t stride, size_t n, double mean) nogil 6 | double gsl_stats_sd_with_fixed_mean ( double data[], size_t stride, size_t n, double mean) nogil 7 | double gsl_stats_absdev ( double data[], size_t stride, size_t n) nogil 8 | double gsl_stats_skew ( double data[], size_t stride, size_t n) nogil 9 | double gsl_stats_kurtosis ( double data[], size_t stride, size_t n) nogil 10 | double gsl_stats_lag1_autocorrelation ( double data[], size_t stride, size_t n) nogil 11 | 12 | double gsl_stats_covariance ( double data1[], size_t stride1, double data2[], size_t stride2, size_t n) nogil 13 | 14 | double gsl_stats_variance_m ( double data[], size_t stride, size_t n, double mean) nogil 15 | double gsl_stats_sd_m ( double data[], size_t stride, size_t n, double mean) nogil 16 | double gsl_stats_absdev_m ( double data[], size_t stride, size_t n, double mean) nogil 17 | double gsl_stats_skew_m_sd ( double data[], size_t stride, size_t n, double mean, double sd) nogil 18 | double gsl_stats_kurtosis_m_sd ( double data[], size_t stride, size_t n, double mean, double sd) nogil 19 | double gsl_stats_lag1_autocorrelation_m ( double data[], size_t stride, size_t n, double mean) nogil 20 | 21 | double gsl_stats_covariance_m ( double data1[], size_t stride1, double data2[], size_t stride2, size_t n, double mean1, double mean2) nogil 22 | 23 | # DEFINED FOR FLOATING POINT TYPES ONLY 24 | 25 | double gsl_stats_wmean ( double w[], size_t wstride, double data[], size_t stride, size_t n) nogil 26 | double gsl_stats_wvariance ( double w[], size_t wstride, double data[], size_t stride, size_t n) nogil 27 | double gsl_stats_wsd ( double w[], size_t wstride, double data[], size_t stride, size_t n) nogil 28 | double gsl_stats_wvariance_with_fixed_mean ( double w[], size_t wstride, double data[], size_t stride, size_t n, double mean) nogil 29 | double gsl_stats_wsd_with_fixed_mean ( double w[], size_t wstride, double data[], size_t stride, size_t n, double mean) nogil 30 | double gsl_stats_wabsdev ( double w[], size_t wstride, double data[], size_t stride, size_t n) nogil 31 | double gsl_stats_wskew ( double w[], size_t wstride, double data[], size_t stride, size_t n) nogil 32 | double gsl_stats_wkurtosis ( double w[], size_t wstride, double data[], size_t stride, size_t n) nogil 33 | 34 | double gsl_stats_wvariance_m ( double w[], size_t wstride, double data[], size_t stride, size_t n, double wmean) nogil 35 | double gsl_stats_wsd_m ( double w[], size_t wstride, double data[], size_t stride, size_t n, double wmean) nogil 36 | double gsl_stats_wabsdev_m ( double w[], size_t wstride, double data[], size_t stride, size_t n, double wmean) nogil 37 | double gsl_stats_wskew_m_sd ( double w[], size_t wstride, double data[], size_t stride, size_t n, double wmean, double wsd) nogil 38 | double gsl_stats_wkurtosis_m_sd ( double w[], size_t wstride, double data[], size_t stride, size_t n, double wmean, double wsd) nogil 39 | 40 | 41 | double gsl_stats_pvariance ( double data1[], size_t stride1, size_t n1, double data2[], size_t stride2, size_t n2) nogil 42 | double gsl_stats_ttest ( double data1[], size_t stride1, size_t n1, double data2[], size_t stride2, size_t n2) nogil 43 | 44 | double gsl_stats_max ( double data[], size_t stride, size_t n) nogil 45 | double gsl_stats_min ( double data[], size_t stride, size_t n) nogil 46 | void gsl_stats_minmax (double * min, double * max, double data[], size_t stride, size_t n) nogil 47 | 48 | size_t gsl_stats_max_index ( double data[], size_t stride, size_t n) nogil 49 | size_t gsl_stats_min_index ( double data[], size_t stride, size_t n) nogil 50 | void gsl_stats_minmax_index (size_t * min_index, size_t * max_index, double data[], size_t stride, size_t n) nogil 51 | 52 | double gsl_stats_median_from_sorted_data ( double sorted_data[], size_t stride, size_t n) nogil 53 | double gsl_stats_quantile_from_sorted_data ( double sorted_data[], size_t stride, size_t n, double f) nogil 54 | 55 | -------------------------------------------------------------------------------- /cython_gsl/gsl_interp.pxd: -------------------------------------------------------------------------------- 1 | cdef extern from "gsl/gsl_interp.h": 2 | 3 | ctypedef struct gsl_interp_accel 4 | 5 | ctypedef struct gsl_interp_type 6 | ctypedef struct gsl_interp 7 | 8 | gsl_interp_type * gsl_interp_linear 9 | gsl_interp_type * gsl_interp_polynomial 10 | gsl_interp_type * gsl_interp_cspline 11 | gsl_interp_type * gsl_interp_cspline_periodic 12 | gsl_interp_type * gsl_interp_akima 13 | gsl_interp_type * gsl_interp_akima_periodic 14 | 15 | gsl_interp_accel * gsl_interp_accel_alloc() nogil 16 | 17 | size_t gsl_interp_accel_find(gsl_interp_accel * a, double x_array[], size_t size, double x) nogil 18 | 19 | int gsl_interp_accel_reset (gsl_interp_accel * a) nogil 20 | 21 | void gsl_interp_accel_free(gsl_interp_accel * a) nogil 22 | 23 | gsl_interp * gsl_interp_alloc( gsl_interp_type * T, size_t n) nogil 24 | 25 | int gsl_interp_init(gsl_interp * obj, double xa[], double ya[], size_t size) nogil 26 | 27 | char * gsl_interp_name( gsl_interp * interp) nogil 28 | unsigned int gsl_interp_min_size( gsl_interp * interp) nogil 29 | 30 | 31 | int gsl_interp_eval_e( gsl_interp * obj, 32 | double xa[], double ya[], double x, 33 | gsl_interp_accel * a, double * y) nogil 34 | 35 | double gsl_interp_eval( gsl_interp * obj, 36 | double xa[], double ya[], double x, 37 | gsl_interp_accel * a) nogil 38 | 39 | int gsl_interp_eval_deriv_e( gsl_interp * obj, 40 | double xa[], double ya[], double x, 41 | gsl_interp_accel * a, 42 | double * d) nogil 43 | 44 | double gsl_interp_eval_deriv( gsl_interp * obj, 45 | double xa[], double ya[], double x, 46 | gsl_interp_accel * a) nogil 47 | 48 | int gsl_interp_eval_deriv2_e( gsl_interp * obj, 49 | double xa[], double ya[], double x, 50 | gsl_interp_accel * a, 51 | double * d2) nogil 52 | 53 | double gsl_interp_eval_deriv2( gsl_interp * obj, 54 | double xa[], double ya[], double x, 55 | gsl_interp_accel * a) nogil 56 | 57 | int gsl_interp_eval_integ_e( gsl_interp * obj, 58 | double xa[], double ya[], 59 | double a, double b, 60 | gsl_interp_accel * acc, 61 | double * result) nogil 62 | 63 | double gsl_interp_eval_integ( gsl_interp * obj, 64 | double xa[], double ya[], 65 | double a, double b, 66 | gsl_interp_accel * acc) nogil 67 | 68 | void gsl_interp_free(gsl_interp * interp) nogil 69 | 70 | size_t gsl_interp_bsearch( double x_array[], double x, 71 | size_t index_lo, size_t index_hi) nogil 72 | 73 | 74 | 75 | cdef extern from "gsl/gsl_spline.h": 76 | ctypedef struct gsl_spline 77 | 78 | gsl_spline * gsl_spline_alloc( gsl_interp_type * T, size_t size) nogil 79 | 80 | int gsl_spline_init(gsl_spline * spline, double xa[], double ya[], size_t size) nogil 81 | 82 | 83 | int gsl_spline_eval_e( gsl_spline * spline, double x, 84 | gsl_interp_accel * a, double * y) nogil 85 | 86 | double gsl_spline_eval( gsl_spline * spline, double x, gsl_interp_accel * a) nogil 87 | 88 | int gsl_spline_eval_deriv_e( gsl_spline * spline, double x, 89 | gsl_interp_accel * a, double * y) nogil 90 | 91 | double gsl_spline_eval_deriv( gsl_spline * spline, double x, gsl_interp_accel * a) nogil 92 | 93 | int gsl_spline_eval_deriv2_e( gsl_spline * spline, double x, 94 | gsl_interp_accel * a, double * y) nogil 95 | 96 | double gsl_spline_eval_deriv2( gsl_spline * spline, double x, 97 | gsl_interp_accel * a) nogil 98 | 99 | int gsl_spline_eval_integ_e( gsl_spline * spline, double a, double b, 100 | gsl_interp_accel * acc, double * y) nogil 101 | 102 | double gsl_spline_eval_integ( gsl_spline * spline, double a, double b, 103 | gsl_interp_accel * acc) nogil 104 | 105 | void gsl_spline_free(gsl_spline * spline) nogil 106 | 107 | -------------------------------------------------------------------------------- /cython_gsl/test/test_vector.py: -------------------------------------------------------------------------------- 1 | import unittest, vector, os 2 | 3 | class BlockTest(unittest.TestCase): 4 | def test_gsl_block(self): 5 | t = vector.t_gsl_block() 6 | for x in t: 7 | self.assertEqual(x, 0) 8 | 9 | class VectorTest(unittest.TestCase): 10 | 11 | def test_gsl_vector_set(self): 12 | t = vector.t_gsl_vector_set() 13 | for x in t: 14 | self.assertAlmostEqual(x, 0, 15) 15 | 16 | def test_gsl_vector_fprintf(self): 17 | self.assertEqual(vector.t_gsl_vector_fprintf(), False) 18 | os.unlink("test.dat") 19 | 20 | def test_gsl_vector_set_zero(self): 21 | t = vector.t_gsl_vector_set_zero() 22 | for x in t: 23 | self.assertEqual(x,0) 24 | 25 | def test_gsl_vector_set_all(self): 26 | t = vector.t_gsl_vector_set_all() 27 | for x in t: 28 | self.assertEqual(x,0) 29 | 30 | def test_gsl_vector_set_basis(self): 31 | t = vector.t_gsl_vector_set_basis() 32 | for x in t: 33 | self.assertEqual(x,0) 34 | 35 | def test_gsl_vector_calloc(self): 36 | t = vector.t_gsl_vector_calloc() 37 | for x in t: 38 | self.assertEqual(x,0) 39 | 40 | def test_gsl_vector_memcpy(self): 41 | t = vector.t_gsl_vector_memcpy() 42 | for x in t: 43 | self.assertAlmostEqual(x,0, 15) 44 | 45 | def test_gsl_vector_reverse(self): 46 | t = vector.t_gsl_vector_reverse() 47 | for x in t: 48 | self.assertAlmostEqual(x,0, 15) 49 | 50 | def test_gsl_vector_swap(self): 51 | t = vector.t_gsl_vector_swap() 52 | for x in t: 53 | self.assertAlmostEqual(x,0, 15) 54 | 55 | def test_gsl_vector_swap_elements(self): 56 | t = vector.t_gsl_vector_swap_elements() 57 | for x in t: 58 | self.assertAlmostEqual(x,0, 15) 59 | 60 | def test_gsl_vector_max(self): 61 | t = vector.t_gsl_vector_max() 62 | for x in t: 63 | self.assertAlmostEqual(x,0, 15) 64 | 65 | 66 | def test_gsl_vector_max_index(self): 67 | t = vector.t_gsl_vector_max_index() 68 | for x in t: 69 | self.assertAlmostEqual(x,0, 15) 70 | 71 | 72 | def test_gsl_vector_add(self): 73 | t = vector.t_gsl_vector_add() 74 | for x in t: 75 | self.assertAlmostEqual(x,0, 15) 76 | 77 | def test_gsl_vector_sub(self): 78 | t = vector.t_gsl_vector_sub() 79 | for x in t: 80 | self.assertAlmostEqual(x,0, 15) 81 | 82 | def test_gsl_vector_mul(self): 83 | t = vector.t_gsl_vector_mul() 84 | for x in t: 85 | self.assertAlmostEqual(x,0, 15) 86 | 87 | def test_gsl_vector_div(self): 88 | t = vector.t_gsl_vector_div() 89 | for x in t: 90 | self.assertAlmostEqual(x,0, 15) 91 | 92 | def test_gsl_vector_scale(self): 93 | t = vector.t_gsl_vector_scale() 94 | for x in t: 95 | self.assertAlmostEqual(x,0, 15) 96 | 97 | def test_gsl_vector_add_constant(self): 98 | t = vector.t_gsl_vector_add_constant() 99 | for x in t: 100 | self.assertAlmostEqual(x,0, 14) 101 | 102 | def test_gsl_vector_isnull(self): 103 | t = vector.t_gsl_vector_isnull() 104 | for x in t: 105 | self.assertAlmostEqual(x,0, 15) 106 | 107 | def test_gsl_vector_subvector(self): 108 | t = vector.t_gsl_vector_subvector() 109 | for x in t: 110 | self.assertAlmostEqual(x,0, 15) 111 | 112 | def test_gsl_vector_subvector_with_stride1(self): 113 | t = vector.t_gsl_vector_subvector_with_stride1() 114 | for x in t: 115 | self.assertAlmostEqual(x,0, 15) 116 | 117 | def test_gsl_vector_subvector_with_stride2(self): 118 | t = vector.t_gsl_vector_subvector_with_stride2() 119 | for x in t: 120 | self.assertAlmostEqual(x,0, 15) 121 | 122 | def test_gsl_vector_view_array(self): 123 | t = vector.t_gsl_vector_view_array() 124 | for x in t: 125 | self.assertAlmostEqual(x,0, 15) 126 | 127 | def test_gsl_vector_view_array_with_stride(self): 128 | t = vector.t_gsl_vector_view_array_with_stride() 129 | for x in t: 130 | self.assertAlmostEqual(x,0, 15) 131 | 132 | if __name__ == '__main__': 133 | unittest.main() 134 | -------------------------------------------------------------------------------- /cython_gsl/test/test_matrix.py: -------------------------------------------------------------------------------- 1 | import unittest, matrix, os 2 | 3 | class MatrixTest(unittest.TestCase): 4 | 5 | def test_gsl_matrix_alloc(self): 6 | t = matrix.t_gsl_matrix_alloc() 7 | for x in t: 8 | self.assertAlmostEqual(x,0, 13) 9 | 10 | def test_gsl_matrix_calloc(self): 11 | t = matrix.t_gsl_matrix_calloc() 12 | for x in t: 13 | self.assertAlmostEqual(x,0, 15) 14 | 15 | def test_gsl_matrix_max(self): 16 | t = matrix.t_gsl_matrix_max() 17 | for x in t: 18 | self.assertAlmostEqual(x,0, 15) 19 | 20 | def test_gsl_matrix_isnull(self): 21 | t = matrix.t_gsl_matrix_isnull() 22 | for x in t: 23 | self.assertAlmostEqual(x,0, 15) 24 | 25 | def test_gsl_matrix_add(self): 26 | t = matrix.t_gsl_matrix_add() 27 | for x in t: 28 | self.assertAlmostEqual(x,0, 15) 29 | 30 | def test_gsl_matrix_mul_elements(self): 31 | t = matrix.t_gsl_matrix_mul_elements() 32 | for x in t: 33 | self.assertAlmostEqual(x,0, 14) 34 | 35 | def test_gsl_matrix_scale(self): 36 | t = matrix.t_gsl_matrix_scale() 37 | for x in t: 38 | self.assertAlmostEqual(x,0, 15) 39 | 40 | def test_gsl_matrix_add_constant(self): 41 | t = matrix.t_gsl_matrix_add_constant() 42 | for x in t: 43 | self.assertAlmostEqual(x,0, 15) 44 | 45 | def test_gsl_matrix_add_diagonal(self): 46 | t = matrix.t_gsl_matrix_add_diagonal() 47 | for x in t: 48 | self.assertAlmostEqual(x,0, 15) 49 | 50 | def test_gsl_matrix_memcpy(self): 51 | t = matrix.t_gsl_matrix_memcpy() 52 | for x in t: 53 | self.assertAlmostEqual(x,0, 15) 54 | 55 | def test_gsl_matrix_swap(self): 56 | t = matrix.t_gsl_matrix_swap() 57 | for x in t: 58 | self.assertAlmostEqual(x,0, 15) 59 | 60 | def test_gsl_matrix_swap_rows(self): 61 | t = matrix.t_gsl_matrix_swap_rows() 62 | for x in t: 63 | self.assertAlmostEqual(x,0, 15) 64 | 65 | def test_gsl_matrix_swap_columns(self): 66 | t = matrix.t_gsl_matrix_swap_columns() 67 | for x in t: 68 | self.assertAlmostEqual(x,0, 15) 69 | 70 | def test_gsl_matrix_set_identity(self): 71 | t = matrix.t_gsl_matrix_set_identity() 72 | for x in t: 73 | self.assertAlmostEqual(x,0, 15) 74 | 75 | def test_gsl_matrix_transpose(self): 76 | t = matrix.t_gsl_matrix_transpose() 77 | for x in t: 78 | self.assertAlmostEqual(x,0, 15) 79 | 80 | def test_gsl_matrix_transpose_memcpy(self): 81 | t = matrix.t_gsl_matrix_transpose_memcpy() 82 | for x in t: 83 | self.assertAlmostEqual(x,0, 15) 84 | 85 | def test_gsl_matrix_row(self): 86 | t = matrix.t_gsl_matrix_row() 87 | for x in t: 88 | self.assertAlmostEqual(x,0, 15) 89 | 90 | def test_gsl_matrix_column(self): 91 | t = matrix.t_gsl_matrix_column() 92 | for x in t: 93 | self.assertAlmostEqual(x,0, 15) 94 | 95 | def test_gsl_matrix_submatrix(self): 96 | t = matrix.t_gsl_matrix_submatrix() 97 | for x in t: 98 | self.assertAlmostEqual(x,0, 15) 99 | 100 | def test_gsl_matrix_diagonal(self): 101 | t = matrix.t_gsl_matrix_diagonal() 102 | for x in t: 103 | self.assertAlmostEqual(x,0, 15) 104 | 105 | def test_gsl_matrix_subdiagonal(self): 106 | t = matrix.t_gsl_matrix_subdiagonal() 107 | for x in t: 108 | self.assertAlmostEqual(x,0, 15) 109 | 110 | def test_gsl_matrix_superdiagonal(self): 111 | t = matrix.t_gsl_matrix_superdiagonal() 112 | for x in t: 113 | self.assertAlmostEqual(x,0, 15) 114 | 115 | def test_gsl_matrix_view_array(self): 116 | t = matrix.t_gsl_matrix_view_array() 117 | for x in t: 118 | self.assertAlmostEqual(x,0, 15) 119 | 120 | def test_gsl_matrix_view_vector(self): 121 | t = matrix.t_gsl_matrix_view_vector() 122 | for x in t: 123 | self.assertAlmostEqual(x,0, 15) 124 | 125 | def test_gsl_matrix_fprintf(self): 126 | t = matrix.t_gsl_matrix_fprintf() 127 | os.unlink('test.dat') 128 | for x in t: 129 | self.assertAlmostEqual(x,0, 15) 130 | 131 | def test_gsl_matrix_set_row(self): 132 | t = matrix.t_gsl_matrix_set_row() 133 | for x in t: 134 | self.assertAlmostEqual(x,0, 13) 135 | 136 | if __name__ == '__main__': 137 | unittest.main() 138 | -------------------------------------------------------------------------------- /cython_gsl/test/test_linalg.py: -------------------------------------------------------------------------------- 1 | import unittest, linalg, os 2 | 3 | class LinalgTest(unittest.TestCase): 4 | 5 | def test_gsl_linalg_LU_decomp(self): 6 | t = linalg.t_gsl_linalg_LU_decomp() 7 | for x in t: 8 | self.assertAlmostEqual(x,0, 14) 9 | 10 | 11 | def test_gsl_linalg_LU_solve(self): 12 | t = linalg.t_gsl_linalg_LU_solve() 13 | for x in t: 14 | self.assertAlmostEqual(x,0, 14) 15 | 16 | def test_gsl_linalg_LU_svx(self): 17 | t = linalg.t_gsl_linalg_LU_svx() 18 | for x in t: 19 | self.assertAlmostEqual(x,0, 14) 20 | 21 | @unittest.expectedFailure 22 | def test_gsl_linalg_LU_refine(self): 23 | t = linalg.t_gsl_linalg_LU_refine() 24 | for x in t: 25 | self.assertAlmostEqual(x,0, 14) 26 | 27 | def test_gsl_linalg_LU_invert(self): 28 | t = linalg.t_gsl_linalg_LU_invert() 29 | for x in t: 30 | self.assertAlmostEqual(x,0, 14) 31 | 32 | def test_gsl_linalg_LU_det(self): 33 | t = linalg.t_gsl_linalg_LU_det() 34 | for x in t: 35 | self.assertAlmostEqual(x,0, 14) 36 | 37 | def test_gsl_linalg_householder_transform(self): 38 | t = linalg.t_gsl_linalg_householder_transform() 39 | for x in t: 40 | self.assertAlmostEqual(x,0, 14) 41 | 42 | def test_gsl_linalg_householder_hm(self): 43 | t = linalg.t_gsl_linalg_householder_hm() 44 | for x in t: 45 | self.assertAlmostEqual(x,0, 14) 46 | 47 | def test_gsl_linalg_householder_mh(self): 48 | t = linalg.t_gsl_linalg_householder_mh() 49 | for x in t: 50 | self.assertAlmostEqual(x,0, 14) 51 | 52 | def test_gsl_linalg_QR_decomp(self): 53 | t = linalg.t_gsl_linalg_QR_decomp() 54 | for x in t: 55 | self.assertAlmostEqual(x,0, 14) 56 | 57 | def test_gsl_linalg_QR_solve(self): 58 | t = linalg.t_gsl_linalg_QR_solve() 59 | for x in t: 60 | self.assertAlmostEqual(x,0, 14) 61 | 62 | def test_gsl_linalg_QR_svx(self): 63 | t = linalg.t_gsl_linalg_QR_svx() 64 | for x in t: 65 | self.assertAlmostEqual(x,0, 14) 66 | 67 | def test_gsl_linalg_QR_lssolve(self): 68 | t = linalg.t_gsl_linalg_QR_lssolve() 69 | for x in t: 70 | self.assertAlmostEqual(x,0, 14) 71 | 72 | def test_gsl_linalg_QR_Qvec(self): 73 | t = linalg.t_gsl_linalg_QR_Qvec() 74 | for x in t: 75 | self.assertAlmostEqual(x,0, 14) 76 | 77 | def test_gsl_linalg_R_solve(self): 78 | t = linalg.t_gsl_linalg_R_solve() 79 | for x in t: 80 | self.assertAlmostEqual(x,0, 14) 81 | 82 | def test_gsl_linalg_QR_QRsolve(self): 83 | t = linalg.t_gsl_linalg_QR_QRsolve() 84 | for x in t: 85 | self.assertAlmostEqual(x,0, 14) 86 | 87 | def test_gsl_linalg_SV_decomp(self): 88 | t = linalg.t_gsl_linalg_SV_decomp() 89 | for x in t: 90 | self.assertAlmostEqual(x,0, 14) 91 | 92 | def test_gsl_linalg_SV_decomp_mod(self): 93 | t = linalg.t_gsl_linalg_SV_decomp_mod() 94 | for x in t: 95 | self.assertAlmostEqual(x,0, 14) 96 | 97 | def test_gsl_linalg_SV_decomp_jacobi(self): 98 | t = linalg.t_gsl_linalg_SV_decomp_jacobi() 99 | for x in t: 100 | self.assertAlmostEqual(x,0, 14) 101 | 102 | def test_gsl_linalg_cholesky_decomp(self): 103 | t = linalg.t_gsl_linalg_cholesky_decomp() 104 | for x in t: 105 | self.assertAlmostEqual(x,0, 14) 106 | 107 | def test_gsl_linalg_cholesky_solve(self): 108 | t = linalg.t_gsl_linalg_cholesky_solve() 109 | for x in t: 110 | self.assertAlmostEqual(x,0, 14) 111 | 112 | def test_gsl_linalg_HH_solve(self): 113 | t = linalg.t_gsl_linalg_HH_solve() 114 | for x in t: 115 | self.assertAlmostEqual(x,0, 14) 116 | 117 | def test_gsl_linalg_solve_symm_tridiag(self): 118 | t = linalg.t_gsl_linalg_solve_symm_tridiag() 119 | for x in t: 120 | self.assertAlmostEqual(x,0, 14) 121 | 122 | def test_gsl_linalg_solve_symm_cyc_tridiag(self): 123 | t = linalg.t_gsl_linalg_solve_symm_cyc_tridiag() 124 | for x in t: 125 | self.assertAlmostEqual(x,0, 14) 126 | 127 | def test_gsl_linalg_symmtd_decomp(self): 128 | t = linalg.t_gsl_linalg_symmtd_decomp() 129 | for x in t: 130 | self.assertAlmostEqual(x,0, 14) 131 | 132 | def test_gsl_linalg_bidiag_decomp(self): 133 | t = linalg.t_gsl_linalg_bidiag_decomp() 134 | for x in t: 135 | self.assertAlmostEqual(x,0, 14) 136 | 137 | if __name__ == '__main__': 138 | unittest.main() 139 | -------------------------------------------------------------------------------- /cython_gsl/test/test_matrix_complex.py: -------------------------------------------------------------------------------- 1 | import unittest, matrix_complex, os 2 | 3 | class MatrixComplexTest(unittest.TestCase): 4 | 5 | def test_gsl_matrix_complex_set(self): 6 | t = matrix_complex.t_gsl_matrix_complex_set() 7 | for x in t: 8 | self.assertAlmostEqual(x,0, 15) 9 | 10 | def test_gsl_matrix_complex_calloc(self): 11 | t = matrix_complex.t_gsl_matrix_complex_calloc() 12 | for x in t: 13 | self.assertAlmostEqual(x,0, 15) 14 | 15 | def test_gsl_matrix_complex_isnull(self): 16 | t = matrix_complex.t_gsl_matrix_complex_isnull() 17 | for x in t: 18 | self.assertAlmostEqual(x,0, 15) 19 | 20 | def test_gsl_matrix_complex_add(self): 21 | t = matrix_complex.t_gsl_matrix_complex_add() 22 | for x in t: 23 | self.assertAlmostEqual(x,0, 15) 24 | 25 | def test_gsl_matrix_complex_mul_elements(self): 26 | t = matrix_complex.t_gsl_matrix_complex_mul_elements() 27 | for x in t: 28 | self.assertAlmostEqual(x,0, 15) 29 | 30 | def test_gsl_matrix_complex_scale(self): 31 | t = matrix_complex.t_gsl_matrix_complex_scale() 32 | for x in t: 33 | self.assertAlmostEqual(x,0, 15) 34 | 35 | def test_gsl_matrix_complex_add_constant(self): 36 | t = matrix_complex.t_gsl_matrix_complex_add_constant() 37 | for x in t: 38 | self.assertAlmostEqual(x,0, 15) 39 | 40 | def test_gsl_matrix_complex_add_diagonal(self): 41 | t = matrix_complex.t_gsl_matrix_complex_add_diagonal() 42 | for x in t: 43 | self.assertAlmostEqual(x,0, 15) 44 | 45 | def test_gsl_matrix_complex_memcpy(self): 46 | t = matrix_complex.t_gsl_matrix_complex_memcpy() 47 | for x in t: 48 | self.assertAlmostEqual(x,0, 15) 49 | 50 | def test_gsl_matrix_complex_swap(self): 51 | t = matrix_complex.t_gsl_matrix_complex_swap() 52 | for x in t: 53 | self.assertAlmostEqual(x,0, 15) 54 | 55 | def test_gsl_matrix_complex_swap_rows(self): 56 | t = matrix_complex.t_gsl_matrix_complex_swap_rows() 57 | for x in t: 58 | self.assertAlmostEqual(x,0, 15) 59 | 60 | def test_gsl_matrix_complex_swap_columns(self): 61 | t = matrix_complex.t_gsl_matrix_complex_swap_columns() 62 | for x in t: 63 | self.assertAlmostEqual(x,0, 15) 64 | 65 | def test_gsl_matrix_complex_set_identity(self): 66 | t = matrix_complex.t_gsl_matrix_complex_set_identity() 67 | for x in t: 68 | self.assertAlmostEqual(x,0, 15) 69 | 70 | def test_gsl_matrix_complex_transpose(self): 71 | t = matrix_complex.t_gsl_matrix_complex_transpose() 72 | for x in t: 73 | self.assertAlmostEqual(x,0, 15) 74 | 75 | def test_gsl_matrix_complex_transpose_memcpy(self): 76 | t = matrix_complex.t_gsl_matrix_complex_transpose_memcpy() 77 | for x in t: 78 | self.assertAlmostEqual(x,0, 15) 79 | 80 | def test_gsl_matrix_complex_row(self): 81 | t = matrix_complex.t_gsl_matrix_complex_row() 82 | for x in t: 83 | self.assertAlmostEqual(x,0, 15) 84 | 85 | def test_gsl_matrix_complex_column(self): 86 | t = matrix_complex.t_gsl_matrix_complex_column() 87 | for x in t: 88 | self.assertAlmostEqual(x,0, 15) 89 | 90 | def test_gsl_matrix_complex_submatrix(self): 91 | t = matrix_complex.t_gsl_matrix_complex_submatrix() 92 | for x in t: 93 | self.assertAlmostEqual(x,0, 15) 94 | 95 | def test_gsl_matrix_complex_diagonal(self): 96 | t = matrix_complex.t_gsl_matrix_complex_diagonal() 97 | for x in t: 98 | self.assertAlmostEqual(x,0, 15) 99 | 100 | def test_gsl_matrix_complex_subdiagonal(self): 101 | t = matrix_complex.t_gsl_matrix_complex_subdiagonal() 102 | for x in t: 103 | self.assertAlmostEqual(x,0, 15) 104 | 105 | def test_gsl_matrix_complex_superdiagonal(self): 106 | t = matrix_complex.t_gsl_matrix_complex_superdiagonal() 107 | for x in t: 108 | self.assertAlmostEqual(x,0, 15) 109 | 110 | def test_gsl_matrix_complex_view_array(self): 111 | t = matrix_complex.t_gsl_matrix_complex_view_array() 112 | for x in t: 113 | self.assertAlmostEqual(x,0, 15) 114 | 115 | def test_gsl_matrix_complex_view_vector(self): 116 | t = matrix_complex.t_gsl_matrix_complex_view_vector() 117 | for x in t: 118 | self.assertAlmostEqual(x,0, 15) 119 | 120 | def test_gsl_matrix_complex_fprintf(self): 121 | t = matrix_complex.t_gsl_matrix_complex_fprintf() 122 | os.unlink('test.dat') 123 | for x in t: 124 | self.assertAlmostEqual(x,0, 15) 125 | 126 | if __name__ == '__main__': 127 | unittest.main() 128 | -------------------------------------------------------------------------------- /cython_gsl/test/mathgsl.pyx: -------------------------------------------------------------------------------- 1 | #cython: cdivision=True 2 | 3 | from cython_gsl cimport * 4 | 5 | 6 | def t_M_PI(): 7 | return M_PI 8 | 9 | def t_M_E(): 10 | return M_E 11 | 12 | def t_M_LOG2E(): 13 | return M_LOG2E 14 | 15 | 16 | def t_M_LOG10E(): 17 | return M_LOG10E 18 | 19 | 20 | def t_M_LN2(): 21 | return M_LN2 22 | 23 | def t_M_PI_2(): 24 | return M_PI_2 25 | 26 | def t_M_PI_4(): 27 | return M_PI_4 28 | 29 | def t_M_1_PI(): 30 | return M_1_PI 31 | 32 | def t_M_2_PI(): 33 | return M_2_PI 34 | 35 | def t_M_2_SQRTPI(): 36 | return M_2_SQRTPI 37 | 38 | def t_M_SQRT2(): 39 | return M_SQRT2 40 | 41 | def t_M_SQRT1_2(): 42 | return M_SQRT1_2 43 | 44 | def t_M_LNPI(): 45 | return M_LNPI - log(M_PI) 46 | 47 | def t_M_EULER(): 48 | return M_EULER 49 | 50 | def t_isnan1(): 51 | cdef double inf, nan 52 | inf = exp (1.0e10) 53 | nan = inf/inf 54 | return gsl_isnan(nan) 55 | 56 | def t_isnan2(): 57 | return gsl_isnan(1.3) 58 | 59 | def t_isinf1(): 60 | cdef double inf 61 | inf = exp (1.0e10) 62 | return gsl_isinf(inf) 63 | 64 | 65 | def t_isinf2(): 66 | cdef double inf 67 | inf = -exp (1.0e10) 68 | return gsl_isinf(inf) 69 | 70 | def t_isinf3(): 71 | cdef double a 72 | a = exp (2.3) 73 | return gsl_isinf(a) 74 | 75 | def t_finite1(): 76 | cdef double inf 77 | inf = exp (1.0e10) 78 | return gsl_finite(inf) 79 | 80 | def t_finite2(): 81 | cdef double a 82 | a = exp (2.3) 83 | return gsl_finite(a) 84 | 85 | def t_gsl_log1p(double x): 86 | return gsl_log1p(x) - log(1 + x) 87 | 88 | def t_gsl_expm1(double x): 89 | return gsl_expm1(x) - (exp(x) - 1) 90 | 91 | def t_gsl_hypot(double x, double y): 92 | return gsl_hypot(x, y) - sqrt(x*x + y*y) 93 | 94 | def t_gsl_acosh(double x): 95 | return cosh(gsl_acosh(x)) - x 96 | 97 | def t_gsl_asinh(double x): 98 | return sinh(gsl_asinh(x)) - x 99 | 100 | def t_gsl_atanh(double x): 101 | return tanh(gsl_atanh(x)) - x 102 | 103 | def t_gsl_ldexp(double x): 104 | return gsl_ldexp(x, 3) - x * 2**3 105 | 106 | def t_gsl_frexp(): 107 | cdef double a 108 | cdef int e 109 | a = gsl_frexp(0.6 * 2**-15, &e) 110 | return (a,e) 111 | 112 | # Small integer powers 113 | def t_gsl_pow_2(double x): 114 | return gsl_pow_2(x) - x*x 115 | 116 | def t_gsl_pow_3(double x): 117 | return gsl_pow_3(x) - x*x*x 118 | 119 | def t_gsl_pow_4(double x): 120 | return gsl_pow_4(x) - x*x*x*x 121 | 122 | def t_gsl_pow_5(double x): 123 | return gsl_pow_5(x) - x*x*x*x*x 124 | 125 | def t_gsl_pow_6(double x): 126 | return gsl_pow_6(x) - x*x*x*x*x*x 127 | 128 | def t_gsl_pow_7(double x): 129 | return gsl_pow_7(x) - x*x*x*x*x*x*x 130 | 131 | def t_gsl_pow_8(double x): 132 | return gsl_pow_8(x) - x*x*x*x*x*x*x*x 133 | 134 | def t_gsl_pow_9(double x): 135 | return gsl_pow_9(x) - x*x*x*x*x*x*x*x*x 136 | 137 | #Testing the Sign of Numbers 138 | def t_GSL_SIGN(double x): 139 | return GSL_SIGN(x) 140 | 141 | #Testing for Odd and Even Numbers 142 | def t_GSL_IS_ODD(int x): 143 | return GSL_IS_ODD(x) 144 | 145 | def t_GSL_IS_EVEN(int x): 146 | return GSL_IS_EVEN(x) 147 | 148 | #Maximum and Minimum functions 149 | def t_GSL_MAX(double x, double y): 150 | return GSL_MAX(x,y) 151 | 152 | def t_GSL_MIN(double x, double y): 153 | return GSL_MIN(x,y) 154 | 155 | def t_GSL_MAX_DBL(double x, double y): 156 | return GSL_MAX_DBL(x,y) 157 | 158 | def t_GSL_MIN_DBL(double x, double y): 159 | return GSL_MIN_DBL(x,y) 160 | 161 | def t_GSL_MAX_INT(int a, int b): 162 | return GSL_MAX_INT(a,b) 163 | 164 | def t_GSL_MIN_INT(int a, int b): 165 | return GSL_MIN_INT(a,b) 166 | 167 | 168 | #Approximate Comparison of Floating Point Numbers 169 | def t_gsl_fcmp1( ): 170 | eps = 1.0e-15 171 | x = 1.0e-12; y = x/8 172 | return gsl_fcmp(x,y,eps) 173 | 174 | def t_gsl_fcmp2( ): 175 | eps = 1.0e-15 176 | # returns 0, i.e. 1 + cos(M_PI/2) =~ 1 177 | return gsl_fcmp(1 + cos(M_PI/2),1,eps) 178 | 179 | def t_gsl_fcmp3( ): 180 | eps = 1.0e-15 181 | # returns 1; cos(M_PI/2) !~ 0 182 | return gsl_fcmp(cos(M_PI/2),0.0,eps) 183 | 184 | # Definition of an arbitrary function with parameters 185 | # see ../examples/integration.pyx 186 | 187 | cdef double gsl_function_f1(double x, void *p) nogil: 188 | cdef double a, b 189 | a = ( p)[0] 190 | return a*x 191 | 192 | cdef double gsl_function_a 193 | gsl_function_a = 3 194 | cdef gsl_function F 195 | F.function = &gsl_function_f1 196 | F.params = &gsl_function_a 197 | 198 | def t_gsl_function(): 199 | cdef double x 200 | x = 7 201 | return F.function(x, F.params) - x*gsl_function_a 202 | 203 | def t_GSL_FN_EVAL(): 204 | cdef double x 205 | x = 7 206 | return GSL_FN_EVAL(&F, x) - x*gsl_function_a 207 | 208 | # Definition of an arbitrary function returning two values, r1, r2 209 | --------------------------------------------------------------------------------