• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python expression.Number类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中mathics.core.expression.Number的典型用法代码示例。如果您正苦于以下问题:Python Number类的具体用法?Python Number怎么用?Python Number使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



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

示例1: pyobject

 def pyobject(self, ex, obj):
     from mathics.core import expression
     from mathics.core.expression import Number
     
     if obj is None:
         return expression.Symbol('Null')
     elif isinstance(obj, (list, tuple)) or is_Vector(obj):
         return expression.Expression('List', *(from_sage(item, self.subs) for item in obj))
     elif isinstance(obj, Constant):
         return expression.Symbol(obj._conversions.get('mathematica', obj._name))
     elif is_Integer(obj):
         return expression.Integer(str(obj))
     elif isinstance(obj, sage.Rational):
         rational = expression.Rational(str(obj))
         if rational.value.denom() == 1:
             return expression.Integer(rational.value.numer())
         else:
             return rational
     elif isinstance(obj, sage.RealDoubleElement) or is_RealNumber(obj):
         return expression.Real(str(obj))
     elif is_ComplexNumber(obj):
         real = Number.from_string(str(obj.real())).value
         imag = Number.from_string(str(obj.imag())).value
         return expression.Complex(real, imag)
     elif isinstance(obj, NumberFieldElement_quadratic):
         # TODO: this need not be a complex number, but we assume so!
         real = Number.from_string(str(obj.real())).value
         imag = Number.from_string(str(obj.imag())).value
         return expression.Complex(real, imag)
     else:
         return expression.from_python(obj)
开发者ID:cjiang,项目名称:Mathics,代码行数:31,代码来源:convert.py


示例2: apply

    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)
开发者ID:0xffea,项目名称:Mathics,代码行数:56,代码来源:arithmetic.py


示例3: append_last

 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))
开发者ID:sitelmi,项目名称:Mathics,代码行数:10,代码来源:arithmetic.py


示例4: apply

    def apply(self, z, evaluation):
        '%(name)s[z__]'

        args = z.numerify(evaluation).get_sequence()
        mpmath_function = self.get_mpmath_function(args)
        result = None

        # if no arguments are inexact attempt to use sympy
        if all(not x.is_inexact() for x in args):
            result = Expression(self.get_name(), *args).to_sympy()
            result = self.prepare_mathics(result)
            result = from_sympy(result)
            # evaluate leaves to convert e.g. Plus[2, I] -> Complex[2, 1]
            return result.evaluate_leaves(evaluation)
        elif mpmath_function is None:
            return

        if not all(isinstance(arg, Number) for arg in args):
            return

        if any(arg.is_machine_precision() for arg in args):
            # if any argument has machine precision then the entire calculation
            # is done with machine precision.
            float_args = [arg.round().get_float_value(permit_complex=True) for arg in args]
            if None in float_args:
                return

            result = self.call_mpmath(mpmath_function, float_args)
            if isinstance(result, (mpmath.mpc, mpmath.mpf)):
                if mpmath.isinf(result) and isinstance(result, mpmath.mpc):
                    result = Symbol('ComplexInfinity')
                elif mpmath.isinf(result) and result > 0:
                    result = Expression('DirectedInfinity', Integer(1))
                elif mpmath.isinf(result) and result < 0:
                    result = Expression('DirectedInfinity', Integer(-1))
                elif mpmath.isnan(result):
                    result = Symbol('Indeterminate')
                else:
                    result = Number.from_mpmath(result)
        else:
            prec = min_prec(*args)
            d = dps(prec)
            args = [Expression('N', arg, Integer(d)).evaluate(evaluation) for arg in args]
            with mpmath.workprec(prec):
                mpmath_args = [x.to_mpmath() for x in args]
                if None in mpmath_args:
                    return
                result = self.call_mpmath(mpmath_function, mpmath_args)
                if isinstance(result, (mpmath.mpc, mpmath.mpf)):
                    result = Number.from_mpmath(result, d)
        return result
