@@ -870,36 +870,71 @@ cdef class Basic(object):
870
870
cdef Basic B = B_
871
871
return c2py(symengine.add(A.thisptr, B.thisptr))
872
872
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
+
873
879
def __sub__ (a , b ):
874
880
cdef Basic A = _sympify(a, False )
875
881
B_ = _sympify(b, False )
876
882
if A is None or B_ is None or isinstance (B_, MatrixBase): return NotImplemented
877
883
cdef Basic B = B_
878
884
return c2py(symengine.sub(A.thisptr, B.thisptr))
879
885
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
+
880
892
def __mul__ (a , b ):
881
893
cdef Basic A = _sympify(a, False )
882
894
B_ = _sympify(b, False )
883
895
if A is None or B_ is None or isinstance (B_, MatrixBase): return NotImplemented
884
896
cdef Basic B = B_
885
897
return c2py(symengine.mul(A.thisptr, B.thisptr))
886
898
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
+
887
905
def __truediv__ (a , b ):
888
906
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_
891
910
return c2py(symengine.div(A.thisptr, B.thisptr))
892
911
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
+
893
918
def __floordiv__ (x , y ):
894
919
return floor(x/ y)
895
920
921
+ def __rfloordiv__ (y , x ):
922
+ return floor(x/ y)
923
+
896
924
def __mod__ (x , y ):
897
925
return x - y * floor(x/ y)
898
926
927
+ def __rmod__ (y , x ):
928
+ return x - y * floor(x/ y)
929
+
899
930
def __divmod__ (x , y ):
900
931
f = floor(x/ y)
901
932
return f, x - y * f
902
933
934
+ def __rdivmod__ (y , x ):
935
+ f = floor(x/ y)
936
+ return f, x - y * f
937
+
903
938
def __pow__ (a , b , c ):
904
939
if c is not None :
905
940
return powermod(a, b, c)
@@ -908,6 +943,11 @@ cdef class Basic(object):
908
943
if A is None or B is None : return NotImplemented
909
944
return c2py(symengine.pow(A.thisptr, B.thisptr))
910
945
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
+
911
951
def __neg__ (Basic self not None ):
912
952
return c2py(symengine.neg(self .thisptr))
913
953
@@ -3392,6 +3432,19 @@ cdef class DenseMatrixBase(MatrixBase):
3392
3432
raise ShapeError(" Invalid shapes for matrix addition. Got %s %s " % (a_.shape, b_.shape))
3393
3433
return a_.add_matrix(b_)
3394
3434
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
+
3395
3448
def __mul__ (a , b ):
3396
3449
a = _sympify(a, False )
3397
3450
b = _sympify(b, False )
@@ -3409,15 +3462,49 @@ cdef class DenseMatrixBase(MatrixBase):
3409
3462
else :
3410
3463
return NotImplemented
3411
3464
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
+
3412
3476
def __matmul__ (a , b ):
3413
3477
a = _sympify(a, False )
3414
3478
b = _sympify(b, False )
3415
3479
if (a.ncols() != b.nrows()):
3416
3480
raise ShapeError(" Invalid shapes for matrix multiplication. Got %s %s " % (a.shape, b.shape))
3417
3481
return a.mul_matrix(b)
3418
3482
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
+
3419
3489
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
3421
3508
3422
3509
def __sub__ (a , b ):
3423
3510
a = _sympify(a, False )
@@ -3430,6 +3517,15 @@ cdef class DenseMatrixBase(MatrixBase):
3430
3517
raise ShapeError(" Invalid shapes for matrix subtraction. Got %s %s " % (a.shape, b.shape))
3431
3518
return a_.add_matrix(- b_)
3432
3519
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
+
3433
3529
def __neg__ (self ):
3434
3530
return self .mul_scalar(- 1 )
3435
3531
@@ -4038,19 +4134,6 @@ cdef class DenseMatrixBase(MatrixBase):
4038
4134
return self .applyfunc(lambda x : x.expand(* args, ** kwargs))
4039
4135
4040
4136
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
-
4054
4137
class DenseMatrixBaseIter (object ):
4055
4138
4056
4139
def __init__ (self , d ):
0 commit comments