Skip to content

div for matrices #184

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

Merged
merged 2 commits into from
Sep 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions symengine/lib/symengine_wrapper.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3087,6 +3087,12 @@ cdef class DenseMatrixBase(MatrixBase):
else:
return NotImplemented

def __div__(a, b):
return div_matrices(a, b)

def __truediv__(a, b):
return div_matrices(a, b)

def __sub__(a, b):
a = _sympify(a, False)
b = _sympify(b, False)
Expand Down Expand Up @@ -3556,6 +3562,20 @@ cdef class DenseMatrixBase(MatrixBase):
def expand(self, *args, **kwargs):
return self.applyfunc(lambda x : x.expand())


def div_matrices(a, b):
a = _sympify(a, False)
b = _sympify(b, False)
if isinstance(a, MatrixBase):
if isinstance(b, MatrixBase):
return a.mul_matrix(b.inv())
elif isinstance(b, Basic):
return a.mul_scalar(1/b)
else:
return NotImplemented
else:
return NotImplemented

class DenseMatrixBaseIter(object):

def __init__(self, d):
Expand Down
13 changes: 13 additions & 0 deletions symengine/tests/test_matrices.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,19 @@ def test_sub():
raises(TypeError, lambda: 5 - A)


def test_div():
w, x, y, z = symbols("w, x, y, z")
A = DenseMatrix([[w, x], [y, z]])
B = DenseMatrix([[1, 1], [1, 0]])
C = DenseMatrix([[x, w - x], [z, y - z]])

assert A / 2 == DenseMatrix([[w/2, x/2], [y/2, z/2]])
assert C * B == A
assert A / B == C

raises(TypeError, lambda: 2/A)


def test_transpose():
A = DenseMatrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9])

Expand Down