本文整理汇总了Python中mathics.core.expression.Number.from_mp方法的典型用法代码示例。如果您正苦于以下问题:Python Number.from_mp方法的具体用法?Python Number.from_mp怎么用?Python Number.from_mp使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mathics.core.expression.Number
的用法示例。
在下文中一共展示了Number.from_mp方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: apply
# 需要导入模块: from mathics.core.expression import Number [as 别名]
# 或者: from mathics.core.expression.Number import from_mp [as 别名]
def apply(self, items, evaluation):
'Times[items___]'
#TODO: Clean this up and optimise it
items = items.numerify(evaluation).get_sequence()
number = (sympy.Integer(1), sympy.Integer(0))
leaves = []
prec = min_prec(*items)
is_real = all([not isinstance(i, Complex) for i in items])
for item in items:
if isinstance(item, Number):
if isinstance(item, Complex):
sym_real, sym_imag = item.real.to_sympy(), item.imag.to_sympy()
else:
sym_real, sym_imag = item.to_sympy(), sympy.Integer(0)
if prec is not None:
sym_real = sym_real.n(dps(prec))
sym_imag = sym_imag.n(dps(prec))
if sym_real.is_zero and sym_imag.is_zero and prec is None:
return Integer('0')
number = (number[0]*sym_real - number[1]*sym_imag, number[0]*sym_imag + number[1]*sym_real)
elif leaves and item == leaves[-1]:
leaves[-1] = Expression('Power', leaves[-1], Integer(2))
elif leaves and item.has_form('Power', 2) and leaves[-1].has_form('Power', 2) and item.leaves[0].same(leaves[-1].leaves[0]):
leaves[-1].leaves[1] = Expression('Plus', item.leaves[1], leaves[-1].leaves[1])
elif leaves and item.has_form('Power', 2) and item.leaves[0].same(leaves[-1]):
leaves[-1] = Expression('Power', leaves[-1], Expression('Plus', item.leaves[1], Integer(1)))
elif leaves and leaves[-1].has_form('Power', 2) and leaves[-1].leaves[0].same(item):
leaves[-1] = Expression('Power', item, Expression('Plus', Integer(1), leaves[-1].leaves[1]))
else:
leaves.append(item)
if number == (1, 0):
number = None
elif number == (-1, 0) and leaves and leaves[0].has_form('Plus', None):
leaves[0].leaves = [Expression('Times', Integer(-1), leaf) for leaf in leaves[0].leaves]
number = None
if number is not None:
if number[1].is_zero and is_real:
leaves.insert(0, Number.from_mp(number[0], prec))
elif number[1].is_zero and number[1].is_Integer and prec is None:
leaves.insert(0, Number.from_mp(number[0], prec))
else:
leaves.insert(0, Complex(from_sympy(number[0]), from_sympy(number[1]), prec))
if not leaves:
return Integer(1)
elif len(leaves) == 1:
return leaves[0]
else:
return Expression('Times', *leaves)
示例2: append_last
# 需要导入模块: from mathics.core.expression import Number [as 别名]
# 或者: from mathics.core.expression.Number import from_mp [as 别名]
def append_last():
if last_item is not None:
if last_count == 1:
leaves.append(last_item)
else:
if last_item.has_form("Times", None):
last_item.leaves.insert(0, Number.from_mp(last_count))
leaves.append(last_item)
else:
leaves.append(Expression("Times", Number.from_mp(last_count), last_item))
示例3: apply_real
# 需要导入模块: from mathics.core.expression import Number [as 别名]
# 或者: from mathics.core.expression.Number import from_mp [as 别名]
def apply_real(self, x, evaluation):
'Abs[x_?RealNumberQ]'
if x.value < 0:
return Number.from_mp(-x.value)
else:
return x
示例4: apply
# 需要导入模块: from mathics.core.expression import Number [as 别名]
# 或者: from mathics.core.expression.Number import from_mp [as 别名]
def apply(self, items, evaluation):
'Times[items___]'
items = items.numerify(evaluation).get_sequence()
number = mpz(1)
leaves = []
for item in items:
if isinstance(item, Number):
if get_type(item.value) == 'z' and item.value == 0:
return Integer('0')
number = mul(number, item.value)
elif leaves and item == leaves[-1]:
leaves[-1] = Expression('Power', leaves[-1], Integer(2))
elif leaves and item.has_form('Power', 2) and leaves[-1].has_form('Power', 2) and item.leaves[0].same(leaves[-1].leaves[0]):
leaves[-1].leaves[1] = Expression('Plus', item.leaves[1], leaves[-1].leaves[1])
elif leaves and item.has_form('Power', 2) and item.leaves[0].same(leaves[-1]):
leaves[-1] = Expression('Power', leaves[-1], Expression('Plus', item.leaves[1], Integer(1)))
elif leaves and leaves[-1].has_form('Power', 2) and leaves[-1].leaves[0].same(item):
leaves[-1] = Expression('Power', item, Expression('Plus', Integer(1), leaves[-1].leaves[1]))
else:
leaves.append(item)
if get_type(number) == 'z':
if number == 1:
number = None
elif number == -1 and leaves and leaves[0].has_form('Plus', None):
leaves[0].leaves = [Expression('Times', Integer(-1), leaf) for leaf in leaves[0].leaves]
number = None
if number is not None:
leaves.insert(0, Number.from_mp(number))
if not leaves:
return Integer(1)
elif len(leaves) == 1:
return leaves[0]
else:
return Expression('Times', *leaves)
示例5: post_parse
# 需要导入模块: from mathics.core.expression import Number [as 别名]
# 或者: from mathics.core.expression.Number import from_mp [as 别名]
def post_parse(self, expression):
if (expression.get_head().get_name() == 'Minus' # noqa
and len(expression.leaves) == 1
and isinstance(expression.leaves[0], Number)):
return Number.from_mp(-expression.leaves[0].to_sympy())
else:
return super(Minus, self).post_parse(expression)
示例6: apply_iter
# 需要导入模块: from mathics.core.expression import Number [as 别名]
# 或者: from mathics.core.expression.Number import from_mp [as 别名]
def apply_iter(self, expr, i, imin, imax, di, evaluation):
'%(name)s[expr_, {i_Symbol, imin_, imax_, di_}]'
index = imin.evaluate(evaluation).get_real_value()
imax = imax.evaluate(evaluation).get_real_value()
di = di.evaluate(evaluation).get_real_value()
if index is None or imax is None or di is None:
if self.throw_iterb:
evaluation.message(self.get_name(), 'iterb')
return
result = []
while index <= imax:
evaluation.check_stopped()
try:
item = dynamic_scoping(expr.evaluate, {i.name: Number.from_mp(index)}, evaluation)
result.append(item)
except ContinueInterrupt:
if self.allow_loopcontrol:
pass
else:
raise
except BreakInterrupt:
if self.allow_loopcontrol:
break
else:
raise
index = add(index, di)
return self.get_result(result)
示例7: apply_real
# 需要导入模块: from mathics.core.expression import Number [as 别名]
# 或者: from mathics.core.expression.Number import from_mp [as 别名]
def apply_real(self, x, evaluation):
"Abs[x_?RealNumberQ]"
sym_x = x.to_sympy()
if sym_x < 0:
return Number.from_mp(-sym_x)
else:
return x
示例8: negate
# 需要导入模块: from mathics.core.expression import Number [as 别名]
# 或者: from mathics.core.expression.Number import from_mp [as 别名]
def negate(item):
if item.has_form("Times", 1, None):
if isinstance(item.leaves[0], (Integer, Rational, Real, Complex)):
neg = Number.from_mp(-item.leaves[0].to_sympy())
if neg.same(Integer(1)):
if len(item.leaves) == 1:
return neg
else:
return Expression("Times", *item.leaves[1:])
else:
return Expression("Times", neg, *item.leaves[1:])
else:
return Expression("Times", -1, *item.leaves)
elif isinstance(item, (Integer, Rational, Real, Complex)):
return Number.from_mp(-item.to_sympy())
else:
return Expression("Times", -1, item)
示例9: inverse
# 需要导入模块: from mathics.core.expression import Number [as 别名]
# 或者: from mathics.core.expression.Number import from_mp [as 别名]
def inverse(item):
if item.has_form("Power", 2) and isinstance(item.leaves[1], (Integer, Rational, Real)):
neg = Number.from_mp(-item.leaves[1].to_sympy())
if neg.same(Integer(1)):
return item.leaves[0]
else:
return Expression("Power", item.leaves[0], neg)
else:
return item
示例10: format_times
# 需要导入模块: from mathics.core.expression import Number [as 别名]
# 或者: from mathics.core.expression.Number import from_mp [as 别名]
def format_times(self, items, evaluation, op='\u2062'):
'Times[items__]'
def inverse(item):
if item.has_form('Power', 2) and isinstance( # noqa
item.leaves[1], (Integer, Rational, Real)):
neg = Number.from_mp(-item.leaves[1].to_sympy())
if neg.same(Integer(1)):
return item.leaves[0]
else:
return Expression('Power', item.leaves[0], neg)
else:
return item
items = items.get_sequence()
positive = []
negative = []
for item in items:
if (item.has_form('Power', 2) and
isinstance(item.leaves[1], (Integer, Rational, Real)) and
item.leaves[1].to_sympy() < 0): # nopep8
negative.append(inverse(item))
elif isinstance(item, Rational):
numerator = item.numerator()
if not numerator.same(Integer(1)):
positive.append(numerator)
negative.append(item.denominator())
else:
positive.append(item)
if (positive and isinstance(positive[0], (Integer, Real)) and
positive[0].to_sympy() < 0): # nopep8
positive[0] = Number.from_mp(-positive[0].to_sympy())
if positive[0].same(Integer(1)):
del positive[0]
minus = True
else:
minus = False
positive = [Expression('HoldForm', item) for item in positive]
negative = [Expression('HoldForm', item) for item in negative]
if positive:
positive = create_infix(positive, op, 400, 'None')
else:
positive = Integer(1)
if negative:
negative = create_infix(negative, op, 400, 'None')
result = Expression('Divide', Expression(
'HoldForm', positive), Expression('HoldForm', negative))
else:
result = positive
if minus:
result = Expression(
'Minus', result) # Expression('PrecedenceForm', result, 481))
result = Expression('HoldForm', result)
return result
示例11: post_parse
# 需要导入模块: from mathics.core.expression import Number [as 别名]
# 或者: from mathics.core.expression.Number import from_mp [as 别名]
def post_parse(self, expression):
if len(expression.leaves) == 2:
if isinstance(expression.leaves[0], Integer) and \
isinstance(expression.leaves[1], Integer) and expression.leaves[1].to_sympy() != 0:
return Number.from_mp(Rational(expression.leaves[0].to_sympy(), expression.leaves[1].to_sympy()).to_sympy())
else:
if isinstance(expression.leaves[0], Integer) and expression.leaves[0].to_sympy() == 1:
return Expression('Power', expression.leaves[1].post_parse(), Integer(-1))
else:
return Expression('Times', expression.leaves[0].post_parse(), Expression('Power', expression.leaves[1].post_parse(), Integer(-1)))
else:
return super(Divide, self).post_parse(expression)
示例12: apply_inexact
# 需要导入模块: from mathics.core.expression import Number [as 别名]
# 或者: from mathics.core.expression.Number import from_mp [as 别名]
def apply_inexact(self, n, k, evaluation):
'Binomial[n_?InexactNumberQ, k_?NumberQ]'
with workprec(min_prec(n, k)):
n = gmpy2mpmath(n.value)
k = gmpy2mpmath(k.value)
result = mpmath.binomial(n, k)
try:
result = mpmath2gmpy(result)
except SpecialValueError, exc:
return Symbol(exc.name)
number = Number.from_mp(result)
return number
示例13: apply
# 需要导入模块: from mathics.core.expression import Number [as 别名]
# 或者: from mathics.core.expression.Number import from_mp [as 别名]
def apply(self, imin, imax, di, evaluation):
'Range[imin_?RealNumberQ, imax_?RealNumberQ, di_?RealNumberQ]'
imin = imin.value
imax = imax.value
di = di.value
index = imin
result = []
while index <= imax:
evaluation.check_stopped()
result.append(Number.from_mp(index))
index += di
return Expression('List', *result)
示例14: apply
# 需要导入模块: from mathics.core.expression import Number [as 别名]
# 或者: from mathics.core.expression.Number import from_mp [as 别名]
def apply(self, r, i, evaluation):
"Complex[r_?NumberQ, i_?NumberQ]"
if isinstance(r, Complex) or isinstance(i, Complex):
sym_form = r.to_sympy() + sympy.I * i.to_sympy()
sym_r, sym_i = sym_form.simplify().as_real_imag()
else:
sym_r, sym_i = r.to_sympy(), i.to_sympy()
if isinstance(sym_i, sympy.Integer) and sym_i == 0:
return Number.from_mp(sym_r)
else:
return Complex(sym_r, sym_i)
示例15: chop
# 需要导入模块: from mathics.core.expression import Number [as 别名]
# 或者: from mathics.core.expression.Number import from_mp [as 别名]
def chop(expr, delta=10.0 ** (-10.0)):
if isinstance(expr, Real):
if -delta < expr.to_python() < delta:
return Integer(0)
# return expr
elif isinstance(expr, Complex) and expr.get_precision() is not None:
real, imag = expr.real, expr.imag
if -delta < real.to_python() < delta:
real = sympy.Integer(0)
if -delta < imag.to_python() < delta:
imag = sympy.Integer(0)
if imag != 0:
return Complex(real, imag)
else:
return Number.from_mp(real)
elif isinstance(expr, Expression):
return Expression(chop(expr.head), *[chop(leaf) for leaf in expr.leaves])
return expr