Class: Matrix

Inherits:
Object
  • Object
show all
Defined in:
lib/mirlo/extensions/matrix.rb

Instance Method Summary collapse

Instance Method Details

#apply_elementwise(other, &op) ⇒ Object

Public: given two matrices of equal dimensions, apply an operation elementwise.

Returns a new matrix with the results of the operation.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mirlo/extensions/matrix.rb', line 14

def apply_elementwise(other, &op)
  unless shape == other.shape
    raise ArgumentError.new 'To perform an element wise operation, matrices must be of the same dimension.'
  end

  new_rows = row_count.times.collect do |row|
    column_count.times.collect do |column|
      op.call(self[row, column], other[row, column])
    end
  end

  Matrix.rows(new_rows)
end

#shapeObject



5
6
7
# File 'lib/mirlo/extensions/matrix.rb', line 5

def shape
  [row_count, column_count]
end