numo-narray 0.9.0.1-x86-mingw32 → 0.9.0.3-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +11 -7
  3. data/Rakefile +21 -6
  4. data/ext/numo/narray/array.c +3 -3
  5. data/ext/numo/narray/data.c +102 -81
  6. data/ext/numo/narray/depend.erb +9 -7
  7. data/ext/numo/narray/extconf.rb +5 -24
  8. data/ext/numo/narray/gen/cogen.rb +19 -4
  9. data/ext/numo/narray/gen/def/bit.rb +31 -0
  10. data/ext/numo/narray/gen/def/robject.rb +1 -1
  11. data/ext/numo/narray/gen/dtype.erb.c +11 -257
  12. data/ext/numo/narray/gen/spec.rb +294 -0
  13. data/ext/numo/narray/gen/tmpl/aset.c +1 -1
  14. data/ext/numo/narray/gen/tmpl/each_with_index.c +1 -1
  15. data/ext/numo/narray/gen/tmpl/eye.c +5 -5
  16. data/ext/numo/narray/gen/tmpl/logseq.c +82 -0
  17. data/ext/numo/narray/gen/tmpl/map_with_index.c +1 -1
  18. data/ext/numo/narray/gen/tmpl/rand.c +126 -5
  19. data/ext/numo/narray/gen/tmpl/seq.c +43 -12
  20. data/ext/numo/narray/gen/tmpl/store_bit.c +55 -0
  21. data/ext/numo/narray/gen/tmpl_bit/allocate.c +28 -0
  22. data/ext/numo/narray/gen/tmpl_bit/aref.c +53 -0
  23. data/ext/numo/narray/gen/tmpl_bit/aset.c +63 -0
  24. data/ext/numo/narray/gen/{tmpl/bit_binary.c → tmpl_bit/binary.c} +1 -1
  25. data/ext/numo/narray/gen/{tmpl → tmpl_bit}/bit_count.c +5 -3
  26. data/ext/numo/narray/gen/tmpl_bit/bit_reduce.c +128 -0
  27. data/ext/numo/narray/gen/tmpl_bit/cast.c +37 -0
  28. data/ext/numo/narray/gen/tmpl_bit/cast_array.c +80 -0
  29. data/ext/numo/narray/gen/tmpl_bit/cast_numeric.c +22 -0
  30. data/ext/numo/narray/gen/tmpl_bit/coerce_cast.c +8 -0
  31. data/ext/numo/narray/gen/tmpl_bit/each.c +44 -0
  32. data/ext/numo/narray/gen/tmpl_bit/each_with_index.c +66 -0
  33. data/ext/numo/narray/gen/tmpl_bit/extract.c +25 -0
  34. data/ext/numo/narray/gen/tmpl_bit/fill.c +65 -0
  35. data/ext/numo/narray/gen/tmpl_bit/format.c +61 -0
  36. data/ext/numo/narray/gen/tmpl_bit/format_to_a.c +48 -0
  37. data/ext/numo/narray/gen/tmpl_bit/inspect.c +18 -0
  38. data/ext/numo/narray/gen/tmpl_bit/map_with_index.c +94 -0
  39. data/ext/numo/narray/gen/tmpl_bit/mask.c +117 -0
  40. data/ext/numo/narray/gen/tmpl_bit/none_p.c +14 -0
  41. data/ext/numo/narray/gen/tmpl_bit/store.c +32 -0
  42. data/ext/numo/narray/gen/tmpl_bit/store_array.c +5 -0
  43. data/ext/numo/narray/gen/tmpl_bit/store_bit.c +66 -0
  44. data/ext/numo/narray/gen/tmpl_bit/store_from.c +56 -0
  45. data/ext/numo/narray/gen/tmpl_bit/store_numeric.c +22 -0
  46. data/ext/numo/narray/gen/tmpl_bit/to_a.c +43 -0
  47. data/ext/numo/narray/gen/{tmpl/bit_unary.c → tmpl_bit/unary.c} +1 -1
  48. data/ext/numo/narray/gen/tmpl_bit/where.c +102 -0
  49. data/ext/numo/narray/gen/tmpl_bit/where2.c +37 -0
  50. data/ext/numo/narray/index.c +9 -7
  51. data/ext/numo/narray/math.c +6 -1
  52. data/ext/numo/narray/narray.c +307 -103
  53. data/ext/numo/narray/ndloop.c +21 -36
  54. data/ext/numo/narray/numo/intern.h +3 -3
  55. data/ext/numo/narray/numo/narray.h +5 -41
  56. data/ext/numo/narray/numo/template.h +2 -2
  57. data/ext/numo/narray/numo/types/bit.h +27 -13
  58. data/ext/numo/narray/numo/types/complex.h +2 -2
  59. data/ext/numo/narray/numo/types/complex_macro.h +19 -7
  60. data/ext/numo/narray/numo/types/dcomplex.h +9 -0
  61. data/ext/numo/narray/numo/types/dfloat.h +6 -0
  62. data/ext/numo/narray/numo/types/float_macro.h +9 -2
  63. data/ext/numo/narray/numo/types/int16.h +0 -1
  64. data/ext/numo/narray/numo/types/int32.h +0 -1
  65. data/ext/numo/narray/numo/types/int64.h +0 -1
  66. data/ext/numo/narray/numo/types/int8.h +0 -1
  67. data/ext/numo/narray/numo/types/int_macro.h +2 -1
  68. data/ext/numo/narray/numo/types/robj_macro.h +9 -2
  69. data/ext/numo/narray/numo/types/robject.h +6 -0
  70. data/ext/numo/narray/numo/types/scomplex.h +9 -0
  71. data/ext/numo/narray/numo/types/sfloat.h +6 -0
  72. data/ext/numo/narray/numo/types/uint16.h +0 -1
  73. data/ext/numo/narray/numo/types/uint32.h +0 -1
  74. data/ext/numo/narray/numo/types/uint64.h +0 -1
  75. data/ext/numo/narray/numo/types/uint8.h +0 -1
  76. data/ext/numo/narray/numo/types/uint_macro.h +2 -1
  77. data/ext/numo/narray/numo/types/xint_macro.h +6 -1
  78. data/ext/numo/narray/rand.c +2 -2
  79. data/ext/numo/narray/step.c +7 -7
  80. data/ext/numo/narray/struct.c +11 -12
  81. data/lib/2.0/numo/narray.so +0 -0
  82. data/lib/2.1/numo/narray.so +0 -0
  83. data/lib/2.2/numo/narray.so +0 -0
  84. data/lib/2.3/numo/narray.so +0 -0
  85. data/lib/2.4/numo/narray.so +0 -0
  86. data/lib/erbpp.rb +6 -2
  87. data/lib/erbpp/line_number.rb +1 -1
  88. data/lib/erbpp/narray_def.rb +23 -19
  89. data/numo-narray.gemspec +2 -2
  90. metadata +45 -10
  91. data/ext/numo/narray/gen/bit.erb.c +0 -811
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  math.c
3
3
  Numerical Array Extension for Ruby
