Class: Spark::Mllib::MatrixAdapter

Inherits:
Matrix
  • Object
show all
Defined in:
lib/spark/mllib/ruby_matrix/matrix_adapter.rb

Direct Known Subclasses

MatrixBase

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, rows, cols, values = nil) ⇒ MatrixAdapter

Returns a new instance of MatrixAdapter.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/spark/mllib/ruby_matrix/matrix_adapter.rb', line 23

def initialize(type, rows, cols, values=nil)
  case type
  when :dense
    values = values.dup
    if rows * cols == values.size
      # Values are on one row
      # 2x2 => [1,2,3,4]
      values = values.each_slice(cols).to_a
    else
      # 2x2 => [[1,2], [3,4]]
    end
  when :sparse
    values = Array.new(rows) { Array.new(cols) { 0.0 } }
  else
    raise Spark::MllibError, 'Unknow vector type.'
  end

  super(values, cols)
end

Class Method Details

.new(*args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/spark/mllib/ruby_matrix/matrix_adapter.rb', line 7

def self.new(*args)
  object = self.allocate

  if args.size == 2
    # Matrix is initialized from Matrix
    # Arguments: rows, column count
    object.__send__(:original_initialize, *args)
  else
    object.__send__(:initialize, *args)
  end

  object
end

Instance Method Details

#original_initializeObject



21
# File 'lib/spark/mllib/ruby_matrix/matrix_adapter.rb', line 21

alias_method :original_initialize, :initialize

#shapeObject



43
44
45
# File 'lib/spark/mllib/ruby_matrix/matrix_adapter.rb', line 43

def shape
  [row_count, column_count]
end

#valuesObject



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

def values
  @values || to_a
end