Exemple #1
0
/*
 *  call-seq:
 *     narray.expand_dims(dim) => narray view
 *
 *  Expand the shape of an array. Insert a new axis with size=1
 *  at a given dimension.
 *  @param [Integer] dim  dimension at which new axis is inserted.
 *  @return [Numo::NArray]  result narray view.
 */
VALUE
na_expand_dims(VALUE self, VALUE vdim)
{
    int  i, j, nd, dim;
    size_t *shape, *na_shape;
    stridx_t *stridx, *na_stridx;
    narray_t *na;
    narray_view_t *na2;
    VALUE view;

    GetNArray(self,na);
    nd = na->ndim;

    dim = NUM2INT(vdim);
    if (dim < -nd-1 || dim > nd) {
        rb_raise(nary_eDimensionError,"invalid axis (%d for %dD NArray)",
                 dim,nd);
    }
    if (dim < 0) {
        dim += nd+1;
    }

    view = na_make_view(self);
    GetNArrayView(view, na2);

    shape = ALLOC_N(size_t,nd+1);
    stridx = ALLOC_N(stridx_t,nd+1);
    na_shape = na2->base.shape;
    na_stridx = na2->stridx;

    for (i=j=0; i<=nd; i++) {
        if (i==dim) {
            shape[i] = 1;
            SDX_SET_STRIDE(stridx[i],0);
        } else {
            shape[i] = na_shape[j];
            stridx[i] = na_stridx[j];
            j++;
        }
    }

    na2->stridx = stridx;
    xfree(na_stridx);
    na2->base.shape = shape;
    xfree(na_shape);
    na2->base.ndim++;
    return view;
}
Exemple #2
0
static VALUE
nst_create_member_views(VALUE self)
{
    VALUE defs, def, types, type, elmt;
    long  i, len;
    narray_view_t *ne;

    defs = nst_definitions(CLASS_OF(self));
    len = RARRAY_LEN(defs);
    types = rb_ary_new2(len);
    //ofsts = rb_ary_new2(len);
    for (i=0; i<len; i++) {
        def  = RARRAY_AREF(defs,i);
        type = RARRAY_AREF(def,1);
        //ofst = RARRAY_AREF(def,2);
        elmt = na_make_view(type);
        rb_ary_push(types, elmt);
        //rb_ary_push(ofsts, ofst);
        GetNArrayView(elmt,ne);
        ne->data = na_original_data(self);
    }
    return rb_assoc_new(types,defs);
}