Class: Daru::Vector

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/daru/vector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source = [], name = nil) ⇒ Vector

Returns a new instance of Vector.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/daru/vector.rb', line 55

def initialize source=[], name=nil
  if source.is_a?(Hash)
    initialize source.values[0], source.keys[0]
  else
    @name = name || SecureRandom.uuid

    @vector = 
    case source
    when Range, Matrix
      source.to_a.flatten
    else
      source
    end

    @size = @vector.size
  end
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



49
50
51
# File 'lib/daru/vector.rb', line 49

def name
  @name
end

#sizeObject (readonly)

Returns the value of attribute size.



51
52
53
# File 'lib/daru/vector.rb', line 51

def size
  @size
end

#vectorObject (readonly)

Returns the value of attribute vector.



53
54
55
# File 'lib/daru/vector.rb', line 53

def vector
  @vector
end

Instance Method Details

#<<(element) ⇒ Object



85
86
87
# File 'lib/daru/vector.rb', line 85

def <<(element)
  @vector << element
end

#==(other) ⇒ Object



81
82
83
# File 'lib/daru/vector.rb', line 81

def ==(other)
  other.vector == @vector and other.name == @name
end

#[](index) ⇒ Object



73
74
75
# File 'lib/daru/vector.rb', line 73

def [](index)
  @vector[index]
end

#[]=(index, value) ⇒ Object



77
78
79
# File 'lib/daru/vector.rb', line 77

def []=(index, value)
  @vector[index] = value
end

#dupObject



119
120
121
# File 'lib/daru/vector.rb', line 119

def dup
  Daru::Vector.new @vector.dup, @name.dup
end

#each(&block) ⇒ Object



45
46
47
# File 'lib/daru/vector.rb', line 45

def each(&block)
  @vector.each(&block)
end

#first(lim = 1) ⇒ Object



103
104
105
# File 'lib/daru/vector.rb', line 103

def first lim=1
  lim == 1 ? @vector.first : @vector.first(lim)
end

#to_aObject



93
94
95
# File 'lib/daru/vector.rb', line 93

def to_a
  @vector.to_a
end

#to_html(threshold = 15) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/daru/vector.rb', line 107

def to_html threshold=15
  html = '<table><tr><th>' + @name.to_s + '</th></tr>>'

  @vector.to_a.each_with_index do |el,i|
    next if threshold < i and i < @arr.length-1
    content = i == threshold ? '...' : el.to_s
    html.concat('<tr><td>' + content  + '</td></tr>')
  end

  html += '</table>'
end

#to_jsonObject



89
90
91
# File 'lib/daru/vector.rb', line 89

def to_json
  self.to_a.to_json
end

#to_nmatrixObject Also known as: to_nm



97
98
99
# File 'lib/daru/vector.rb', line 97

def to_nmatrix
  @vector.to_nm
end