4
- (C) Copyright 1999-2011 by Masahiro TANAKA
4
+ (C) Copyright 1999-2016 by Masahiro TANAKA
5
5
 
6
6
  This program is free software.
7
7
  You can distribute/modify this program
@@ -13,6 +13,7 @@
13
13
 
14
14
  VALUE numo_mNMath;
15
15
  EXTERN VALUE numo_mDFloatMath, numo_mDComplexMath;
16
+ EXTERN VALUE numo_mSFloatMath, numo_mSComplexMath;
16
17
  static ID id_send;
17
18
 
18
19
  VALUE
@@ -130,8 +131,12 @@ Init_nary_math()
130
131
  rb_hash_aset(hCast, numo_cDFloat, numo_mDFloatMath);
131
132
  rb_hash_aset(hCast, numo_cDFloat, numo_mDFloatMath);
132
133
  rb_hash_aset(hCast, numo_cDComplex, numo_mDComplexMath);
134
+ rb_hash_aset(hCast, numo_cSFloat, numo_mSFloatMath);
135
+ rb_hash_aset(hCast, numo_cSComplex, numo_mSComplexMath);
136
+ #ifdef HAVE_RB_CFIXNUM
133
137
  rb_hash_aset(hCast, rb_cFixnum, rb_mMath);
134
138
  rb_hash_aset(hCast, rb_cBignum, rb_mMath);
139
+ #endif
135
140
  rb_hash_aset(hCast, rb_cInteger, rb_mMath);
136
141
  rb_hash_aset(hCast, rb_cFloat, rb_mMath);
137
142
  rb_hash_aset(hCast, rb_cComplex, numo_mDComplexMath);
@@ -11,7 +11,6 @@
11
11
  #define NARRAY_C
12
12
  #include <ruby.h>
13
13
  #include "numo/narray.h"
14
- #include "extconf.h"
15
14
  #include <assert.h>
16
15
 
17
16
  /* global variables within this module */
@@ -40,6 +39,9 @@ VALUE na_cStep;
40
39
  VALUE rb_cComplex;
41
40
  #endif
42
41
 
42
+ int numo_na_inspect_rows=20;
43
+ int numo_na_inspect_cols=80;
44
+
43
45
  void Init_nary_data();
44
46
  void Init_nary_step();
45
47
  void Init_nary_index();
@@ -116,7 +118,7 @@ rb_narray_debug_info(VALUE self)
116
118
  GetNArray(self,na);
117
119
 
118
120
  printf("%s:\n",rb_class2name(CLASS_OF(self)));
119
- printf(" id = 0x%"SZF"x\n", self);
121
+ printf(" id = 0x%"PRI_VALUE_PREFIX"x\n", self);
120
122
  printf(" type = %d\n", na->type);
