Class: CooCoo::Math::AbstractVector

Inherits:
Object
  • Object
show all
Defined in:
lib/coo-coo/math/abstract_vector.rb

Direct Known Subclasses

CUDA::Vector, NMatrix::Vector, Ruby::Vector, Sequence

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ones(length) ⇒ Object



15
16
17
# File 'lib/coo-coo/math/abstract_vector.rb', line 15

def self.ones(length)
  new(length, 1.0)
end

.rand(length, range = nil) ⇒ Object



4
5
6
7
8
9
# File 'lib/coo-coo/math/abstract_vector.rb', line 4

def self.rand(length, range = nil)
  new(length) do |i|
    args = [ range ] if range
    Random.rand(*args)
  end
end

.zeros(length) ⇒ Object



11
12
13
# File 'lib/coo-coo/math/abstract_vector.rb', line 11

def self.zeros(length)
  new(length, 0.0)
end

Instance Method Details

#collect_equal?(n) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
# File 'lib/coo-coo/math/abstract_vector.rb', line 88

def collect_equal?(n)
  if n.respond_to?(:each)
    self.class[each.zip(n).collect { |a, b| a == b ? 1.0 : 0.0 }]
  else
    self.class[each.collect { |e| e == n ? 1.0 : 0.0 }]
  end
end

#collect_infinite?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/coo-coo/math/abstract_vector.rb', line 112

def collect_infinite?
  self.class[each.collect { |e| e.infinite? ? 1.0 : 0.0 }]
end

#collect_nan?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/coo-coo/math/abstract_vector.rb', line 104

def collect_nan?
  self.class[each.collect { |e| e.nan? ? 1.0 : 0.0 }]
end

#collect_not_equal?(n) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
99
100
101
102
# File 'lib/coo-coo/math/abstract_vector.rb', line 96

def collect_not_equal?(n)
  if n.respond_to?(:each)
    self.class[each.zip(n).collect { |a, b| a != b ? 1.0 : 0.0 }]
  else
    self.class[each.collect { |e| e != n ? 1.0 : 0.0 }]
  end
end

#infinite?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/coo-coo/math/abstract_vector.rb', line 116

def infinite?
  each.any?(&:infinite?)
end

#maxObject



23
24
25
# File 'lib/coo-coo/math/abstract_vector.rb', line 23

def max
  minmax[1]
end

#minObject



27
28
29
# File 'lib/coo-coo/math/abstract_vector.rb', line 27

def min
  minmax[0]
end

#minmaxObject



31
32
33
# File 'lib/coo-coo/math/abstract_vector.rb', line 31

def minmax
  each.minmax
end

#minmax_normalize(use_zeros = false) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/coo-coo/math/abstract_vector.rb', line 35

def minmax_normalize(use_zeros = false)
  min, max = minmax
  delta = (max - min)
  if use_zeros && delta == 0.0
    zero
  else
    (self - min) / delta
  end
end

#nan?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/coo-coo/math/abstract_vector.rb', line 108

def nan?
  each.any?(&:nan?)
end

#set2d!(width, src, src_width, x, y) ⇒ Object

Raises:

  • (ArgumentError)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/coo-coo/math/abstract_vector.rb', line 73

def set2d!(width, src, src_width, x, y)
  raise ArgumentError.new("src's size needs to be a multiple of the width") if src.kind_of?(self.class) && src.size % src_width > 0
  
  src.each_slice(src_width).with_index do |row, i|
    index = (y+i) * width + x
    next if index >= size
    row.each_with_index do |p, px|
      break if (x + px) >= width
      self[index.to_i + px] = p
    end
  end

  self
end

#slice_2d(src_width, src_height, origin_x, origin_y, width, height, initial = 0.0) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/coo-coo/math/abstract_vector.rb', line 51

def slice_2d(src_width, src_height, origin_x, origin_y, width, height, initial = 0.0)
  samples = height.times.collect do |y|
    py = origin_y + y

    width.times.collect do |x|
      px = origin_x + x
      if px >= 0 && px < src_width
        i = py * src_width + px
        if i >= 0 && i < size
          self[i]
        else
          initial
        end
      else
        initial
      end
    end
  end.flatten

  self.class[samples]
end

#zeroObject



19
20
21
# File 'lib/coo-coo/math/abstract_vector.rb', line 19

def zero
  self.class.zeros(size)
end