Class: Numo::GSL::Ran::Discrete

Inherits:
Object
  • Object
show all
Defined in:
ext/numo/gsl/ran/gsl_ran.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(K) ⇒ Object

allocate instance of Discrete class.

This function returns a pointer to a structure that contains the lookup table for the discrete random number generator. The array P[] contains the probabilities of the discrete events; these array elements must all be positive, but they needn’t add up to one (so you can think of them more generally as ``weights’’)—the preprocessor will normalize appropriately. This return value is used as an argument for the gsl_ran_discrete function below.

Parameters:

  • K (DFloat)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'ext/numo/gsl/ran/gsl_ran.c', line 73

static VALUE
ran_discrete_s_new(VALUE self, VALUE v1)
{
    narray_t *na;
    double *d;
    gsl_ran_discrete_t *w;

    v1 = rb_funcall(cDF,rb_intern("cast"),1,v1);
    GetNArray(v1,na);
    d = (double*)na_get_pointer_for_read(v1);
    w = gsl_ran_discrete_preproc(na->size, d);
    RB_GC_GUARD(v1);
    if (!w) {
        rb_raise(rb_eNoMemError,"fail to allocate struct");
    }
    return TypedData_Wrap_Struct(cDiscrete, &ran_discrete_data_type, (void*)w);
}

Instance Method Details

#pdf(g) ⇒ DFloat

Returns the probability P[k] of observing the variable k. Since P[k] is not stored as part of the lookup table, it must be recomputed; this computation takes O(K), so if K is large and you care about the original array P[k] used to create the lookup table, then you should just keep this original array P[k] around.

Parameters:

  • g (UInt)

Returns:

  • (DFloat)

    result



127
128
129
130
131
132
133
134
135
136
137
138
# File 'ext/numo/gsl/ran/gsl_ran.c', line 127

static VALUE
ran_discrete_pdf(VALUE self, VALUE v1)
{
    gsl_ran_discrete_t *w;
    ndfunc_arg_in_t ain[1] = {{cSZ,0}};
    ndfunc_arg_out_t aout[1] = {{cDF,0}};
    ndfunc_t ndf = {iter_ran_discrete_pdf, STRIDE_LOOP|NDF_EXTRACT, 1,1, ain,aout};

    TypedData_Get_Struct(self, gsl_ran_discrete_t, &ran_discrete_data_type, w);

    return na_ndloop3(&ndf, w, 1, v1);
}