Method: NMatrix#+

Defined in:
lib/nmatrix/jruby/operators.rb

#+(other) ⇒ Object

A dummy matrix is a matrix without the elements atrribute. NMatrix#create_dummy_matrix prevents creating copies as @s is set explicitly.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/nmatrix/jruby/operators.rb', line 5

def +(other)
  result = create_dummy_nmatrix
  if (other.is_a?(NMatrix))
    #check dimension
    raise(ShapeError, "Cannot add matrices with different dimension") if (@dim != other.dim)
    #check shape
    (0...dim).each do |i|
      raise(ShapeError, "Cannot add matrices with different shapes") if (@shape[i] != other.shape[i])
    end
    result.s = @s.copy.add(other.s)
  else
    result.s = @s.copy.mapAddToSelf(other)
  end
  result
end