Class: NumRu::NMatrix
- Inherits:
-
NArray
show all
- Defined in:
- lib/numru/nmatrix.rb,
ext/numru/narray/na_linalg.c
Overview
Constant Summary
collapse
- CLASS_DIMENSION =
2
Constants inherited
from NArray
NumRu::NArray::BYTE, NumRu::NArray::COMPLEX, NumRu::NArray::DCOMPLEX, NumRu::NArray::DFLOAT, NumRu::NArray::ENDIAN, NumRu::NArray::FLOAT, NumRu::NArray::INT, NumRu::NArray::LINT, NumRu::NArray::LLINT, NumRu::NArray::NARRAY_VERSION, NumRu::NArray::NONE, NumRu::NArray::OBJECT, NumRu::NArray::ROBJ, NumRu::NArray::SCOMPLEX, NumRu::NArray::SFLOAT, NumRu::NArray::SINT, NumRu::NArray::SUPPORT_BIGMEM
Instance Method Summary
collapse
Methods inherited from NArray
#%, #&, #-@, #<=>, #==, [], #[], #[]=, #^, #abs, #accum, #add!, #all?, #and, #angle, #any?, byte, cast, #ceil, #clone, #coerce, #collect, #collect!, complex, #complex?, #conj, #conj!, #count_false, #count_true, #cumprod, #cumprod!, #cumsum, #cumsum!, dcomplex, #delete_at, dfloat, div, #div!, #each, #element_size, #empty?, #eq, #eql?, #fill!, #flatten, #flatten!, float, #floor, #ge, #gt, #hash, #hton, #htov, #im, #imag, #imag=, #indgen!, #inspect, int, #integer?, #le, lint, llint, #lt, #mask, #max, #mean, #median, #min, #mod!, mul, #mul!, #mul_accum, mul_add, #mul_add, #ne, new, #newdim, #newdim!, #none?, #not, object, #or, #original, #prod, #random, #random!, #randomn, #randomn!, #rank, #rank_total, #real, #recip, ref, refer, #refer, #reshape, #reshape!, #reverse, #rms, #rmsdev, #rot90, #round, #sbt!, scomplex, sfloat, #shape, sint, #size, #slice, #sort, #sort!, #sort_index, srand, #stddev, #sum, #swap_byte, #to_a, #to_binary, #to_f, #to_i, to_na, to_narray, #to_s, #to_s_refer, #to_string, #to_type, #to_type_as_binary, #typecode, #where, #where2, #xor, #|, #~
Instance Method Details
#*(other) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/numru/nmatrix.rb', line 35
def *(other)
case other
when NumRu::NMatrix
NumRu::NMatrix.mul_add( NumRu::NArray.refer(self).newdim!(0),other.newdim(2), 1 )
when NumRu::NVector
NumRu::NVector.mul_add( NumRu::NArray.refer(self), other.newdim(1), 0 )
when NumRu::NArray
if other.instance_of?(NumRu::NArray)
NumRu::NMatrix.mul( NumRu::NArray.refer(self), other.newdim(0,0) )
else
other.coerce_rev( self, :* )
end
when Numeric
super
when Array
NumRu::NMatrix.mul( self, NumRu::NArray[*other].newdim!(0,0) )
else
raise TypeError,"Illegal operation: NumRu::NMatrix * %s" % other.class
end
end
|
#**(n) ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/numru/nmatrix.rb', line 80
def **(n)
case n
when Integer
if n==0
return 1.0
elsif n<0
m = self.inverse
n = -n
else
m = self
end
(2..n).each{ m *= self }
m
else
raise TypeError,"Illegal operation: NumRu::NMatrix ** %s" % other.class
end
end
|
#+(other) ⇒ Object
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/numru/nmatrix.rb', line 11
def +(other)
case other
when NumRu::NMatrix
return super(NumRu::NArray.refer(other))
when NumRu::NArray
unless other.instance_of?(NumRu::NArray)
return other.coerce_rev( self, :+ )
end
end
raise TypeError,"Illegal operation: NumRu::NMatrix + %s" % other.class
end
|
#-(other) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/numru/nmatrix.rb', line 23
def -(other)
case other
when NumRu::NMatrix
return super(NumRu::NArray.refer(other))
when NumRu::NArray
unless other.instance_of?(NumRu::NArray)
return other.coerce_rev( self, :- )
end
end
raise TypeError,"Illegal operation: NumRu::NMatrix - %s" % other.class
end
|
#coerce_rev(other, id) ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/numru/nmatrix.rb', line 98
def coerce_rev(other,id)
case id
when :*
if other.instance_of?(NumRu::NArray)
return NumRu::NMatrix.mul( other.newdim(0,0), self )
end
if other.instance_of?(NumRu::NArrayScalar)
return NumRu::NMatrix.mul( other.newdim(0), self )
end
when :/
if other.instance_of?(NumRu::NArray)
return NumRu::NMatrix.mul( other.newdim(0,0), self.inverse )
end
if other.instance_of?(NumRu::NArrayScalar)
return NumRu::NMatrix.mul( other.newdim(0), self.inverse )
end
end
raise TypeError,"Illegal operation: %s %s NumRu::NMatrix" %
[other.class, id.id2name]
end
|
#diagonal(val) ⇒ Object
145
146
147
|
# File 'lib/numru/nmatrix.rb', line 145
def diagonal(val)
self.dup.diagonal!(val)
end
|
#diagonal!(val = 1) ⇒ Object
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/numru/nmatrix.rb', line 131
def diagonal!(val=1)
shp = self.shape
idx = NumRu::NArray.int(shp[0..1].min).indgen! * (shp[0]+1)
ref = reshape(shp[0]*shp[1],true)
if val.kind_of?(Numeric)
ref[idx,true] = val
else
val = NumRu::NArray.to_na(val)
raise ArgumentError, "must be 1-d array" if val.dim!=1
ref[idx,true] = val.newdim!(-1)
end
self
end
|
#inverse ⇒ Object
119
120
121
|
# File 'lib/numru/nmatrix.rb', line 119
def inverse
self.lu.solve( NumRu::NMatrix.new(self.typecode, *self.shape).fill!(0).unit )
end
|
#lu_fact ⇒ Object
Also known as:
lu
336
337
338
339
340
|
# File 'ext/numru/narray/na_linalg.c', line 336
static VALUE
na_lu_fact(VALUE self)
{
return na_lu_fact_bang( na_clone(self) );
}
|
#lu_fact! ⇒ Object
Also known as:
lu!
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
# File 'ext/numru/narray/na_linalg.c', line 291
static VALUE
na_lu_fact_bang(VALUE self)
{
na_shape_t i, total, n;
int sz, stat;
struct NARRAY *ary;
VALUE piv;
char *ptr, *idx;
void (*func)();
GetNArray(self,ary);
/* shape & dimension check */
if (ary->rank<2)
rb_raise(rb_eTypeError,"dim(=%i) < 2", ary->rank);
n = ary->shape[0];
if (n != ary->shape[1])
rb_raise(rb_eTypeError,"not square matrix");
total=1;
for (i=2; i<ary->rank; ++i)
total *= ary->shape[i];
piv = na_make_object(NA_LINT, ary->rank-1, ary->shape+1, cNVector);
/* prepare pivot index */
func = IndGenFuncs[NA_LINT];
sz = na_sizeof[NA_LINT];
ptr = idx = ((struct NARRAY *)DATA_PTR(piv))->ptr;
for (i=0; i<total; ++i) {
func(n,ptr,sz,0,1);
ptr += n*sz;
}
stat = na_lu_fact_func(total, ary->ptr, idx, ary->shape, ary->type);
if (stat!=0)
rb_raise(rb_eZeroDivError,"singular matrix, status=%i",stat);
return rb_funcall(cNMatrixLU,na_id_new,2,self,piv);
}
|
#transpose(*arg) ⇒ Object
123
124
125
126
127
128
129
|
# File 'lib/numru/nmatrix.rb', line 123
def transpose(*arg)
if arg.size==0
super(1,0)
else
super
end
end
|
#unit ⇒ Object
Also known as:
identity, I
149
150
151
|
# File 'lib/numru/nmatrix.rb', line 149
def unit
diagonal!
end
|