Skip to content

Commit f09fb79

Browse files
committed
reversed methods for cython 3
1 parent 4f16855 commit f09fb79

File tree

1 file changed

+99
-16
lines changed

1 file changed

+99
-16
lines changed

symengine/lib/symengine_wrapper.pyx

Lines changed: 99 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -870,36 +870,71 @@ cdef class Basic(object):
870870
cdef Basic B = B_
871871
return c2py(symengine.add(A.thisptr, B.thisptr))
872872

873+
def __radd__(Basic self, b):
874+
B_ = _sympify(b, False)
875+
if B_ is None or isinstance(B_, MatrixBase): return NotImplemented
876+
cdef Basic B = B_
877+
return c2py(symengine.add(B.thisptr, self.thisptr))
878+
873879
def __sub__(a, b):
874880
cdef Basic A = _sympify(a, False)
875881
B_ = _sympify(b, False)
876882
if A is None or B_ is None or isinstance(B_, MatrixBase): return NotImplemented
877883
cdef Basic B = B_
878884
return c2py(symengine.sub(A.thisptr, B.thisptr))
879885

886+
def __rsub__(Basic self, b):
887+
B_ = _sympify(b, False)
888+
if B_ is None or isinstance(B_, MatrixBase): return NotImplemented
889+
cdef Basic B = B_
890+
return c2py(symengine.sub(B.thisptr, self.thisptr))
891+
880892
def __mul__(a, b):
881893
cdef Basic A = _sympify(a, False)
882894
B_ = _sympify(b, False)
883895
if A is None or B_ is None or isinstance(B_, MatrixBase): return NotImplemented
884896
cdef Basic B = B_
885897
return c2py(symengine.mul(A.thisptr, B.thisptr))
886898

899+
def __rmul__(Basic self, b):
900+
B_ = _sympify(b, False)
901+
if B_ is None or isinstance(B_, MatrixBase): return NotImplemented
902+
cdef Basic B = B_
903+
return c2py(symengine.mul(B.thisptr, self.thisptr))
904+
887905
def __truediv__(a, b):
888906
cdef Basic A = _sympify(a, False)
889-
cdef Basic B = _sympify(b, False)
890-
if A is None or B is None: return NotImplemented
907+
B_ = _sympify(b, False)
908+
if A is None or B_ is None or isinstance(B_, MatrixBase): return NotImplemented
909+
cdef Basic B = B_
891910
return c2py(symengine.div(A.thisptr, B.thisptr))
892911

912+
def __rtruediv__(Basic self, b):
913+
B_ = _sympify(b, False)
914+
if B_ is None or isinstance(B_, MatrixBase): return NotImplemented
915+
cdef Basic B = B_
916+
return c2py(symengine.div(B.thisptr, self.thisptr))
917+
893918
def __floordiv__(x, y):
894919
return floor(x/y)
895920

921+
def __rfloordiv__(y, x):
922+
return floor(x/y)
923+
896924
def __mod__(x, y):
897925
return x - y * floor(x/y)
898926

927+
def __rmod__(y, x):
928+
return x - y * floor(x/y)
929+
899930
def __divmod__(x, y):
900931
f = floor(x/y)
901932
return f, x - y * f
902933

934+
def __rdivmod__(y, x):
935+
f = floor(x/y)
936+
return f, x - y * f
937+
903938
def __pow__(a, b, c):
904939
if c is not None:
905940
return powermod(a, b, c)
@@ -908,6 +943,11 @@ cdef class Basic(object):
908943
if A is None or B is None: return NotImplemented
909944
return c2py(symengine.pow(A.thisptr, B.thisptr))
910945

946+
def __rpow__(Basic self, b):
947+
cdef Basic B = _sympify(b, False)
948+
if B is None: return NotImplemented
949+
return c2py(symengine.pow(B.thisptr, self.thisptr))
950+
911951
def __neg__(Basic self not None):
912952
return c2py(symengine.neg(self.thisptr))
913953

@@ -3392,6 +3432,19 @@ cdef class DenseMatrixBase(MatrixBase):
33923432
raise ShapeError("Invalid shapes for matrix addition. Got %s %s" % (a_.shape, b_.shape))
33933433
return a_.add_matrix(b_)
33943434

