Class: CooCoo::Sequence
Class Method Summary
collapse
Instance Method Summary
collapse
#collect_equal?, #collect_infinite?, #collect_nan?, #collect_not_equal?, #infinite?, #max, #min, #minmax, #minmax_normalize, #nan?, ones, rand, #set2d!, #slice_2d, zeros
Constructor Details
#initialize(length, &init) ⇒ Sequence
Returns a new instance of Sequence.
5
6
7
8
9
10
11
|
# File 'lib/coo-coo/sequence.rb', line 5
def initialize(length, &init)
if length.kind_of?(Array)
@elements = length
else
@elements = Array.new(length, &init)
end
end
|
Class Method Details
.[](value, max_size = nil) ⇒ Object
13
14
15
16
17
18
19
20
|
# File 'lib/coo-coo/sequence.rb', line 13
def self.[](value, max_size = nil)
self.new(value.to_a)
end
|
Instance Method Details
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
# File 'lib/coo-coo/sequence.rb', line 151
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
|
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/coo-coo/sequence.rb', line 113
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
|
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/coo-coo/sequence.rb', line 128
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
|
109
110
111
|
# File 'lib/coo-coo/sequence.rb', line 109
def -@
self.class[@elements.collect(&:-@)]
end
|
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
# File 'lib/coo-coo/sequence.rb', line 166
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
|
#==(other) ⇒ Object
181
182
183
184
185
|
# File 'lib/coo-coo/sequence.rb', line 181
def ==(other)
other.size == size && each.zip(other.each).all? do |a, b|
a == b
end
end
|
#[](i, len = nil) ⇒ Object
42
43
44
45
46
47
48
49
|
# File 'lib/coo-coo/sequence.rb', line 42
def [](i, len = nil)
v = @elements[i, len || 1]
if len
self.class[v]
else
v[0]
end
end
|
#[]=(i, v) ⇒ Object
51
52
53
54
|
# File 'lib/coo-coo/sequence.rb', line 51
def []=(i, v)
@elements[i] = v
self
end
|
#append(other) ⇒ Object
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/coo-coo/sequence.rb', line 78
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
|
101
102
103
|
# File 'lib/coo-coo/sequence.rb', line 101
def average
sum / size.to_f
end
|
#collect(&block) ⇒ Object
66
67
68
|
# File 'lib/coo-coo/sequence.rb', line 66
def collect(&block)
self.class[super]
end
|
#each(&block) ⇒ Object
56
57
58
|
# File 'lib/coo-coo/sequence.rb', line 56
def each(&block)
@elements.each(&block)
end
|
#each_with_index(&block) ⇒ Object
60
61
62
|
# File 'lib/coo-coo/sequence.rb', line 60
def each_with_index(&block)
each.each_with_index(&block)
end
|
#last(*args) ⇒ Object
74
75
76
|
# File 'lib/coo-coo/sequence.rb', line 74
def last(*args)
@elements.last(*args)
end
|
147
148
149
|
# File 'lib/coo-coo/sequence.rb', line 147
def length
@elements.size
end
|
70
71
72
|
# File 'lib/coo-coo/sequence.rb', line 70
def reverse
self.class[@elements.reverse]
end
|
143
144
145
|
# File 'lib/coo-coo/sequence.rb', line 143
def size
@elements.size
end
|
105
106
107
|
# File 'lib/coo-coo/sequence.rb', line 105
def sqrt
self.class[@elements.collect(&:sqrt)]
end
|
95
96
97
98
99
|
# File 'lib/coo-coo/sequence.rb', line 95
def sum
@elements.drop(1).inject(@elements[0]) do |acc, e|
acc + e
end
end
|
def coerce(other)
if other.respond_to?(:each)
return self.class[other], self
else
return self.class.new(self.size, other), self
end
end
30
31
32
|
# File 'lib/coo-coo/sequence.rb', line 30
def to_a
@elements
end
|
34
35
36
37
38
39
40
|
# File 'lib/coo-coo/sequence.rb', line 34
def to_s
values = each.collect do |e|
e.to_s
end
"[#{values.join(', ')}]"
end
|
89
90
91
92
93
|
# File 'lib/coo-coo/sequence.rb', line 89
def zero
self.class.new(size) do |i|
self[0].zero
end
end
|