当前位置: 首页>>代码示例>>Python>>正文


Python sympy.Rational方法代码示例

本文整理汇总了Python中sympy.Rational方法的典型用法代码示例。如果您正苦于以下问题:Python sympy.Rational方法的具体用法?Python sympy.Rational怎么用?Python sympy.Rational使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sympy的用法示例。


在下文中一共展示了sympy.Rational方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: testDiv

# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Rational [as 别名]
def testDiv(self):
    div = ops.Div(2, 3)
    self.assertEqual(str(div), '2/3')
    self.assertEqual(div.sympy(), sympy.Rational(2, 3))

    div = ops.Div(2, sympy.Rational(4, 5))
    self.assertEqual(str(div), '2/(4/5)')
    self.assertEqual(div.sympy(), sympy.Rational(5, 2))

    div = ops.Div(1, ops.Div(2, 3))
    self.assertEqual(str(div), '1/(2/3)')
    self.assertEqual(div.sympy(), sympy.Rational(3, 2))

    div = ops.Div(ops.Div(2, 3), 4)
    self.assertEqual(str(div), '(2/3)/4')
    self.assertEqual(div.sympy(), sympy.Rational(1, 6))

    div = ops.Div(2, ops.Mul(3, 4))
    self.assertEqual(str(div), '2/(3*4)')

    div = ops.Div(2, sympy.Function('f')(sympy.Symbol('x')))
    self.assertEqual(str(div), '2/f(x)') 
开发者ID:deepmind,项目名称:mathematics_dataset,代码行数:24,代码来源:ops_test.py

示例2: testPow

# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Rational [as 别名]
def testPow(self):
    pow_ = ops.Pow(2, 3)
    self.assertEqual(str(pow_), '2**3')
    self.assertEqual(pow_.sympy(), 8)

    pow_ = ops.Pow(4, sympy.Rational(1, 2))
    self.assertEqual(str(pow_), '4**(1/2)')
    self.assertEqual(pow_.sympy(), 2)

    pow_ = ops.Pow(sympy.Rational(1, 2), 3)
    self.assertEqual(str(pow_), '(1/2)**3')
    self.assertEqual(pow_.sympy(), 1/8)

    pow_ = ops.Pow(3, ops.Pow(2, 1))
    self.assertEqual(str(pow_), '3**(2**1)')
    self.assertEqual(pow_.sympy(), 9)

    pow_ = ops.Pow(ops.Pow(2, 3), 4)
    self.assertEqual(str(pow_), '(2**3)**4')
    self.assertEqual(pow_.sympy(), 4096)

    pow_ = ops.Pow(-5, 2)
    self.assertEqual(str(pow_), '(-5)**2')
    self.assertEqual(pow_.sympy(), 25) 
开发者ID:deepmind,项目名称:mathematics_dataset,代码行数:26,代码来源:ops_test.py

示例3: __init__

# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Rational [as 别名]
def __init__(self, value):
    """Initializes a `Decimal`.

    Args:
      value: (Sympy) value to display as a decimal.

    Raises:
      ValueError: If `value` cannot be represented as a non-terminating decimal.
    """
    self._value = sympy.Rational(value)

    numer = int(sympy.numer(self._value))
    denom = int(sympy.denom(self._value))

    denom_factors = list(sympy.factorint(denom).keys())
    for factor in denom_factors:
      if factor not in [2, 5]:
        raise ValueError('Cannot represent {} as a non-recurring decimal.'
                         .format(value))
    self._decimal = decimal.Decimal(numer) / decimal.Decimal(denom) 
开发者ID:deepmind,项目名称:mathematics_dataset,代码行数:22,代码来源:display.py

示例4: testBasic

# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Rational [as 别名]
def testBasic(self):
    space = probability.SampleWithoutReplacementSpace({0: 1, 1: 1}, 2)
    event_0_0 = probability.FiniteProductEvent(
        [probability.DiscreteEvent({0}), probability.DiscreteEvent({0})])
    event_0_1 = probability.FiniteProductEvent(
        [probability.DiscreteEvent({0}), probability.DiscreteEvent({1})])
    p_0_0 = space.probability(event_0_0)
    p_0_1 = space.probability(event_0_1)
    self.assertEqual(p_0_0, 0)
    self.assertEqual(p_0_1, sympy.Rational(1, 2))

    space = probability.SampleWithoutReplacementSpace({0: 1, 1: 0}, 1)
    event_0 = probability.FiniteProductEvent([probability.DiscreteEvent({0})])
    event_1 = probability.FiniteProductEvent([probability.DiscreteEvent({1})])
    event_2 = probability.FiniteProductEvent([probability.DiscreteEvent({2})])
    p_0 = space.probability(event_0)
    p_1 = space.probability(event_1)
    p_2 = space.probability(event_2)
    self.assertEqual(p_0, 1)
    self.assertEqual(p_1, 0)
    self.assertEqual(p_2, 0) 
开发者ID:deepmind,项目名称:mathematics_dataset,代码行数:23,代码来源:probability_test.py

示例5: test_fcode_Pow

# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Rational [as 别名]
def test_fcode_Pow():
    x, y = symbols('x,y')
    n = symbols('n', integer=True)

    assert fcode(x**3) == "      x**3"
    assert fcode(x**(y**3)) == "      x**(y**3)"
    assert fcode(1/(sin(x)*3.5)**(x - y**x)/(x**2 + y)) == \
        "      (3.5d0*sin(x))**(-x + y**x)/(x**2 + y)"
    assert fcode(sqrt(x)) == '      sqrt(x)'
    assert fcode(sqrt(n)) == '      sqrt(dble(n))'
    assert fcode(x**0.5) == '      sqrt(x)'
    assert fcode(sqrt(x)) == '      sqrt(x)'
    assert fcode(sqrt(10)) == '      sqrt(10.0d0)'
    assert fcode(x**-1.0) == '      1.0/x'
    assert fcode(x**-2.0,
                 assign_to='y',
                 source_format='free',
                 human=True) == 'y = x**(-2.0d0)'  # 2823
    assert fcode(x**Rational(3, 7)) == '      x**(3.0d0/7.0d0)' 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:21,代码来源:test_fcode.py

示例6: franke_1

# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Rational [as 别名]
def franke_1(lmbda):
    assert -frac(9, 5) <= lmbda <= frac(9, 4)

    a = sqrt(frac(9 + 5 * lmbda, 15))
    b = sqrt(frac(9 - 4 * lmbda, 15))
    c = sqrt(frac(3, 5))

    weights, points = concat(
        zero(frac(16 * (4 + 5 * lmbda), 9 * (9 + 5 * lmbda))),
        pm2([frac(25, 9 * (9 - 4 * lmbda)), b, c]),
        pm(
            [frac(40, 9 * (9 + 5 * lmbda)), a, 0],
            [frac(40 * (1 - lmbda), 9 * (9 - 4 * lmbda)), 0, c],
        ),
    )
    weights /= 4
    return C2Scheme(f"Franke(1, {lmbda})", weights, points, 5, source) 
开发者ID:nschloe,项目名称:quadpy,代码行数:19,代码来源:_franke.py

示例7: _stroud_5_5

# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Rational [as 别名]
def _stroud_5_5(n, variant_a, symbolic=False):
    frac = sympy.Rational if symbolic else lambda a, b: a / b
    sqrt = sympy.sqrt if symbolic else math.sqrt
    p_m = +1 if variant_a else -1

    # r is complex-valued for n >= 3
    r = sqrt((n + 2 + p_m * (n - 1) * sqrt(2 * (n + 2))) / (2 * n))
    s = sqrt((n + 2 - p_m * sqrt(2 * (n + 2))) / (2 * n))
    A = frac(2, n + 2)
    B = frac(1, 2 ** n * (n + 2))

    data = [(A, [n * [0]]), (B, fsd(n, (r, 1), (s, n - 1)))]

    points, weights = untangle(data)
    variant = "a" if variant_a else "b"
    return Enr2Scheme(f"Stroud Enr2 5-5{variant}", n, weights, points, 5, source) 
