本文整理汇总了Python中sympy.Basic类的典型用法代码示例。如果您正苦于以下问题:Python Basic类的具体用法?Python Basic怎么用?Python Basic使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Basic类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: my_compare
def my_compare(a, b):
main_var = s
p1, p2, p3 = Wild("p1"), Wild("p2"), Wild("p3")
r_a = a.match(p1 * s**p3)
r_b = b.match(p1 * s**p3)
if r_a is not None and r_b is not None:
c = Basic.compare(r_a[p3], r_b[p3])
if c!=0:
return c
return Basic._compare_pretty(a,b)
示例2: __new__
def __new__(cls, i, j):
i, j = map(sympify, (i, j))
r = cls.canonize(i, j)
if isinstance(r, Basic):
return r
obj = Basic.__new__(cls, i, j, commutative=True)
return obj
示例3: __new__
def __new__(cls, dimension):
dimension = sympify(dimension)
r = cls.eval(dimension)
if isinstance(r, Basic):
return r
obj = Basic.__new__(cls, dimension, **{'commutative': False})
return obj
示例4: __new__
def __new__ (cls, arg, **options):
# print "Inverse(", arg, ")"
# print INVERTIBLE
if isinstance(arg, Inverse):
return arg.args[0]
if arg.is_Number:
return 1 / arg
arg_rank = expr_rank(arg)
if arg_rank == 1:
raise NotInvertibleError
if is_one(arg):
return arg
if is_zero(arg):
raise NotInvertibleError
# FIXME: Funky case trying to catch lower triangular or diagonal
# muls like T(P_0)*A*P_0
if arg in INVERTIBLE:
pass
elif isinstance(arg, TensorExpr) and not arg.has_inverse:
raise NotInvertibleError
elif isinstance(arg, Mul):
if arg.args[0] == S(-1):
return - Inverse(reduce(operator.mul, arg.args[1:]))
if not expr_invertible(arg):
raise NotInvertibleError
options['commutative'] = arg.is_commutative
return Basic.__new__(cls, arg, **options)
示例5: __new__
def __new__(cls, sym, dist):
sym = sympify(sym)
if isinstance(dist, SingleContinuousDistribution):
return SingleContinuousPSpace(sym, dist)
if isinstance(dist, SingleDiscreteDistribution):
return SingleDiscretePSpace(sym, dist)
return Basic.__new__(cls, sym, dist)
示例6: _new
def _new(cls, *args, **kwargs):
if len(args)==1 and isinstance(args[0], ImmutableMatrix):
return args[0]
rows, cols, mat = MatrixBase._handle_creation_inputs(*args, **kwargs)
shape = Tuple(rows, cols)
mat = Tuple(*mat)
return Basic.__new__(cls, shape, mat)
示例7: __new__
def __new__(cls, iterable=None, shape=None, **kwargs):
from sympy.utilities.iterables import flatten
shape, flat_list = cls._handle_ndarray_creation_inputs(iterable, shape, **kwargs)
shape = Tuple(*map(_sympify, shape))
loop_size = functools.reduce(lambda x,y: x*y, shape) if shape else 0
# Sparse array:
if isinstance(flat_list, (dict, Dict)):
sparse_array = Dict(flat_list)
else:
sparse_array = {}
for i, el in enumerate(flatten(flat_list)):
if el != 0:
sparse_array[i] = _sympify(el)
sparse_array = Dict(sparse_array)
self = Basic.__new__(cls, sparse_array, shape, **kwargs)
self._shape = shape
self._rank = len(shape)
self._loop_size = loop_size
self._sparse_array = sparse_array
return self
示例8: sample2d
def sample2d(f, x_args):
"""
Samples a 2d function f over specified intervals and returns two
arrays (X, Y) suitable for plotting with matlab (matplotlib)
syntax. See examples\mplot2d.py.
f is a function of one variable, such as x**2.
x_args is an interval given in the form (var, min, max, n)
"""
try:
f = Basic.sympify(f)
except:
raise ValueError("f could not be interpretted as a SymPy function")
try:
x, x_min, x_max, x_n = x_args
except:
raise ValueError("x_args must be a tuple of the form (var, min, max, n)")
x_l = float(x_max - x_min)
x_d = x_l/float(x_n)
X = arange(float(x_min), float(x_max)+x_d, x_d)
Y = empty(len(X))
for i in range(len(X)):
try:
Y[i] = float(f.subs(x, X[i]))
except:
Y[i] = None
return X, Y
示例9: __new__
def __new__(cls, dimension):
dimension = sympify(dimension)
r = cls.eval(dimension)
if isinstance(r, Basic):
return r
obj = Basic.__new__(cls, dimension)
return obj
示例10: __new__
def __new__(cls, M):
if not isinstance(M, Matrix):
M = Matrix(M)
data = Tuple(*M._mat)
shape = Tuple(*sympify(M.shape))
obj = Basic.__new__(cls, data, shape)
obj._mat = M
return obj
示例11: __new__
def __new__(cls, mat):
if not mat.is_Matrix:
raise TypeError("input to Trace, %s, is not a matrix" % str(mat))
if not mat.is_square:
raise ShapeError("Trace of a non-square matrix")
return Basic.__new__(cls, mat)
示例12: _new
def _new(cls, *args, **kwargs):
if len(args) == 1 and isinstance(args[0], ImmutableMatrix):
return args[0]
rows, cols, flat_list = MatrixBase._handle_creation_inputs(*args, **kwargs)
rows = Integer(rows)
cols = Integer(cols)
mat = Tuple(*flat_list)
return Basic.__new__(cls, rows, cols, mat)
示例13: __new__
def __new__(cls, *args):
# args should be a tuple - a variable length argument list
obj = Basic.__new__(cls, *args)
obj._circuit = Mul(*args)
obj._rules = generate_gate_rules(args)
obj._eq_ids = generate_equivalent_ids(args)
return obj
示例14: __new__
def __new__(cls, mat):
if not isinstance(mat, Matrix):
mat = Matrix(mat)
data = Tuple(*mat.mat)
shape = Tuple(*sympify(mat.shape))
obj = Basic.__new__(cls, data, shape)
obj.mat = mat
return obj
示例15: set_v_max
def set_v_max(self, v_max):
if v_max is None:
self._v_max = None
return
try:
self._v_max = Basic.sympify(v_max)
float(self._v_max.evalf())
except:
raise ValueError("v_max could not be interpreted as a number.")