121
123
  printf(" flag = [%d,%d]\n", na->flag[0], na->flag[1]);
122
124
  printf(" size = %"SZF"d\n", na->size);
@@ -226,6 +228,7 @@ void
226
228
  na_array_to_internal_shape(VALUE self, VALUE ary, size_t *shape)
227
229
  {
228
230
  size_t i, n, c, s;
231
+ ssize_t x;
229
232
  VALUE v;
230
233
  narray_t *na;
231
234
  int flag = 0;
@@ -245,13 +248,11 @@ na_array_to_internal_shape(VALUE self, VALUE ary, size_t *shape)
245
248
  }
246
249
  for (i=0; i<n; i++) {
247
250
  v = RARRAY_AREF(ary,i);
248
- if (!FIXNUM_P(v) && !rb_obj_is_kind_of(v, rb_cInteger)) {
249
- rb_raise(rb_eTypeError, "array size must be Integer");
250
- }
251
- if (RTEST(rb_funcall(v, rb_intern("<"), 1, INT2FIX(0)))) {
251
+ x = NUM2SSIZET(v);
252
+ if (x < 0) {
252
253
  rb_raise(rb_eArgError,"size must be non-negative");
253
254
  }
254
- shape[c] = NUM2SIZE(v);
255
+ shape[c] = x;
255
256
  c += s;
256
257
  }
257
258
  }
@@ -334,13 +335,13 @@ na_initialize(VALUE self, VALUE args)
334
335
  } else {
335
336
  v = args;
336
337
  }
337
- ndim = RARRAY_LEN(v);
338
- if (ndim > NA_MAX_DIMENSION) {
339
- rb_raise(rb_eArgError,"ndim=%d exceeds maximum dimension",ndim);
340
- }
341
- shape = ALLOCA_N(size_t, ndim);
342
- // setup size_t shape[] from VALUE shape argument
343
- na_array_to_internal_shape(self, v, shape);
338
+ ndim = RARRAY_LEN(v);
339
+ if (ndim > NA_MAX_DIMENSION) {
340
+ rb_raise(rb_eArgError,"ndim=%d exceeds maximum dimension",ndim);
341
+ }
342
+ shape = ALLOCA_N(size_t, ndim);
343
+ // setup size_t shape[] from VALUE shape argument
344
+ na_array_to_internal_shape(self, v, shape);
344
345
  na_setup(self, ndim, shape);
345
346
 
346
347
  return self;
@@ -405,7 +406,7 @@ na_initialize_copy(VALUE self, VALUE orig)
405
406
  * [0, 0, 0, 0, 0]]
406
407
  */
407
408
  static VALUE
408
- na_s_zeros(int argc, const VALUE *argv, VALUE klass)
409
+ na_s_zeros(int argc, VALUE *argv, VALUE klass)
409
410
  {
410
411
  VALUE obj;
411
412
  obj = rb_class_new_instance(argc, argv, klass);
@@ -429,7 +430,7 @@ na_s_zeros(int argc, const VALUE *argv, VALUE klass)
429
430
  * [1, 1, 1, 1, 1]]
430
431
  */
431
432
  static VALUE
432
- na_s_ones(int argc, const VALUE *argv, VALUE klass)
433
+ na_s_ones(int argc, VALUE *argv, VALUE klass)
433
434
  {
434
435
  VALUE obj;
435
436
  obj = rb_class_new_instance(argc, argv, klass);
@@ -437,6 +438,89 @@ na_s_ones(int argc, const VALUE *argv, VALUE klass)
437
438
  }
438
439
 
439
440
 
