Class: Spark::Mllib::VectorAdapter

Inherits:
Vector
  • Object
show all
Defined in:
lib/spark/mllib/ruby_matrix/vector_adapter.rb

Direct Known Subclasses

VectorBase

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Vector

elements

Constructor Details

#initialize(*args) ⇒ VectorAdapter

Returns a new instance of VectorAdapter.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/spark/mllib/ruby_matrix/vector_adapter.rb', line 21

def initialize(*args)
  case args.shift
  when :dense
    values = args.shift.dup
  when :sparse
    values = [0.0] * args.shift.to_i
  else
    raise Spark::MllibError, 'Unknow vector type.'
  end

  super(values)
end

Class Method Details

.new(*args) ⇒ Object



15
16
17
18
19
# File 'lib/spark/mllib/ruby_matrix/vector_adapter.rb', line 15

def self.new(*args)
  object = self.allocate
  object.__send__(:initialize, *args)
  object
end

Instance Method Details

#[]=(index, value) ⇒ Object



34
35
36
# File 'lib/spark/mllib/ruby_matrix/vector_adapter.rb', line 34

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

#dot(other) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/spark/mllib/ruby_matrix/vector_adapter.rb', line 38

def dot(other)
  if other.is_a?(Spark::Mllib::MatrixBase)
    other * self
  else
    inner_product(other)
  end
end

#squared_distance(other) ⇒ Object



46
47
48
49
# File 'lib/spark/mllib/ruby_matrix/vector_adapter.rb', line 46

def squared_distance(other)
  diff = self - other
  diff.dot(diff)
end

#valuesObject



51
52
53
# File 'lib/spark/mllib/ruby_matrix/vector_adapter.rb', line 51

def values
  @values || to_a
end