Skip to content

Commit 18b94bc

Browse files
authored
Merge pull request #184 from isuruf/div
div for matrices
2 parents 5bb89c0 + 92664fc commit 18b94bc

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

symengine/lib/symengine_wrapper.pyx

+20
Original file line numberDiff line numberDiff line change
@@ -3087,6 +3087,12 @@ cdef class DenseMatrixBase(MatrixBase):
30873087
else:
30883088
return NotImplemented
30893089

3090+
def __div__(a, b):
3091+
return div_matrices(a, b)
3092+
3093+
def __truediv__(a, b):
3094+
return div_matrices(a, b)
3095+
30903096
def __sub__(a, b):
30913097
a = _sympify(a, False)
30923098
b = _sympify(b, False)
@@ -3556,6 +3562,20 @@ cdef class DenseMatrixBase(MatrixBase):
35563562
def expand(self, *args, **kwargs):
35573563
return self.applyfunc(lambda x : x.expand())
35583564

3565+
3566+
def div_matrices(a, b):
3567+
a = _sympify(a, False)
3568+
b = _sympify(b, False)
3569+
if isinstance(a, MatrixBase):
3570+
if isinstance(b, MatrixBase):
3571+
return a.mul_matrix(b.inv())
3572+
elif isinstance(b, Basic):
3573+
return a.mul_scalar(1/b)
3574+
else:
3575+
return NotImplemented
3576+
else:
3577+
return NotImplemented
3578+
35593579
class DenseMatrixBaseIter(object):
35603580

35613581
def __init__(self, d):

symengine/tests/test_matrices.py

+13
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,19 @@ def test_sub():
217217
raises(TypeError, lambda: 5 - A)
218218

219219

220+
def test_div():
221+
w, x, y, z = symbols("w, x, y, z")
222+
A = DenseMatrix([[w, x], [y, z]])
223+
B = DenseMatrix([[1, 1], [1, 0]])
224+
C = DenseMatrix([[x, w - x], [z, y - z]])
225+
226+
assert A / 2 == DenseMatrix([[w/2, x/2], [y/2, z/2]])
227+
assert C * B == A
228+
assert A / B == C
229+
230+
raises(TypeError, lambda: 2/A)
231+
232+
220233
def test_transpose():
221234
A = DenseMatrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9])
222235

0 commit comments

Comments
 (0)