Skip to content

Commit cb5be46

Browse files
committed
Fix Max and Min
1 parent 7ce3b5a commit cb5be46

File tree

3 files changed

+36
-57
lines changed

3 files changed

+36
-57
lines changed

symengine/lib/symengine_wrapper.pxd

-6
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,6 @@ cdef class PyFunction(FunctionSymbol):
130130
cdef class Abs(Function):
131131
pass
132132

133-
cdef class _Max(Function):
134-
pass
135-
136-
cdef class _Min(Function):
137-
pass
138-
139133
cdef class Gamma(Function):
140134
pass
141135

symengine/lib/symengine_wrapper.pyx

+35-36
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ cdef c2py(RCP[const symengine.Basic] o):
4646
elif (symengine.is_a_Abs(deref(o))):
4747
r = Abs.__new__(Abs)
4848
elif (symengine.is_a_Max(deref(o))):
49-
r = _Max.__new__(_Max)
49+
r = Max.__new__(Max)
5050
elif (symengine.is_a_Min(deref(o))):
51-
r = _Min.__new__(_Min)
51+
r = Min.__new__(Min)
5252
elif (symengine.is_a_Gamma(deref(o))):
5353
r = Gamma.__new__(Gamma)
5454
elif (symengine.is_a_Derivative(deref(o))):
@@ -662,6 +662,20 @@ cdef class Basic(object):
662662
def has(self, *symbols):
663663
return any([has_symbol(self, symbol) for symbol in symbols])
664664

665+
def args_as_sage(Basic self):
666+
cdef symengine.vec_basic Y = deref(self.thisptr).get_args()
667+
s = []
668+
for i in range(Y.size()):
669+
s.append(c2py(<RCP[const symengine.Basic]>(Y[i]))._sage_())
670+
return s
671+
672+
def args_as_sympy(Basic self):
673+
cdef symengine.vec_basic Y = deref(self.thisptr).get_args()
674+
s = []
675+
for i in range(Y.size()):
676+
s.append(c2py(<RCP[const symengine.Basic]>(Y[i]))._sympy_())
677+
return s
678+
665679
def series(ex, x=None, x0=0, n=6, as_deg_coef_pair=False):
666680
# TODO: check for x0 an infinity, see sympy/core/expr.py
667681
# TODO: nonzero x0
@@ -1400,58 +1414,43 @@ cdef class Abs(Function):
14001414
arg = c2py(deref(X).get_arg())._sage_()
14011415
return abs(arg)
14021416

1403-
cdef class _Max(Function):
1404-
'''
1405-
Class named as such to prevent namespace issues with
1406-
Python's min. Import as Min for aesthetics.
1407-
'''
1417+
1418+
class Max(Function):
1419+
1420+
def __new__(cls, *args):
1421+
if not args:
1422+
return super(Max, cls).__new__(cls)
1423+
return _max(*args)
14081424

14091425
def _sympy_(self):
1410-
cdef RCP[const symengine.Max] X = \
1411-
symengine.rcp_static_cast_Max(self.thisptr)
1412-
cdef symengine.vec_basic Y = deref(X).get_args()
1413-
s = []
1414-
for i in range(Y.size()):
1415-
s.append(c2py(<RCP[const symengine.Basic]>(Y[i]))._sympy_())
14161426
import sympy
1427+
s = self.args_as_sympy()
14171428
return sympy.Max(*s)
14181429

14191430
def _sage_(self):
14201431
import sage.all as sage
1421-
cdef RCP[const symengine.Max] X = \
1422-
symengine.rcp_static_cast_Max(self.thisptr)
1423-
cdef symengine.vec_basic Y = deref(X).get_args()
1424-
s = []
1425-
for i in range(Y.size()):
1426-
s.append(c2py(<RCP[const symengine.Basic]>(Y[i]))._sage_())
1432+
s = self.args_as_sage()
14271433
return sage.max(*s)
14281434

1429-
cdef class _Min(Function):
1430-
'''
1431-
Class named as such to prevent namespace issues with
1432-
Python's min. Import as Min for aesthetics.
1433-
'''
1435+
1436+
class Min(Function):
1437+
1438+
def __new__(cls, *args):
1439+
if not args:
1440+
return super(Min, cls).__new__(cls)
1441+
return _min(*args)
14341442

14351443
def _sympy_(self):
1436-
cdef RCP[const symengine.Min] X = \
1437-
symengine.rcp_static_cast_Min(self.thisptr)
1438-
cdef symengine.vec_basic Y = deref(X).get_args()
1439-
s = []
1440-
for i in range(Y.size()):
1441-
s.append(c2py(<RCP[const symengine.Basic]>(Y[i]))._sympy_())
14421444
import sympy
1445+
s = self.args_as_sympy()
14431446
return sympy.Min(*s)
14441447

14451448
def _sage_(self):
14461449
import sage.all as sage
1447-
cdef RCP[const symengine.Min] X = \
1448-
symengine.rcp_static_cast_Min(self.thisptr)
1449-
cdef symengine.vec_basic Y = deref(X).get_args()
1450-
s = []
1451-
for i in range(Y.size()):
1452-
s.append(c2py(<RCP[const symengine.Basic]>(Y[i]))._sage_())
1450+
s = self.args_as_sage()
14531451
return sage.min(*s)
14541452

1453+
14551454
cdef class Derivative(Basic):
14561455

14571456
@property

symengine/sympy_compat.py

+1-15
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
SympifyError, sqrt, I, E, pi, Matrix, Derivative, exp,
66
nextprime, mod_inverse, primitive_root, Lambdify as lambdify,
77
symarray, diff, eye, diag, ones, zeros, expand, Subs,
8-
FunctionSymbol as AppliedUndef)
8+
FunctionSymbol as AppliedUndef, Max, Min)
99
from types import ModuleType
1010
import sys
1111

@@ -151,20 +151,6 @@ def __new__(cls, a):
151151
return symengine.tan(a)
152152

153153

154-
class Max(Basic):
155-
_classes = (symengine._Max,)
156-
157-
def __new__(cls, *args):
158-
return symengine._max(*args)
159-
160-
161-
class Min(Basic):
162-
_classes = (symengine._Min,)
163-
164-
def __new__(cls, *args):
165-
return symengine._min(*args)
166-
167-
168154
class gamma(_RegisteredFunction):
169155
_classes = (symengine.Gamma,)
170156

0 commit comments

Comments
 (0)