开发者ID:poke1024,项目名称:Mathics,代码行数:51,代码来源:arithmetic.py


示例5: apply

 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)
开发者ID:mikexstudios,项目名称:Mathics,代码行数:35,代码来源:arithmetic.py


示例6: apply_iter

 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)
开发者ID:cjiang,项目名称:Mathics,代码行数:28,代码来源:lists.py


示例7: post_parse

 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)
开发者ID:GitAnt,项目名称:Mathics,代码行数:7,代码来源:arithmetic.py


示例8: apply_real

 def apply_real(self, x, evaluation):
     'Abs[x_?RealNumberQ]'
     
     if x.value < 0:
         return Number.from_mp(-x.value)
     else:
         return x
开发者ID:mikexstudios,项目名称:Mathics,代码行数:7,代码来源:arithmetic.py


示例9: negate

 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)
开发者ID:sitelmi,项目名称:Mathics,代码行数:17,代码来源:arithmetic.py


示例10: apply_real

    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
开发者ID:sitelmi,项目名称:Mathics,代码行数:8,代码来源:arithmetic.py


示例11: inverse

 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
开发者ID:sitelmi,项目名称:Mathics,代码行数:9,代码来源:arithmetic.py


示例12: format_times

    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
开发者ID:bnjones,项目名称:Mathics,代码行数:56,代码来源:arithmetic.py


示例13: post_parse

 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)
开发者ID:0xffea,项目名称:Mathics,代码行数:12,代码来源:arithmetic.py


示例14: apply

 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)
开发者ID:bwright,项目名称:Mathics,代码行数:13,代码来源:lists.py


示例15: apply_inexact

 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
开发者ID:cjiang,项目名称:Mathics,代码行数:13,代码来源:combinatorial.py


示例16: apply

    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)
开发者ID:sitelmi,项目名称:Mathics,代码行数:13,代码来源:arithmetic.py


示例17: apply_N

    def apply_N(self, k, precision, evaluation):
        'N[AiryBiZero[k_Integer], precision_]'

        try:
            d = get_precision(precision, evaluation)
        except PrecisionValueError:
            return

        if d is None:
            p = machine_precision
        else:
            p = _prec(d)

        k_int = k.get_int_value()

        with mpmath.workprec(p):
            result = mpmath.airybizero(k_int)
            return Number.from_mpmath(result, d)
开发者ID:Piruzzolo,项目名称:Mathics,代码行数:18,代码来源:specialfunctions.py


示例18: chop

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
开发者ID:NitikaAgarwal,项目名称:Mathics,代码行数:18,代码来源:numeric.py


示例19: apply_iter

 def apply_iter(self, expr, i, imin, imax, di, evaluation):
     '%(name)s[expr_, {i_Symbol, imin_, imax_, di_}]'
     
     if di.get_int_value() == 1 and isinstance(self, SageFunction):
         whole_expr = Expression(self.get_name(), expr, Expression('List', i, imin, imax))
         sympy_expr = whole_expr.to_sympy()
         
         # apply Together to produce results similar to Mathematica
         result = sympy.together(sympy_expr)
         result = from_sympy(result)
         result = cancel(result)
         
         if not result.same(whole_expr):
             return result
     
     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)
开发者ID:craftoid,项目名称:Mathics,代码行数:42,代码来源:lists.py


示例20: apply_complex

 def apply_complex(self, z, evaluation):
     'Abs[z_Complex]'
     
     real, imag = z.to_sympy().as_real_imag()
     return Expression('Sqrt', Expression('Plus', Number.from_mp(real ** 2), Number.from_mp(imag ** 2)))
开发者ID:0xffea,项目名称:Mathics,代码行数:5,代码来源:arithmetic.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python numbers.dps函数代码示例发布时间:2022-05-27
下一篇:
Python expression.Expression类代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap