Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Two dimensional input/output arrays */
- %define TYPEMAP_SGMATRIX(SGTYPE, R2SG, SG2R)
- %typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) shogun::SGMatrix<SGTYPE>
- {
- $1 = (
- ($input && TYPE($input) == T_ARRAY && RARRAY_LEN($input) > 0 && TYPE(rb_ary_entry($input, 0)) == T_ARRAY) ||
- ($input && NM_IsNMatrix($input) && NM_SHAPE1($input) > 0 && NM_SHAPE0($input) > 0)
- ) ? 1 : 0;
- }
- %typemap(in) shogun::SGMatrix<SGTYPE> {
- int32_t i, j, rows, cols;
- SGTYPE *array;
- if (rb_obj_is_kind_of($input,rb_cArray)) {
- VALUE v = $input;
- VALUE vec;
- rows = RARRAY_LEN(v);
- cols = 0;
- for (i = 0; i < rows; i++) {
- vec = rb_ary_entry(v, i);
- if (!rb_obj_is_kind_of(vec,rb_cArray)) {
- rb_raise(rb_eArgError, "Expected Arrays");
- }
- if (cols == 0) {
- cols = RARRAY_LEN(vec);
- array = SG_MALLOC(SGTYPE, rows * cols);
- }
- // check that the array is not "jagged" ?
- for (j = 0; j < cols; j++) {
- array[i * cols + j] = R2SG(rb_ary_entry(vec, j));
- }
- }
- }
- else if(NM_IsNMatrix($input)) {
- rows = NM_SHAPE0($input);
- cols = NM_SHAPE1($input);
- int32_t dtype = NM_DTYPE($input);
- DENSE_STORAGE* s = (DENSE_STORAGE*)NM_STORAGE($input);
- array = SG_MALLOC(SGTYPE, rows * cols);
- for (i = 0; i < rows; i++) {
- for (j = 0; j < cols; j++) {
- switch(dtype) {
- case 1:
- array[i * cols + j] = ((int8_t*)s->elements)[i * cols + j];
- break;
- case 2:
- array[i * cols + j] = ((int16_t*)s->elements)[i * cols + j];
- break;
- case 3:
- array[i * cols + j] = ((int32_t*)s->elements)[i * cols + j];
- break;
- case 4:
- array[i * cols + j] = ((int64_t*)s->elements)[i * cols + j];
- break;
- case 5:
- array[i * cols + j] = ((float32_t*)s->elements)[i * cols + j];
- break;
- case 6:
- array[i * cols + j] = ((float64_t*)s->elements)[i * cols + j];
- break;
- }
- }
- }
- }
- else {
- rb_raise(rb_eArgError, "Expected Arrays");
- }
- $1 = shogun::SGMatrix<SGTYPE>((SGTYPE*)array, rows, cols, true);
- }
- %typemap(out) shogun::SGMatrix<SGTYPE> {
- int32_t rows = $1.num_rows;
- int32_t cols = $1.num_cols;
- int32_t len = rows * cols;
- VALUE arr;
- int32_t i, j;
- arr = rb_ary_new2(rows);
- for (i = 0; i < rows; i++) {
- VALUE vec = rb_ary_new2(cols);
- for (j = 0; j < cols; j++) {
- rb_ary_push(vec, SG2R($1.matrix[i * cols + j]));
- }
- rb_ary_push(arr, vec);
- }
- $result = arr;
- }
- %enddef
- /* Define concrete examples of the TYPEMAP_SGMATRIX macros */
- TYPEMAP_SGMATRIX(char, NUM2CHR, CHR2FIX)
- TYPEMAP_SGMATRIX(uint16_t, NUM2INT, INT2NUM)
- TYPEMAP_SGMATRIX(int32_t, NUM2INT, INT2NUM)
- TYPEMAP_SGMATRIX(uint32_t, NUM2UINT, UINT2NUM)
- TYPEMAP_SGMATRIX(int64_t, NUM2LONG, LONG2NUM)
- TYPEMAP_SGMATRIX(uint64_t, NUM2ULONG, ULONG2NUM)
- TYPEMAP_SGMATRIX(long long, NUM2LL, LL2NUM)
- TYPEMAP_SGMATRIX(float32_t, NUM2DBL, rb_float_new)
- TYPEMAP_SGMATRIX(float64_t, NUM2DBL, rb_float_new)
- #undef TYPEMAP_SGMATRIX
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement