In the following code, I sample a one-dimensional double-valued
matrix (mu
) from the beta distribution and then I use this mu
matrix to sample a 2-d binary-valued matrix (Z
), using the Bernoulli distribution. However sometimes I end up getting some columns which are just filled by zero
values. How can I write efficiently a function that discard columns which are occupied by zeros and also discard the corresponding values in the matrix mu
without causing any confliction in function func
where the gsl matrices Z
, mu
were first defined?
Since I will need to frequently call the function which deletes zero-valued columns in the code, I am wondering what is the best way to define kind of dynamic gsl matrix and allocate a specific size but be able to resize
the matrix over and over without occurring any error?
Here is my code so far:
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
from libc.string cimport memset
cdef extern from "stdlib.h":
long rand()
void srand(int seed)
cdef extern from "time.h":
ctypedef long time_t
time_t time(time_t*)
cdef gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937)
srand(time(NULL))
@cython.cdivision(True)
@cython.boundscheck(False)
@cython.wraparound(False)
cdef void initPar( double* alpha, int* K, int* N, gsl_matrix* mu, gsl_matrix *Z ):
cdef:
int i, j
int seed = rand()
#generate random seed
gsl_rng_set(r, seed)
cdef double prev_mu
for i from 0 <= i < K[0]:
if i >= 1:
prev_mu *= gsl_ran_beta(r, alpha[0], 1)
else:
prev_mu = gsl_ran_beta(r, alpha[0], 1)
gsl_matrix_set(mu, i, 0, prev_mu)
cdef double mi
for i from 0 <= i < K[0]:
mi= gsl_matrix_get(mu, i, 0)
for j from 0 <= j < N[0]:
gsl_matrix_set(Z, j, i, gsl_ran_bernoulli(r, mi) )
return
def func(np.ndarray[ndim=2, dtype=np.float64_t] inputs, int LFeatures = 5, double alpha):
cdef:
int *K_init= &LFeatures
int N = inputs.shape[0]
int D = inputs.shape[1]
gsl_matrix *mu = gsl_matrix_alloc(K_init[0], 1)
gsl_matrix *Z = gsl_matrix_alloc(N, K_init[0])
int i, j
gsl_matrix *X = gsl_matrix_alloc(N, D)
for i from 0 <= i < N:
for j from 0 <= j < D:
gsl_matrix_set(X, i, j, inputs[i,j])
gsl_matrix_set_zero(mu)
gsl_matrix_set_zero(Z)
initPar(&alpha, K_init, &N, mu, Z )
Thanks!