本文整理汇总了Python中sympify._sympify函数的典型用法代码示例。如果您正苦于以下问题:Python _sympify函数的具体用法?Python _sympify怎么用?Python _sympify使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了_sympify函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __new__
def __new__(cls, lhs, rhs, rop=None, **assumptions):
lhs = _sympify(lhs)
rhs = _sympify(rhs)
if cls is not Relational:
rop_cls = cls
else:
try:
rop_cls = Relational.ValidRelationOperator[rop]
except KeyError:
msg = "Invalid relational operator symbol: '%r'"
raise ValueError(msg % repr(rop))
if lhs.is_number and rhs.is_number and (rop_cls in (Equality, Unequality) or lhs.is_real and rhs.is_real):
diff = lhs - rhs
know = diff.equals(0, failing_expression=True)
if know is True: # exclude failing expression case
Nlhs = S.Zero
elif know is False:
from sympy import sign
Nlhs = sign(diff.n(1))
else:
Nlhs = None
lhs = know
rhs = S.Zero
if Nlhs is not None:
return rop_cls._eval_relation(Nlhs, S.Zero)
obj = Expr.__new__(rop_cls, lhs, rhs, **assumptions)
return obj
示例2: __new__
def __new__(cls, start, end, left_open=False, right_open=False):
start = _sympify(start)
end = _sympify(end)
# Only allow real intervals (use symbols with 'is_real=True').
if not start.is_real or not end.is_real:
raise ValueError("Only real intervals are supported")
# Make sure that the created interval will be valid.
if end.is_comparable and start.is_comparable:
if end < start:
return S.EmptySet
if end == start and (left_open or right_open):
return S.EmptySet
if end == start and not (left_open or right_open):
return FiniteSet(end)
# Make sure infinite interval end points are open.
if start == S.NegativeInfinity:
left_open = True
if end == S.Infinity:
right_open = True
return Basic.__new__(cls, start, end, left_open, right_open)
示例3: compare_pretty
def compare_pretty(a, b):
"""
Is a > b in the sense of ordering in printing?
::
yes ..... return 1
no ...... return -1
equal ... return 0
Strategy:
It uses Basic.compare as a fallback, but improves it in many cases,
like x**3, x**4, O(x**3) etc. In those simple cases, it just parses the
expression and returns the "sane" ordering such as::
1 < x < x**2 < x**3 < O(x**4) etc.
Examples
========
>>> from sympy.abc import x
>>> from sympy import Basic, Number
>>> Basic._compare_pretty(x, x**2)
-1
>>> Basic._compare_pretty(x**2, x**2)
0
>>> Basic._compare_pretty(x**3, x**2)
1
>>> Basic._compare_pretty(Number(1, 2), Number(1, 3))
1
>>> Basic._compare_pretty(Number(0), Number(-1))
1
"""
try:
a = _sympify(a)
except SympifyError:
pass
try:
b = _sympify(b)
except SympifyError:
pass
# both objects are non-SymPy
if (not isinstance(a, Basic)) and (not isinstance(b, Basic)):
return cmp(a,b)
if not isinstance(a, Basic):
return -1 # other < sympy
if not isinstance(b, Basic):
return +1 # sympy > other
# now both objects are from SymPy, so we can proceed to usual comparison
return cmp(a.sort_key(), b.sort_key())
示例4: __new__
def __new__(cls, lhs, rhs, rop=None, **assumptions):
lhs = _sympify(lhs)
rhs = _sympify(rhs)
if cls is not Relational:
rop_cls = cls
else:
rop_cls, swap = Relational.get_relational_class(rop)
if swap: lhs, rhs = rhs, lhs
obj = Basic.__new__(rop_cls, lhs, rhs, **assumptions)
return obj
示例5: __new__
def __new__(cls, lhs, rhs, rop=None, **assumptions):
lhs = _sympify(lhs)
rhs = _sympify(rhs)
if cls is not Relational:
rop_cls = cls
else:
rop_cls, swap = Relational.get_relational_class(rop)
if swap: lhs, rhs = rhs, lhs
if lhs.is_real and lhs.is_number and rhs.is_real and rhs.is_number:
return rop_cls._eval_relation(lhs.evalf(), rhs.evalf())
else:
obj = Expr.__new__(rop_cls, lhs, rhs, **assumptions)
return obj
示例6: __new__
def __new__(cls, a, b, **assumptions):
a = _sympify(a)
b = _sympify(b)
if assumptions.get("evaluate") is False:
return Basic.__new__(cls, a, b, **assumptions)
if b is S.Zero:
return S.One
if b is S.One:
return a
obj = a._eval_power(b)
if obj is None:
obj = Basic.__new__(cls, a, b, **assumptions)
obj.is_commutative = a.is_commutative and b.is_commutative
return obj
示例7: __new__
def __new__(cls, b, e, **assumptions):
b = _sympify(b)
e = _sympify(e)
if assumptions.get("evaluate") is False:
return Basic.__new__(cls, b, e, **assumptions)
if e is S.Zero:
return S.One
if e is S.One:
return b
obj = b._eval_power(e)
if obj is None:
obj = Basic.__new__(cls, b, e, **assumptions)
obj.is_commutative = b.is_commutative and e.is_commutative
return obj
示例8: __new__
def __new__(cls, b, e, evaluate=True):
b = _sympify(b)
e = _sympify(e)
if evaluate:
if e is S.Zero:
return S.One
elif e is S.One:
return b
else:
obj = b._eval_power(e)
if obj is not None:
return obj
obj = Expr.__new__(cls, b, e)
obj.is_commutative = (b.is_commutative and e.is_commutative)
return obj
示例9: __new__
def __new__(cls, b, e, **assumptions):
b = _sympify(b)
e = _sympify(e)
if assumptions.pop('evaluate', True):
if e is S.Zero:
return S.One
elif e is S.One:
return b
else:
obj = b._eval_power(e)
if obj is not None:
return obj
obj = Expr.__new__(cls, b, e, **assumptions)
obj.is_commutative = (b.is_commutative and e.is_commutative)
return obj
示例10: __new__
def __new__(cls, lhs, rhs, rop=None, **assumptions):
lhs = _sympify(lhs)
rhs = _sympify(rhs)
if cls is not Relational:
rop_cls = cls
else:
try:
rop_cls = cls.ValidRelationOperator[ rop ]
except KeyError:
msg = "Invalid relational operator symbol: '%r'"
raise ValueError(msg % repr(rop))
diff = S.NaN
if isinstance(lhs, Expr) and isinstance(rhs, Expr):
diff = lhs - rhs
if not (diff is S.NaN or diff.has(Symbol)):
know = diff.equals(0, failing_expression=True)
if know is True: # exclude failing expression case
diff = S.Zero
elif know is False:
diff = diff.n()
if rop_cls is Equality:
if (lhs == rhs) is True or (diff == S.Zero) is True:
return True
elif diff is S.NaN:
pass
elif diff.is_Number or diff.is_Float:
return False
elif lhs.is_real is not rhs.is_real and \
lhs.is_real is not None and \
rhs.is_real is not None:
return False
elif rop_cls is Unequality:
if (lhs == rhs) is True or (diff == S.Zero) is True:
return False
elif diff is S.NaN:
pass
elif diff.is_Number or diff.is_Float:
return True
elif lhs.is_real is not rhs.is_real and \
lhs.is_real is not None and \
rhs.is_real is not None:
return True
elif diff.is_Number and diff.is_real:
return rop_cls._eval_relation(diff, S.Zero)
obj = Expr.__new__(rop_cls, lhs, rhs, **assumptions)
return obj
示例11: __eq__
def __eq__(self, other):
"""a == b -> Compare two symbolic trees and see whether they are equal
this is the same as:
a.compare(b) == 0
but faster
"""
if type(self) is not type(other):
# issue 3001 a**1.0 == a like a**2.0 == a**2
while isinstance(self, C.Pow) and self.exp == 1:
self = self.base
while isinstance(other, C.Pow) and other.exp == 1:
other = other.base
try:
other = _sympify(other)
except SympifyError:
return False # sympy != other
if type(self) is not type(other):
return False
# type(self) == type(other)
st = self._hashable_content()
ot = other._hashable_content()
return st == ot and self._assume_type_keys == other._assume_type_keys
示例12: matches
def matches(self, expr, repl_dict={}, old=False):
expr = _sympify(expr)
# special case, pattern = 1 and expr.exp can match to 0
if expr is S.One:
d = repl_dict.copy()
d = self.exp.matches(S.Zero, d)
if d is not None:
return d
b, e = expr.as_base_exp()
# special case number
sb, se = self.as_base_exp()
if sb.is_Symbol and se.is_Integer and expr:
if e.is_rational:
return sb.matches(b**(e/se), repl_dict)
return sb.matches(expr**(1/se), repl_dict)
d = repl_dict.copy()
d = self.base.matches(b, d)
if d is None:
return None
d = self.exp.xreplace(d).matches(e, d)
if d is None:
return Expr.matches(self, expr, repl_dict)
return d
示例13: matches
def matches(pattern, expr, repl_dict={}, evaluate=False):
if evaluate:
pat = pattern
for old, new in repl_dict.items():
pat = pat.subs(old, new)
if pat != pattern:
return pat.matches(expr, repl_dict)
expr = _sympify(expr)
b, e = expr.as_base_exp()
# special case, pattern = 1 and expr.exp can match to 0
if expr is S.One:
d = repl_dict.copy()
d = pattern.exp.matches(S.Zero, d, evaluate=False)
if d is not None:
return d
d = repl_dict.copy()
d = pattern.base.matches(b, d, evaluate=False)
if d is None:
return None
d = pattern.exp.matches(e, d, evaluate=True)
if d is None:
return Basic.matches(pattern, expr, repl_dict, evaluate)
return d
示例14: __mul__
def __mul__(self, other):
try:
other = _sympify(other)
except SympifyError:
return NotImplemented
if isinstance(other, Number):
rhs, prec = other._as_mpf_op(self._prec)
return Real._new(mlib.mpf_mul(self._mpf_, rhs, prec, rnd), prec)
return Number.__mul__(self, other)
示例15: __ne__
def __ne__(self, other):
try:
other = _sympify(other)
except SympifyError:
return True # sympy != other
if self is other: return False
if isinstance(other, Number) and self.is_irrational: return True
return True # NumberSymbol != non(Number|self)