Class: Brainy::JMatrix

Inherits:
Object
  • Object
show all
Defined in:
lib/brainy/jblas.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ JMatrix

Returns a new instance of JMatrix.



7
8
9
10
11
12
13
14
15
# File 'lib/brainy/jblas.rb', line 7

def initialize(data)
  if data.is_a?(DoubleMatrix)
    @java_matrix = data
  elsif data.first.is_a?(Array)
    @java_matrix = DoubleMatrix.new(data.to_java(Java::double[]))
  else
    @java_matrix = DoubleMatrix.new(data.to_java(Java::double))
  end
end

Instance Attribute Details

#java_matrixObject

Returns the value of attribute java_matrix.



5
6
7
# File 'lib/brainy/jblas.rb', line 5

def java_matrix
  @java_matrix
end

Class Method Details

.build(row_count, column_count, &block) ⇒ Object

TODO refactor for performance (!!!)



17
18
19
20
21
# File 'lib/brainy/jblas.rb', line 17

def self.build(row_count, column_count, &block) #TODO refactor for performance (!!!)
  JMatrix.new(row_count.times.map do |row|
    column_count.times.map { |col| block.yield(row, col) }
  end)
end

Instance Method Details

#*(x) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/brainy/jblas.rb', line 23

def *(x)
  if x.is_a? JMatrix
    JMatrix.new(@java_matrix.mmul(x.java_matrix))
  else
    JMatrix.new(@java_matrix.mul(x))
  end
end

#-(mat) ⇒ Object



31
32
33
# File 'lib/brainy/jblas.rb', line 31

def -(mat)
  JMatrix.new(@java_matrix.sub(mat.java_matrix))
end

#[](idx) ⇒ Object



62
63
64
# File 'lib/brainy/jblas.rb', line 62

def [](idx)
  @java_matrix.get(idx)
end

#columnsObject



49
50
51
# File 'lib/brainy/jblas.rb', line 49

def columns
  @java_matrix.columns
end

#each_with_index(&block) ⇒ Object



58
59
60
# File 'lib/brainy/jblas.rb', line 58

def each_with_index(&block)
  to_a.each_with_index(&block)
end

#map(&block) ⇒ Object



53
54
55
56
# File 'lib/brainy/jblas.rb', line 53

def map(&block)
  return to_a.map { |a| a.to_a.map(&block) } if to_a.first.is_a?(Array)
  to_a.map(&block)
end

#row_vectorsObject



41
42
43
# File 'lib/brainy/jblas.rb', line 41

def row_vectors
  @java_matrix.rowsAsList.toArray.map { |row| JMatrix.new(row) }
end

#rowsObject



45
46
47
# File 'lib/brainy/jblas.rb', line 45

def rows
  @java_matrix.rows
end

#to_aObject



35
36
37
38
39
# File 'lib/brainy/jblas.rb', line 35

def to_a
  return @java_matrix.getRow(0).toArray.to_a if rows == 1
  return @java_matrix.getColumn(0).toArray.to_a if columns == 1
  @java_matrix.rowsAsList.toArray.map { |row| row.toArray.to_a }
end