gsl_matrix_int* nm_to_gm_int(VALUE nm) { DENSE_STORAGE* s = NM_DENSE_STORAGE(nm); gsl_matrix_int* m = gsl_matrix_int_alloc( s->shape[0], s->shape[1] ); if (s->dtype != NM_INT64) { rb_raise(nm_eDataTypeError, "requires dtype of :int64 to convert from a GSL int vector"); } memcpy(m->data, s->elements, s->count); return m; }
static VALUE rb_gsl_vector_int_mul(VALUE obj, VALUE b) { VALUE argv[2]; gsl_vector_int *v, *vnew, *v2; gsl_matrix_int *m; int val; size_t i, j; switch (TYPE(b)) { case T_FIXNUM: case T_FLOAT: return rb_gsl_vector_int_scale(obj, b); break; default: if (VECTOR_INT_ROW_P(obj) && VECTOR_INT_COL_P(b)) { argv[0] = obj; argv[1] = b; return rb_gsl_vector_int_inner_product(2, argv, CLASS_OF(obj)); } else if (VECTOR_INT_ROW_P(obj) && MATRIX_INT_P(b)) { Data_Get_Struct(obj, gsl_vector_int, v); Data_Get_Struct(b, gsl_matrix_int, m); vnew = mygsl_vector_int_mul_matrix(v, m); return Data_Wrap_Struct(cgsl_vector_int, 0, gsl_vector_int_free, vnew); } else if (VECTOR_INT_COL_P(obj) && VECTOR_INT_ROW_P(b)) { Data_Get_Struct(obj, gsl_vector_int, v); Data_Get_Struct(b, gsl_vector_int, v2); if (v->size != v2->size) rb_raise(rb_eIndexError, "Vector sizes does not match."); m = gsl_matrix_int_alloc(v->size, v2->size); for (i = 0; i < v->size; i++) { for (j = 0; j < v2->size; j++) { val = gsl_vector_int_get(v, i)*gsl_vector_int_get(v2, j); gsl_matrix_int_set(m, i, j, val); } } return Data_Wrap_Struct(cgsl_matrix_int, 0, gsl_matrix_int_free, m); } else { return rb_gsl_vector_mul(rb_gsl_vector_int_to_f(obj), b); } break; } }
/** * The function creates and loads the segmentation image * of a flux_cube file into a gsl_matrix_int structure. * * @param fcube_file - the file name of the fluxcube * * @return ret - pointer to the gsl_matrix_int structure */ gsl_matrix_int * load_segmentation(const char fcube_file[]) { gsl_matrix_int *segmentation; gsl_matrix *dvals; int i,j; double diff; // load the segmentation image into a gsl_matrix_(double) dvals = FITSimage_to_gsl(fcube_file, 2, 1); // add 0.5 to get proper rounding with (int) gsl_matrix_add_constant (dvals, 0.5); // allocate space for the integer matrix segmentation = gsl_matrix_int_alloc(dvals->size1,dvals->size2); // fill the integer matrix with values from the double matrix for (i=0; i < dvals->size1; i++) { for (j=0; j < dvals->size2; j++) { gsl_matrix_int_set(segmentation, i, j, (int)gsl_matrix_get(dvals, i, j)); diff = gsl_matrix_get(dvals, i, j) - (double)gsl_matrix_int_get(segmentation, i, j); if (diff > 0.6 || diff < 0.4) fprintf(stdout, "diff %f; %f --> %i, (%i, %i)\n", diff, gsl_matrix_get(dvals, i, j), gsl_matrix_int_get(segmentation, i, j), i, j); } } // free the space for the double matrix gsl_matrix_free(dvals); return segmentation; }