Class: CooCoo::Ruby::Vector
Class Method Summary
collapse
Instance Method Summary
collapse
-
#!=(other) ⇒ Object
-
#*(other) ⇒ Object
-
#**(other) ⇒ Object
-
#+(other) ⇒ Object
-
#-(other) ⇒ Object
-
#-@ ⇒ Object
-
#/(other) ⇒ Object
-
#==(other) ⇒ Object
-
#[](i, len = nil) ⇒ Object
-
#[]=(i, l, v = nil) ⇒ Object
-
#append(other) ⇒ Object
-
#coerce(other) ⇒ Object
-
#dot(width, height, other, owidth = nil, oheight = nil) ⇒ Object
-
#each(&block) ⇒ Object
-
#each_slice(n, &block) ⇒ Object
-
#each_with_index(&block) ⇒ Object
-
#initialize(length, initial_value = 0.0, &block) ⇒ Vector
constructor
A new instance of Vector.
-
#length ⇒ Object
-
#magnitude ⇒ Object
-
#magnitude_squared ⇒ Object
-
#normalize ⇒ Object
-
#resize(new_size) ⇒ Object
-
#set(values) ⇒ Object
-
#size ⇒ Object
-
#sum ⇒ Object
-
#to_a ⇒ Object
-
#to_s ⇒ Object
#collect_equal?, #collect_infinite?, #collect_nan?, #collect_not_equal?, #infinite?, #max, #min, #minmax, #minmax_normalize, #nan?, ones, rand, #set2d!, #slice_2d, #zero, zeros
Constructor Details
#initialize(length, initial_value = 0.0, &block) ⇒ Vector
Returns a new instance of Vector.
11
12
13
14
15
16
17
18
19
|
# File 'lib/coo-coo/math.rb', line 11
def initialize(length, initial_value = 0.0, &block)
raise ArgumentError.new("Invalid size for a Vector") if length <= 0
if block_given?
@elements = Array.new(length, &block)
else
@elements = Array.new(length, initial_value)
end
end
|
Class Method Details
.[](value, max_size = nil, default_value = 0.0) ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/coo-coo/math.rb', line 21
def self.[](value, max_size = nil, default_value = 0.0)
if value.respond_to?(:[])
v = new(max_size || value.size, default_value) do |i|
value[i].to_f || default_value
end
else
v = new(max_size || value.size, default_value) do |i|
begin
value.next.to_f || default_value
rescue StopIteration
default_value
end
end
end
end
|
Instance Method Details
#!=(other) ⇒ Object
270
271
272
|
# File 'lib/coo-coo/math.rb', line 270
def !=(other)
!(self == other)
end
|
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
# File 'lib/coo-coo/math.rb', line 217
def *(other)
v = if other.respond_to?(:each)
raise ArgumentError.new("Size mismatch") if size != other.size
other.each.zip(each).collect do |oe, se|
se * oe
end
else
each.collect do |e|
e * other
end
end
self.class[v]
end
|
#**(other) ⇒ Object
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
# File 'lib/coo-coo/math.rb', line 232
def **(other)
v = if other.respond_to?(:each)
raise ArgumentError.new("Size mismatch") if size != other.size
other.each.zip(each).collect do |oe, se|
se ** oe
end
else
each.collect do |e|
e ** other
end
end
self.class[v]
end
|
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
# File 'lib/coo-coo/math.rb', line 175
def +(other)
v = if other.respond_to?(:each)
raise ArgumentError.new("Size mismatch") if size != other.size
other.each.zip(each).collect do |oe, se|
se + oe
end
else
each.collect do |e|
e + other
end
end
self.class[v]
end
|
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
# File 'lib/coo-coo/math.rb', line 194
def -(other)
v = if other.respond_to?(:each)
raise ArgumentError.new("Size mismatch: #{size} != #{other.size}") if size != other.size
other.each.zip(each).collect do |oe, se|
se - oe
end
else
each.collect do |e|
e - other
end
end
self.class[v]
end
|
190
191
192
|
# File 'lib/coo-coo/math.rb', line 190
def -@
self * -1.0
end
|
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
# File 'lib/coo-coo/math.rb', line 247
def /(other)
v = if other.respond_to?(:each)
raise ArgumentError.new("Size mismatch") if size != other.size
other.each.zip(each).collect do |oe, se|
se / oe
end
else
each.collect do |e|
e / other
end
end
self.class[v]
end
|
#==(other) ⇒ Object
262
263
264
265
266
267
268
|
# File 'lib/coo-coo/math.rb', line 262
def ==(other)
other && size == other.size && each.zip(other.each).all? do |a, b|
a == b || (a.nan? && b.nan?)
end || false
rescue NoMethodError
false
end
|
#[](i, len = nil) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/coo-coo/math.rb', line 57
def [](i, len = nil)
i = size + i if i < 0
raise RangeError.new if i >= size || i < 0
v = @elements[i, len || 1]
if len
self.class[v]
elsif v
v[0]
end
end
|
#[]=(i, l, v = nil) ⇒ Object
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/coo-coo/math.rb', line 70
def []=(i, l, v = nil)
i = size + i if i < 0
raise RangeError.new if i >= size || i < 0
if v
@elements[i, l] = v
else
@elements[i] = l
end
end
|
#append(other) ⇒ Object
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/coo-coo/math.rb', line 120
def append(other)
v = self.class.new(size + other.size)
each_with_index do |e, i|
v[i] = e
end
other.each_with_index do |e, i|
v[i + size] = e
end
v
end
|
#coerce(other) ⇒ Object
37
38
39
40
41
42
43
|
# File 'lib/coo-coo/math.rb', line 37
def coerce(other)
if other.respond_to?(:each)
return self.class[other], self
else
return self.class.new(self.size, other), self
end
end
|
#dot(width, height, other, owidth = nil, oheight = nil) ⇒ Object
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
# File 'lib/coo-coo/math.rb', line 147
def dot(width, height, other, owidth = nil, oheight = nil)
if other.kind_of?(self.class) || other.respond_to?(:[])
owidth ||= width
oheight ||= height
if width * height != size
raise ArgumentError.new("width & height, #{width}x#{height} don't match our size: #{size}")
end
if owidth * oheight != other.size
raise ArgumentError.new("owidth & oheight, #{owidth}x#{oheight} don't match the argument's size: #{other.size}")
end
if width != oheight
raise ArgumentError.new("argument's height != this' width")
end
self.class[height.times.collect do |row|
owidth.times.collect do |col|
oheight.times.collect do |i|
self[row * width + i] * other[i * owidth + col]
end.sum
end
end.flatten]
else
raise ArgumentError.new("argument must be a #{self.class} or enumerable")
end
end
|
#each(&block) ⇒ Object
92
93
94
|
# File 'lib/coo-coo/math.rb', line 92
def each(&block)
@elements.each(&block)
end
|
#each_slice(n, &block) ⇒ Object
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/coo-coo/math.rb', line 100
def each_slice(n, &block)
if block
num_slices = (size / n.to_f).ceil.to_i
@elements.each_slice(n).with_index do |slice, i|
block.call(self.class[slice, n])
end
else
to_enum(__method__, n)
end
end
|
#each_with_index(&block) ⇒ Object
96
97
98
|
# File 'lib/coo-coo/math.rb', line 96
def each_with_index(&block)
each.each_with_index(&block)
end
|
213
214
215
|
# File 'lib/coo-coo/math.rb', line 213
def length
@elements.size
end
|
#magnitude ⇒ Object
139
140
141
|
# File 'lib/coo-coo/math.rb', line 139
def magnitude
magnitude_squared.sqrt
end
|
#magnitude_squared ⇒ Object
135
136
137
|
# File 'lib/coo-coo/math.rb', line 135
def magnitude_squared
(self * self).sum
end
|
#normalize ⇒ Object
143
144
145
|
# File 'lib/coo-coo/math.rb', line 143
def normalize
self / magnitude
end
|
#resize(new_size) ⇒ Object
112
113
114
115
116
117
118
|
# File 'lib/coo-coo/math.rb', line 112
def resize(new_size)
if new_size > size
@elements = @elements + Array.new(new_size - size)
elsif new_size < size
@elements = @elements[0, new_size]
end
end
|
#set(values) ⇒ Object
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/coo-coo/math.rb', line 81
def set(values)
values = [ values ].cycle(size) if values.kind_of?(Numeric)
values.each_with_index do |v, i|
break if i >= @elements.size
@elements[i] = v
end
self
end
|
209
210
211
|
# File 'lib/coo-coo/math.rb', line 209
def size
@elements.size
end
|
131
132
133
|
# File 'lib/coo-coo/math.rb', line 131
def sum
@elements.each.sum
end
|
45
46
47
|
# File 'lib/coo-coo/math.rb', line 45
def to_a
@elements
end
|
49
50
51
52
53
54
55
|
# File 'lib/coo-coo/math.rb', line 49
def to_s
values = each.collect do |e|
e.to_s
end
"[#{values.join(', ')}]"
end
|