narray-ruby19 0.5.9.7-x86-mingw32
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.
- data/lib/libnarray.a +0 -0
- data/lib/narray.h +186 -0
- data/lib/narray.so +0 -0
- data/lib/narray_config.h +5 -0
- data/lib/narray_ext.rb +245 -0
- data/lib/nmatrix.rb +244 -0
- metadata +59 -0
data/lib/libnarray.a
ADDED
Binary file
|
data/lib/narray.h
ADDED
@@ -0,0 +1,186 @@
|
|
1
|
+
/*
|
2
|
+
narray.h
|
3
|
+
Numerical Array Extention for Ruby
|
4
|
+
(C) Copyright 1999-2008 by Masahiro TANAKA
|
5
|
+
|
6
|
+
This program is free software.
|
7
|
+
You can distribute/modify this program
|
8
|
+
under the same terms as Ruby itself.
|
9
|
+
NO WARRANTY.
|
10
|
+
*/
|
11
|
+
#ifndef NARRAY_H
|
12
|
+
#define NARRAY_H
|
13
|
+
|
14
|
+
#include <math.h>
|
15
|
+
|
16
|
+
#include "narray_config.h"
|
17
|
+
|
18
|
+
#ifdef HAVE_STDINT_H
|
19
|
+
# include <stdint.h>
|
20
|
+
#endif
|
21
|
+
|
22
|
+
#ifdef HAVE_SYS_TYPES_H
|
23
|
+
# include <sys/types.h>
|
24
|
+
#endif
|
25
|
+
|
26
|
+
#define NARRAY_VERSION "0.5.9p7"
|
27
|
+
#define NARRAY_VERSION_CODE 597
|
28
|
+
|
29
|
+
/*
|
30
|
+
Data types used in NArray :
|
31
|
+
Please modify these types if your system has any different type.
|
32
|
+
*/
|
33
|
+
|
34
|
+
|
35
|
+
/* NA_BYTE : unsigned 8-bit integer */
|
36
|
+
#ifndef HAVE_U_INT8_T
|
37
|
+
# ifdef HAVE_UINT8_T
|
38
|
+
typedef uint8_t u_int8_t;
|
39
|
+
# else
|
40
|
+
typedef unsigned char u_int8_t;
|
41
|
+
# endif
|
42
|
+
#endif
|
43
|
+
|
44
|
+
#ifndef HAVE_INT16_T
|
45
|
+
# if SIZEOF_SHORT == 2
|
46
|
+
typedef short int16_t; /* NA_SINT */
|
47
|
+
# else
|
48
|
+
---->> Please define int16_t manually because sizeof(short) != 2. <<----
|
49
|
+
# endif
|
50
|
+
#endif /* HAVE_INT16_T */
|
51
|
+
|
52
|
+
#ifndef HAVE_INT32_T
|
53
|
+
# if SIZEOF_LONG == 4
|
54
|
+
typedef long int32_t; /* NA_LINT */
|
55
|
+
# else
|
56
|
+
# if SIZEOF_INT == 4
|
57
|
+
typedef int int32_t; /* NA_LINT */
|
58
|
+
# else
|
59
|
+
---->> Please define int32_t manually because sizeof(long) != 4. <<----
|
60
|
+
# endif
|
61
|
+
# endif
|
62
|
+
#endif /* HAVE_INT32_T */
|
63
|
+
|
64
|
+
/* unsigned 32-bit integer */
|
65
|
+
#ifndef HAVE_U_INT32_T
|
66
|
+
# ifdef HAVE_UINT32_T
|
67
|
+
typedef uint32_t u_int32_t;
|
68
|
+
# else
|
69
|
+
# if SIZEOF_LONG == 4
|
70
|
+
typedef unsigned long u_int32_t;
|
71
|
+
# else
|
72
|
+
# if SIZEOF_INT == 4
|
73
|
+
typedef unsigned int u_int32_t;
|
74
|
+
# else
|
75
|
+
---->> Please define u_int32_t manually because sizeof(long) != 4. <<----
|
76
|
+
# endif
|
77
|
+
# endif
|
78
|
+
# endif
|
79
|
+
#endif /* HAVE_U_INT32_T */
|
80
|
+
|
81
|
+
typedef struct { float r,i; } scomplex;
|
82
|
+
typedef struct { double r,i; } dcomplex;
|
83
|
+
|
84
|
+
enum NArray_Types {
|
85
|
+
NA_NONE,
|
86
|
+
NA_BYTE, /* 1 */
|
87
|
+
NA_SINT, /* 2 */
|
88
|
+
NA_LINT, /* 3 */
|
89
|
+
NA_SFLOAT, /* 4 */
|
90
|
+
NA_DFLOAT, /* 5 */
|
91
|
+
NA_SCOMPLEX, /* 6 */
|
92
|
+
NA_DCOMPLEX, /* 7 */
|
93
|
+
NA_ROBJ, /* 8 */
|
94
|
+
NA_NTYPES /* 9 */
|
95
|
+
};
|
96
|
+
|
97
|
+
/* struct for Numerical Array */
|
98
|
+
struct NARRAY {
|
99
|
+
int rank; /* # of dimension */
|
100
|
+
int total; /* # of total element */
|
101
|
+
int type; /* data type */
|
102
|
+
int *shape;
|
103
|
+
char *ptr; /* pointer to data */
|
104
|
+
VALUE ref; /* NArray object wrapping this structure */
|
105
|
+
};
|
106
|
+
|
107
|
+
#ifndef NARRAY_C
|
108
|
+
extern VALUE cNArray;
|
109
|
+
|
110
|
+
extern const int na_sizeof[NA_NTYPES+1];
|
111
|
+
#endif
|
112
|
+
|
113
|
+
#define NA_MAX_RANK 15
|
114
|
+
|
115
|
+
#define GetNArray(obj,var) Data_Get_Struct(obj, struct NARRAY, var)
|
116
|
+
#define IsNArray(obj) (rb_obj_is_kind_of(obj,cNArray)==Qtrue)
|
117
|
+
|
118
|
+
#define NA_PTR(a,p) ((a)->ptr+(p)*na_sizeof[(a)->type])
|
119
|
+
#define NA_STRUCT(val) ((struct NARRAY*)DATA_PTR(val))
|
120
|
+
#define NA_PTR_TYPE(val,type) (type)(((struct NARRAY*)DATA_PTR(val))->ptr)
|
121
|
+
#define NA_RANK(val) (((struct NARRAY*)DATA_PTR(val))->rank)
|
122
|
+
#define NA_TYPE(val) (((struct NARRAY*)DATA_PTR(val))->type)
|
123
|
+
#define NA_TOTAL(val) (((struct NARRAY*)DATA_PTR(val))->total)
|
124
|
+
#define NA_SHAPE0(val) (((struct NARRAY*)DATA_PTR(val))->shape[0])
|
125
|
+
#define NA_SHAPE1(val) (((struct NARRAY*)DATA_PTR(val))->shape[1])
|
126
|
+
|
127
|
+
#define NA_IsNArray(obj) \
|
128
|
+
(rb_obj_is_kind_of(obj,cNArray)==Qtrue)
|
129
|
+
#define NA_IsArray(obj) \
|
130
|
+
(TYPE(obj)==T_ARRAY || rb_obj_is_kind_of(obj,cNArray)==Qtrue)
|
131
|
+
#define NA_IsROBJ(d) ((d)->type==NA_ROBJ)
|
132
|
+
#define NA_IsINTEGER(a) \
|
133
|
+
((a)->type==NA_BYTE || (a)->type==NA_SINT || (a)->type==NA_LINT )
|
134
|
+
#define NA_IsCOMPLEX(a) \
|
135
|
+
((a)->type==NA_SCOMPLEX || (a)->type==NA_DCOMPLEX)
|
136
|
+
#define NA_MAX(a,b) (((a)>(b))?(a):(b))
|
137
|
+
#define NA_SWAP(a,b,tmp) {(tmp)=(a);(a)=(b);(b)=(tmp);}
|
138
|
+
|
139
|
+
#define na_class_dim(klass) NUM2INT(rb_const_get(klass, na_id_class_dim))
|
140
|
+
|
141
|
+
#define NUM2REAL(v) NUM2DBL( rb_funcall((v),na_id_real,0) )
|
142
|
+
#define NUM2IMAG(v) NUM2DBL( rb_funcall((v),na_id_imag,0) )
|
143
|
+
|
144
|
+
#define NA_ALLOC_SLICE(slc,nc,shp,np) \
|
145
|
+
{ slc = (struct slice*)xmalloc( sizeof(struct slice)*(nc) + \
|
146
|
+
sizeof(int)*(np) );\
|
147
|
+
shp = (int*)&( (slc)[nc] ); }
|
148
|
+
|
149
|
+
|
150
|
+
/* Function Prototypes */
|
151
|
+
|
152
|
+
/* narray.c */
|
153
|
+
VALUE na_make_object(int type, int rank, int *shape, VALUE klass);
|
154
|
+
VALUE na_make_scalar(VALUE obj, int type);
|
155
|
+
VALUE na_make_empty(int type, VALUE klass);
|
156
|
+
int na_get_typecode(VALUE v);
|
157
|
+
void na_clear_data(struct NARRAY *ary);
|
158
|
+
VALUE na_clone(VALUE self);
|
159
|
+
VALUE na_fill(VALUE self, volatile VALUE obj);
|
160
|
+
void na_copy_nary(struct NARRAY *dst, struct NARRAY *src);
|
161
|
+
|
162
|
+
/* na_array.c */
|
163
|
+
VALUE na_to_array(VALUE obj);
|
164
|
+
VALUE na_make_inspect(VALUE self);
|
165
|
+
VALUE na_ary_to_nary(VALUE ary, VALUE klass);
|
166
|
+
int na_object_type(VALUE v);
|
167
|
+
|
168
|
+
VALUE na_cast_object(VALUE obj, int type);
|
169
|
+
VALUE na_cast_unless_narray(VALUE obj, int type);
|
170
|
+
VALUE na_cast_unless_array(VALUE obj, int type);
|
171
|
+
VALUE na_upcast_object(VALUE obj, int type);
|
172
|
+
VALUE na_dup_w_type(VALUE obj, int type);
|
173
|
+
VALUE na_change_type(VALUE obj, int type);
|
174
|
+
VALUE na_upcast_type(VALUE obj, int type);
|
175
|
+
VALUE na_to_narray(VALUE obj);
|
176
|
+
|
177
|
+
/* na_index.c */
|
178
|
+
VALUE na_aset(int argc, VALUE *argv, VALUE self);
|
179
|
+
VALUE na_aref(int argc, VALUE *argv, VALUE self);
|
180
|
+
VALUE na_slice(int argc, VALUE *argv, VALUE self);
|
181
|
+
VALUE na_count_true(VALUE self);
|
182
|
+
VALUE na_count_false(VALUE self);
|
183
|
+
VALUE na_aref_mask(VALUE self, VALUE mask);
|
184
|
+
void na_aset_mask(VALUE self, VALUE mask, VALUE v);
|
185
|
+
|
186
|
+
#endif /* ifndef NARRAY_H */
|
data/lib/narray.so
ADDED
Binary file
|
data/lib/narray_config.h
ADDED
data/lib/narray_ext.rb
ADDED
@@ -0,0 +1,245 @@
|
|
1
|
+
# Numerical Array Extention for Ruby
|
2
|
+
# (C) Copyright 2000-2008 by Masahiro TANAKA
|
3
|
+
#
|
4
|
+
# This program is free software.
|
5
|
+
# You can distribute/modify this program
|
6
|
+
# under the same terms as Ruby itself.
|
7
|
+
# NO WARRANTY.
|
8
|
+
#
|
9
|
+
class NArray
|
10
|
+
|
11
|
+
def integer?
|
12
|
+
self.typecode==NArray::BYTE ||
|
13
|
+
self.typecode==NArray::SINT ||
|
14
|
+
self.typecode==NArray::LINT
|
15
|
+
end
|
16
|
+
def complex?
|
17
|
+
self.typecode==NArray::DCOMPLEX ||
|
18
|
+
self.typecode==NArray::SCOMPLEX
|
19
|
+
end
|
20
|
+
|
21
|
+
def all?
|
22
|
+
where.size == size
|
23
|
+
end
|
24
|
+
|
25
|
+
def any?
|
26
|
+
where.size > 0
|
27
|
+
end
|
28
|
+
|
29
|
+
def none?
|
30
|
+
where.size == 0
|
31
|
+
end
|
32
|
+
|
33
|
+
def ==(other)
|
34
|
+
if other.kind_of?(NArray)
|
35
|
+
(shape == other.shape) && eq(other).all?
|
36
|
+
else
|
37
|
+
false
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def rank_total(*ranks)
|
42
|
+
if ranks.size>0
|
43
|
+
idx = []
|
44
|
+
ranks.each{|i| idx.push(*i)}
|
45
|
+
# ranks is expected to be, e.g., [1, 3..5, 7]
|
46
|
+
a = self.shape
|
47
|
+
n = 1
|
48
|
+
idx.each{|i| n *= a[i]}
|
49
|
+
n
|
50
|
+
else
|
51
|
+
self.total
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Statistics
|
56
|
+
def mean(*ranks)
|
57
|
+
if integer?
|
58
|
+
a = self.to_type(NArray::DFLOAT)
|
59
|
+
else
|
60
|
+
a = self
|
61
|
+
end
|
62
|
+
a = NArray.ref(a)
|
63
|
+
a.sum(*ranks) / (rank_total(*ranks))
|
64
|
+
end
|
65
|
+
|
66
|
+
def stddev(*ranks)
|
67
|
+
if integer?
|
68
|
+
a = self.to_type(NArray::DFLOAT)
|
69
|
+
else
|
70
|
+
a = self
|
71
|
+
end
|
72
|
+
a = NArray.ref(a)
|
73
|
+
n = rank_total(*ranks)
|
74
|
+
if complex?
|
75
|
+
NMath::sqrt( (( a-a.accum(*ranks).div!(n) ).abs**2).sum(*ranks)/(n-1) )
|
76
|
+
else
|
77
|
+
NMath::sqrt( (( a-a.accum(*ranks).div!(n) )**2).sum(*ranks)/(n-1) )
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def rms(*ranks)
|
82
|
+
if integer?
|
83
|
+
a = self.to_type(NArray::DFLOAT)
|
84
|
+
else
|
85
|
+
a = self
|
86
|
+
end
|
87
|
+
a = NArray.ref(a)
|
88
|
+
n = rank_total(*ranks)
|
89
|
+
if complex?
|
90
|
+
NMath::sqrt( (a.abs**2).sum(*ranks)/n )
|
91
|
+
else
|
92
|
+
NMath::sqrt( (a**2).sum(*ranks)/n )
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def rmsdev(*ranks)
|
97
|
+
if integer?
|
98
|
+
a = self.to_type(NArray::DFLOAT)
|
99
|
+
else
|
100
|
+
a = self
|
101
|
+
end
|
102
|
+
a = NArray.ref(a)
|
103
|
+
n = rank_total(*ranks)
|
104
|
+
if complex?
|
105
|
+
NMath::sqrt( (( a-a.accum(*ranks).div!(n) ).abs**2).sum(*ranks)/n )
|
106
|
+
else
|
107
|
+
NMath::sqrt( (( a-a.accum(*ranks).div!(n) )**2).sum(*ranks)/n )
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def median(rank=nil)
|
112
|
+
shape = self.shape
|
113
|
+
rank = shape.size-1 if rank==nil
|
114
|
+
s = sort(rank).reshape!(true,*shape[rank+1..-1])
|
115
|
+
n = s.shape[0]
|
116
|
+
if n%2==1
|
117
|
+
s[n/2,false]
|
118
|
+
else
|
119
|
+
s[n/2-1..n/2,false].sum(0)/2
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
|
124
|
+
# Normal distributed random number; valid for floating point types
|
125
|
+
def randomn
|
126
|
+
size = self.size
|
127
|
+
case type = self.typecode
|
128
|
+
when COMPLEX; type=FLOAT
|
129
|
+
when SCOMPLEX; type=SFLOAT
|
130
|
+
when FLOAT
|
131
|
+
when SFLOAT
|
132
|
+
else
|
133
|
+
raise TypeError, "NArray type must be (S)FLOAT or (S)COMPLEX."
|
134
|
+
end
|
135
|
+
rr = NArray.new(type,size)
|
136
|
+
xx = NArray.new(type,size)
|
137
|
+
i = 0
|
138
|
+
while i < size
|
139
|
+
n = size-i
|
140
|
+
m = ((n+Math::sqrt(n))*1.27).to_i
|
141
|
+
x = NArray.new(type,m).random!(1) * 2 - 1
|
142
|
+
y = NArray.new(type,m).random!(1) * 2 - 1
|
143
|
+
r = x**2 + y**2
|
144
|
+
idx = (r<1).where
|
145
|
+
idx = idx[0...n] if idx.size > n
|
146
|
+
if idx.size>0
|
147
|
+
rr[i] = r[idx]
|
148
|
+
xx[i] = x[idx]
|
149
|
+
i += idx.size
|
150
|
+
end
|
151
|
+
end
|
152
|
+
# Box-Muller transform
|
153
|
+
rr = ( xx * NMath::sqrt( -2 * NMath::log(rr) / rr ) )
|
154
|
+
# finish
|
155
|
+
rr.reshape!(*self.shape) if self.rank > 1
|
156
|
+
rr = rr.to_type(self.typecode) if type!=self.typecode
|
157
|
+
if RUBY_VERSION < "1.8.0"
|
158
|
+
self.type.refer(rr)
|
159
|
+
else
|
160
|
+
self.class.refer(rr)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
alias randomn! randomn
|
164
|
+
|
165
|
+
#SFloatOne = NArray.sfloat(1).fill!(1)
|
166
|
+
end
|
167
|
+
|
168
|
+
|
169
|
+
module NMath
|
170
|
+
PI = Math::PI
|
171
|
+
E = Math::E
|
172
|
+
|
173
|
+
def recip x
|
174
|
+
1/x.to_f
|
175
|
+
end
|
176
|
+
|
177
|
+
# Trigonometric function
|
178
|
+
def csc x
|
179
|
+
1/sin(x)
|
180
|
+
end
|
181
|
+
def csch x
|
182
|
+
1/sinh(x)
|
183
|
+
end
|
184
|
+
def acsc x
|
185
|
+
asin(1/x.to_f)
|
186
|
+
end
|
187
|
+
def acsch x
|
188
|
+
asinh(1/x.to_f)
|
189
|
+
end
|
190
|
+
|
191
|
+
def sec x
|
192
|
+
1/cos(x)
|
193
|
+
end
|
194
|
+
def sech x
|
195
|
+
1/cosh(x)
|
196
|
+
end
|
197
|
+
def asec x
|
198
|
+
acos(1/x.to_f)
|
199
|
+
end
|
200
|
+
def asech x
|
201
|
+
acosh(1/x.to_f)
|
202
|
+
end
|
203
|
+
|
204
|
+
def cot x
|
205
|
+
1/tan(x)
|
206
|
+
end
|
207
|
+
def coth x
|
208
|
+
1/atanh(x)
|
209
|
+
end
|
210
|
+
def acot x
|
211
|
+
atan(1/x.to_f)
|
212
|
+
end
|
213
|
+
def acoth x
|
214
|
+
atanh(1/x.to_f)
|
215
|
+
end
|
216
|
+
|
217
|
+
# Statistics
|
218
|
+
def covariance(x,y,*ranks)
|
219
|
+
x = NArray.to_na(x) unless x.kind_of?(NArray)
|
220
|
+
x = x.to_type(NArray::DFLOAT) if x.integer?
|
221
|
+
y = NArray.to_na(y) unless y.kind_of?(NArray)
|
222
|
+
y = y.to_type(NArray::DFLOAT) if y.integer?
|
223
|
+
n = x.rank_total(*ranks)
|
224
|
+
xm = x.accum(*ranks).div!(n)
|
225
|
+
ym = y.accum(*ranks).div!(n)
|
226
|
+
((x-xm)*(y-ym)).sum(*ranks) / (n-1)
|
227
|
+
end
|
228
|
+
|
229
|
+
module_function :csc,:sec,:cot,:csch,:sech,:coth
|
230
|
+
module_function :acsc,:asec,:acot,:acsch,:asech,:acoth
|
231
|
+
module_function :covariance
|
232
|
+
end
|
233
|
+
|
234
|
+
|
235
|
+
module FFTW
|
236
|
+
def convol(a1,a2)
|
237
|
+
n1x,n1y = a1.shape
|
238
|
+
n2x,n2y = a2.shape
|
239
|
+
raise "arrays must have same shape" if n1x!=n2x || n1y!=n2y
|
240
|
+
(FFTW.fftw( FFTW.fftw(a1,-1) * FFTW.fftw(a2,-1), 1).real) / (n1x*n1y)
|
241
|
+
end
|
242
|
+
module_function :convol
|
243
|
+
end
|
244
|
+
|
245
|
+
require 'nmatrix'
|
data/lib/nmatrix.rb
ADDED
@@ -0,0 +1,244 @@
|
|
1
|
+
# Numerical Array Extention for Ruby
|
2
|
+
# (C) Copyright 2000-2003 by Masahiro TANAKA
|
3
|
+
#
|
4
|
+
|
5
|
+
#
|
6
|
+
# ------ NMatrix ------
|
7
|
+
#
|
8
|
+
class NMatrix < NArray
|
9
|
+
CLASS_DIMENSION = 2
|
10
|
+
|
11
|
+
def +(other)
|
12
|
+
case other
|
13
|
+
when NMatrix
|
14
|
+
return super(NArray.refer(other))
|
15
|
+
when NArray
|
16
|
+
unless other.instance_of?(NArray)
|
17
|
+
return other.coerce_rev( self, :+ )
|
18
|
+
end
|
19
|
+
end
|
20
|
+
raise TypeError,"Illegal operation: NMatrix + %s" % other.class
|
21
|
+
end
|
22
|
+
|
23
|
+
def -(other)
|
24
|
+
case other
|
25
|
+
when NMatrix
|
26
|
+
return super(NArray.refer(other))
|
27
|
+
when NArray
|
28
|
+
unless other.instance_of?(NArray)
|
29
|
+
return other.coerce_rev( self, :- )
|
30
|
+
end
|
31
|
+
end
|
32
|
+
raise TypeError,"Illegal operation: NMatrix - %s" % other.class
|
33
|
+
end
|
34
|
+
|
35
|
+
def *(other)
|
36
|
+
case other
|
37
|
+
when NMatrix
|
38
|
+
NMatrix.mul_add( NArray.refer(self).newdim!(0),other.newdim(2), 1 )
|
39
|
+
#NMatrix.mul_add( NArray.refer(self).newdim!(0),
|
40
|
+
# other.transpose(1,0).newdim!(2), 0 )
|
41
|
+
when NVector
|
42
|
+
NVector.mul_add( NArray.refer(self), other.newdim(1), 0 )
|
43
|
+
when NArray
|
44
|
+
if other.instance_of?(NArray)
|
45
|
+
NMatrix.mul( NArray.refer(self), other.newdim(0,0) )
|
46
|
+
else
|
47
|
+
other.coerce_rev( self, :* )
|
48
|
+
end
|
49
|
+
when Numeric
|
50
|
+
super
|
51
|
+
#NMatrix.mul( NArray.refer(self), other )
|
52
|
+
when Array
|
53
|
+
NMatrix.mul( self, NArray[*other].newdim!(0,0) )
|
54
|
+
else
|
55
|
+
raise TypeError,"Illegal operation: NMatrix * %s" % other.class
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def /(other)
|
60
|
+
case other
|
61
|
+
when NMatrix
|
62
|
+
other.lu.solve(self)
|
63
|
+
when NVector
|
64
|
+
raise TypeError,"Illegal operation: NMatrix / %s" % other.class
|
65
|
+
when NArray
|
66
|
+
if other.instance_of?(NArray)
|
67
|
+
NMatrix.div( NArray.refer(self), other.newdim(0,0) )
|
68
|
+
else
|
69
|
+
other.coerce_rev( self, :/ )
|
70
|
+
end
|
71
|
+
when Numeric
|
72
|
+
NMatrix.div( NArray.refer(self), other )
|
73
|
+
when Array
|
74
|
+
NMatrix.div( self, NArray[*other].newdim!(0,0) )
|
75
|
+
else
|
76
|
+
raise TypeError,"Illegal operation: NMatrix / %s" % other.class
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def **(n)
|
81
|
+
case n
|
82
|
+
when Integer
|
83
|
+
if n==0
|
84
|
+
return 1.0
|
85
|
+
elsif n<0
|
86
|
+
m = self.inverse
|
87
|
+
n = -n
|
88
|
+
else
|
89
|
+
m = self
|
90
|
+
end
|
91
|
+
(2..n).each{ m *= self }
|
92
|
+
m
|
93
|
+
else
|
94
|
+
raise TypeError,"Illegal operation: NMatrix ** %s" % other.class
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def coerce_rev(other,id)
|
99
|
+
case id
|
100
|
+
when :*
|
101
|
+
if other.instance_of?(NArray)
|
102
|
+
return NMatrix.mul( other.newdim(0,0), self )
|
103
|
+
end
|
104
|
+
if other.instance_of?(NArrayScalar)
|
105
|
+
return NMatrix.mul( other.newdim(0), self )
|
106
|
+
end
|
107
|
+
when :/
|
108
|
+
if other.instance_of?(NArray)
|
109
|
+
return NMatrix.mul( other.newdim(0,0), self.inverse )
|
110
|
+
end
|
111
|
+
if other.instance_of?(NArrayScalar)
|
112
|
+
return NMatrix.mul( other.newdim(0), self.inverse )
|
113
|
+
end
|
114
|
+
end
|
115
|
+
raise TypeError,"Illegal operation: %s %s NMatrix" %
|
116
|
+
[other.class, id.id2name]
|
117
|
+
end
|
118
|
+
|
119
|
+
def inverse
|
120
|
+
self.lu.solve( NMatrix.new(self.typecode, *self.shape).fill!(0).unit )
|
121
|
+
end
|
122
|
+
|
123
|
+
def transpose(*arg)
|
124
|
+
if arg.size==0
|
125
|
+
super(1,0)
|
126
|
+
else
|
127
|
+
super
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
def diagonal!(val=1)
|
132
|
+
shp = self.shape
|
133
|
+
idx = NArray.int(shp[0..1].min).indgen! * (shp[0]+1)
|
134
|
+
ref = reshape(shp[0]*shp[1],true)
|
135
|
+
val = NArray.to_na(val)
|
136
|
+
raise ArgumentError, "must be 1-d array" if val.dim!=1
|
137
|
+
ref[idx,true] = val.newdim!(-1)
|
138
|
+
self
|
139
|
+
end
|
140
|
+
|
141
|
+
def diagonal(val)
|
142
|
+
self.dup.diagonal!(val)
|
143
|
+
end
|
144
|
+
|
145
|
+
def unit
|
146
|
+
diagonal!
|
147
|
+
end
|
148
|
+
alias identity unit
|
149
|
+
alias I unit
|
150
|
+
|
151
|
+
end # class NMatrix
|
152
|
+
|
153
|
+
|
154
|
+
#
|
155
|
+
# ------ NVector ------
|
156
|
+
#
|
157
|
+
class NVector < NArray
|
158
|
+
CLASS_DIMENSION = 1
|
159
|
+
|
160
|
+
def +(other)
|
161
|
+
case other
|
162
|
+
when NVector
|
163
|
+
return super(NArray.refer(other))
|
164
|
+
when NArray
|
165
|
+
unless other.instance_of?(NArray)
|
166
|
+
return other.coerce_rev( self, :+ )
|
167
|
+
end
|
168
|
+
end
|
169
|
+
raise TypeError,"Illegal operation: NVector + %s" % other.class
|
170
|
+
end
|
171
|
+
|
172
|
+
def -(other)
|
173
|
+
case other
|
174
|
+
when NVector
|
175
|
+
return super(NArray.refer(other))
|
176
|
+
when NArray
|
177
|
+
unless other.instance_of?(NArray)
|
178
|
+
return other.coerce_rev( self, :- )
|
179
|
+
end
|
180
|
+
end
|
181
|
+
raise TypeError,"Illegal operation: NVector - %s" % other.class
|
182
|
+
end
|
183
|
+
|
184
|
+
def *(other)
|
185
|
+
case other
|
186
|
+
when NMatrix
|
187
|
+
NVector.mul_add( NArray.refer(self).newdim!(0), other, 1 )
|
188
|
+
when NVector
|
189
|
+
NArray.mul_add( NArray.refer(self), other, 0 ) # inner product
|
190
|
+
when NArray
|
191
|
+
if other.instance_of?(NArray)
|
192
|
+
NVector.mul( NArray.refer(self), other.newdim(0) )
|
193
|
+
else
|
194
|
+
other.coerce_rev( self, :* )
|
195
|
+
end
|
196
|
+
when Numeric
|
197
|
+
NVector.mul( NArray.refer(self), other )
|
198
|
+
else
|
199
|
+
raise TypeError,"Illegal operation: NVector * %s" % other.class
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
def /(other)
|
204
|
+
case other
|
205
|
+
when NMatrix
|
206
|
+
other.lu.solve(self)
|
207
|
+
when NVector
|
208
|
+
raise TypeError,"Illegal operation: NVector / %s" % other.class
|
209
|
+
when NArray
|
210
|
+
if other.instance_of?(NArray)
|
211
|
+
NVector.div( NArray.refer(self), other.newdim(0) )
|
212
|
+
else
|
213
|
+
other.coerce_rev( self, :/ )
|
214
|
+
end
|
215
|
+
when Numeric
|
216
|
+
NVector.div( NArray.refer(self), other )
|
217
|
+
else
|
218
|
+
raise TypeError,"Illegal operation: NVector / %s" % other.class
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
def **(n)
|
223
|
+
if n==2
|
224
|
+
self*self
|
225
|
+
else
|
226
|
+
raise ArgumentError,"Only v**2 is implemented"
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
def coerce_rev(other,id)
|
231
|
+
case id
|
232
|
+
when :*
|
233
|
+
if other.instance_of?(NArray)
|
234
|
+
return NVector.mul( other.newdim(0), self )
|
235
|
+
end
|
236
|
+
if other.instance_of?(NArrayScalar)
|
237
|
+
return NVector.mul( other, self )
|
238
|
+
end
|
239
|
+
end
|
240
|
+
raise TypeError,"Illegal operation: %s %s NVector" %
|
241
|
+
[other.class, id.id2name]
|
242
|
+
end
|
243
|
+
|
244
|
+
end # class NVector
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: narray-ruby19
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.9.7
|
5
|
+
platform: x86-mingw32
|
6
|
+
authors:
|
7
|
+
- Masahiro Tanaka
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain:
|
11
|
+
date: 2009-08-15 00:00:00 +09:00
|
12
|
+
default_executable:
|
13
|
+
dependencies: []
|
14
|
+
|
15
|
+
description:
|
16
|
+
email: masa16.tanaka@gmail.com
|
17
|
+
executables: []
|
18
|
+
|
19
|
+
extensions: []
|
20
|
+
|
21
|
+
extra_rdoc_files: []
|
22
|
+
|
23
|
+
files:
|
24
|
+
- lib/narray_ext.rb
|
25
|
+
- lib/nmatrix.rb
|
26
|
+
- lib/narray.so
|
27
|
+
- lib/libnarray.a
|
28
|
+
- lib/narray.h
|
29
|
+
- lib/narray_config.h
|
30
|
+
has_rdoc: true
|
31
|
+
homepage: http://narray.rubyforge.org/
|
32
|
+
licenses: []
|
33
|
+
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.9.0
|
44
|
+
version:
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
requirements: []
|
52
|
+
|
53
|
+
rubyforge_project: narray
|
54
|
+
rubygems_version: 1.3.4
|
55
|
+
signing_key:
|
56
|
+
specification_version: 1
|
57
|
+
summary: N-dimensional Numerical Array class for Ruby
|
58
|
+
test_files: []
|
59
|
+
|