本文整理汇总了Python中expr.Expr.__new__方法的典型用法代码示例。如果您正苦于以下问题:Python Expr.__new__方法的具体用法?Python Expr.__new__怎么用?Python Expr.__new__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类expr.Expr
的用法示例。
在下文中一共展示了Expr.__new__方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __new__
# 需要导入模块: from expr import Expr [as 别名]
# 或者: from expr.Expr import __new__ [as 别名]
def __new__(cls, *args, **options):
if len(args) == 0:
return cls.identity
args = map(_sympify, args)
if len(args) == 1:
return args[0]
if not options.pop('evaluate', True):
obj = Expr.__new__(cls, *args)
obj.is_commutative = all(a.is_commutative for a in args)
return obj
c_part, nc_part, order_symbols = cls.flatten(args)
if len(c_part) + len(nc_part) <= 1:
if c_part:
obj = c_part[0]
elif nc_part:
obj = nc_part[0]
else:
obj = cls.identity
else:
obj = Expr.__new__(cls, *(c_part + nc_part))
obj.is_commutative = not nc_part
if order_symbols is not None:
obj = C.Order(obj, *order_symbols)
return obj
示例2: __new__
# 需要导入模块: from expr import Expr [as 别名]
# 或者: from expr.Expr import __new__ [as 别名]
def __new__(cls, expr, *symbols, **assumptions):
expr = sympify(expr)
if not symbols:
return expr
symbols = Derivative._symbolgen(*symbols)
if expr.is_commutative:
assumptions['commutative'] = True
evaluate = assumptions.pop('evaluate', False)
if not evaluate and not isinstance(expr, Derivative):
symbols = list(symbols)
if len(symbols) == 0:
# We make a special case for 0th derivative, because there
# is no good way to unambiguously print this.
return expr
obj = Expr.__new__(cls, expr, *symbols, **assumptions)
return obj
unevaluated_symbols = []
for s in symbols:
s = sympify(s)
if not isinstance(s, C.Symbol):
raise ValueError('Invalid literal: %s is not a valid variable' % s)
obj = expr._eval_derivative(s)
if obj is None:
unevaluated_symbols.append(s)
elif obj is S.Zero:
return S.Zero
else:
expr = obj
if not unevaluated_symbols:
return expr
return Expr.__new__(cls, expr, *unevaluated_symbols, **assumptions)
示例3: __new__
# 需要导入模块: from expr import Expr [as 别名]
# 或者: from expr.Expr import __new__ [as 别名]
def __new__(cls, p, q = None):
if q is None:
if isinstance(p, basestring):
p, q = _parse_rational(p)
elif isinstance(p, Rational):
return p
else:
return Integer(p)
q = int(q)
p = int(p)
if q==0:
if p==0:
if _errdict["divide"]:
raise ValueError("Indeterminate 0/0")
else:
return S.NaN
if p<0: return S.NegativeInfinity
return S.Infinity
if q<0:
q = -q
p = -p
n = igcd(abs(p), q)
if n>1:
p //= n
q //= n
if q==1: return Integer(p)
if p==1 and q==2: return S.Half
obj = Expr.__new__(cls)
obj.p = p
obj.q = q
#obj._args = (p, q)
return obj
示例4: __new__
# 需要导入模块: from expr import Expr [as 别名]
# 或者: from expr.Expr import __new__ [as 别名]
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
示例5: __new__
# 需要导入模块: from expr import Expr [as 别名]
# 或者: from expr.Expr import __new__ [as 别名]
def __new__(cls, expr, variables, point, **assumptions):
if not ordered_iter(variables, Tuple):
variables = [variables]
variables = Tuple(*sympify(variables))
if uniq(variables) != variables:
repeated = repeated = [ v for v in set(variables)
if list(variables).count(v) > 1 ]
raise ValueError('cannot substitute expressions %s more than '
'once.' % repeated)
if not ordered_iter(point, Tuple):
point = [point]
point = Tuple(*sympify(point))
if len(point) != len(variables):
raise ValueError('Number of point values must be the same as '
'the number of variables.')
# it's necessary to use dummy variables internally
new_variables = Tuple(*[ arg.as_dummy() if arg.is_Symbol else
C.Dummy(str(arg)) for arg in variables ])
expr = sympify(expr).subs(tuple(zip(variables, new_variables)))
if expr.is_commutative:
assumptions['commutative'] = True
obj = Expr.__new__(cls, expr, new_variables, point, **assumptions)
return obj
示例6: _new
# 需要导入模块: from expr import Expr [as 别名]
# 或者: from expr.Expr import __new__ [as 别名]
def _new(cls, _mpf_, _prec):
if _mpf_ == mlib.fzero:
return S.Zero
obj = Expr.__new__(cls)
obj._mpf_ = _mpf_
obj._prec = _prec
return obj
示例7: __new_stage2__
# 需要导入模块: from expr import Expr [as 别名]
# 或者: from expr.Expr import __new__ [as 别名]
def __new_stage2__(cls, name, **assumptions):
if not isinstance(name, basestring):
raise TypeError("name should be a string, not %s" % repr(type(name)))
obj = Expr.__new__(cls)
obj.name = name
obj._assumptions = StdFactKB(assumptions)
return obj
示例8: __new__
# 需要导入模块: from expr import Expr [as 别名]
# 或者: from expr.Expr import __new__ [as 别名]
def __new__(cls, p, q=None):
if q is None:
if isinstance(p, Rational):
return p
if isinstance(p, basestring):
try:
# we might have a Real
neg_pow, digits, expt = decimal.Decimal(p).as_tuple()
p = [1, -1][neg_pow] * int("".join(str(x) for x in digits))
if expt > 0:
rv = Rational(p*Pow(10, expt), 1)
return Rational(p, Pow(10, -expt))
except decimal.InvalidOperation:
import re
f = re.match(' *([-+]? *[0-9]+)( */ *)([0-9]+)', p)
if f:
p, _, q = f.groups()
return Rational(int(p), int(q))
else:
raise ValueError('invalid literal: %s' % p)
elif not isinstance(p, Basic):
return Rational(S(p))
q = S.One
if isinstance(q, Rational):
p *= q.q
q = q.p
if isinstance(p, Rational):
q *= p.q
p = p.p
p = int(p)
q = int(q)
if q == 0:
if p == 0:
if _errdict["divide"]:
raise ValueError("Indeterminate 0/0")
else:
return S.NaN
if p < 0:
return S.NegativeInfinity
return S.Infinity
if q < 0:
q = -q
p = -p
n = igcd(abs(p), q)
if n > 1:
p //= n
q //= n
if q == 1:
return Integer(p)
if p == 1 and q == 2:
return S.Half
obj = Expr.__new__(cls)
obj.p = p
obj.q = q
#obj._args = (p, q)
return obj
示例9: _from_args
# 需要导入模块: from expr import Expr [as 别名]
# 或者: from expr.Expr import __new__ [as 别名]
def _from_args(cls, args, is_commutative=None):
"""Create new instance with already-processed args"""
if len(args) == 0:
return cls.identity
elif len(args) == 1:
return args[0]
obj = Expr.__new__(cls, *args)
if is_commutative is None:
is_commutative = all(a.is_commutative for a in args)
obj.is_commutative = is_commutative
return obj
示例10: __new__
# 需要导入模块: from expr import Expr [as 别名]
# 或者: from expr.Expr import __new__ [as 别名]
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
示例11: __new__
# 需要导入模块: from expr import Expr [as 别名]
# 或者: from expr.Expr import __new__ [as 别名]
def __new__(cls, *args, **options):
if len(args) == 0:
return cls.identity
args = map(_sympify, args)
try:
args = [a for a in args if a is not cls.identity]
except AttributeError:
pass
if len(args) == 0: # recheck after filtering
return cls.identity
if len(args) == 1:
return args[0]
if not options.pop("evaluate", True):
obj = Expr.__new__(cls, *args)
obj.is_commutative = all(a.is_commutative for a in args)
return obj
c_part, nc_part, order_symbols = cls.flatten(args)
if len(c_part) + len(nc_part) <= 1:
if c_part:
obj = c_part[0]
elif nc_part:
obj = nc_part[0]
else:
obj = cls.identity
else:
obj = Expr.__new__(cls, *(c_part + nc_part))
obj.is_commutative = not nc_part
if order_symbols is not None:
obj = C.Order(obj, *order_symbols)
return obj
示例12: eval
# 需要导入模块: from expr import Expr [as 别名]
# 或者: from expr.Expr import __new__ [as 别名]
def eval(cls,*args):
obj = Expr.__new__(cls, *args)
#use dummy variables internally, just to be sure
nargs = len(args)-1
expression = args[nargs]
funargs = [C.Symbol(arg.name, dummy=True) for arg in args[:nargs]]
#probably could use something like foldl here
for arg,funarg in zip(args[:nargs],funargs):
expression = expression.subs(arg,funarg)
funargs.append(expression)
obj._args = tuple(funargs)
return obj
示例13: __new__
# 需要导入模块: from expr import Expr [as 别名]
# 或者: from expr.Expr import __new__ [as 别名]
def __new__(cls, *args, **assumptions):
args = (sympify(arg) for arg in args)
try:
_args = frozenset(cls._new_args_filter(args))
except ShortCircuit:
return cls.zero
if not _args:
return cls.identity
elif len(_args) == 1:
return set(_args).pop()
else:
obj = Expr.__new__(cls, _args, **assumptions)
obj._argset = _args
return obj
示例14: __new__
# 需要导入模块: from expr import Expr [as 别名]
# 或者: from expr.Expr import __new__ [as 别名]
def __new__(cls, variables, expr):
try:
variables = Tuple(*variables)
except TypeError:
variables = Tuple(variables)
if len(variables) == 1 and variables[0] == expr:
return S.IdentityFunction
#use dummy variables internally, just to be sure
new_variables = [C.Dummy(arg.name) for arg in variables]
expr = sympify(expr).subs(tuple(zip(variables, new_variables)))
obj = Expr.__new__(cls, Tuple(*new_variables), expr)
return obj
示例15: __new__
# 需要导入模块: from expr import Expr [as 别名]
# 或者: from expr.Expr import __new__ [as 别名]
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