Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding scalar Addition and Substraction #685

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 35 additions & 2 deletions lib/matrix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,10 @@ def *(m) # m is matrix or vector or number
def +(m)
case m
when Numeric
Matrix.Raise ErrOperationNotDefined, "+", self.class, m.class
rows = @rows.collect {|row|
row.collect {|e| e + m }
}
return new_matrix rows, column_count
when Vector
m = self.class.column_vector(m)
when Matrix
Expand All @@ -913,7 +916,10 @@ def +(m)
def -(m)
case m
when Numeric
Matrix.Raise ErrOperationNotDefined, "-", self.class, m.class
rows = @rows.collect {|row|
row.collect {|e| e - m }
}
return new_matrix rows, column_count
when Vector
m = self.class.column_vector(m)
when Matrix
Expand Down Expand Up @@ -951,6 +957,33 @@ def /(other)
end
end

#
# Element-wise division
# Matrix[[1,2], [3,4]].element_division(Matrix[[1,2], [3,2]])
# => 1 1
# 1 2
#
def element_division(m)
case m
when Numeric
return self./ m
when Vector
m = self.class.column_vector(m)
when Matrix
else
return apply_through_coercion(m, __method__)
end

Matrix.Raise ErrDimensionMismatch unless row_count == m.row_count && column_count == m.column_count

rows = Array.new(row_count) do |i|
Array.new(column_count) do|j|
self[i, j] / m[i, j]
end
end
new_matrix rows, column_count
end

#
# Returns the inverse of the matrix.
# Matrix[[-1, -1], [0, -1]].inverse
Expand Down
18 changes: 16 additions & 2 deletions test/matrix/test_matrix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,9 @@ def test_add
assert_equal(Matrix[[3,5,7],[9,11,13]], @m1 + @n1)
assert_equal(Matrix[[3,5,7],[9,11,13]], @n1 + @m1)
assert_equal(Matrix[[2],[4],[6]], Matrix[[1],[2],[3]] + Vector[1,2,3])
assert_raise(Matrix::ErrOperationNotDefined) { @m1 + 1 }
assert_equal(Matrix[[2,3,4], [5,6,7]], @m4 + 1)
assert_equal(Matrix[[2.5,3.5,4.5], [5.5,6.5,7.5]], @m1 + 1.5)
assert_equal(@m1, @m1 + 0)
o = Object.new
def o.coerce(m)
[m, m]
Expand All @@ -325,7 +327,9 @@ def test_sub
assert_equal(Matrix[[-1,-1,-1],[-1,-1,-1]], @m1 - @n1)
assert_equal(Matrix[[1,1,1],[1,1,1]], @n1 - @m1)
assert_equal(Matrix[[0],[0],[0]], Matrix[[1],[2],[3]] - Vector[1,2,3])
assert_raise(Matrix::ErrOperationNotDefined) { @m1 - 1 }
assert_equal(Matrix[[-1,0,1], [2,3,4]], @m4 - 2)
assert_equal(Matrix[[0.5,1.5,2.5], [3.5,4.5,5.5]], @m1 - 0.5)
assert_equal(@m1, @m1 - 0)
o = Object.new
def o.coerce(m)
[m, m]
Expand All @@ -343,6 +347,16 @@ def o.coerce(m)
assert_equal(Matrix[[1,1],[1,1]], Matrix[[2,2],[2,2]] / o)
end

def test_element_div
assert_equal(Matrix[[0,1,1],[2,2,3]], @m1.element_division(2))
assert_equal(Matrix[[1, 1, 1], [1, 1, 1]], @m1.element_division(@m1))
assert_equal(Matrix[[2, 1, 2]], Matrix[[2, 4, 6]].element_division(Matrix[[1, 4, 3]]))
assert_equal(Matrix[[2], [1], [2]], Matrix[[2], [4], [6]].element_division(Vector[1, 4, 3]))
assert_equal(Matrix[[2, 1, 2], [2, 2, 1]], Matrix[[2, 4, 6], [8, 10, 12]].element_division(Matrix[[1, 4, 3], [4, 5, 12]]))
assert_raise(Matrix::ErrDimensionMismatch) { @m1.element_division(Vector[[1, 2, 3]]) }
assert_raise(Matrix::ErrDimensionMismatch) { @m1.element_division(Matrix[[1,2], [4,5]]) }
end

def test_exp
assert_equal(Matrix[[67,96],[48,99]], Matrix[[7,6],[3,9]] ** 2)
assert_equal(Matrix.I(5), Matrix.I(5) ** -1)
Expand Down