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


Python expression.Number类代码示例

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


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


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