def mpc_to_str(z, dps, **kwargs): re, im = z rs = to_str(re, dps) if im[0]: return rs + " - " + to_str(mpf_neg(im), dps, **kwargs) + "j" else: return rs + " + " + to_str(im, dps, **kwargs) + "j"
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)
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)
def mpi_str(s, prec): sa, sb = s dps = prec_to_dps(prec) + 5 return "[%s, %s]" % (to_str(sa, dps), to_str(sb, dps))
def complex_to_str(re, im, dps): rs = to_str(re, dps) if im[0]: return rs + " - " + to_str(mpf_neg(im), dps) + "j" else: return rs + " + " + to_str(im, dps) + "j"
def mpi_to_str(x, dps, 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* pair of strings (or two-character string) giving left and right brackets *mode* mode of display: 'plusminus', 'percent', 'brackets' (default) or 'diff' *error_dps* limit the error to *error_dps* digits (mode 'plusminus and 'percent') Additional keyword arguments are forwarded to the mpf-to-string conversion for the components of the output. **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%)' """ prec = dps_to_prec(dps) wp = prec + 20 a, b = x mid = mpi_mid(x, prec) delta = mpi_delta(x, prec) a_str = to_str(a, dps, **kwargs) b_str = to_str(b, dps, **kwargs) mid_str = to_str(mid, dps, **kwargs) sp = "" if use_spaces: sp = " " br1, br2 = brackets if mode == 'plusminus': delta_str = to_str(mpf_shift(delta, -1), dps, **kwargs) s = mid_str + sp + "+-" + sp + delta_str elif mode == 'percent': if mid == fzero: p = fzero else: # p = 100 * delta(x) / (2*mid(x)) p = mpf_mul(delta, from_int(100)) p = mpf_div(p, mpf_mul(mid, from_int(2)), wp) s = mid_str + sp + "(" + to_str(p, error_dps) + "%)" elif mode == 'brackets': s = br1 + a_str + "," + sp + b_str + br2 elif mode == 'diff': # use more digits if str(x.a) and str(x.b) are equal if a_str == b_str: a_str = to_str(a, dps + 3, **kwargs) b_str = to_str(b, dps + 3, **kwargs) # separate mantissa and exponent a = a_str.split('e') if len(a) == 1: a.append('') b = b_str.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
def mpi_to_str(x, dps, 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* pair of strings (or two-character string) giving left and right brackets *mode* mode of display: 'plusminus', 'percent', 'brackets' (default) or 'diff' *error_dps* limit the error to *error_dps* digits (mode 'plusminus and 'percent') Additional keyword arguments are forwarded to the mpf-to-string conversion for the components of the output. **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%)' """ prec = dps_to_prec(dps) wp = prec + 20 a, b = x mid = mpi_mid(x, prec) delta = mpi_delta(x, prec) a_str = to_str(a, dps, **kwargs) b_str = to_str(b, dps, **kwargs) mid_str = to_str(mid, dps, **kwargs) sp = "" if use_spaces: sp = " " br1, br2 = brackets if mode == 'plusminus': delta_str = to_str(mpf_shift(delta,-1), dps, **kwargs) s = mid_str + sp + "+-" + sp + delta_str elif mode == 'percent': if mid == fzero: p = fzero else: # p = 100 * delta(x) / (2*mid(x)) p = mpf_mul(delta, from_int(100)) p = mpf_div(p, mpf_mul(mid, from_int(2)), wp) s = mid_str + sp + "(" + to_str(p, error_dps) + "%)" elif mode == 'brackets': s = br1 + a_str + "," + sp + b_str + br2 elif mode == 'diff': # use more digits if str(x.a) and str(x.b) are equal if a_str == b_str: a_str = to_str(a, dps+3, **kwargs) b_str = to_str(b, dps+3, **kwargs) # separate mantissa and exponent a = a_str.split('e') if len(a) == 1: a.append('') b = b_str.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
def __str__(s): return to_str(s._mpf_, mp.dps) def __hash__(s): return mpf_hash(s._mpf_)
def __repr__(s): return "mpf('%s')" % to_str(s._mpf_, repr_dps(mp.prec)) def __str__(s): return to_str(s._mpf_, mp.dps)