Class: NArray

Inherits:
Object
  • Object
show all
Defined in:
lib/narray_ext.rb

Overview

Numerical Array Extention for Ruby

  (C) Copyright 2000-2008 by Masahiro TANAKA

This program is free software.
You can distribute/modify this program
under the same terms as Ruby itself.
NO WARRANTY.

Direct Known Subclasses

NMatrix, NVector

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/narray_ext.rb', line 33

def ==(other)
  if other.kind_of?(NArray)
    (shape == other.shape) && eq(other).all?
  else
    false
  end
end

#all?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/narray_ext.rb', line 21

def all?
  where.size == size
end

#any?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/narray_ext.rb', line 25

def any?
  where.size > 0
end

#complex?Boolean

Returns:

  • (Boolean)


16
17
18
19
# File 'lib/narray_ext.rb', line 16

def complex?
  self.typecode==NArray::DCOMPLEX ||
  self.typecode==NArray::SCOMPLEX
end

#integer?Boolean

Returns:

  • (Boolean)


11
12
13
14
15
# File 'lib/narray_ext.rb', line 11

def integer?
  self.typecode==NArray::BYTE ||
  self.typecode==NArray::SINT ||
  self.typecode==NArray::LINT
end

#mean(*ranks) ⇒ Object

Statistics



56
57
58
59
60
61
62
63
64
# File 'lib/narray_ext.rb', line 56

def mean(*ranks)
  if integer?
    a = self.to_type(NArray::DFLOAT)
  else
    a = self
  end
  a = NArray.ref(a)
  a.sum(*ranks) / (rank_total(*ranks))
end

#median(rank = nil) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/narray_ext.rb', line 111

def median(rank=nil)
  shape = self.shape
  rank = shape.size-1 if rank==nil
  s = sort(rank).reshape!(true,*shape[rank+1..-1])
  n = s.shape[0]
  if n%2==1
    s[n/2,false]
  else
    s[n/2-1..n/2,false].sum(0)/2
  end
end

#none?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/narray_ext.rb', line 29

def none?
  where.size == 0
end

#randomnObject Also known as: randomn!

Normal distributed random number; valid for floating point types



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/narray_ext.rb', line 125

def randomn
  size = self.size
  case type = self.typecode
  when COMPLEX; type=FLOAT
  when SCOMPLEX; type=SFLOAT
  when FLOAT
  when SFLOAT
  else
    raise TypeError, "NArray type must be (S)FLOAT or (S)COMPLEX."
  end
  rr = NArray.new(type,size)
  xx = NArray.new(type,size)
  i = 0
  while i < size
    n = size-i
    m = ((n+Math::sqrt(n))*1.27).to_i
    x = NArray.new(type,m).random!(1) * 2 - 1
    y = NArray.new(type,m).random!(1) * 2 - 1
    r = x**2 + y**2
    idx = (r<1).where
    idx = idx[0...n] if idx.size > n
    if idx.size>0
	rr[i] = r[idx]
	xx[i] = x[idx]
	i += idx.size
    end
  end
  # Box-Muller transform
  rr = ( xx * NMath::sqrt( -2 * NMath::log(rr) / rr ) )
  # finish
  rr.reshape!(*self.shape) if self.rank > 1
  rr = rr.to_type(self.typecode) if type!=self.typecode
  if RUBY_VERSION < "1.8.0"
    self.type.refer(rr)
  else
    self.class.refer(rr)
  end
end

#rank_total(*ranks) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/narray_ext.rb', line 41

def rank_total(*ranks)
  if ranks.size>0
    idx = []
    ranks.each{|i| idx.push(*i)}
    # ranks is expected to be, e.g., [1, 3..5, 7]
    a = self.shape
    n = 1
    idx.each{|i| n *= a[i]}
    n
  else
    self.total
  end
end

#rms(*ranks) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/narray_ext.rb', line 81

def rms(*ranks)
  if integer?
    a = self.to_type(NArray::DFLOAT)
  else
    a = self
  end
  a = NArray.ref(a)
  n = rank_total(*ranks)
  if complex?
    NMath::sqrt( (a.abs**2).sum(*ranks)/n )
  else
    NMath::sqrt( (a**2).sum(*ranks)/n )
  end
end

#rmsdev(*ranks) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/narray_ext.rb', line 96

def rmsdev(*ranks)
  if integer?
    a = self.to_type(NArray::DFLOAT)
  else
    a = self
  end
  a = NArray.ref(a)
  n = rank_total(*ranks)
  if complex?
    NMath::sqrt( (( a-a.accum(*ranks).div!(n) ).abs**2).sum(*ranks)/n )
  else
    NMath::sqrt( (( a-a.accum(*ranks).div!(n) )**2).sum(*ranks)/n )
  end
end

#stddev(*ranks) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/narray_ext.rb', line 66

def stddev(*ranks)
  if integer?
    a = self.to_type(NArray::DFLOAT)
  else
    a = self
  end
  a = NArray.ref(a)
  n = rank_total(*ranks)
  if complex?
    NMath::sqrt( (( a-a.accum(*ranks).div!(n) ).abs**2).sum(*ranks)/(n-1) )
  else
    NMath::sqrt( (( a-a.accum(*ranks).div!(n) )**2).sum(*ranks)/(n-1) )
  end
end