441
+ /*
442
+ Returns an array of N linearly spaced points between x1 and x2.
443
+ This singleton method is valid not for NArray class itself
444
+ but for typed NArray subclasses, e.g., DFloat, Int64.
445
+
446
+ @overload linspace(x1, x2, [n])
447
+ @param [Numeric] x1 The start value
448
+ @param [Numeric] x2 The end value
449
+ @param [Integer] n The number of elements. (default is 100).
450
+ @return [Numo::NArray] result array.
451
+
452
+ @example
453
+ a = Numo::DFloat.linspace(-5,5,7)
454
+ => Numo::DFloat#shape=[7]
455
+ [-5, -3.33333, -1.66667, 0, 1.66667, 3.33333, 5]
456
+ */
457
+ static VALUE
458
+ na_s_linspace(int argc, VALUE *argv, VALUE klass)
459
+ {
460
+ VALUE obj, vx1, vx2, vstep, vsize;
461
+ double n;
462
+ int narg;
463
+
464
+ narg = rb_scan_args(argc,argv,"21",&vx1,&vx2,&vsize);
465
+ if (narg==3) {
466
+ n = NUM2DBL(vsize);
467
+ } else {
468
+ n = 100;
469
+ vsize = INT2FIX(100);
470
+ }
471
+
472
+ obj = rb_funcall(vx2, '-', 1, vx1);
473
+ vstep = rb_funcall(obj, '/', 1, DBL2NUM(n-1));
474
+
475
+ obj = rb_class_new_instance(1, &vsize, klass);
476
+ return rb_funcall(obj, rb_intern("seq"), 2, vx1, vstep);
477
+ }
478
+
479
+ /*
480
+ Returns an array of N logarithmically spaced points between 10^a and 10^b.
481
+ This singleton method is valid not for NArray having +logseq+ method,
482
+ i.e., DFloat, SFloat, DComplex, and SComplex.
483
+
484
+ @overload logspace(a, b, [n, base])
485
+ @param [Numeric] a The start value
486
+ @param [Numeric] b The end value
487
+ @param [Integer] n The number of elements. (default is 50)
488
+ @param [Numeric] base The base of log space. (default is 10)
489
+ @return [Numo::NArray] result array.
490
+
491
+ @example
492
+ Numo::DFloat.logspace(4,0,5,2)
493
+ => Numo::DFloat#shape=[5]
494
+ [16, 8, 4, 2, 1]
495
+ Numo::DComplex.logspace(0,1i*Math::PI,5,Math::E)
496
+ => Numo::DComplex#shape=[5]
497
+ [1+4.44659e-323i, 0.707107+0.707107i, 6.12323e-17+1i, -0.707107+0.707107i, ...]
498
+ */
499
+ static VALUE
500
+ na_s_logspace(int argc, VALUE *argv, VALUE klass)
501
+ {
502
+ VALUE obj, vx1, vx2, vstep, vsize, vbase;
503
+ double n;
504
+
505
+ rb_scan_args(argc,argv,"22",&vx1,&vx2,&vsize,&vbase);
506
+ if (vsize == Qnil) {
507
+ vsize = INT2FIX(50);
508
+ n = 50;
509
+ } else {
510
+ n = NUM2DBL(vsize);
511
+ }
512
+ if (vbase == Qnil) {
513
+ vbase = DBL2NUM(10);
514
+ }
515
+
516
+ obj = rb_funcall(vx2, '-', 1, vx1);
517
+ vstep = rb_funcall(obj, '/', 1, DBL2NUM(n-1));
518
+
519
+ obj = rb_class_new_instance(1, &vsize, klass);
520
+ return rb_funcall(obj, rb_intern("logseq"), 3, vx1, vstep, vbase);
521
+ }
522
+
523
+
440
524
  /*
441
525
  Returns a NArray with shape=(n,n) whose diagonal elements are 1, otherwise 0.
442
526
  @overload eye(n)
@@ -450,7 +534,7 @@ na_s_ones(int argc, const VALUE *argv, VALUE klass)
450
534
  [0, 0, 1]]
451
535
  */
452
536
  static VALUE
453
- na_s_eye(int argc, const VALUE *argv, VALUE klass)
537
+ na_s_eye(int argc, VALUE *argv, VALUE klass)
454
538
  {
455
539
  VALUE obj;
456
540
  VALUE tmp[2];
@@ -468,82 +552,89 @@ na_s_eye(int argc, const VALUE *argv, VALUE klass)
468
552
  }
469
553
 
470
554
 
471
- char *
472
- na_get_pointer(VALUE self)
473
- {
474
- narray_t *na;
475
- GetNArray(self,na);
476
555
 
477
- switch(NA_TYPE(na)) {
478
- case NARRAY_DATA_T:
479
- return NA_DATA_PTR(na);
480
- case NARRAY_FILEMAP_T:
481
- return ((narray_filemap_t*)na)->ptr;
482
- case NARRAY_VIEW_T:
483
- return na_get_pointer(NA_VIEW_DATA(na));
484
- //puts("pass NARRAY_VIEW_T in na_get_pointer_for_write");
485
- //ptr += ((narray_view_t*)na)->offset;
486
- //ptr += NA_VIEW_OFFSET(na);
487
- break;
488
- default:
489
- rb_raise(rb_eRuntimeError,"invalid NA_TYPE");
490
- }
491
- return NULL;
492
- }
556
+ #define READ 1
557
+ #define WRITE 2
493
558
 
494
- char *
495
- na_get_pointer_for_write(VALUE self)
559
+ static char *
560
+ na_get_pointer_for_rw(VALUE self, int flag)
496
561
  {
497
562
  char *ptr;
563
+ VALUE obj;
498
564
  narray_t *na;
499
- GetNArray(self,na);
500
565
 
501
- if (OBJ_FROZEN(self)) {
566
+ if ((flag & WRITE) && OBJ_FROZEN(self)) {
502
567
  rb_raise(rb_eRuntimeError, "cannot write to frozen NArray.");
503
568
  }
504
569
 
505
- if (NA_TYPE(na) == NARRAY_DATA_T) {
570
+ GetNArray(self,na);
571
+
572
+ switch(NA_TYPE(na)) {
573
+ case NARRAY_DATA_T:
506
574
  ptr = NA_DATA_PTR(na);
507
- if (na->size > 0 && ptr == NULL) {
508
- rb_funcall(self, id_allocate, 0);
509
- ptr = NA_DATA_PTR(na);
510
- }
511
- } else {
512
- ptr = na_get_pointer(self);
513
575
  if (NA_SIZE(na) > 0 && ptr == NULL) {
514
- rb_raise(rb_eRuntimeError,"cannot write to unallocated NArray");
576
+ if (flag & READ) {
577
+ rb_raise(rb_eRuntimeError,"cannot read unallocated NArray");
578
+ }
579
+ if (flag & WRITE) {
580
+ rb_funcall(self, id_allocate, 0);
581
+ ptr = NA_DATA_PTR(na);
582
+ }
583
+ }
584
+ return ptr;
585
+ case NARRAY_VIEW_T:
586
+ obj = NA_VIEW_DATA(na);
587
+ if ((flag & WRITE) && OBJ_FROZEN(obj)) {
588
+ rb_raise(rb_eRuntimeError, "cannot write to frozen NArray.");
515
589
  }
516
- }
517
590
 
518
- //NA_SET_LOCK(na);
591
+ if (flag & WRITE) {
592
+ if (OBJ_FROZEN(obj)) {
593
+ rb_raise(rb_eRuntimeError, "cannot write to frozen NArray.");
594
+ }
595
+ }
596
+ GetNArray(obj,na);
597
+ switch(NA_TYPE(na)) {
598
+ case NARRAY_DATA_T:
599
+ ptr = NA_DATA_PTR(na);
600
+ if (flag & (READ|WRITE)) {
601
+ if (NA_SIZE(na) > 0 && ptr == NULL) {
602
+ rb_raise(rb_eRuntimeError,"cannot read/write unallocated NArray");
603
+ }
604
+ }
605
+ return ptr;
606
+ default:
607
+ rb_raise(rb_eRuntimeError,"invalid NA_TYPE of view: %d",NA_TYPE(na));
608
+ }
609
+ default:
610
+ rb_raise(rb_eRuntimeError,"invalid NA_TYPE: %d",NA_TYPE(na));
611
+ }
519
612
 
520
- return ptr;
613
+ return NULL;
521
614
  }
522
615
 
523
616
  char *
524
617
  na_get_pointer_for_read(VALUE self)
525
618
  {
526
- char *ptr;
527
- narray_t *na;
528
- GetNArray(self,na);
529
-
530
- //if (NA_TEST_LOCK(na)) {
531
- // rb_raise(rb_eRuntimeError, "cannot read locked NArray.");
532
- //}
533
-
534
- if (NA_TYPE(na) == NARRAY_DATA_T) {
535
- ptr = NA_DATA_PTR(na);
536
- } else {
537
- ptr = na_get_pointer(self);
538
- }
619
+ return na_get_pointer_for_rw(self, READ);
620
+ }
539
621
 
540
- if (NA_SIZE(na) > 0 && ptr == NULL) {
541
- rb_raise(rb_eRuntimeError,"cannot read unallocated NArray");
542
- }
622
+ char *
623
+ na_get_pointer_for_write(VALUE self)
624
+ {
625
+ return na_get_pointer_for_rw(self, WRITE);
626
+ }
543
627
 
544
- //NA_SET_LOCK(na);
628
+ char *
629
+ na_get_pointer_for_read_write(VALUE self)
630
+ {
631
+ return na_get_pointer_for_rw(self, READ|WRITE);
632
+ }
545
633
 
546
- return ptr;
634
+ char *
635
+ na_get_pointer(VALUE self)
636
+ {
637
+ return na_get_pointer_for_rw(self, 0);
547
638
  }
548
639
 
549
640
 
@@ -636,7 +727,7 @@ na_get_stride(VALUE v)
636
727
 
637
728
  if (!stridx) {
638
729
  stridx = ALLOC_N(stridx_t, na->ndim);
639
- st = NUM2SIZE(rb_const_get(CLASS_OF(v), id_contiguous_stride));
730
+ st = NUM2SIZET(rb_const_get(CLASS_OF(v), id_contiguous_stride));
640
731
  //printf("step_unit=%ld, CLASS_OF(v)=%lx\n",st, CLASS_OF(v));
641
732
  for (i=na->ndim; i>0;) {
642
733
  SDX_SET_STRIDE(stridx[--i],st);
@@ -655,7 +746,7 @@ na_size(VALUE self)
655
746
  {
656
747
  narray_t *na;
657
748
  GetNArray(self,na);
658
- return SIZE2NUM(na->size);
749
+ return SIZET2NUM(na->size);
659
750
  }
660
751
 
661
752
 
@@ -669,6 +760,22 @@ na_ndim(VALUE self)
669
760
  }
670
761
 
671
762
 
763
+ /*
764
+ Returns true if self.size == 0.
765
+ @overload empty?
766
+ */
767
+ static VALUE
768
+ na_empty_p(VALUE self)
769
+ {
770
+ narray_t *na;
771
+ GetNArray(self,na);
772
+ if (NA_SIZE(na)==0) {
773
+ return Qtrue;
774
+ }
775
+ return Qfalse;
776
+ }
777
+
778
+
672
779
  /* method: shape() -- returns shape, array of the size of dimensions */
673
780
  static VALUE
674
781
  na_shape(VALUE self)
@@ -688,7 +795,7 @@ static VALUE
688
795
  }
689
796
  v = rb_ary_new2(n);
690
797
  for (i=0; i<n; i++) {
691
- rb_ary_push(v, SIZE2NUM(na->shape[c]));
798
+ rb_ary_push(v, SIZET2NUM(na->shape[c]));
692
799
  c += s;
693
800
  }
694
801
  return v;
@@ -698,13 +805,13 @@ static VALUE
698
805
  size_t
699
806
  na_get_elmsz(VALUE vna)
700
807
  {
701
- return NUM2SIZE(rb_const_get(CLASS_OF(vna), id_contiguous_stride));
808
+ return NUM2SIZET(rb_const_get(CLASS_OF(vna), id_contiguous_stride));
702
809
  }
703
810
 
704
811
  size_t
705
812
  na_dtype_elmsz(VALUE klass)
706
813
  {
707
- return NUM2SIZE(rb_const_get(klass, id_contiguous_stride));
814
+ return NUM2SIZET(rb_const_get(klass, id_contiguous_stride));
708
815
  }
709
816
 
710
817
  size_t
@@ -890,7 +997,9 @@ na_expand_dims(VALUE self, VALUE vdim)
890
997
  na2->stridx = stridx;
891
998
  xfree(na_stridx);
892
999
  na2->base.shape = shape;
893
- xfree(na_shape);
1000
+ if (na_shape != &(na2->base.size)) {
1001
+ xfree(na_shape);
1002
+ }
894
1003
  na2->base.ndim++;
895
1004
  return view;
896
1005
  }
@@ -1035,14 +1144,15 @@ nary_coerce(VALUE x, VALUE y)
1035
1144
  static VALUE
1036
1145
  nary_byte_size(VALUE self)
1037
1146
  {
1038
- VALUE velmsz; //klass,
1147
+ VALUE velmsz;
1039
1148
  narray_t *na;
1040
- size_t sz;
1041
1149
 
1042
1150
  GetNArray(self,na);
1043
1151
  velmsz = rb_const_get(CLASS_OF(self), rb_intern(ELEMENT_BYTE_SIZE));
1044
- sz = SIZE2NUM(NUM2SIZE(velmsz) * na->size);
1045
- return sz;
1152
+ if (FIXNUM_P(velmsz)) {
1153
+ return SIZET2NUM(NUM2SIZET(velmsz) * na->size);
1154
+ }
1155
+ return SIZET2NUM(ceil(NUM2DBL(velmsz) * na->size));
1046
1156
  }
1047
1157
 
1048
1158
  /*
@@ -1066,41 +1176,65 @@ nary_s_byte_size(VALUE type)
1066
1176
  static VALUE
1067
1177
  nary_s_from_string(int argc, VALUE *argv, VALUE type)
1068
1178
  {
1069
- size_t len, str_len, elmsz;
1179
+ size_t len, str_len, byte_size;
1070
1180
  size_t *shape;
1071
1181
  char *ptr;
1072
- int i, nd, narg;
1073
- VALUE vstr,vshape,vna;
1182
+ int i, nd, narg;
1183
+ VALUE vstr, vshape, vna;
1184
+ VALUE velmsz;
1074
1185
 
1075
1186
  narg = rb_scan_args(argc,argv,"11",&vstr,&vshape);
1076
1187
  str_len = RSTRING_LEN(vstr);
1077
- elmsz = na_dtype_elmsz(type);
1188
+ velmsz = rb_const_get(type, rb_intern(ELEMENT_BYTE_SIZE));
1078
1189
  if (narg==2) {
1079
- nd = RARRAY_LEN(vshape);
1080
- if (nd == 0 || nd > NA_MAX_DIMENSION) {
1081
- rb_raise(nary_eDimensionError,"too long or empty shape (%d)", nd);
1190
+ switch(TYPE(vshape)) {
1191
+ case T_FIXNUM:
1192
+ nd = 1;
1193
+ len = NUM2SIZET(vshape);
1194
+ shape = &len;
1195
+ break;
1196
+ case T_ARRAY:
1197
+ nd = RARRAY_LEN(vshape);
1198
+ if (nd == 0 || nd > NA_MAX_DIMENSION) {
1199
+ rb_raise(nary_eDimensionError,"too long or empty shape (%d)", nd);
1200
+ }
1201
+ shape = ALLOCA_N(size_t,nd);
1202
+ len = 1;
1203
+ for (i=0; i<nd; ++i) {
1204
+ len *= shape[i] = NUM2SIZET(RARRAY_AREF(vshape,i));
1205
+ }
1206
+ break;
1207
+ default:
1208
+ rb_raise(rb_eArgError,"second argument must be size or shape");
1082
1209
  }
1083
- shape = ALLOCA_N(size_t,nd);
1084
- len = 1;
1085
- for (i=0; i<nd; ++i) {
1086
- len *= shape[i] = NUM2SIZE(RARRAY_AREF(vshape,i));
1210
+ if (FIXNUM_P(velmsz)) {
1211
+ byte_size = len * NUM2SIZET(velmsz);
1212
+ } else {
1213
+ byte_size = ceil(len * NUM2DBL(velmsz));
1087
1214
  }
1088
- if (len*elmsz != str_len) {
1089
- rb_raise(rb_eArgError, "size mismatch");
1215
+ if (byte_size > str_len) {
1216
+ rb_raise(rb_eArgError, "specified size is too large");
1090
1217
  }
1091
1218
  } else {
1092
1219
  nd = 1;
1093
- shape = ALLOCA_N(size_t,nd);
1094
- shape[0] = len = str_len / elmsz;
1220
+ if (FIXNUM_P(velmsz)) {
1221
+ len = str_len / NUM2SIZET(velmsz);
1222
+ byte_size = len * NUM2SIZET(velmsz);
1223
+ } else {
1224
+ len = floor(str_len / NUM2DBL(velmsz));
1225
+ byte_size = str_len;
1226
+ }
1095
1227
  if (len == 0) {
1096
1228
  rb_raise(rb_eArgError, "string is empty or too short");
1097
1229
  }
1230
+ shape = ALLOCA_N(size_t,nd);
1231
+ shape[0] = len;
1098
1232
  }
1099
1233
 
1100
1234
  vna = rb_narray_new(type, nd, shape);
1101
1235
  ptr = na_get_pointer_for_write(vna);
1102
1236
 
1103
- memcpy(ptr, RSTRING_PTR(vstr), elmsz*len);
1237
+ memcpy(ptr, RSTRING_PTR(vstr), byte_size);
1104
1238
 
1105
1239
  return vna;
1106
1240
  }
@@ -1113,17 +1247,16 @@ nary_s_from_string(int argc, VALUE *argv, VALUE type)
1113
1247
  static VALUE
1114
1248
  nary_to_string(VALUE self)
1115
1249
  {
1116
- size_t len, esz;
1250
+ size_t len;
1117
1251
  char *ptr;
1118
1252
  VALUE str;
1119
1253
  narray_t *na;
1120
1254
 
1121
1255
  GetNArray(self,na);
1122
1256
  if (na->type == NARRAY_VIEW_T) {
1123
- self = na_copy(self);
1257
+ self = rb_funcall(self,rb_intern("copy"),0);
1124
1258
  }
1125
- esz = na_get_elmsz(self);
1126
- len = na->size * esz;
1259
+ len = NUM2SIZET(nary_byte_size(self));
1127
1260
  ptr = na_get_pointer_for_read(self);
1128
1261
  str = rb_usascii_str_new(ptr,len);
1129
1262
  RB_GC_GUARD(self);
@@ -1236,14 +1369,14 @@ na_reduce_dimension(int argc, VALUE *argv, int naryc, VALUE *naryv)
1236
1369
  m |= ((size_t)1) << r;
1237
1370
  continue;
1238
1371
  } else {
1239
- reduce = SIZE2NUM(m);
1372
+ reduce = SIZET2NUM(m);
1240
1373
  }
1241
1374
  }
1242
1375
  v = rb_funcall( INT2FIX(1), rb_intern("<<"), 1, INT2FIX(r) );
1243
1376
  reduce = rb_funcall( reduce, rb_intern("|"), 1, v );
1244
1377
  }
1245
1378
  }
1246
- if (reduce==Qnil) reduce = SIZE2NUM(m);
1379
+ if (reduce==Qnil) reduce = SIZET2NUM(m);
1247
1380
  return reduce;
1248
1381
  }
1249
1382
 
@@ -1395,6 +1528,67 @@ VALUE na_profile_set(VALUE mod, VALUE val)
1395
1528
  }
1396
1529
 
1397
1530
 
1531
+ /*
1532
+ Returns the number of rows used for NArray#inspect
1533
+ @overload inspect_rows
1534
+ @return [Integer or nil] the number of rows.
1535
+ */
1536
+ static VALUE na_inspect_rows(VALUE mod)
1537
+ {
1538
+ if (numo_na_inspect_rows > 0) {
1539
+ return INT2NUM(numo_na_inspect_rows);
1540
+ } else {
1541
+ return Qnil;
1542
+ }
1543
+ }
1544
+
1545
+ /*
1546
+ Set the number of rows used for NArray#inspect
1547
+ @overload inspect_rows=(rows)
1548
+ @param [Integer or nil] rows the number of rows
1549
+ @return [nil]
1550
+ */
1551
+ static VALUE na_inspect_rows_set(VALUE mod, VALUE num)
1552
+ {
1553
+ if (RTEST(num)) {
1554
+ numo_na_inspect_rows = NUM2INT(num);
1555
+ } else {
1556
+ numo_na_inspect_rows = 0;
1557
+ }
1558
+ return Qnil;
1559
+ }
1560
+
1561
+ /*
1562
+ Returns the number of cols used for NArray#inspect
1563
+ @overload inspect_cols
1564
+ @return [Integer or nil] the number of cols.
1565
+ */
1566
+ static VALUE na_inspect_cols(VALUE mod)
1567
+ {
1568
+ if (numo_na_inspect_cols > 0) {
1569
+ return INT2NUM(numo_na_inspect_cols);
1570
+ } else {
1571
+ return Qnil;
1572
+ }
1573
+ }
1574
+
1575
+ /*
1576
+ Set the number of cols used for NArray#inspect
1577
+ @overload inspect_cols=(cols)
1578
+ @param [Integer or nil] cols the number of cols
1579
+ @return [nil]
1580
+ */
1581
+ static VALUE na_inspect_cols_set(VALUE mod, VALUE num)
1582
+ {
1583
+ if (RTEST(num)) {
1584
+ numo_na_inspect_cols = NUM2INT(num);
1585
+ } else {
1586
+ numo_na_inspect_cols = 0;
1587
+ }
1588
+ return Qnil;
1589
+ }
1590
+
1591
+
1398
1592
  /*
1399
1593
  Equality of self and other in view of numerical array.
1400
1594
  i.e., both arrays have same shape and corresponding elements are equal.
@@ -1444,6 +1638,8 @@ Init_narray()
1444
1638
  rb_cComplex = rb_const_get(rb_cObject, rb_intern("Complex"));
1445
1639
  #endif
1446
1640
 
1641
+ rb_define_const(cNArray, "VERSION", rb_str_new2(NARRAY_VERSION));
1642
+
1447
1643
  nary_eCastError = rb_define_class_under(cNArray, "CastError", rb_eStandardError);
1448
1644
  nary_eShapeError = rb_define_class_under(cNArray, "ShapeError", rb_eStandardError);
1449
1645
  nary_eOperationError = rb_define_class_under(cNArray, "OperationError", rb_eStandardError);
@@ -1453,6 +1649,11 @@ Init_narray()
1453
1649
  rb_define_singleton_method(cNArray, "profile", na_profile, 0);
1454
1650
  rb_define_singleton_method(cNArray, "profile=", na_profile_set, 1);
1455
1651
 
1652
+ rb_define_singleton_method(cNArray, "inspect_rows", na_inspect_rows, 0);
1653
+ rb_define_singleton_method(cNArray, "inspect_rows=", na_inspect_rows_set, 1);
1654
+ rb_define_singleton_method(cNArray, "inspect_cols", na_inspect_cols, 0);
1655
+ rb_define_singleton_method(cNArray, "inspect_cols=", na_inspect_cols_set, 1);
1656
+
1456
1657
  /* Ruby allocation framework */
1457
1658
  rb_define_alloc_func(cNArray, na_s_allocate);
1458
1659
  rb_define_method(cNArray, "initialize", na_initialize, -2);
@@ -1460,6 +1661,8 @@ Init_narray()
1460
1661
 
1461
1662
  rb_define_singleton_method(cNArray, "zeros", na_s_zeros, -1);
1462
1663
  rb_define_singleton_method(cNArray, "ones", na_s_ones, -1);
1664
+ rb_define_singleton_method(cNArray, "linspace", na_s_linspace, -1);
1665
+ rb_define_singleton_method(cNArray, "logspace", na_s_logspace, -1);
1463
1666
  rb_define_singleton_method(cNArray, "eye", na_s_eye, -1);
1464
1667
 
1465
1668
  rb_define_method(cNArray, "size", na_size, 0);
@@ -1468,6 +1671,7 @@ Init_narray()
1468
1671
  rb_define_method(cNArray, "shape", na_shape, 0);
1469
1672
  rb_define_method(cNArray, "ndim", na_ndim,0);
1470
1673
  rb_define_alias (cNArray, "rank","ndim");
1674
+ rb_define_method(cNArray, "empty?", na_empty_p, 0);
1471
1675
 
1472
1676
  rb_define_method(cNArray, "debug_info", rb_narray_debug_info, 0);
1473
1677