Exemplo n.º 1
0
def nstr(x, n=6):
    """
    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:`nstr` is applied recursively
    to each element. For unrecognized classes, :func:`nstr`
    simply returns ``str(x)``.

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

        >>> from sympy.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(nstr(c, n) for c in x))
    if isinstance(x, tuple):
        return "(%s)" % (", ".join(nstr(c, n) for c in x))
    if isinstance(x, mpf):
        return to_str(x._mpf_, n)
    if isinstance(x, mpc):
        return "(" + complex_to_str(x._mpc_[0], x._mpc_[1], n)  + ")"
    if isinstance(x, basestring):
        return repr(x)
    from matrices import matrix
    if isinstance(x, matrix):
        return x.__nstr__(n)
    return str(x)
Exemplo n.º 2
0
def nstr(x, n=6):
    """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.

    If x is a list or tuple, the function is applied to each element.
    For unrecognized classes, this simply returns str(x).

    There is also a companion function nprint that prints the string
    instead of returning it.

        >>> nstr([+pi, ldexp(1,-500)])
        '[3.14159, 3.05494e-151]'
        >>> print([+pi, ldexp(1,-500)])
        [3.14159, 3.05494e-151]
    """
    if isinstance(x, list):
        return "[%s]" % (", ".join(nstr(c, n) for c in x))
    if isinstance(x, tuple):
        return "(%s)" % (", ".join(nstr(c, n) for c in x))
    if isinstance(x, mpf):
        return to_str(x._mpf_, n)
    if isinstance(x, mpc):
        return "(" + complex_to_str(x._mpc_[0], x._mpc_[1], n)  + ")"
    if isinstance(x, basestring):
        return repr(x)
    from matrices import matrix
    if isinstance(x, matrix):
        return x.__nstr__(n)
    return str(x)
Exemplo n.º 3
0
 def __str__(s):
     return "(%s)" % complex_to_str(s.real._mpf_, s.imag._mpf_, mp.dps)
Exemplo n.º 4
0
 def __str__(s):
     return "(%s)" % complex_to_str(s.real._mpf_, s.imag._mpf_, mp.dps)