narray 0.5.9.5 → 0.5.9.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,16 @@
1
+ 2008-11-05 Masahiro TANAKA <masa16.tanaka at gmail.com>
2
+
3
+ * work with ruby 1.9.1 preview1
4
+ - use RARRAY_*** macros instead of struct RArray.
5
+ - follow changes in Complex feature.
6
+ - change oprator arg of coerce_rev func : id -> symbol
7
+ - modify test scripts.
8
+
9
+ * na_array.c (na_do_mdai):
10
+ - bug fix in counting narray's rank.
11
+
12
+ * ver 0.5.9p6
13
+
1
14
  2008-06-10 Masahiro TANAKA <masa16.tanaka at gmail.com>
2
15
 
3
16
  * bench/: new sophisticated benchmark script.
@@ -51,12 +51,3 @@
51
51
 
52
52
  $BEDCf>;9((B
53
53
  $B$40U8+!$%P%0%l%]!<%H$=$NB>$O(B masa16.tanaka@gmail.com $B$^$G$*4j$$$7$^$9!#(B
54
-
55
- * $B<U<-(B
56
-
57
- $B$^$D$b$H$f$-$R$m(B $B$5$s(B $B$r$O$8$a$H$9$k(BRuby$B3+H/<T$N$_$J$5$^(B for Ruby
58
-
59
- $B0KF#(B $B>4B'(B $B$5$s(B for MDArray
60
- $B3HD%%i%$%V%i%j$N:n$jJ}$d!"(BArray$B$+$iB?<!85G[Ns$KJQ49$9$kItJ,$r(B
61
- $B;29M$K$5$;$F$$$?$@$-$^$7$?!#(B
62
-
@@ -14,7 +14,7 @@ class NMatrix < NArray
14
14
  return super(NArray.refer(other))
15
15
  when NArray
16
16
  unless other.instance_of?(NArray)
17
- return other.coerce_rev( self, :+.to_i )
17
+ return other.coerce_rev( self, :+ )
18
18
  end
19
19
  end
20
20
  raise TypeError,"Illegal operation: NMatrix + %s" % other.class
@@ -26,7 +26,7 @@ class NMatrix < NArray
26
26
  return super(NArray.refer(other))
27
27
  when NArray
28
28
  unless other.instance_of?(NArray)
29
- return other.coerce_rev( self, :-.to_i )
29
+ return other.coerce_rev( self, :- )
30
30
  end
31
31
  end
32
32
  raise TypeError,"Illegal operation: NMatrix - %s" % other.class
@@ -44,7 +44,7 @@ class NMatrix < NArray
44
44
  if other.instance_of?(NArray)
45
45
  NMatrix.mul( NArray.refer(self), other.newdim(0,0) )
46
46
  else
47
- other.coerce_rev( self, :*.to_i )
47
+ other.coerce_rev( self, :* )
48
48
  end
49
49
  when Numeric
50
50
  super
@@ -66,7 +66,7 @@ class NMatrix < NArray
66
66
  if other.instance_of?(NArray)
67
67
  NMatrix.div( NArray.refer(self), other.newdim(0,0) )
68
68
  else
69
- other.coerce_rev( self, :/.to_i )
69
+ other.coerce_rev( self, :/ )
70
70
  end
71
71
  when Numeric
72
72
  NMatrix.div( NArray.refer(self), other )
@@ -97,14 +97,14 @@ class NMatrix < NArray
97
97
 
98
98
  def coerce_rev(other,id)
99
99
  case id
100
- when :*.to_i
100
+ when :*
101
101
  if other.instance_of?(NArray)
102
102
  return NMatrix.mul( other.newdim(0,0), self )
103
103
  end
104
104
  if other.instance_of?(NArrayScalar)
105
105
  return NMatrix.mul( other.newdim(0), self )
106
106
  end
107
- when :/.to_i
107
+ when :/
108
108
  if other.instance_of?(NArray)
109
109
  return NMatrix.mul( other.newdim(0,0), self.inverse )
110
110
  end
@@ -163,7 +163,7 @@ class NVector < NArray
163
163
  return super(NArray.refer(other))
164
164
  when NArray
165
165
  unless other.instance_of?(NArray)
166
- return other.coerce_rev( self, :+.to_i )
166
+ return other.coerce_rev( self, :+ )
167
167
  end
168
168
  end
169
169
  raise TypeError,"Illegal operation: NVector + %s" % other.class
@@ -175,7 +175,7 @@ class NVector < NArray
175
175
  return super(NArray.refer(other))
176
176
  when NArray
177
177
  unless other.instance_of?(NArray)
178
- return other.coerce_rev( self, :-.to_i )
178
+ return other.coerce_rev( self, :- )
179
179
  end
180
180
  end
181
181
  raise TypeError,"Illegal operation: NVector - %s" % other.class
@@ -191,7 +191,7 @@ class NVector < NArray
191
191
  if other.instance_of?(NArray)
192
192
  NVector.mul( NArray.refer(self), other.newdim(0) )
193
193
  else
194
- other.coerce_rev( self, :*.to_i )
194
+ other.coerce_rev( self, :* )
195
195
  end
196
196
  when Numeric
197
197
  NVector.mul( NArray.refer(self), other )
@@ -210,7 +210,7 @@ class NVector < NArray
210
210
  if other.instance_of?(NArray)
211
211
  NVector.div( NArray.refer(self), other.newdim(0) )
212
212
  else
213
- other.coerce_rev( self, :/.to_i )
213
+ other.coerce_rev( self, :/ )
214
214
  end
215
215
  when Numeric
216
216
  NVector.div( NArray.refer(self), other )
@@ -229,7 +229,7 @@ class NVector < NArray
229
229
 
230
230
  def coerce_rev(other,id)
231
231
  case id
232
- when :*.to_i
232
+ when :*
233
233
  if other.instance_of?(NArray)
234
234
  return NVector.mul( other.newdim(0), self )
235
235
  end
@@ -152,14 +152,14 @@ static int
152
152
  {
153
153
  int i, j, len, length, start, dir;
154
154
  VALUE v;
155
- struct RArray *ary;
155
+ VALUE ary;
156
156
 
157
- ary = RARRAY(mdai->item[rank-1].val);
158
- len = ary->len;
157
+ ary = mdai->item[rank-1].val;
158
+ len = RARRAY_LEN(ary);
159
159
 
160
- for (i=0; i < ary->len; ++i) {
160
+ for (i=0; i < RARRAY_LEN(ary); ++i) {
161
161
 
162
- v = ary->ptr[i];
162
+ v = RARRAY_PTR(ary)[i];
163
163
 
164
164
  if (TYPE(v) == T_ARRAY) {
165
165
  /* check recursive array */
@@ -196,7 +196,7 @@ static int
196
196
  if ( rank+na->rank > mdai->n ) {
197
197
  na_realloc_mdai(mdai,((na->rank-1)/4+1)*4);
198
198
  }
199
- for ( j=na->rank, r=rank; --j > 0 ; ++r ) {
199
+ for ( j=na->rank, r=rank; j-- > 0 ; ++r ) {
200
200
  if ( mdai->item[r].shape < na->shape[j] )
201
201
  mdai->item[r].shape = na->shape[j];
202
202
  }
@@ -269,15 +269,15 @@ static void
269
269
 
270
270
  /* copy Array to NArray */
271
271
  static void
272
- na_copy_ary_to_nary( struct RArray *ary, struct NARRAY *na,
272
+ na_copy_ary_to_nary( VALUE ary, struct NARRAY *na,
273
273
  int thisrank, int *idx, int type )
274
274
  {
275
275
  int i, j, pos, len, start, step, dir;
276
276
  VALUE v;
277
277
 
278
278
  if (thisrank==0) {
279
- for (i = idx[0] = 0; i < ary->len; ++i) {
280
- v = ary->ptr[i];
279
+ for (i = idx[0] = 0; i < RARRAY_LEN(ary); ++i) {
280
+ v = RARRAY_PTR(ary)[i];
281
281
  if (rb_obj_is_kind_of(v, rb_cRange)) {
282
282
  na_range_to_sequence(v,&len,&start,&dir);
283
283
  if (len>0) {
@@ -299,10 +299,10 @@ static void
299
299
  }
300
300
  else /* thisrank > 0 */
301
301
  {
302
- for (i = idx[thisrank] = 0; i < ary->len; ++i) {
303
- v = ary->ptr[i];
302
+ for (i = idx[thisrank] = 0; i < RARRAY_LEN(ary); ++i) {
303
+ v = RARRAY_PTR(ary)[i];
304
304
  if (TYPE(v) == T_ARRAY) {
305
- na_copy_ary_to_nary(RARRAY(v),na,thisrank-1,idx,type);
305
+ na_copy_ary_to_nary(v,na,thisrank-1,idx,type);
306
306
  if (idx[thisrank-1]>0) ++idx[thisrank];
307
307
  }
308
308
  else if (IsNArray(v)) {
@@ -325,7 +325,7 @@ static void
325
325
  }
326
326
  else {
327
327
  pos = na_index_pos(na,idx);
328
- SetFuncs[type][NA_ROBJ]( 1, NA_PTR(na,pos), 0, ary->ptr+i, 0 );
328
+ SetFuncs[type][NA_ROBJ]( 1, NA_PTR(na,pos), 0, &(RARRAY_PTR(ary)[i]), 0 );
329
329
  ++idx[thisrank];
330
330
  }
331
331
  /* copy here */
@@ -346,7 +346,7 @@ static VALUE
346
346
  VALUE v;
347
347
 
348
348
  /* empty array */
349
- if (RARRAY(ary)->len < 1) {
349
+ if (RARRAY_LEN(ary) < 1) {
350
350
  return na_make_empty( type, klass );
351
351
  }
352
352
 
@@ -380,7 +380,7 @@ static VALUE
380
380
  idx = ALLOCA_N(int,rank);
381
381
  for (i=0; i<rank; ++i) idx[i]=0;
382
382
 
383
- na_copy_ary_to_nary( RARRAY(ary), na, rank-1, idx, type );
383
+ na_copy_ary_to_nary( ary, na, rank-1, idx, type );
384
384
 
385
385
  return v;
386
386
  }
@@ -607,7 +607,7 @@ static VALUE
607
607
 
608
608
  if (klass==Qnil) { /* coerce_rev */
609
609
  if ((id=na_bifunc_to_id(funcs))!=0)
610
- return rb_funcall( obj2, na_id_coerce_rev, 2, obj1, INT2FIX(id) );
610
+ return rb_funcall( obj2, na_id_coerce_rev, 2, obj1, ID2SYM(id) );
611
611
  else
612
612
  klass = cNArray;
613
613
  }
@@ -1047,7 +1047,7 @@ static int
1047
1047
  r = NUM2INT(v);
1048
1048
  if (r<0) r += rankc; /* negative for from end */
1049
1049
  if (r<0 || r>=rankc)
1050
- rb_raise(rb_eArgError, "rank %d out of range", r);
1050
+ rb_raise(rb_eArgError, "rank %ld out of range", r);
1051
1051
  if (flag)
1052
1052
  rankv[c] = r;
1053
1053
  else
@@ -266,11 +266,14 @@ static int
266
266
  int status, size, n=shape[0];
267
267
 
268
268
  if (type==NA_ROBJ) {
269
+ VALUE *mem;
270
+ int i;
269
271
  size = n*2+1;
270
- val = rb_ary_new2(size);
271
- rb_mem_clear(RARRAY(val)->ptr,size);
272
- RARRAY(val)->len = size;
273
- buf = (char*)((RARRAY(val))->ptr);
272
+ mem = ALLOC_N(VALUE, size);
273
+ for (i=0; i<size; i++) mem[i] = Qnil;
274
+ val = rb_ary_new4(size, mem);
275
+ xfree(mem);
276
+ buf = (char*)((RARRAY_PTR(val)));
274
277
  status = na_lu_fact_func_body( ni, a, idx, shape, type, buf );
275
278
  } else {
276
279
  size = na_sizeof[type]*n + na_sizeof[na_cast_real[type]]*(n+1);
@@ -427,11 +430,14 @@ na_lu_solve_func( int ni,
427
430
  int size;
428
431
 
429
432
  if (type==NA_ROBJ) {
433
+ VALUE *mem;
434
+ int i;
430
435
  size = shape[1];
431
- val = rb_ary_new2(size);
432
- rb_mem_clear(RARRAY(val)->ptr,size);
433
- RARRAY(val)->len = size;
434
- buf = (char*)((RARRAY(val))->ptr);
436
+ mem = ALLOC_N(VALUE, size);
437
+ for (i=0; i<size; i++) mem[i] = Qnil;
438
+ val = rb_ary_new4(size, mem);
439
+ xfree(mem);
440
+ buf = (char*)((RARRAY_PTR(val)));
435
441
  na_lu_solve_func_body( ni, x, ps1, a, ps2, shape, type, buf );
436
442
  } else {
437
443
  size = shape[1] * na_sizeof[type];
@@ -24,6 +24,7 @@ ID na_id_add, na_id_sbt, na_id_mul, na_id_div, na_id_mod;
24
24
  ID na_id_real, na_id_imag;
25
25
  ID na_id_coerce_rev;
26
26
  ID na_id_new;
27
+ ID na_id_Complex;
27
28
  static ID na_id_to_i, na_id_usec, na_id_now;
28
29
 
29
30
  const int na_sizeof[NA_NTYPES+1] = {
@@ -775,7 +776,7 @@ static VALUE
775
776
  struct NARRAY *ary;
776
777
  int i;
777
778
  char buf[256];
778
- char *classname;
779
+ const char *classname;
779
780
  char *ref = "%s(ref).%s(%i";
780
781
  char *org = "%s.%s(%i";
781
782
 
@@ -1061,7 +1062,7 @@ static VALUE
1061
1062
  static VALUE
1062
1063
  na_where(VALUE self)
1063
1064
  {
1064
- return RARRAY( na_where2(self) )->ptr[0];
1065
+ return RARRAY_PTR( na_where2(self) )[0];
1065
1066
  }
1066
1067
 
1067
1068
 
@@ -1271,7 +1272,7 @@ void
1271
1272
  na_id_end = rb_intern("end");
1272
1273
  na_id_exclude_end = rb_intern("exclude_end?");
1273
1274
  na_id_real = rb_intern("real");
1274
- na_id_imag = rb_intern("image");
1275
+ na_id_imag = rb_intern("imag");
1275
1276
  na_id_new = rb_intern("new");
1276
1277
  na_id_to_i = rb_intern("to_i");
1277
1278
  na_id_usec = rb_intern("usec");
@@ -1288,6 +1289,7 @@ void
1288
1289
  na_id_div = rb_intern("/");
1289
1290
  na_id_mod = rb_intern("%");
1290
1291
  na_id_coerce_rev = rb_intern("coerce_rev");
1292
+ na_id_Complex = rb_intern("Complex");
1291
1293
 
1292
1294
  na_id_class_dim = rb_intern("CLASS_DIMENSION");
1293
1295
 
@@ -19,8 +19,8 @@
19
19
  # include <sys/types.h>
20
20
  #endif
21
21
 
22
- #define NARRAY_VERSION "0.5.9p5"
23
- #define NARRAY_VERSION_CODE 595
22
+ #define NARRAY_VERSION "0.5.9p6"
23
+ #define NARRAY_VERSION_CODE 596
24
24
 
25
25
  /*
26
26
  Data types used in NArray :
@@ -92,6 +92,7 @@ extern ID na_id_add, na_id_sbt, na_id_mul, na_id_div, na_id_mod;
92
92
  extern ID na_id_real, na_id_imag;
93
93
  extern ID na_id_coerce_rev;
94
94
  extern ID na_id_new;
95
+ extern ID na_id_Complex;
95
96
 
96
97
  extern const int na_upcast[NA_NTYPES][NA_NTYPES];
97
98
  extern const int na_no_cast[NA_NTYPES];
@@ -128,7 +129,7 @@ int na_shrink_class(int class_dim, int *shrink);
128
129
  VALUE na_shrink_rank(VALUE obj, int class_dim, int *shrink);
129
130
 
130
131
  #define rb_complex_new(r,i) \
131
- rb_funcall(cComplex, na_id_new, 2, rb_float_new(r), rb_float_new(i))
132
+ rb_funcall(rb_mKernel, na_id_Complex, 2, rb_float_new(r), rb_float_new(i))
132
133
 
133
134
 
134
135
  typedef union {
@@ -208,3 +209,9 @@ typedef union {
208
209
  #if !defined RSTRING_PTR
209
210
  #define RSTRING_PTR(a) RSTRING(a)->ptr
210
211
  #endif
212
+ #if !defined RARRAY_LEN
213
+ #define RARRAY_LEN(a) RARRAY(a)->len
214
+ #endif
215
+ #if !defined RARRAY_PTR
216
+ #define RARRAY_PTR(a) RARRAY(a)->ptr
217
+ #endif
@@ -2,14 +2,14 @@ require "narray"
2
2
 
3
3
  a = NArray.byte(10)
4
4
  a[2..4] = 1
5
- p a.type, a.count_true, a.count_false
5
+ p a.class, a.count_true, a.count_false
6
6
 
7
7
  begin
8
8
  a = NArray.float(10)
9
9
  a[2..4] = 1
10
- p a.type, a.count_true, a.count_false
10
+ p a.class, a.count_true, a.count_false
11
11
  rescue
12
- print a.type," -- Exception raised as expected. The message was: ", $!,"\n"
12
+ print a.class," -- Exception raised as expected. The message was: ", $!,"\n"
13
13
  end
14
14
 
15
15
  #-------------------
@@ -1,33 +1,42 @@
1
1
  require 'narray'
2
- require 'irb/xmp'
2
+ #require 'irb/xmp'
3
3
  # xmp :: http://www.ruby-lang.org/en/raa-list.rhtml?name=xmp
4
+ def xp(s)
5
+ begin
6
+ puts s+" #=>"
7
+ p eval(s)
8
+ rescue
9
+ puts $!
10
+ end
11
+ puts
12
+ end
4
13
 
5
- m1 = NMatrix.float(2,2).indgen!
6
- m2 = NMatrix[[0,1.2],[1.5,0]]
14
+ $m1 = NMatrix.float(2,2).indgen!
15
+ $m2 = NMatrix[[0,1.2],[1.5,0]]
7
16
 
8
- v1 = NVector[0.5,1.5]
9
- v2 = NVector.float(2,2).indgen!
17
+ $v1 = NVector[0.5,1.5]
18
+ $v2 = NVector.float(2,2).indgen!
10
19
 
11
- a = NArray.float(2,2).indgen!
20
+ $a = NArray.float(2,2).indgen!
12
21
 
13
- xmp 'm1'
14
- xmp 'm1.inverse'
15
- xmp 'm2'
16
- xmp 'm1*m2'
17
- xmp 'm2*m1'
18
- xmp 'm1+m2'
19
- xmp '3.14*m1'
20
- xmp 'm2*1.25'
21
- xmp 'v1'
22
- xmp 'v2'
23
- xmp '1.25*v1'
24
- xmp 'NMath.sqrt(v2**2)'
25
- xmp 'v1*v2'
26
- xmp 'm1*v1'
27
- xmp 'v2*m2'
28
- xmp 'm1.diagonal([98,99])'
29
- xmp 'NMatrix.float(4,3).unit'
22
+ xp '$m1'
23
+ xp '$m1.inverse'
24
+ xp '$m2'
25
+ xp '$m1*$m2'
26
+ xp '$m2*$m1'
27
+ xp '$m1+$m2'
28
+ xp '3.14*$m1'
29
+ xp '$m2*1.25'
30
+ xp '$v1'
31
+ xp '$v2'
32
+ xp '1.25*$v1'
33
+ xp 'NMath.sqrt($v2**2)'
34
+ xp '$v1*$v2'
35
+ xp '$m1*$v1'
36
+ xp '$v2*$m2'
37
+ xp '$m1.diagonal([98,99])'
38
+ xp 'NMatrix.float(4,3).unit'
30
39
 
31
40
  puts "\n=== following will fail ...\n"
32
- xmp 'm1+v1'
33
- xmp 'm1+1'
41
+ xp '$m1+$v1'
42
+ xp '$m1+1'
@@ -1,11 +1,11 @@
1
1
  require 'narray'
2
2
  require 'rational'
3
3
 
4
- class Rational
5
- def inspect
6
- @numerator.to_s+"/"+@denominator.to_s
7
- end
8
- end
4
+ #class Rational
5
+ # def inspect
6
+ # @numerator.to_s+"/"+@denominator.to_s
7
+ # end
8
+ #end
9
9
 
10
10
  srand(1)
11
11
  n=5
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: narray
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.5.9.5
7
- date: 2008-06-10 00:00:00 +09:00
6
+ version: 0.5.9.6
7
+ date: 2008-11-05 00:00:00 +09:00
8
8
  summary: N-dimensional Numerical Array class for Ruby
9
9
  require_paths:
10
10
  - .