Skip to content

Instantly share code, notes, and snippets.

@lzamparo
Created July 30, 2015 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lzamparo/39a951106dafe9aaabf0 to your computer and use it in GitHub Desktop.
Save lzamparo/39a951106dafe9aaabf0 to your computer and use it in GitHub Desktop.
Cython error compiling test example with overloaded constructor
import numpy as np
cimport numpy as np
cimport cython
include "random.pyx"
@cython.boundscheck(False)
def example(n):
cdef int N = n
cdef rng r
cdef rng_sampler[double] * rng_p = new rng_sampler[double](r)
cdef rng_sampler[double] rng = deref(rng_p)
cdef np.ndarray[np.double_t, ndim=1] result = np.empty(N, dtype=np.double)
for i in range(N):
result[i] = rng.normal(0.0, 2.0)
print result
return result
import numpy as np
cimport numpy as np
cimport cython
include "random.pyx"
@cython.boundscheck(False)
def example_seed(n, seed):
cdef int N = n
cdef unsigned long Seed = seed
cdef rng r
cdef rng_sampler[double] * rng_p = new rng_sampler[double](Seed)
cdef rng_sampler[double] rng = deref(rng_p)
cdef np.ndarray[np.double_t, ndim=1] result = np.empty(N, dtype=np.double)
for i in range(N):
result[i] = rng.normal(0.0, 2.0)
print result
return result
import numpy as np
cimport numpy as np
cimport cython
from cython.operator cimport dereference as deref
from libcpp cimport bool
cdef extern from "boost/random/mersenne_twister.hpp" namespace "boost::random" nogil:
# random number generator
cdef cppclass mt19937:
#init
mt19937() nogil
#attributes
#methods
seed(unsigned long)
cdef extern from "rng_wrapper.hpp" nogil:
# wrapper to distributions ...
cdef cppclass rng_sampler[result_type]:
#init
rng_sampler(mt19937) nogil
rng_sampler(unsigned long) nogil
rng_sampler() nogil
# methods (gamma and exp are using rate param)
result_type normal(result_type, result_type) nogil
result_type gamma(result_type, result_type) nogil
result_type uniform(result_type, result_type) nogil
result_type exp(result_type) nogil
result_type chisq(result_type) nogil
ctypedef mt19937 rng
#include <ctime>
#include "boost/random/mersenne_twister.hpp"
#include "boost/random/normal_distribution.hpp"
#include "boost/random/gamma_distribution.hpp"
#include "boost/random/exponential_distribution.hpp"
#include "boost/random/uniform_real_distribution.hpp"
#include "boost/random/chi_squared_distribution.hpp"
using namespace boost::random;
template<typename float_t=double>
class rng_sampler {
public:
typedef float_t result_type;
rng_sampler(mt19937 &in_R) : R(in_R) {}
rng_sampler() {
R = mt19937();
R.seed(std::clock());
}
rng_sampler(unsigned long seed) {
R = mt19937();
R.seed(seed);
}
result_type normal(result_type mu, result_type sigma) {
normal_distribution<result_type> nrm(mu, sigma);
return nrm(R);
}
result_type gamma(result_type alpha, result_type beta) {
gamma_distribution<result_type> gam(alpha);
return (1/beta)*gam(R);
}
result_type chisq(result_type nu) {
chi_squared_distribution<result_type> chi(nu);
return chi(R);
}
result_type uniform(result_type a, result_type b) {
uniform_real_distribution<result_type> unif(a, b);
return unif(R);
}
result_type exp(result_type mu) {
exponential_distribution<result_type> expdist(1/mu);
return expdist(R);
}
private:
mt19937 R;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment