Class: CooCoo::CUDA::Vector
- Inherits:
-
Math::AbstractVector
- Object
- Math::AbstractVector
- CooCoo::CUDA::Vector
- Defined in:
- lib/coo-coo/cuda/vector.rb
Class Method Summary collapse
- .[](value, max_size = nil, default_value = 0.0) ⇒ Object
- ._load(args) ⇒ Object
- .identity(w, h) ⇒ Object
- .ones(length) ⇒ Object
- .zeros(length) ⇒ Object
Instance Method Summary collapse
-
#*(other) ⇒ Vector
Calls the equivalent of #* on each element of
self
againstother
. -
#**(other) ⇒ Vector
Calls the equivalent of #** on each element of
self
againstother
. -
#+(other) ⇒ Vector
Calls the equivalent of #+ on each element of
self
againstother
. -
#-(other) ⇒ Vector
Calls the equivalent of
#-
on each element ofself
againstother
. -
#-@ ⇒ Vector
Negates every element in the vector.
-
#/(other) ⇒ Vector
Calls the equivalent of
#/
on each element ofself
againstother
. -
#<(other) ⇒ Vector
Calls the equivalent of #< on each element of
self
againstother
. -
#<=(other) ⇒ Vector
Calls the equivalent of #<= on each element of
self
againstother
. - #==(other) ⇒ Object
-
#>(other) ⇒ Vector
Calls the equivalent of #> on each element of
self
againstother
. -
#>=(other) ⇒ Vector
Calls the equivalent of #>= on each element of
self
againstother
. - #[](i, len = nil) ⇒ Object
- #[]=(i, v, l = nil) ⇒ Object
- #_dump(depth) ⇒ Object
-
#abs ⇒ Vector
Calls the equivalent of
Math.abs
on each element ofself
. -
#acos ⇒ Vector
Calls the equivalent of
Math.acos
on each element ofself
. -
#acosh ⇒ Vector
Calls the equivalent of
Math.acosh
on each element ofself
. - #append(other) ⇒ Object
-
#asin ⇒ Vector
Calls the equivalent of
Math.asin
on each element ofself
. -
#asinh ⇒ Vector
Calls the equivalent of
Math.asinh
on each element ofself
. -
#atan ⇒ Vector
Calls the equivalent of
Math.atan
on each element ofself
. -
#atanh ⇒ Vector
Calls the equivalent of
Math.atanh
on each element ofself
. - #average ⇒ Object
-
#ceil ⇒ Vector
Calls the equivalent of
Math.ceil
on each element ofself
. - #clone ⇒ Object
- #coerce(other) ⇒ Object
-
#collect_equal?(n) ⇒ Vector
Calls the equivalent of
#collect_equal?
on each element ofself
againstother
. -
#collect_infinite? ⇒ Vector
Calls the equivalent of
Math.collect_infinite?
on each element ofself
. -
#collect_nan? ⇒ Vector
Calls the equivalent of
Math.collect_nan?
on each element ofself
. -
#collect_not_equal?(other) ⇒ Vector
Calls the equivalent of
#collect_not_equal?
on each element ofself
againstother
. -
#cos ⇒ Vector
Calls the equivalent of
Math.cos
on each element ofself
. -
#cosh ⇒ Vector
Calls the equivalent of
Math.cosh
on each element ofself
. - #diagflat ⇒ Object
- #dot(w, h, other, ow = nil, oh = nil) ⇒ Object
- #each(&block) ⇒ Object
- #each_slice(n, &block) ⇒ Object
- #each_with_index(&block) ⇒ Object
-
#exp ⇒ Vector
Calls the equivalent of
Math.exp
on each element ofself
. -
#floor ⇒ Vector
Calls the equivalent of
Math.floor
on each element ofself
. - #infinite? ⇒ Boolean
-
#initialize(length, initial_value = 0.0, &block) ⇒ Vector
constructor
A new instance of Vector.
- #inspect ⇒ Object
- #length ⇒ Object
-
#log ⇒ Vector
Calls the equivalent of
Math.log
on each element ofself
. -
#log10 ⇒ Vector
Calls the equivalent of
Math.log10
on each element ofself
. -
#log2 ⇒ Vector
Calls the equivalent of
Math.log2
on each element ofself
. - #magnitude ⇒ Object
- #magnitude_squared ⇒ Object
-
#max ⇒ Float
Maximum value of
self
. -
#min ⇒ Float
Minimum value of
self
. - #minmax ⇒ [Float, Float]
- #nan? ⇒ Boolean
- #normalize ⇒ Object
- #null? ⇒ Boolean
-
#round ⇒ Vector
Calls the equivalent of
Math.round
on each element ofself
. - #set(values) ⇒ Object
- #set2d!(width, src, src_width, x, y) ⇒ Object
-
#sin ⇒ Vector
Calls the equivalent of
Math.sin
on each element ofself
. -
#sinh ⇒ Vector
Calls the equivalent of
Math.sinh
on each element ofself
. - #size ⇒ Object
- #slice_2d(*args) ⇒ Object
-
#sqrt ⇒ Vector
Calls the equivalent of
Math.sqrt
on each element ofself
. -
#sum ⇒ Float
Reduces the vector with #+.
-
#tan ⇒ Vector
Calls the equivalent of
Math.tan
on each element ofself
. -
#tanh ⇒ Vector
Calls the equivalent of
Math.tanh
on each element ofself
. - #to_a ⇒ Object
- #to_s ⇒ Object
- #zeros ⇒ Object
Methods inherited from Math::AbstractVector
#minmax_normalize, rand, #zero
Constructor Details
#initialize(length, initial_value = 0.0, &block) ⇒ Vector
Returns a new instance of Vector.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/coo-coo/cuda/vector.rb', line 8 def initialize(length, initial_value = 0.0, &block) if length != nil && length <= 0 raise ArgumentError.new("Invalid Vector size") elsif length != nil @elements = DeviceBuffer.create(length, initial_value) if block @elements.size.times.each_slice(1024).with_index do |slice, slice_idx| @elements[slice_idx * 1024, 1024] = slice.collect do |i| block.call(i) end end end end end |
Class Method Details
.[](value, max_size = nil, default_value = 0.0) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/coo-coo/cuda/vector.rb', line 23 def self.[](value, max_size = nil, default_value = 0.0) if value.kind_of?(DeviceBuffer) v = new(nil) v.instance_variable_set('@elements', value) v else if value.respond_to?(:each) max_size ||= value.size else max_size ||= 1 end new(max_size, default_value).set(value) end end |
._load(args) ⇒ Object
103 104 105 106 |
# File 'lib/coo-coo/cuda/vector.rb', line 103 def self._load(args) arr = args.unpack('E*') self[arr] end |
.identity(w, h) ⇒ Object
60 61 62 |
# File 'lib/coo-coo/cuda/vector.rb', line 60 def self.identity(w, h) self[DeviceBuffer.identity(w, h)] end |
.ones(length) ⇒ Object
52 53 54 |
# File 'lib/coo-coo/cuda/vector.rb', line 52 def self.ones(length) self.new(length, 1.0) end |
.zeros(length) ⇒ Object
48 49 50 |
# File 'lib/coo-coo/cuda/vector.rb', line 48 def self.zeros(length) self.new(length, 0.0) end |
Instance Method Details
#*(other) ⇒ Vector
Calls the equivalent of #* on each element of self
against other
.
211 |
# File 'lib/coo-coo/cuda/vector.rb', line 211 bin_op('*') |
#**(other) ⇒ Vector
Calls the equivalent of #** on each element of self
against other
.
213 |
# File 'lib/coo-coo/cuda/vector.rb', line 213 bin_op('**') |
#+(other) ⇒ Vector
Calls the equivalent of #+ on each element of self
against other
.
209 |
# File 'lib/coo-coo/cuda/vector.rb', line 209 bin_op('+') |
#-(other) ⇒ Vector
Calls the equivalent of #-
on each element of self
against other
.
210 |
# File 'lib/coo-coo/cuda/vector.rb', line 210 bin_op('-') |
#-@ ⇒ Vector
Negates every element in the vector.
219 220 221 |
# File 'lib/coo-coo/cuda/vector.rb', line 219 def -@ self * -1.0 end |
#/(other) ⇒ Vector
Calls the equivalent of #/
on each element of self
against other
.
212 |
# File 'lib/coo-coo/cuda/vector.rb', line 212 bin_op('/') |
#<(other) ⇒ Vector
Calls the equivalent of #< on each element of self
against other
.
205 |
# File 'lib/coo-coo/cuda/vector.rb', line 205 bin_op('<') |
#<=(other) ⇒ Vector
Calls the equivalent of #<= on each element of self
against other
.
206 |
# File 'lib/coo-coo/cuda/vector.rb', line 206 bin_op('<=') |
#==(other) ⇒ Object
239 240 241 242 243 244 245 246 247 248 |
# File 'lib/coo-coo/cuda/vector.rb', line 239 def ==(other) if other.kind_of?(self.class) @elements == other.elements elsif other != nil b, a = coerce(other) self == b else false end end |
#>(other) ⇒ Vector
Calls the equivalent of #> on each element of self
against other
.
208 |
# File 'lib/coo-coo/cuda/vector.rb', line 208 bin_op('>') |
#>=(other) ⇒ Vector
Calls the equivalent of #>= on each element of self
against other
.
207 |
# File 'lib/coo-coo/cuda/vector.rb', line 207 bin_op('>=') |
#[](i, len = nil) ⇒ Object
112 113 114 115 116 117 118 119 |
# File 'lib/coo-coo/cuda/vector.rb', line 112 def [](i, len = nil) v = @elements[i, len] if len self.class[v] else v end end |
#[]=(i, v, l = nil) ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/coo-coo/cuda/vector.rb', line 121 def []=(i, v, l = nil) if l == nil @elements[i] = v else @elements[i, v] = l end end |
#_dump(depth) ⇒ Object
99 100 101 |
# File 'lib/coo-coo/cuda/vector.rb', line 99 def _dump(depth) @elements.to_a.pack('E*') end |
#abs ⇒ Vector
Calls the equivalent of Math.abs
on each element of self
.
265 |
# File 'lib/coo-coo/cuda/vector.rb', line 265 f :abs |
#acos ⇒ Vector
Calls the equivalent of Math.acos
on each element of self
.
274 |
# File 'lib/coo-coo/cuda/vector.rb', line 274 f :acos |
#acosh ⇒ Vector
Calls the equivalent of Math.acosh
on each element of self
.
280 |
# File 'lib/coo-coo/cuda/vector.rb', line 280 f :acosh |
#append(other) ⇒ Object
72 73 74 75 76 77 |
# File 'lib/coo-coo/cuda/vector.rb', line 72 def append(other) b = self.class.new(size + other.size) b[0, size] = self b[size, other.size] = other b end |
#asin ⇒ Vector
Calls the equivalent of Math.asin
on each element of self
.
272 |
# File 'lib/coo-coo/cuda/vector.rb', line 272 f :asin |
#asinh ⇒ Vector
Calls the equivalent of Math.asinh
on each element of self
.
278 |
# File 'lib/coo-coo/cuda/vector.rb', line 278 f :asinh |
#atan ⇒ Vector
Calls the equivalent of Math.atan
on each element of self
.
276 |
# File 'lib/coo-coo/cuda/vector.rb', line 276 f :atan |
#atanh ⇒ Vector
Calls the equivalent of Math.atanh
on each element of self
.
282 |
# File 'lib/coo-coo/cuda/vector.rb', line 282 f :atanh |
#average ⇒ Object
160 161 162 |
# File 'lib/coo-coo/cuda/vector.rb', line 160 def average @elements.sum / size end |
#ceil ⇒ Vector
Calls the equivalent of Math.ceil
on each element of self
.
283 |
# File 'lib/coo-coo/cuda/vector.rb', line 283 f :ceil |
#clone ⇒ Object
68 69 70 |
# File 'lib/coo-coo/cuda/vector.rb', line 68 def clone self.class.new(self.size).set(@elements) end |
#coerce(other) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/coo-coo/cuda/vector.rb', line 79 def coerce(other) if other.respond_to?(:each) return self.class[other], self else return self.class.new(self.size, other), self end end |
#collect_equal?(n) ⇒ Vector
Calls the equivalent of #collect_equal?
on each element of self
against other
.
214 |
# File 'lib/coo-coo/cuda/vector.rb', line 214 bin_op('collect_equal?') |
#collect_infinite? ⇒ Vector
Calls the equivalent of Math.collect_infinite?
on each element of self
.
287 |
# File 'lib/coo-coo/cuda/vector.rb', line 287 f :collect_infinite?, :collect_inf |
#collect_nan? ⇒ Vector
Calls the equivalent of Math.collect_nan?
on each element of self
.
286 |
# File 'lib/coo-coo/cuda/vector.rb', line 286 f :collect_nan?, :collect_nan |
#collect_not_equal?(other) ⇒ Vector
Calls the equivalent of #collect_not_equal?
on each element of self
against other
.
215 |
# File 'lib/coo-coo/cuda/vector.rb', line 215 bin_op('collect_not_equal?') |
#cos ⇒ Vector
Calls the equivalent of Math.cos
on each element of self
.
273 |
# File 'lib/coo-coo/cuda/vector.rb', line 273 f :cos |
#cosh ⇒ Vector
Calls the equivalent of Math.cosh
on each element of self
.
279 |
# File 'lib/coo-coo/cuda/vector.rb', line 279 f :cosh |
#diagflat ⇒ Object
64 65 66 |
# File 'lib/coo-coo/cuda/vector.rb', line 64 def diagflat self.class[@elements.diagflat] end |
#dot(w, h, other, ow = nil, oh = nil) ⇒ Object
176 177 178 179 180 181 182 183 184 |
# File 'lib/coo-coo/cuda/vector.rb', line 176 def dot(w, h, other, ow = nil, oh = nil) if other.kind_of?(self.class) self.class[@elements.dot(w, h, other.elements, ow, oh)] elsif other.respond_to?(:each) dot(w, h, self.class[other.each], ow, oh) else raise ArgumentError.new("argument is not a #{self.class} or Enumerator") end end |
#each(&block) ⇒ Object
129 130 131 |
# File 'lib/coo-coo/cuda/vector.rb', line 129 def each(&block) @elements.each(&block) end |
#each_slice(n, &block) ⇒ Object
137 138 139 140 141 142 143 |
# File 'lib/coo-coo/cuda/vector.rb', line 137 def each_slice(n, &block) return to_enum(__method__, n) unless block @elements.each_slice(n) do |slice| block.call(self.class[slice]) end end |
#each_with_index(&block) ⇒ Object
133 134 135 |
# File 'lib/coo-coo/cuda/vector.rb', line 133 def each_with_index(&block) @elements.each.with_index(&block) end |
#exp ⇒ Vector
Calls the equivalent of Math.exp
on each element of self
.
266 |
# File 'lib/coo-coo/cuda/vector.rb', line 266 f :exp |
#floor ⇒ Vector
Calls the equivalent of Math.floor
on each element of self
.
284 |
# File 'lib/coo-coo/cuda/vector.rb', line 284 f :floor |
#infinite? ⇒ Boolean
293 294 295 |
# File 'lib/coo-coo/cuda/vector.rb', line 293 def infinite? collect_infinite?.sum > 0 end |
#inspect ⇒ Object
91 92 93 |
# File 'lib/coo-coo/cuda/vector.rb', line 91 def inspect to_a.inspect end |
#length ⇒ Object
235 236 237 |
# File 'lib/coo-coo/cuda/vector.rb', line 235 def length @elements.size end |
#log ⇒ Vector
Calls the equivalent of Math.log
on each element of self
.
267 |
# File 'lib/coo-coo/cuda/vector.rb', line 267 f :log |
#log10 ⇒ Vector
Calls the equivalent of Math.log10
on each element of self
.
268 |
# File 'lib/coo-coo/cuda/vector.rb', line 268 f :log10 |
#log2 ⇒ Vector
Calls the equivalent of Math.log2
on each element of self
.
269 |
# File 'lib/coo-coo/cuda/vector.rb', line 269 f :log2 |
#magnitude ⇒ Object
168 169 170 |
# File 'lib/coo-coo/cuda/vector.rb', line 168 def magnitude ::Math.sqrt(magnitude_squared) end |
#magnitude_squared ⇒ Object
164 165 166 |
# File 'lib/coo-coo/cuda/vector.rb', line 164 def magnitude_squared (self * self).sum end |
#max ⇒ Float
Returns maximum value of self
.
154 |
# File 'lib/coo-coo/cuda/vector.rb', line 154 delegate :min, :max, :minmax, :sum, :to => :elements |
#min ⇒ Float
Returns minimum value of self
.
154 |
# File 'lib/coo-coo/cuda/vector.rb', line 154 delegate :min, :max, :minmax, :sum, :to => :elements |
#minmax ⇒ [Float, Float]
154 |
# File 'lib/coo-coo/cuda/vector.rb', line 154 delegate :min, :max, :minmax, :sum, :to => :elements |
#nan? ⇒ Boolean
289 290 291 |
# File 'lib/coo-coo/cuda/vector.rb', line 289 def nan? collect_nan?.sum > 0 end |
#normalize ⇒ Object
172 173 174 |
# File 'lib/coo-coo/cuda/vector.rb', line 172 def normalize self / magnitude end |
#null? ⇒ Boolean
108 109 110 |
# File 'lib/coo-coo/cuda/vector.rb', line 108 def null? @elements.null? end |
#round ⇒ Vector
Calls the equivalent of Math.round
on each element of self
.
285 |
# File 'lib/coo-coo/cuda/vector.rb', line 285 f :round |
#set(values) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/coo-coo/cuda/vector.rb', line 38 def set(values) if values.kind_of?(self.class) @elements.set(values.elements) else @elements.set(values) end self end |
#set2d!(width, src, src_width, x, y) ⇒ Object
301 302 303 304 305 306 307 |
# File 'lib/coo-coo/cuda/vector.rb', line 301 def set2d!(width, src, src_width, x, y) raise ArgumentError.new("src's size #{src.size} must be divisible by src_width #{src_width}") if src.respond_to?(:each) && src.size % src_width > 0 src = src.elements if src.kind_of?(self.class) @elements.set2d!(width, src, src_width, x, y) self end |
#sin ⇒ Vector
Calls the equivalent of Math.sin
on each element of self
.
271 |
# File 'lib/coo-coo/cuda/vector.rb', line 271 f :sin |
#sinh ⇒ Vector
Calls the equivalent of Math.sinh
on each element of self
.
277 |
# File 'lib/coo-coo/cuda/vector.rb', line 277 f :sinh |
#size ⇒ Object
231 232 233 |
# File 'lib/coo-coo/cuda/vector.rb', line 231 def size @elements.size end |
#slice_2d(*args) ⇒ Object
297 298 299 |
# File 'lib/coo-coo/cuda/vector.rb', line 297 def slice_2d(*args) self.class[@elements.slice_2d(*args)] end |
#sqrt ⇒ Vector
Calls the equivalent of Math.sqrt
on each element of self
.
270 |
# File 'lib/coo-coo/cuda/vector.rb', line 270 f :sqrt |
#sum ⇒ Float
Reduces the vector with #+.
154 |
# File 'lib/coo-coo/cuda/vector.rb', line 154 delegate :min, :max, :minmax, :sum, :to => :elements |
#tan ⇒ Vector
Calls the equivalent of Math.tan
on each element of self
.
275 |
# File 'lib/coo-coo/cuda/vector.rb', line 275 f :tan |
#tanh ⇒ Vector
Calls the equivalent of Math.tanh
on each element of self
.
281 |
# File 'lib/coo-coo/cuda/vector.rb', line 281 f :tanh |
#to_a ⇒ Object
95 96 97 |
# File 'lib/coo-coo/cuda/vector.rb', line 95 def to_a @elements.to_a end |
#to_s ⇒ Object
87 88 89 |
# File 'lib/coo-coo/cuda/vector.rb', line 87 def to_s '[' + each.collect(&:to_f).join(', ') + ']' end |
#zeros ⇒ Object
56 57 58 |
# File 'lib/coo-coo/cuda/vector.rb', line 56 def zeros self.zeros(size) end |