Guest User

Untitled

a guest
Nov 23rd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. import numpy as np
  2. cimport numpy as np
  3. cimport cython
  4. from cython.view cimport array as cvarray
  5. from libc.stdlib cimport malloc, free
  6. from libc.math cimport log, exp
  7. from cython_gsl cimport *
  8. import ctypes
  9. cdef gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937)
  10.  
  11.  
  12. @cython.cdivision(True)
  13. @cython.wraparound(False)
  14. @cython.boundscheck(False)
  15. cdef void generator(double* alpha,int* D, double* m):
  16.  
  17. cdef Py_ssize_t i
  18. for i from 0 <= i < D[0]:
  19. m[i]=gsl_ran_beta(r, alpha[0], 1)
  20. return
  21.  
  22. @cython.cdivision(True)
  23. @cython.boundscheck(False)
  24. @cython.wraparound(False)
  25. cdef void initializer(double* alpha, int* D, int* N, double* m, int** Z ):
  26. cdef int i, j
  27. Z = <int **> malloc(N[0] * sizeof(int*))
  28. for i from 0 <= i < N[0]:
  29. Z[i] = <int *> malloc(D[0] * sizeof(int))
  30.  
  31. generator(&alpha[0], &D[0], &m[0])
  32.  
  33. for i from 0 <= i < D[0]:
  34. for j from 0 <= j < N[0]:
  35. Z[j][i]= gsl_ran_bernoulli(r, m[i])
  36. print Z[j][i]
  37. return
  38.  
  39. def run(int n, int d, double alpha):
  40. cdef np.ndarray[double, ndim=1, mode='c'] mu=np.empty((d,), dtype=ctypes.c_double)
  41. cdef np.ndarray[int, ndim=2, mode='c'] Z=np.zeros((n,d), dtype=ctypes.c_int)
  42. initializer(&alpha, &d, &n, &mu[0], &Z[0][0] )
  43.  
  44. from distutils.core import setup, Extension
  45. from Cython.Build import cythonize
  46. from numpy import get_include
  47. import numpy
  48. import cython_gsl
  49.  
  50. ext_modules = [
  51. Extension(
  52. "test",
  53. ["test.pyx"],
  54. libraries=cython_gsl.get_libraries(),
  55. library_dirs=[cython_gsl.get_library_dir()],
  56. include_dirs=[numpy.get_include(), cython_gsl.get_include()])
  57.  
  58. ]
  59. ext_modules = cythonize(ext_modules)
  60.  
  61. setup(
  62. name='test',
  63. ext_modules=ext_modules,
  64. cmdclass={'build_ext': build_ext})
  65.  
  66. def run(int n, int d, double alpha):
  67. cdef np.ndarray[double, ndim=1, mode='c'] mu=np.empty((d,), dtype=ctypes.c_double)
  68. cdef np.ndarray[int, ndim=2, mode='c'] Z=np.zeros((n,d), dtype=ctypes.c_int)
  69. initializer(&alpha, &d, &n, &mu[0], &Z[0][0] ) ^
  70. ------------------------------------------------------------
  71.  
  72. test.pyx:46:39: Cannot take address of Python variable
Add Comment
Please, Sign In to add comment