Skip to content

Commit 5bb89c0

Browse files
authored
Merge pull request #182 from ShikharJ/Expr
Add Expr Class
2 parents 296fce1 + 85dc74a commit 5bb89c0

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

symengine/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
LessThan, StrictGreaterThan, StrictLessThan, Eq, Ne, Ge, Le,
1212
Gt, Lt, golden_ratio as GoldenRatio, catalan as Catalan,
1313
eulergamma as EulerGamma, Dummy, perfect_power, integer_nthroot,
14-
isprime, sqrt_mod)
14+
isprime, sqrt_mod, Expr)
1515
from .utilities import var, symbols
1616
from .functions import *
1717

symengine/lib/symengine_wrapper.pyx

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ class SympifyError(Exception):
3131
cdef c2py(RCP[const symengine.Basic] o):
3232
cdef Basic r
3333
if (symengine.is_a_Add(deref(o))):
34-
r = Basic.__new__(Add)
34+
r = Expr.__new__(Add)
3535
elif (symengine.is_a_Mul(deref(o))):
36-
r = Basic.__new__(Mul)
36+
r = Expr.__new__(Mul)
3737
elif (symengine.is_a_Pow(deref(o))):
38-
r = Basic.__new__(Pow)
38+
r = Expr.__new__(Pow)
3939
elif (symengine.is_a_Integer(deref(o))):
4040
if (deref(symengine.rcp_static_cast_Integer(o)).is_zero()):
4141
return S.Zero
@@ -59,7 +59,7 @@ cdef c2py(RCP[const symengine.Basic] o):
5959
elif (symengine.is_a_Symbol(deref(o))):
6060
if (symengine.is_a_PySymbol(deref(o))):
6161
return <object>(deref(symengine.rcp_static_cast_PySymbol(o)).get_py_object())
62-
r = Basic.__new__(Symbol)
62+
r = Expr.__new__(Symbol)
6363
elif (symengine.is_a_Constant(deref(o))):
6464
r = S.Pi
6565
if (symengine.eq(deref(o), deref(r.thisptr))):
@@ -110,9 +110,9 @@ cdef c2py(RCP[const symengine.Basic] o):
110110
elif (symengine.is_a_Gamma(deref(o))):
111111
r = Function.__new__(Gamma)
112112
elif (symengine.is_a_Derivative(deref(o))):
113-
r = Basic.__new__(Derivative)
113+
r = Expr.__new__(Derivative)
114114
elif (symengine.is_a_Subs(deref(o))):
115-
r = Basic.__new__(Subs)
115+
r = Expr.__new__(Subs)
116116
elif (symengine.is_a_RealDouble(deref(o))):
117117
r = Number.__new__(RealDouble)
118118
elif (symengine.is_a_ComplexDouble(deref(o))):
@@ -208,7 +208,7 @@ cdef c2py(RCP[const symengine.Basic] o):
208208
elif (symengine.is_a_PyNumber(deref(o))):
209209
r = PyNumber.__new__(PyNumber)
210210
elif (symengine.is_a_Piecewise(deref(o))):
211-
r = Basic.__new__(Piecewise)
211+
r = Function.__new__(Piecewise)
212212
elif (symengine.is_a_Contains(deref(o))):
213213
r = Boolean.__new__(Contains)
214214
elif (symengine.is_a_Interval(deref(o))):
@@ -1078,7 +1078,11 @@ def series(ex, x=None, x0=0, n=6, as_deg_coef_pair=False):
10781078
return add(*l)
10791079

10801080

1081-
class Symbol(Basic):
1081+
cdef class Expr(Basic):
1082+
pass
1083+
1084+
1085+
class Symbol(Expr):
10821086

10831087
"""
10841088
Symbol is a class to store a symbolic variable with a given name.
@@ -1166,7 +1170,7 @@ def symarray(prefix, shape, **kwargs):
11661170
return arr
11671171

11681172

1169-
cdef class Constant(Basic):
1173+
cdef class Constant(Expr):
11701174

11711175
def __cinit__(self, name = None):
11721176
if name is None:
@@ -1268,7 +1272,7 @@ cdef class EulerGamma(Constant):
12681272
eulergamma = EulerGamma()
12691273

12701274

1271-
cdef class Boolean(Basic):
1275+
cdef class Boolean(Expr):
12721276

12731277
def logical_not(self):
12741278
return c2py(<RCP[const symengine.Basic]>(deref(symengine.rcp_static_cast_Boolean(self.thisptr)).logical_not()))
@@ -1452,7 +1456,7 @@ class StrictLessThan(Relational):
14521456
Lt = StrictLessThan
14531457

14541458

1455-
cdef class Number(Basic):
1459+
cdef class Number(Expr):
14561460
@property
14571461
def is_Atom(self):
14581462
return True
@@ -1583,7 +1587,6 @@ class Integer(Rational):
15831587
def is_integer(self):
15841588
return True
15851589

1586-
@property
15871590
def doit(self, **hints):
15881591
return self
15891592

@@ -1970,7 +1973,7 @@ class Half(Rational):
19701973
half = Half()
19711974

19721975

1973-
class AssocOp(Basic):
1976+
class AssocOp(Expr):
19741977

19751978
@classmethod
19761979
def make_args(cls, expr):
@@ -2086,7 +2089,7 @@ class Mul(AssocOp):
20862089
return d
20872090

20882091

2089-
class Pow(Basic):
2092+
class Pow(Expr):
20902093

20912094
def __new__(cls, a, b):
20922095
return _sympify(a) ** b
@@ -2108,6 +2111,10 @@ class Pow(Basic):
21082111
def is_Pow(self):
21092112
return True
21102113

2114+
@property
2115+
def is_commutative(self):
2116+
return (self.base.is_commutative and self.exp.is_commutative)
2117+
21112118
def _sympy_(Basic self):
21122119
cdef RCP[const symengine.Pow] X = symengine.rcp_static_cast_Pow(self.thisptr)
21132120
base = c2py(deref(X).get_base())
@@ -2125,7 +2132,7 @@ class Pow(Basic):
21252132
return self.__class__
21262133

21272134

2128-
class Function(Basic):
2135+
class Function(Expr):
21292136

21302137
def __new__(cls, *args, **kwargs):
21312138
if cls == Function and len(args) == 1:
@@ -2694,7 +2701,7 @@ class Min(Function):
26942701
return self.__class__
26952702

26962703

2697-
class Derivative(Basic):
2704+
class Derivative(Expr):
26982705

26992706
def __new__(self, expr, *variables):
27002707
if len(variables) == 1 and is_sequence(variables[0]):
@@ -2740,7 +2747,7 @@ class Derivative(Basic):
27402747
return self.__class__
27412748

27422749

2743-
class Subs(Basic):
2750+
class Subs(Expr):
27442751

27452752
def __new__(self, expr, variables, point):
27462753
return sympify(expr).subs(variables, point)
@@ -2791,7 +2798,7 @@ class Subs(Basic):
27912798
return self.__class__
27922799

27932800

2794-
class Piecewise(Basic):
2801+
class Piecewise(Function):
27952802

27962803
def __new__(self, *args):
27972804
return piecewise(*args)
@@ -2805,7 +2812,7 @@ class Piecewise(Basic):
28052812
return sympy.Piecewise(*l)
28062813

28072814

2808-
cdef class Set(Basic):
2815+
cdef class Set(Expr):
28092816

28102817
def intersection(self, a):
28112818
cdef Set other = sympify(a)

0 commit comments

Comments
 (0)