narray 0.5.9.9 → 0.6.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/src/na_linalg.c CHANGED
@@ -32,6 +32,7 @@ typedef struct NARRAY_FUNCSET {
32
32
  void (*sbt)();
33
33
  void (*mul)();
34
34
  void (*div)();
35
+ void (*mod)();
35
36
  void (*muladd)();
36
37
  void (*mulsbt)();
37
38
  void (*cmp)();
@@ -590,6 +591,7 @@ void Init_na_linalg()
590
591
  na_funcset[i].sbt = SbtUFuncs[i];
591
592
  na_funcset[i].mul = MulUFuncs[i];
592
593
  na_funcset[i].div = DivUFuncs[i];
594
+ na_funcset[i].mod = ModUFuncs[i];
593
595
  na_funcset[i].muladd = MulAddFuncs[i];
594
596
  na_funcset[i].mulsbt = MulSbtFuncs[i];
595
597
  na_funcset[i].cmp = CmpFuncs[i];
data/src/narray.c CHANGED
@@ -101,7 +101,7 @@ static void
101
101
  {
102
102
  if ( ary->total > 0 ) {
103
103
  if (ary->ref == Qnil || ary->ref == Qtrue) { /* non reference */
104
- xfree(ary->ptr);
104
+ xfree(ary->ptr);
105
105
  }
106
106
  xfree(ary->shape);
107
107
  #ifdef DEBUG
@@ -117,17 +117,22 @@ static void
117
117
  struct NARRAY*
118
118
  na_alloc_struct(int type, int rank, int *shape)
119
119
  {
120
- int total=1;
120
+ int total=1, total_bak;
121
121
  int i, memsz;
122
122
  struct NARRAY *ary;
123
123
 
124
- for (i=0; i<rank; ++i)
125
- total *= shape[i];
124
+ for (i=0; i<rank; ++i) {
125
+ total_bak = total;
126
+ total = total_bak * shape[i];
127
+ if (total>2147483647 || total/shape[i] != total_bak) {
128
+ rb_raise(rb_eArgError, "array size is too large");
129
+ }
130
+ }
126
131
 
127
132
  if (rank<=0 || total<=0) {
128
133
  /* empty array */
129
134
  ary = ALLOC(struct NARRAY);
130
- ary->rank =
135
+ ary->rank =
131
136
  ary->total = 0;
132
137
  ary->shape = NULL;
133
138
  ary->ptr = NULL;
@@ -136,6 +141,10 @@ struct NARRAY*
136
141
  else {
137
142
  memsz = na_sizeof[type] * total;
138
143
 
144
+ if (memsz>2147483647 || memsz/na_sizeof[type] != total) {
145
+ rb_raise(rb_eArgError, "allocation size is too large");
146
+ }
147
+
139
148
  /* Garbage Collection */
140
149
  #ifdef NARRAY_GC
141
150
  mem_count += memsz;
@@ -231,7 +240,7 @@ VALUE
231
240
  if (type==NA_ROBJ) {
232
241
  rb_mem_clear((VALUE*)(na->ptr), na->total);
233
242
  }
234
- return na_wrap_struct_class(na, klass);
243
+ return na_wrap_struct_class(na, klass);
235
244
  }
236
245
 
237
246
 
@@ -257,7 +266,7 @@ VALUE
257
266
  struct NARRAY *na;
258
267
 
259
268
  na = na_alloc_struct(type, 0, NULL);
260
- return na_wrap_struct_class(na, klass);
269
+ return na_wrap_struct_class(na, klass);
261
270
  }
262
271
 
263
272
 
@@ -610,7 +619,7 @@ static VALUE
610
619
  if ( len != str_len )
611
620
  rb_raise(rb_eArgError, "size mismatch");
612
621
  }
613
-
622
+
614
623
  v = na_make_object( type, rank, shape, cNArray );
615
624
  GetNArray(v,ary);
616
625
  memcpy( ary->ptr, RSTRING_PTR(str), ary->total*na_sizeof[type] );
@@ -720,7 +729,7 @@ static VALUE
720
729
  }
721
730
 
722
731
 
723
- /* singleton method:
732
+ /* singleton method:
724
733
  NArray.to_na( string, type, size1,size2,...,sizeN )
725
734
  NArray.to_na( array )
726
735
  */
@@ -751,7 +760,7 @@ static VALUE
751
760
  }
752
761
 
753
762
 
754
- /* singleton method:
763
+ /* singleton method:
755
764
  NArray[object]
756
765
  */
757
766
  static VALUE
@@ -986,7 +995,7 @@ VALUE na_fill(VALUE self, volatile VALUE val)
986
995
  if (a2->total != 1)
987
996
  rb_raise(rb_eArgError, "single-element argument required");
988
997
 
989
- SetFuncs[a1->type][a2->type]( a1->total,
998
+ SetFuncs[a1->type][a2->type]( a1->total,
990
999
  a1->ptr, na_sizeof[a1->type],
991
1000
  a2->ptr, 0 );
992
1001
  return self;
@@ -1010,7 +1019,7 @@ VALUE
1010
1019
  }
1011
1020
 
1012
1021
  GetNArray(self,ary);
1013
- IndGenFuncs[ary->type]( ary->total,
1022
+ IndGenFuncs[ary->type]( ary->total,
1014
1023
  ary->ptr, na_sizeof[ary->type],
1015
1024
  start, step );
1016
1025
  return self;
data/src/narray.h CHANGED
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  narray.h
3
3
  Numerical Array Extention for Ruby
4
- (C) Copyright 1999-2008 by Masahiro TANAKA
4
+ (C) Copyright 1999-2011 by Masahiro TANAKA
5
5
 
6
6
  This program is free software.
7
7
  You can distribute/modify this program
@@ -23,8 +23,8 @@
23
23
  # include <sys/types.h>
24
24
  #endif
25
25
 
26
- #define NARRAY_VERSION "0.5.9p9"
27
- #define NARRAY_VERSION_CODE 599
26
+ #define NARRAY_VERSION "0.6.0.0"
27
+ #define NARRAY_VERSION_CODE 600
28
28
 
29
29
  /*
30
30
  Data types used in NArray :
data/src/narray_local.h CHANGED
@@ -53,6 +53,7 @@ extern na_ufunc_t AddUFuncs;
53
53
  extern na_ufunc_t SbtUFuncs;
54
54
  extern na_ufunc_t MulUFuncs;
55
55
  extern na_ufunc_t DivUFuncs;
56
+ extern na_ufunc_t ModUFuncs;
56
57
  extern na_bifunc_t AddBFuncs;
57
58
  extern na_bifunc_t SbtBFuncs;
58
59
  extern na_bifunc_t MulBFuncs;
metadata CHANGED
@@ -1,13 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: narray
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 5
8
- - 9
9
- - 9
10
- version: 0.5.9.9
4
+ version: 0.6.0.0
11
5
  platform: ruby
12
6
  authors:
13
7
  - Masahiro Tanaka
@@ -15,11 +9,11 @@ autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
11
 
18
- date: 2010-12-14 00:00:00 +09:00
12
+ date: 2011-08-27
19
13
  default_executable:
20
14
  dependencies: []
21
15
 
22
- description:
16
+ description: Numerical N-dimensional Array class
23
17
  email: masa16.tanaka@gmail.com
24
18
  executables: []
25
19
 
@@ -50,38 +44,7 @@ files:
50
44
  - src/narray_local.h
51
45
  - src/lib/narray_ext.rb
52
46
  - src/lib/nmatrix.rb
53
- - src/test/statistics.rb
54
- - src/test/testarray.rb
55
- - src/test/testbit.rb
56
- - src/test/testcast.rb
57
- - src/test/testcomplex.rb
58
- - src/test/testfftw.rb
59
- - src/test/testindex.rb
60
- - src/test/testindexary.rb
61
- - src/test/testindexset.rb
62
- - src/test/testmask.rb
63
- - src/test/testmath.rb
64
- - src/test/testmath2.rb
65
- - src/test/testmatrix.rb
66
- - src/test/testmatrix2.rb
67
- - src/test/testmatrix3.rb
68
- - src/test/testminmax.rb
69
- - src/test/testobject.rb
70
- - src/test/testpow.rb
71
- - src/test/testrandom.rb
72
- - src/test/testround.rb
73
- - src/test/testsort.rb
74
- - src/test/teststr.rb
75
- - src/test/testtrans.rb
76
- - src/test/testwhere.rb
77
- - src/bench/all.rb
78
- - src/bench/bench.m
79
- - src/bench/bench.py
80
- - src/bench/bench.rb
81
- - src/bench/dummy.m
82
- - src/bench/dummy.py
83
- - src/bench/dummy.rb
84
- has_rdoc: true
47
+ has_rdoc: false
85
48
  homepage: http://narray.rubyforge.org/
86
49
  licenses: []
87
50
 
@@ -91,27 +54,23 @@ rdoc_options: []
91
54
  require_paths:
92
55
  - .
93
56
  required_ruby_version: !ruby/object:Gem::Requirement
94
- none: false
95
57
  requirements:
96
58
  - - ">="
97
59
  - !ruby/object:Gem::Version
98
- segments:
99
- - 0
100
60
  version: "0"
61
+ version:
101
62
  required_rubygems_version: !ruby/object:Gem::Requirement
102
- none: false
103
63
  requirements:
104
64
  - - ">="
105
65
  - !ruby/object:Gem::Version
106
- segments:
107
- - 0
108
66
  version: "0"
67
+ version:
109
68
  requirements: []
110
69
 
111
70
  rubyforge_project: narray
112
71
  rubygems_version: 1.3.7
113
72
  signing_key:
114
- specification_version: 3
73
+ specification_version: 2
115
74
  summary: N-dimensional Numerical Array class for Ruby
116
75
  test_files: []
117
76
 
data/src/bench/all.rb DELETED
@@ -1,88 +0,0 @@
1
- require "narray"
2
- T = (RUBY_VERSION<"1.8.0") ? Time : Process
3
-
4
- ruby_narray = system( "ruby -r narray dummy.rb" )
5
- python_numeric = system( "python dummy.py numeric" )
6
- python_numarray = system( "python dummy.py numarray" )
7
- python_numpy = system( "python dummy.py numpy" )
8
- octave = system( "octave -qf dummy.m" )
9
-
10
- def array_size
11
- list = [
12
- 100000, 200000, 500000,
13
- 1000000, 2000000, 5000000,
14
- 10000000, 20000000, 50000000
15
- ]
16
- mlist = [
17
- 150, 200, 300, 500, 700, 1000, 1500, 2000, 3000
18
- #300, 400, 700, 1000, 1400, 2000, 3000, 4000, 7000
19
- ]
20
-
21
- r = 50
22
- n = nil
23
- i = nil
24
- list.each_with_index do |n,i|
25
- a = NArray.float(n)
26
- b = NArray.float(n)
27
- t = bench_time(r) { c = a+b }
28
- break if t>0.5
29
- end
30
- [n, mlist[i], r*2]
31
- end
32
-
33
- def bench_time(n)
34
- t1 = T.times.utime
35
- for i in 1..n
36
- yield
37
- end
38
- t = T.times.utime - t1
39
- puts " Time: %.2f sec\n" % [t]
40
- t
41
- end
42
-
43
- n,m,r = array_size
44
- puts "array size = #{n}, repeat = #{r}\n\n"
45
-
46
- system "ruby bench.rb float mul #{n} #{r}" if ruby_narray
47
- system "python bench.py numeric float mul #{n} #{r}" if python_numeric
48
- system "python bench.py numarray float mul #{n} #{r}" if python_numarray
49
- system "python bench.py numpy float mul #{n} #{r}" if python_numpy
50
- system "octave -qf bench.m float mul #{n} #{r}" if octave
51
- puts
52
-
53
- system "ruby bench.rb int add #{n} #{r}" if ruby_narray
54
- system "python bench.py numeric int add #{n} #{r}" if python_numeric
55
- system "python bench.py numarray int add #{n} #{r}" if python_numarray
56
- system "python bench.py numpy int add #{n} #{r}" if python_numpy
57
- system "octave -qf bench.m int add #{n} #{r}" if octave
58
- puts
59
-
60
- system "ruby bench.rb complex mul #{n} #{r}" if ruby_narray
61
- system "python bench.py numeric complex mul #{n} #{r}" if python_numeric
62
- system "python bench.py numarray complex mul #{n} #{r}" if python_numarray
63
- system "python bench.py numpy complex mul #{n} #{r}" if python_numpy
64
- system "octave -qf bench.m complex mul #{n} #{r}" if octave
65
- puts
66
-
67
- system "ruby bench.rb float_cross mul #{m*2} #{r}" if ruby_narray
68
- system "python bench.py numeric float_cross mul #{m*2} #{r}" if python_numeric
69
- system "python bench.py numarray float_cross mul #{m*2} #{r}" if python_numarray
70
- system "python bench.py numpy float_cross mul #{m*2} #{r}" if python_numpy
71
- system "octave -qf bench.m float_cross matmul #{m*2} #{r}" if octave
72
- puts
73
-
74
- system "ruby bench.rb float_matrix mul #{m} 4" if ruby_narray
75
- system "python bench.py numeric float_matrix matmul #{m} 4" if python_numeric
76
- system "python bench.py numarray float_matrix matmul #{m} 4" if python_numarray
77
- system "python bench.py numpy float_matrix matmul #{m} 4" if python_numpy
78
- system "octave -qf bench.m float_matrix matmul #{m} 4" if octave
79
- puts
80
-
81
- system "ruby bench.rb float_solve solve #{m} 2" if ruby_narray
82
- system "python bench.py numeric float_solve solve #{m} 2" if python_numeric
83
- system "python bench.py numarray float_solve solve #{m} 2" if python_numarray
84
- system "python bench.py numpy float_solve solve #{m} 2" if python_numpy
85
- system "octave -qf bench.m float_solve solve #{m} 2" if octave
86
- puts
87
-
88
- exit
data/src/bench/bench.m DELETED
@@ -1,61 +0,0 @@
1
- #! /bin/octave -qf
2
-
3
- arg_list = argv();
4
- TYPE = arg_list{1};
5
- OP = arg_list{2};
6
- ARRSZ = str2num(arg_list{3});
7
- REPEAT = str2num(arg_list{4});
8
- n = ARRSZ;
9
-
10
- switch(TYPE)
11
- case "float"
12
- a = linspace(0,n-1,n);
13
- b = linspace(0,n-1,n);
14
- case "int"
15
- a = int32(linspace(0,n-1,n));
16
- b = int32(linspace(0,n-1,n));
17
- case "complex"
18
- a = complex(linspace(0,n-1,n));
19
- b = complex(linspace(0,n-1,n));
20
- case "float_cross"
21
- a = linspace(0,n-1,n)';
22
- b = linspace(0,n-1,n);
23
- case "float_matrix"
24
- a = linspace(0,n*n-1,n*n);
25
- a = rem(a, n+1) + 1;
26
- a = reshape(a,n,n);
27
- b = linspace(0,n*n-1,n*n);
28
- b = rem(b, n-1) + 1;
29
- b = reshape(b,n,n);
30
- case "float_solve"
31
- a = linspace(0,n*n-1,n*n);
32
- a = rem(a, n+1) + 1;
33
- a = reshape(a,n,n);
34
- b = reshape(linspace(1,n*n,n*n),n,n);
35
- endswitch
36
-
37
- [t1, u1, s1] = cputime ();
38
- switch(OP)
39
- case "add"
40
- for i = 1:REPEAT
41
- c = a + b;
42
- endfor
43
- case "mul"
44
- for i = 1:REPEAT
45
- c = a .* b;
46
- endfor
47
- case "matmul"
48
- for i = 1:REPEAT
49
- c = a * b;
50
- endfor
51
- #size(c)
52
- case "solve"
53
- for i = 1:REPEAT
54
- c = a \ b;
55
- endfor
56
- #size(c)
57
- endswitch
58
- [t2, u2, s2] = cputime ();
59
-
60
- printf ("Octave type=%s size=%d op=%s repeat=%d Time: %.2f sec",
61
- TYPE,ARRSZ,OP,REPEAT,u2 - u1);
data/src/bench/bench.py DELETED
@@ -1,89 +0,0 @@
1
- import time
2
- import sys
3
-
4
- MODULE = sys.argv[1]
5
- TYPE = sys.argv[2]
6
- OP = sys.argv[3]
7
- ARRSZ = int(sys.argv[4])
8
- REPEAT = int(sys.argv[5])
9
-
10
- if MODULE=="numeric":
11
- from Numeric import *
12
- from LinearAlgebra import *
13
- elif MODULE=="numarray":
14
- from numarray import *
15
- from LinearAlgebra import *
16
- elif MODULE=="numpy":
17
- from numpy import *
18
- from numpy.linalg import solve
19
-
20
- def bench_time(func,repeat=REPEAT):
21
- start = time.clock()
22
- for i in range(repeat):
23
- c = func()
24
- stop = time.clock()
25
- print "Python %s type=%s size=%d op=%s repeat=%d Time: %.2f sec" % \
26
- (MODULE,TYPE,ARRSZ,OP,REPEAT,stop-start)
27
- #print shape(c)
28
-
29
- n = ARRSZ
30
-
31
- if MODULE=="numpy":
32
- def bench_array(type=float):
33
- return arange(ARRSZ,dtype=type)
34
-
35
- if TYPE=="float":
36
- a = bench_array(float)
37
- b = bench_array(float)
38
- elif TYPE=="int":
39
- a = bench_array(int)
40
- b = bench_array(int)
41
- elif TYPE=="complex":
42
- a = bench_array(complex)
43
- b = bench_array(complex)
44
- elif TYPE=="float_cross":
45
- a = reshape(arange(ARRSZ,dtype=float),(ARRSZ,1))
46
- b = reshape(arange(ARRSZ,dtype=float),(1,ARRSZ))
47
- elif TYPE=="float_matrix":
48
- a = reshape(arange(ARRSZ**2,dtype=float),(ARRSZ,ARRSZ))
49
- b = reshape(arange(ARRSZ**2,dtype=float),(ARRSZ,ARRSZ))
50
- elif TYPE=="float_solve":
51
- a = reshape(arange(n**2,dtype=float)%(n+1)+1,(n,n))
52
- b = reshape(arange(n**2,dtype=float)+1,(n,n))
53
- else:
54
- def bench_array(type=float):
55
- return arrayrange(ARRSZ).astype(type)
56
- if TYPE=="float":
57
- a = bench_array(Float64)
58
- b = bench_array(Float64)
59
- elif TYPE=="int":
60
- a = bench_array(Int32)
61
- b = bench_array(Int32)
62
- elif TYPE=="complex":
63
- a = bench_array(Complex64)
64
- b = bench_array(Complex64)
65
- elif TYPE=="float_cross":
66
- a = reshape(arrayrange(ARRSZ),(ARRSZ,1)).astype(Float64)
67
- b = reshape(arrayrange(ARRSZ),(1,ARRSZ)).astype(Float64)
68
- elif TYPE=="float_matrix":
69
- a = reshape(arrayrange(ARRSZ**2),(ARRSZ,ARRSZ)).astype(Float64)
70
- b = reshape(arrayrange(ARRSZ**2),(ARRSZ,ARRSZ)).astype(Float64)
71
- elif TYPE=="float_solve":
72
- a = reshape(arrayrange(n*n)%(n+1)+1,(n,n)).astype(Float64)
73
- b = reshape(arrayrange(n*n)+1,(n,n)).astype(Float64)
74
- dot = matrixmultiply
75
- solve = solve_linear_equations
76
-
77
- def lambda_add(a=a,b=b): c = a+b; return c;
78
- def lambda_mul(a=a,b=b): c = a*b; return c;
79
- def lambda_matmul(a=a,b=b): c = dot(a,b); return c;
80
- def lambda_solve(a=a,b=b): c = solve(a,b); return c;
81
-
82
- if OP=="add":
83
- bench_time(lambda_add)
84
- elif OP=="mul":
85
- bench_time(lambda_mul)
86
- elif OP=="matmul":
87
- bench_time(lambda_matmul)
88
- elif OP=="solve":
89
- bench_time(lambda_solve)