本文整理汇总了Python中sympy.Expr类的典型用法代码示例。如果您正苦于以下问题:Python Expr类的具体用法?Python Expr怎么用?Python Expr使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Expr类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __new__
def __new__(cls, *args, **hints):
if not len(args) == 6:
raise ValueError('6 parameters expected, got %s' % args)
evaluate = hints.get('evaluate', False)
if evaluate:
return Expr.__new__(cls, *args)._eval_wignerd()
return Expr.__new__(cls, *args, **{'evaluate': False})
示例2: __new__
def __new__(cls, expr, condition=None, **kwargs):
expr = _sympify(expr)
if not kwargs.pop('evaluate', global_evaluate[0]):
if condition is None:
obj = Expr.__new__(cls, expr)
else:
condition = _sympify(condition)
obj = Expr.__new__(cls, expr, condition)
obj._condition = condition
return obj
if not expr.has(RandomSymbol):
return expr
if condition is not None:
condition = _sympify(condition)
if isinstance(expr, Add):
return Add(*[Expectation(a, condition=condition) for a in expr.args])
elif isinstance(expr, Mul):
rv = []
nonrv = []
for a in expr.args:
if isinstance(a, RandomSymbol) or a.has(RandomSymbol):
rv.append(a)
else:
nonrv.append(a)
return Mul(*nonrv)*Expectation(Mul(*rv), condition=condition, evaluate=False)
else:
if condition is None:
obj = Expr.__new__(cls, expr)
else:
obj = Expr.__new__(cls, expr, condition)
obj._condition = condition
return obj
示例3: _eval_adjoint
def _eval_adjoint(self):
obj = Expr._eval_adjoint(self)
if obj is None:
obj = Expr.__new__(Dagger, self)
if isinstance(obj, QExpr):
obj.hilbert_space = self.hilbert_space
return obj
示例4: __new__
def __new__(cls, *args):
""" Construct a Trace object.
Parameters
==========
args = sympy expression
"""
expr = args[0]
indices = Tuple(*args[1]) if len(args) == 2 else Tuple()
if isinstance(expr, Matrix):
return expr.trace()
elif hasattr(expr, 'trace') and callable(t.x):
#for any objects that have trace() defined e.g numpy
return expr.trace()
elif isinstance(expr, Add):
return Add(*[Tr(arg, indices) for arg in expr.args])
elif isinstance(expr, Mul):
c_part, nc_part = expr.args_cnc()
if len(nc_part) == 0:
return Mul(*c_part)
else:
nc_part_ordered = _cycle_permute(_rearrange_args(nc_part))
return Mul(*c_part) * Expr.__new__(cls, Mul(*nc_part_ordered), indices )
elif isinstance(expr, Pow):
if (_is_scalar(expr.args[0]) and
_is_scalar(expr.args[1])):
return expr
else:
return Expr.__new__(cls, expr, indices)
else:
if (_is_scalar(expr)):
return expr
return Expr.__new__(cls, expr, indices)
示例5: __new__
def __new__(cls, *args, **kwargs):
from sympy.tensor.array import NDimArray, tensorproduct, Array
from sympy import MatrixBase, MatrixExpr
from sympy.strategies import flatten
args = [sympify(arg) for arg in args]
evaluate = kwargs.get("evaluate", global_evaluate[0])
if not evaluate:
obj = Expr.__new__(cls, *args)
return obj
arrays = []
other = []
scalar = S.One
for arg in args:
if isinstance(arg, (Iterable, MatrixBase, NDimArray)):
arrays.append(Array(arg))
elif isinstance(arg, (MatrixExpr,)):
other.append(arg)
else:
scalar *= arg
coeff = scalar*tensorproduct(*arrays)
if len(other) == 0:
return coeff
if coeff != 1:
newargs = [coeff] + other
else:
newargs = other
obj = Expr.__new__(cls, *newargs, **kwargs)
return flatten(obj)
示例6: _call_super_constructor
def _call_super_constructor(cls, arg1, arg2, condition):
if condition is not None:
obj = Expr.__new__(cls, arg1, arg2, condition)
else:
obj = Expr.__new__(cls, arg1, arg2)
obj._condition = condition
return obj
示例7: __new__
def __new__(cls, *args):
""" Construct a Trace object.
"""
expr = args[0]
indices = args[1] if len(args) == 2 else -1 # -1 indicates full trace
if isinstance(expr, Matrix):
return expr.trace()
elif hasattr(expr, "trace") and callable(t.x):
# for any objects that have trace() defined e.g numpy
return expr.trace()
elif isinstance(expr, Add):
return Add(*[Tr(arg, indices) for arg in expr.args])
elif isinstance(expr, Mul):
c_part, nc_part = expr.args_cnc()
if len(nc_part) == 0:
return Mul(*c_part)
else:
# cyclic permute nc_part for canonical ordering
nc_part_ordered = _cycle_permute(nc_part)
return Mul(*c_part) * Expr.__new__(cls, Mul(*nc_part_ordered), indices)
elif isinstance(expr, Pow):
if _is_scalar(expr.args[0]) and _is_scalar(expr.args[1]):
return expr
else:
return Expr.__new__(cls, expr, indices)
else:
if _is_scalar(expr):
return expr
return Expr.__new__(cls, expr, indices)
示例8: __new__
def __new__(cls, *args, **hints):
if not len(args) == 6:
raise ValueError("6 parameters expected, got %s" % args)
args = sympify(args)
evaluate = hints.get("evaluate", False)
if evaluate:
return Expr.__new__(cls, *args)._eval_wignerd()
return Expr.__new__(cls, *args, **{"evaluate": False})
示例9: __new__
def __new__(cls, arg, condition=None, **kwargs):
arg = _sympify(arg)
if condition is None:
obj = Expr.__new__(cls, arg)
else:
condition = _sympify(condition)
obj = Expr.__new__(cls, arg, condition)
obj._condition = condition
return obj
示例10: __new__
def __new__(cls, *args):
""" Construct a Trace object.
Parameters
==========
args = sympy expression
indices = tuple/list if indices, optional
"""
# expect no indices,int or a tuple/list/Tuple
if (len(args) == 2):
if not isinstance(args[1], (list, Tuple, tuple)):
indices = Tuple(args[1])
else:
indices = Tuple(*args[1])
expr = args[0]
elif (len(args) == 1):
indices = Tuple()
expr = args[0]
else:
raise ValueError("Arguments to Tr should be of form"
"(expr[, [indices]])")
if isinstance(expr, Matrix):
return expr.trace()
elif hasattr(expr, 'trace') and callable(expr.trace):
#for any objects that have trace() defined e.g numpy
return expr.trace()
elif isinstance(expr, Add):
return Add(*[Tr(arg, indices) for arg in expr.args])
elif isinstance(expr, Mul):
c_part, nc_part = expr.args_cnc()
if len(nc_part) == 0:
return Mul(*c_part)
else:
obj = Expr.__new__(cls, Mul(*nc_part), indices )
#this check is needed to prevent cached instances
#being returned even if len(c_part)==0
return Mul(*c_part)*obj if len(c_part)>0 else obj
elif isinstance(expr, Pow):
if (_is_scalar(expr.args[0]) and
_is_scalar(expr.args[1])):
return expr
else:
return Expr.__new__(cls, expr, indices)
else:
if (_is_scalar(expr)):
return expr
return Expr.__new__(cls, expr, indices)
示例11: __new__
def __new__(cls, arg, **old_assumptions):
# Return the dagger of a sympy Matrix immediately.
if isinstance(arg, (Matrix, numpy_ndarray, scipy_sparse_matrix)):
return matrix_dagger(arg)
arg = sympify(arg)
r = cls.eval(arg)
if isinstance(r, Expr):
return r
#make unevaluated dagger commutative or non-commutative depending on arg
if arg.is_commutative:
obj = Expr.__new__(cls, arg, **{'commutative':True})
else:
obj = Expr.__new__(cls, arg, **{'commutative':False})
if isinstance(obj, QExpr):
obj.hilbert_space = arg.hilbert_space
return obj
示例12: __new__
def __new__(cls, *args, **old_assumptions):
"""Construct a new quantum object.
Parameters
==========
args : tuple
The list of numbers or parameters that uniquely specify the
quantum object. For a state, this will be its symbol or its
set of quantum numbers.
Examples
========
>>> from sympy.physics.quantum.qexpr import QExpr
>>> q = QExpr(0)
>>> q
0
>>> q.label
(0,)
>>> q.hilbert_space
H
>>> q.args
(0,)
>>> q.is_commutative
False
"""
# First compute args and call Expr.__new__ to create the instance
args = cls._eval_args(args)
inst = Expr.__new__(cls, *args, **{'commutative':False})
# Now set the slots on the instance
inst.hilbert_space = cls._eval_hilbert_space(args)
return inst
示例13: __new__
def __new__(cls, bra, ket):
if not isinstance(ket, KetBase):
raise TypeError('KetBase subclass expected, got: %r' % ket)
if not isinstance(bra, BraBase):
raise TypeError('BraBase subclass expected, got: %r' % ket)
obj = Expr.__new__(cls, bra, ket)
return obj
示例14: __mul__
def __mul__(self, other):
"""KetBase*other"""
from sympsi.operator import OuterProduct
if isinstance(other, BraBase):
return OuterProduct(self, other)
else:
return Expr.__mul__(self, other)
示例15: __new__
def __new__(cls, bra, ket, **old_assumptions):
if not isinstance(ket, KetBase):
raise TypeError('KetBase subclass expected, got: %r' % ket)
if not isinstance(bra, BraBase):
raise TypeError('BraBase subclass expected, got: %r' % ket)
obj = Expr.__new__(cls, *(bra, ket), **{'commutative':True})
return obj