本文整理汇总了Python中sympy.Poly方法的典型用法代码示例。如果您正苦于以下问题:Python sympy.Poly方法的具体用法?Python sympy.Poly怎么用?Python sympy.Poly使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sympy
的用法示例。
在下文中一共展示了sympy.Poly方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _polynomial_coeffs_with_roots
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Poly [as 别名]
def _polynomial_coeffs_with_roots(roots, scale_entropy):
"""Returns a polynomial with the given roots.
The polynomial is generated by expanding product_{root in roots} (x - root),
and then (1) scaling by the coefficients so they are all integers with lcm 1,
and then (2) further scaling the coefficients by a random integer or rational
with `scale_entropy` digits.
Args:
roots: List of values.
scale_entropy: Float; entropy of the random coefficient scaling.
Returns:
List of coefficients `coeffs`, such that `coeffs[i]` is the coefficient of
variable ** i.
"""
variable = sympy.Symbol('x') # doesn't matter, only use coefficients
polynomial = sympy.Poly(sympy.prod([variable - root for root in roots]))
coeffs_reversed = polynomial.all_coeffs()
assert len(coeffs_reversed) == len(roots) + 1
coeffs = list(reversed(coeffs_reversed))
# Multiply terms to change rationals to integers, and then maybe reintroduce.
lcm = sympy.lcm([sympy.denom(coeff) for coeff in coeffs])
if scale_entropy > 0:
while True:
scale = number.integer_or_rational(scale_entropy, signed=True)
if scale != 0:
break
else:
scale = 1
return [coeff * scale * lcm for coeff in coeffs]
示例2: coefficient_named
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Poly [as 别名]
def coefficient_named(value, sample_args, context=None):
"""E.g., "Express x^2 + 2x in the form h * x^2 + k * x + t and give h."."""
del value # not used
if context is None:
context = composition.Context()
variable = sympy.Symbol(context.pop())
entropy, sample_args = sample_args.peel()
degree = random.randint(1, 4)
if random.choice([False, True]):
coefficients = polynomials.sample_coefficients(
degree, entropy/2, min_non_zero=random.randint(degree - 1, degree))
expanded = polynomials.expand_coefficients(coefficients, entropy/2)
expression = polynomials.coefficients_to_polynomial(expanded, variable)
else:
expression = polynomials.sample_with_brackets(variable, degree, entropy)
coefficients = list(reversed(sympy.Poly(expression).all_coeffs()))
named_coeffs = [sympy.Symbol(context.pop()) for _ in range(degree + 1)]
canonical = polynomials.coefficients_to_polynomial(named_coeffs, variable)
if random.random() < 0.2: # only small probability of non-zero power
power = random.randint(0, degree)
else:
non_zero_powers = [i for i in range(degree + 1) if coefficients[i] != 0]
power = random.choice(non_zero_powers)
value = coefficients[power]
named_coeff = named_coeffs[power]
template = random.choice([
'Express {expression} as {canonical} and give {target}.',
'Rearrange {expression} to {canonical} and give {target}.',
'Express {expression} in the form {canonical} and give {target}.',
'Rearrange {expression} to the form {canonical} and give {target}.',
])
return example.Problem(
question=example.question(
context, template, expression=expression, canonical=canonical,
target=named_coeff),
answer=value)
示例3: generate_A
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Poly [as 别名]
def generate_A(K):
A = [Poly(1, x)]
for k in range(K):
A.append(Poly(1 - 2*k*x, x)*A[k] + Poly(x*(x + 1))*A[k].diff())
return A
示例4: test_generate_A
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Poly [as 别名]
def test_generate_A():
# Data from DLMF 8.20.5
x = sympy.symbols('x')
Astd = [Poly(1, x),
Poly(1, x),
Poly(1 - 2*x),
Poly(1 - 8*x + 6*x**2)]
Ares = generate_A(len(Astd))
for p, q in zip(Astd, Ares):
assert_equal(p, q)
示例5: Lx
# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Poly [as 别名]
def Lx(a,n):
x=symbols('x')
return Matrix(n, 1, lambda i,j: Poly((reduce(mul, ((x-a[k] if k!=i else 1) for k in range(0,n)), 1)).expand(basic=True), x))