3435+
def __radd__(MatrixBase self, a):
3436+
a = _sympify(a, False)
3437+
if not isinstance(a, MatrixBase):
3438+
return NotImplemented
3439+
cdef MatrixBase a_ = a
3440+
if (a_.shape == (0, 0)):
3441+
return self
3442+
if (self.shape == (0, 0)):
3443+
return a_
3444+
if (self.shape != a_.shape):
3445+
raise ShapeError("Invalid shapes for matrix addition. Got %s %s" % (a_.shape, self.shape))
3446+
return a_.add_matrix(self)
3447+
33953448
def __mul__(a, b):
33963449
a = _sympify(a, False)
33973450
b = _sympify(b, False)
@@ -3409,15 +3462,49 @@ cdef class DenseMatrixBase(MatrixBase):
34093462
else:
34103463
return NotImplemented
34113464

3465+
def __rmul__(Basic self, a):
3466+
a = _sympify(a, False)
3467+
if isinstance(a, MatrixBase):
3468+
if (a.ncols() != self.nrows()):
3469+
raise ShapeError("Invalid shapes for matrix multiplication. Got %s %s" % (a.shape, self.shape))
3470+
return a.mul_matrix(self)
3471+
elif isinstance(a, Basic):
3472+
return self.mul_scalar(a)
3473+
else:
3474+
return NotImplemented
3475+
34123476
def __matmul__(a, b):
34133477
a = _sympify(a, False)
34143478
b = _sympify(b, False)
34153479
if (a.ncols() != b.nrows()):
34163480
raise ShapeError("Invalid shapes for matrix multiplication. Got %s %s" % (a.shape, b.shape))
34173481
return a.mul_matrix(b)
34183482

3483+
def __rmatmul__(Basic self, a):
3484+
a = _sympify(a, False)
3485+
if (a.ncols() != self.nrows()):
3486+
raise ShapeError("Invalid shapes for matrix multiplication. Got %s %s" % (a.shape, self.shape))
3487+
return a.mul_matrix(self)
3488+
34193489
def __truediv__(a, b):
3420-
return div_matrices(a, b)
3490+
a = _sympify(a, False)
3491+
b = _sympify(b, False)
3492+
if isinstance(a, MatrixBase):
3493+
if isinstance(b, MatrixBase):
3494+
return a.mul_matrix(b.inv())
3495+
elif isinstance(b, Basic):
3496+
return a.mul_scalar(1/b)
3497+
else:
3498+
return NotImplemented
3499+
else:
3500+
return NotImplemented
3501+
3502+
def __rtruediv__(Basic self, a):
3503+
a = _sympify(a, False)
3504+
if isinstance(a, MatrixBase):
3505+
return a.mul_matrix(self.inv())
3506+
else:
3507+
return NotImplemented
34213508

34223509
def __sub__(a, b):
34233510
a = _sympify(a, False)
@@ -3430,6 +3517,15 @@ cdef class DenseMatrixBase(MatrixBase):
34303517
raise ShapeError("Invalid shapes for matrix subtraction. Got %s %s" % (a.shape, b.shape))
34313518
return a_.add_matrix(-b_)
34323519

3520+
def __rsub__(MatrixBase self, a):
3521+
a = _sympify(a, False)
3522+
if not isinstance(a, MatrixBase):
3523+
return NotImplemented
3524+
cdef MatrixBase a_ = a
3525+
if (a_.shape != self.shape):
3526+
raise ShapeError("Invalid shapes for matrix subtraction. Got %s %s" % (a.shape, self.shape))
3527+
return a_.add_matrix(-self)
3528+
34333529
def __neg__(self):
34343530
return self.mul_scalar(-1)
34353531

@@ -4038,19 +4134,6 @@ cdef class DenseMatrixBase(MatrixBase):
40384134
return self.applyfunc(lambda x : x.expand(*args, **kwargs))
40394135

40404136

4041-
def div_matrices(a, b):
4042-
a = _sympify(a, False)
4043-
b = _sympify(b, False)
4044-
if isinstance(a, MatrixBase):
4045-
if isinstance(b, MatrixBase):
4046-
return a.mul_matrix(b.inv())
4047-
elif isinstance(b, Basic):
4048-
return a.mul_scalar(1/b)
4049-
else:
4050-
return NotImplemented
4051-
else:
4052-
return NotImplemented
4053-
40544137
class DenseMatrixBaseIter(object):
40554138

40564139
def __init__(self, d):

0 commit comments

Comments
 (0)