Ejemplo n.º 1
0
 def __repr__(self):
     if self.ctx.pretty:
         return str(self)
     a, b = self._mpi_
     n = repr_dps(self.ctx.prec)
     a = libmp.to_str(a, n)
     b = libmp.to_str(b, n)
     return "mpi(%r, %r)" % (a, b)
Ejemplo n.º 2
0
 def __repr__(self):
     if self.ctx.pretty:
         return str(self)
     a, b = self._mpi_
     n = repr_dps(self.ctx.prec)
     a = libmp.to_str(a, n)
     b = libmp.to_str(b, n)
     return "mpi(%r, %r)" % (a, b)
Ejemplo n.º 3
0
    def nstr(ctx, x, n=6, **kwargs):
        """
        Convert an ``mpf`` or ``mpc`` to a decimal string literal with *n*
        significant digits. The small default value for *n* is chosen to
        make this function useful for printing collections of numbers
        (lists, matrices, etc).

        If *x* is a list or tuple, :func:`~mpmath.nstr` is applied recursively
        to each element. For unrecognized classes, :func:`~mpmath.nstr`
        simply returns ``str(x)``.

        The companion function :func:`~mpmath.nprint` prints the result
        instead of returning it.

            >>> from mpmath import *
            >>> nstr([+pi, ldexp(1,-500)])
            '[3.14159, 3.05494e-151]'
            >>> nprint([+pi, ldexp(1,-500)])
            [3.14159, 3.05494e-151]
        """
        if isinstance(x, list):
            return "[%s]" % (", ".join(ctx.nstr(c, n, **kwargs) for c in x))
        if isinstance(x, tuple):
            return "(%s)" % (", ".join(ctx.nstr(c, n, **kwargs) for c in x))
        if hasattr(x, '_mpf_'):
            return to_str(x._mpf_, n, **kwargs)
        if hasattr(x, '_mpc_'):
            return "(" + mpc_to_str(x._mpc_, n, **kwargs)  + ")"
        if isinstance(x, basestring):
            return repr(x)
        if isinstance(x, ctx.matrix):
            return x.__nstr__(n, **kwargs)
        return str(x)
Ejemplo n.º 4
0
 def __str__(s):
     return to_str(s._mpf_, s.context._str_digits)
Ejemplo n.º 5
0
 def __repr__(s):
     if s.context.pretty:
         return str(s)
     return "mpf('%s')" % to_str(s._mpf_, s.context._repr_digits)
Ejemplo n.º 6
0
    def mpi_to_str(ctx, x, dps=None, use_spaces=True, brackets=('[', ']'),
                   mode='brackets', error_dps=4, **kwargs):
        """
        Convert a mpi interval to a string.

        **Arguments**

        *dps*
            decimal places to use for printing
        *use_spaces*
            use spaces for more readable output, defaults to true
        *brackets*
            tuple of two strings indicating the brackets to use
        *mode*
            mode of display: 'plusminus', 'percent', 'brackets' (default) or 'diff'
        *error_dps*
            limit the error to *error_dps* digits (mode 'plusminus and 'percent')

        **Examples**

            >>> from mpmath import mpi, mp
            >>> mp.dps = 30
            >>> x = mpi(1, 2)
            >>> mpi_to_str(x, mode='plusminus')
            '1.5 +- 5.0e-1'
            >>> mpi_to_str(x, mode='percent')
            '1.5 (33.33%)'
            >>> mpi_to_str(x, mode='brackets')
            '[1.0, 2.0]'
            >>> mpi_to_str(x, mode='brackets' , brackets=('<', '>'))
            '<1.0, 2.0>'
            >>> x = mpi('5.2582327113062393041', '5.2582327113062749951')
            >>> mpi_to_str(x, mode='diff')
            '5.2582327113062[4, 7]'
            >>> mpi_to_str(mpi(0), mode='percent')
            '0.0 (0%)'

        """
        if dps is None:
            dps = ctx.dps # TODO: maybe choose a smaller default value
        a = to_str(x.a._mpf_, dps, **kwargs)
        b = to_str(x.b._mpf_, dps, **kwargs)
        mid = to_str(x.mid._mpf_, dps, **kwargs)
        delta = to_str((x.delta/2)._mpf_, error_dps, **kwargs)
        sp = ""
        if use_spaces:
            sp = " "
        br1, br2 = brackets
        if mode == 'plusminus':
            s = mid + sp + "+-" + sp + delta
        elif mode == 'percent':
            a = x.mid
            if x.mid != 0:
                b = 100*x.delta/(2*x.mid)
            else:
                b = MPZ_ZERO
            m = str(a)
            p = ctx.nstr(b, error_dps)
            s = m + sp + "(" + p + "%)"
        elif mode == 'brackets':
            s = br1 + a.strip() + "," + sp + b + br2
        elif mode == 'diff':
            # use more digits if str(x.a) and str(x.b) are equal
            if a == b:
                a = to_str(x.a._mpf_, repr_dps(ctx.prec), **kwargs)
                b = to_str(x.b._mpf_, repr_dps(ctx.prec), **kwargs)
            # separate mantissa and exponent
            a = a.split('e')
            if len(a) == 1:
                a.append('')
            b = b.split('e')
            if len(b) == 1:
                b.append('')
            if a[1] == b[1]:
                if a[0] != b[0]:
                    for i in xrange(len(a[0]) + 1):
                        if a[0][i] != b[0][i]:
                            break
                    s = (a[0][:i] + br1 + a[0][i:] + ',' + sp + b[0][i:] + br2
                         + 'e'*min(len(a[1]), 1) + a[1])
                else: # no difference
                    s = a[0] + br1 + br2 + 'e'*min(len(a[1]), 1) + a[1]
            else:
                s = br1 + 'e'.join(a) + ',' + sp + 'e'.join(b) + br2
        else:
            raise ValueError("'%s' is unknown mode for printing mpi" % mode)
        return s
Ejemplo n.º 7
0
 def __str__(s): return to_str(s._mpf_, s.context._str_digits)
 def __hash__(s): return mpf_hash(s._mpf_)
Ejemplo n.º 8
0
 def __repr__(s):
     if s.context.pretty:
         return str(s)
     return "mpf('%s')" % to_str(s._mpf_, s.context._repr_digits)