开发者ID:nschloe,项目名称:quadpy,代码行数:18,代码来源:_stroud.py

示例8: stroud_enr2_5_6

# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Rational [as 别名]
def stroud_enr2_5_6(n, symbolic=False):
    frac = sympy.Rational if symbolic else lambda a, b: a / b
    sqrt = sympy.sqrt if symbolic else math.sqrt
    assert n >= 5

    sqrt2 = sqrt(2)
    sqrt2n1 = sqrt(2 * (n + 1))
    r = sqrt((n - sqrt2 + (n - 1) * sqrt2n1) / (2 * n))
    s = sqrt((n - sqrt2 - sqrt2n1) / (2 * n))
    t = sqrt((1 + sqrt2) / 2)
    A = frac(1, 2 ** n * (n + 1))

    data = [(A, fsd(n, (r, 1), (s, n - 1))), (A, pm(n * [t]))]

    points, weights = untangle(data)
    return Enr2Scheme("Stroud Enr2 5-6", n, weights, points, 5, source) 
开发者ID:nschloe,项目名称:quadpy,代码行数:18,代码来源:_stroud.py

示例9: xiu

# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Rational [as 别名]
def xiu(n):
    points = []
    for k in range(n + 1):
        pt = []
        # Slight adaptation:
        # The article has points for the weight 1/sqrt(2*pi) exp(−x**2/2)
        # so divide by sqrt(2) to adapt for 1/sqrt(pi) exp(−x ** 2)
        for r in range(1, n // 2 + 1):
            alpha = (2 * r * k * pi) / (n + 1)
            pt += [cos(alpha), sin(alpha)]
        if n % 2 == 1:
            pt += [(-1) ** k / sqrt(2)]
        points.append(pt)

    points = numpy.array(points)
    weights = numpy.full(n + 1, frac(1, n + 1))
    return Enr2Scheme("Xiu", n, weights, points, 2, source) 
开发者ID:nschloe,项目名称:quadpy,代码行数:19,代码来源:_xiu.py

示例10: cools_haegemans_1

# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Rational [as 别名]
def cools_haegemans_1(n, delta2=1, symbolic=False):
    frac = sympy.Rational if symbolic else lambda a, b: a / b
    sqrt = sympy.sqrt if symbolic else math.sqrt
    assert frac(1, 2) <= delta2
    m = 1

    w0 = frac(2 * delta2 - 1, 2 * delta2)
    w = frac(_mu(2, symbolic) ** m * _mu(0, symbolic) ** (n - m), 2 ** n * delta2 ** m)

    data = [
        (w0, z(n)),
        (w, pm(n * [sqrt(delta2)])),
    ]

    points, weights = untangle(data)
    return Enr2Scheme("Cools-Haegemans 1", n, weights, points, 3, _source) 
开发者ID:nschloe,项目名称:quadpy,代码行数:18,代码来源:_cools_haegemans.py

示例11: _sample_conversion_decimal

# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Rational [as 别名]
def _sample_conversion_decimal(dimension, is_extrapolation):
  """Samples to and from units and values."""
  base_unit, target_unit = random.sample(list(dimension.keys()), 2)
  scale = sympy.Rational(dimension[base_unit]) / dimension[target_unit]
  scale_non_decimal = _factor_non_decimal(sympy.denom(scale))
  entropy = 9 if is_extrapolation else 7
  base_value = number.non_integer_decimal(entropy, signed=False)
  base_value = display.Decimal(base_value.value * scale_non_decimal)
  target_value = display.Decimal(base_value.value * scale)
  return base_value, base_unit, target_value, target_unit 
开发者ID:deepmind,项目名称:mathematics_dataset,代码行数:12,代码来源:measurement.py

示例12: _conversion_fraction

# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Rational [as 别名]
def _conversion_fraction(context, is_train):
  """E.g., "How many grams are in three quarters of a kg?"."""
  dimension = random.choice(DIMENSIONS)

  # Limit probability of giving zero answer.
  allow_zero = random.random() < 0.2

  # Repeat until we find a pair with an integral answer. (Avoids ambiguity with
  # decimals.)
  while True:
    base_unit, target_unit = random.sample(list(dimension.keys()), 2)
    base_value = number.non_integer_rational(2, signed=False)
    if train_test_split.is_train(base_value) != is_train:
      continue
    answer = (base_value * sympy.Rational(dimension[base_unit])
              / sympy.Rational(dimension[target_unit]))
    if (abs(answer) <= 100000
        and sympy.denom(answer) == 1
        and (allow_zero or answer != 0)):
      break

  template = random.choice([
      'How many {target_name} are there in {base_value} of a {base_name}?',
      'What is {base_value} of a {base_name} in {target_name}?',
  ])

  if sympy.denom(base_value) > 20 or random.choice([False, True]):
    base_value_string = base_value  # Will be represented as e.g., 2/3.
  else:
    base_value_string = display.StringNumber(base_value)  # e.g., two thirds

  question = example.question(
      context, template,
      base_name=base_unit.name,
      base_value=base_value_string,
      target_name=pluralize(target_unit.name))
  return example.Problem(question=question, answer=answer) 
开发者ID:deepmind,项目名称:mathematics_dataset,代码行数:39,代码来源:measurement.py

示例13: _value_sampler

# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Rational [as 别名]
def _value_sampler(value):
  """Returns sampler (e.g., number.integer) appropriate for `value`."""
  if value == _INT or number.is_integer(value):
    return functools.partial(number.integer, signed=True)
  if value == _INT_OR_RATIONAL or isinstance(value, sympy.Rational):
    return functools.partial(number.integer_or_rational, signed=True)
  if isinstance(value, display.Decimal):
    return functools.partial(number.integer_or_decimal, signed=True)
  raise ValueError('Unrecognized value {} of type {}'
                   .format(value, type(value))) 
开发者ID:deepmind,项目名称:mathematics_dataset,代码行数:12,代码来源:arithmetic.py

示例14: _div_op

# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Rational [as 别名]
def _div_op(value, sample_args, rationals_allowed):
  """Returns sampled args for `ops.Div`."""
  assert rationals_allowed  # should be True if this function gets invoked
  entropy, sample_args = sample_args.peel()

  numer = sympy.numer(value)
  denom = sympy.denom(value)

  if sample_args.count == 1:
    mult = number.integer(entropy, signed=True, min_abs=1)
    op_args = [numer * mult, denom * mult]
  elif sample_args.count == 2:
    if numer == 0 or random.choice([False, True]):
      x = number.integer(entropy, signed=True, min_abs=1, coprime_to=denom)
      op_args = [sympy.Rational(x * numer, denom), x]
    else:
      x = number.integer(entropy, signed=True, min_abs=1, coprime_to=numer)
      op_args = [x, sympy.Rational(x * denom, numer)]
  else:
    assert sample_args.count >= 3
    p2, p1 = _split_factors(numer)
    q1, q2 = _split_factors(denom)
    entropy -= _entropy_of_factor_split(numer) + _entropy_of_factor_split(denom)
    entropy_r = random.uniform(0, entropy)
    entropy_s = entropy - entropy_r
    r = number.integer(entropy_r, signed=True, min_abs=1, coprime_to=q1*p2)
    s = number.integer(entropy_s, signed=False, min_abs=1, coprime_to=p1*q2)
    op_args = [sympy.Rational(r*p1, s*q1), sympy.Rational(r*q2, s*p2)]

  return ops.Div, op_args, sample_args 
开发者ID:deepmind,项目名称:mathematics_dataset,代码行数:32,代码来源:arithmetic.py

示例15: non_integer_rational

# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import Rational [as 别名]
def non_integer_rational(entropy, signed):
  """Similar args to `integer`. Entropy split between denom and numer."""
  numer_entropy = random.uniform(0, entropy)
  denom_entropy = entropy - numer_entropy
  numer = integer(numer_entropy, signed, min_abs=1)
  denom = integer(denom_entropy, False, min_abs=2, coprime_to=numer)
  return sympy.Rational(numer, denom) 
开发者ID:deepmind,项目名称:mathematics_dataset,代码行数:9,代码来源:number.py


注:本文中的sympy.Rational方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。