Advertisement
Guest User

Untitled

a guest
Aug 30th, 2013
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. /* Two dimensional input/output arrays */
  2. %define TYPEMAP_SGMATRIX(SGTYPE, R2SG, SG2R)
  3.  
  4. %typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) shogun::SGMatrix<SGTYPE>
  5. {
  6. $1 = (
  7. ($input && TYPE($input) == T_ARRAY && RARRAY_LEN($input) > 0 && TYPE(rb_ary_entry($input, 0)) == T_ARRAY) ||
  8. ($input && NM_IsNMatrix($input) && NM_SHAPE1($input) > 0 && NM_SHAPE0($input) > 0)
  9. ) ? 1 : 0;
  10. }
  11.  
  12. %typemap(in) shogun::SGMatrix<SGTYPE> {
  13.  
  14. int32_t i, j, rows, cols;
  15. SGTYPE *array;
  16.  
  17. if (rb_obj_is_kind_of($input,rb_cArray)) {
  18. VALUE v = $input;
  19. VALUE vec;
  20.  
  21. rows = RARRAY_LEN(v);
  22. cols = 0;
  23.  
  24. for (i = 0; i < rows; i++) {
  25. vec = rb_ary_entry(v, i);
  26. if (!rb_obj_is_kind_of(vec,rb_cArray)) {
  27. rb_raise(rb_eArgError, "Expected Arrays");
  28. }
  29. if (cols == 0) {
  30. cols = RARRAY_LEN(vec);
  31. array = SG_MALLOC(SGTYPE, rows * cols);
  32. }
  33. // check that the array is not "jagged" ?
  34. for (j = 0; j < cols; j++) {
  35. array[i * cols + j] = R2SG(rb_ary_entry(vec, j));
  36. }
  37. }
  38. }
  39. else if(NM_IsNMatrix($input)) {
  40. rows = NM_SHAPE0($input);
  41. cols = NM_SHAPE1($input);
  42. int32_t dtype = NM_DTYPE($input);
  43. DENSE_STORAGE* s = (DENSE_STORAGE*)NM_STORAGE($input);
  44.  
  45. array = SG_MALLOC(SGTYPE, rows * cols);
  46.  
  47. for (i = 0; i < rows; i++) {
  48. for (j = 0; j < cols; j++) {
  49.  
  50. switch(dtype) {
  51. case 1:
  52. array[i * cols + j] = ((int8_t*)s->elements)[i * cols + j];
  53. break;
  54. case 2:
  55. array[i * cols + j] = ((int16_t*)s->elements)[i * cols + j];
  56. break;
  57. case 3:
  58. array[i * cols + j] = ((int32_t*)s->elements)[i * cols + j];
  59. break;
  60. case 4:
  61. array[i * cols + j] = ((int64_t*)s->elements)[i * cols + j];
  62. break;
  63. case 5:
  64. array[i * cols + j] = ((float32_t*)s->elements)[i * cols + j];
  65. break;
  66. case 6:
  67. array[i * cols + j] = ((float64_t*)s->elements)[i * cols + j];
  68. break;
  69. }
  70. }
  71. }
  72. }
  73. else {
  74. rb_raise(rb_eArgError, "Expected Arrays");
  75. }
  76. $1 = shogun::SGMatrix<SGTYPE>((SGTYPE*)array, rows, cols, true);
  77. }
  78.  
  79. %typemap(out) shogun::SGMatrix<SGTYPE> {
  80. int32_t rows = $1.num_rows;
  81. int32_t cols = $1.num_cols;
  82. int32_t len = rows * cols;
  83. VALUE arr;
  84. int32_t i, j;
  85.  
  86. arr = rb_ary_new2(rows);
  87.  
  88. for (i = 0; i < rows; i++) {
  89. VALUE vec = rb_ary_new2(cols);
  90. for (j = 0; j < cols; j++) {
  91. rb_ary_push(vec, SG2R($1.matrix[i * cols + j]));
  92. }
  93. rb_ary_push(arr, vec);
  94. }
  95.  
  96. $result = arr;
  97. }
  98.  
  99. %enddef
  100.  
  101. /* Define concrete examples of the TYPEMAP_SGMATRIX macros */
  102. TYPEMAP_SGMATRIX(char, NUM2CHR, CHR2FIX)
  103. TYPEMAP_SGMATRIX(uint16_t, NUM2INT, INT2NUM)
  104. TYPEMAP_SGMATRIX(int32_t, NUM2INT, INT2NUM)
  105. TYPEMAP_SGMATRIX(uint32_t, NUM2UINT, UINT2NUM)
  106. TYPEMAP_SGMATRIX(int64_t, NUM2LONG, LONG2NUM)
  107. TYPEMAP_SGMATRIX(uint64_t, NUM2ULONG, ULONG2NUM)
  108. TYPEMAP_SGMATRIX(long long, NUM2LL, LL2NUM)
  109. TYPEMAP_SGMATRIX(float32_t, NUM2DBL, rb_float_new)
  110. TYPEMAP_SGMATRIX(float64_t, NUM2DBL, rb_float_new)
  111.  
  112. #undef TYPEMAP_SGMATRIX
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement