當前位置: 首頁>>代碼示例>>Python>>正文


Python sympy.Poly方法代碼示例

本文整理匯總了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] 
開發者ID:deepmind,項目名稱:mathematics_dataset,代碼行數:33,代碼來源:algebra.py

示例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) 
開發者ID:deepmind,項目名稱:mathematics_dataset,代碼行數:43,代碼來源:polynomials.py

示例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 
開發者ID:ryfeus,項目名稱:lambda-packs,代碼行數:7,代碼來源:expn_asy.py

示例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) 
開發者ID:Relph1119,項目名稱:GraphicDesignPatternByPython,代碼行數:13,代碼來源:test_precompute_expn_asy.py

示例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)) 
開發者ID:andravin,項目名稱:wincnn,代碼行數:5,代碼來源:wincnn.py


注:本文中的sympy.Poly方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。