Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- cimport numpy as np
- cimport cython
- from cython.view cimport array as cvarray
- from libc.stdlib cimport malloc, free
- from libc.math cimport log, exp
- from cython_gsl cimport *
- import ctypes
- cdef gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937)
- @cython.cdivision(True)
- @cython.wraparound(False)
- @cython.boundscheck(False)
- cdef void generator(double* alpha,int* D, double* m):
- cdef Py_ssize_t i
- for i from 0 <= i < D[0]:
- m[i]=gsl_ran_beta(r, alpha[0], 1)
- return
- @cython.cdivision(True)
- @cython.boundscheck(False)
- @cython.wraparound(False)
- cdef void initializer(double* alpha, int* D, int* N, double* m, int** Z ):
- cdef int i, j
- Z = <int **> malloc(N[0] * sizeof(int*))
- for i from 0 <= i < N[0]:
- Z[i] = <int *> malloc(D[0] * sizeof(int))
- generator(&alpha[0], &D[0], &m[0])
- for i from 0 <= i < D[0]:
- for j from 0 <= j < N[0]:
- Z[j][i]= gsl_ran_bernoulli(r, m[i])
- print Z[j][i]
- return
- def run(int n, int d, double alpha):
- cdef np.ndarray[double, ndim=1, mode='c'] mu=np.empty((d,), dtype=ctypes.c_double)
- cdef np.ndarray[int, ndim=2, mode='c'] Z=np.zeros((n,d), dtype=ctypes.c_int)
- initializer(&alpha, &d, &n, &mu[0], &Z[0][0] )
- from distutils.core import setup, Extension
- from Cython.Build import cythonize
- from numpy import get_include
- import numpy
- import cython_gsl
- ext_modules = [
- Extension(
- "test",
- ["test.pyx"],
- libraries=cython_gsl.get_libraries(),
- library_dirs=[cython_gsl.get_library_dir()],
- include_dirs=[numpy.get_include(), cython_gsl.get_include()])
- ]
- ext_modules = cythonize(ext_modules)
- setup(
- name='test',
- ext_modules=ext_modules,
- cmdclass={'build_ext': build_ext})
- def run(int n, int d, double alpha):
- cdef np.ndarray[double, ndim=1, mode='c'] mu=np.empty((d,), dtype=ctypes.c_double)
- cdef np.ndarray[int, ndim=2, mode='c'] Z=np.zeros((n,d), dtype=ctypes.c_int)
- initializer(&alpha, &d, &n, &mu[0], &Z[0][0] ) ^
- ------------------------------------------------------------
- test.pyx:46:39: Cannot take address of Python variable
Add Comment
Please, Sign In to add comment