"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "mpmath/ctx_mp_python.py" between
mpmath-1.0.0.tar.gz and mpmath-1.1.0.tar.gz

About: mpmath is a Python library for arbitrary-precision floating-point arithmetic.

ctx_mp_python.py  (mpmath-1.0.0):ctx_mp_python.py  (mpmath-1.1.0)
#from ctx_base import StandardBaseContext #from ctx_base import StandardBaseContext
from .libmp.backend import basestring, exec_ from .libmp.backend import basestring, exec_
from .libmp import (MPZ, MPZ_ZERO, MPZ_ONE, int_types, repr_dps, from .libmp import (MPZ, MPZ_ZERO, MPZ_ONE, int_types, repr_dps,
round_floor, round_ceiling, dps_to_prec, round_nearest, prec_to_dps, round_floor, round_ceiling, dps_to_prec, round_nearest, prec_to_dps,
ComplexResult, to_pickable, from_pickable, normalize, ComplexResult, to_pickable, from_pickable, normalize,
from_int, from_float, from_str, to_int, to_float, to_str, from_int, from_float, from_npfloat, from_Decimal, from_str, to_int, to_float , to_str,
from_rational, from_man_exp, from_rational, from_man_exp,
fone, fzero, finf, fninf, fnan, fone, fzero, finf, fninf, fnan,
mpf_abs, mpf_pos, mpf_neg, mpf_add, mpf_sub, mpf_mul, mpf_mul_int, mpf_abs, mpf_pos, mpf_neg, mpf_add, mpf_sub, mpf_mul, mpf_mul_int,
mpf_div, mpf_rdiv_int, mpf_pow_int, mpf_mod, mpf_div, mpf_rdiv_int, mpf_pow_int, mpf_mod,
mpf_eq, mpf_cmp, mpf_lt, mpf_gt, mpf_le, mpf_ge, mpf_eq, mpf_cmp, mpf_lt, mpf_gt, mpf_le, mpf_ge,
mpf_hash, mpf_rand, mpf_hash, mpf_rand,
mpf_sum, mpf_sum,
bitcount, to_fixed, bitcount, to_fixed,
mpc_to_str, mpc_to_str,
mpc_to_complex, mpc_hash, mpc_pos, mpc_is_nonzero, mpc_neg, mpc_conjugate, mpc_to_complex, mpc_hash, mpc_pos, mpc_is_nonzero, mpc_neg, mpc_conjugate,
skipping to change at line 644 skipping to change at line 644
mpf('0.75') mpf('0.75')
>>> mpmathify('2+3j') >>> mpmathify('2+3j')
mpc(real='2.0', imag='3.0') mpc(real='2.0', imag='3.0')
""" """
if type(x) in ctx.types: return x if type(x) in ctx.types: return x
if isinstance(x, int_types): return ctx.make_mpf(from_int(x)) if isinstance(x, int_types): return ctx.make_mpf(from_int(x))
if isinstance(x, float): return ctx.make_mpf(from_float(x)) if isinstance(x, float): return ctx.make_mpf(from_float(x))
if isinstance(x, complex): if isinstance(x, complex):
return ctx.make_mpc((from_float(x.real), from_float(x.imag))) return ctx.make_mpc((from_float(x.real), from_float(x.imag)))
if type(x).__module__ == 'numpy': return ctx.npconvert(x)
if isinstance(x, numbers.Rational): # e.g. Fraction
try: x = rational.mpq(int(x.numerator), int(x.denominator))
except: pass
prec, rounding = ctx._prec_rounding prec, rounding = ctx._prec_rounding
if isinstance(x, rational.mpq): if isinstance(x, rational.mpq):
p, q = x._mpq_ p, q = x._mpq_
return ctx.make_mpf(from_rational(p, q, prec)) return ctx.make_mpf(from_rational(p, q, prec))
if strings and isinstance(x, basestring): if strings and isinstance(x, basestring):
try: try:
_mpf_ = from_str(x, prec, rounding) _mpf_ = from_str(x, prec, rounding)
return ctx.make_mpf(_mpf_) return ctx.make_mpf(_mpf_)
except ValueError: except ValueError:
pass pass
if hasattr(x, '_mpf_'): return ctx.make_mpf(x._mpf_) if hasattr(x, '_mpf_'): return ctx.make_mpf(x._mpf_)
if hasattr(x, '_mpc_'): return ctx.make_mpc(x._mpc_) if hasattr(x, '_mpc_'): return ctx.make_mpc(x._mpc_)
if hasattr(x, '_mpmath_'): if hasattr(x, '_mpmath_'):
return ctx.convert(x._mpmath_(prec, rounding)) return ctx.convert(x._mpmath_(prec, rounding))
if type(x).__module__ == 'decimal':
try: return ctx.make_mpf(from_Decimal(x, prec, rounding))
except: pass
return ctx._convert_fallback(x, strings) return ctx._convert_fallback(x, strings)
def npconvert(ctx, x):
"""
Converts *x* to an ``mpf`` or ``mpc``. *x* should be a numpy
scalar.
"""
import numpy as np
if isinstance(x, np.integer): return ctx.make_mpf(from_int(int(x)))
if isinstance(x, np.floating): return ctx.make_mpf(from_npfloat(x))
if isinstance(x, np.complexfloating):
return ctx.make_mpc((from_npfloat(x.real), from_npfloat(x.imag)))
raise TypeError("cannot create mpf from " + repr(x))
def isnan(ctx, x): def isnan(ctx, x):
""" """
Return *True* if *x* is a NaN (not-a-number), or for a complex Return *True* if *x* is a NaN (not-a-number), or for a complex
number, whether either the real or complex part is NaN; number, whether either the real or complex part is NaN;
otherwise return *False*:: otherwise return *False*::
>>> from mpmath import * >>> from mpmath import *
>>> isnan(3.14) >>> isnan(3.14)
False False
>>> isnan(nan) >>> isnan(nan)
skipping to change at line 913 skipping to change at line 932
>>> fdot(_) >>> fdot(_)
mpf('6.5') mpf('6.5')
>>> A = [2, 1.5, 3j] >>> A = [2, 1.5, 3j]
>>> B = [1+j, 3, -1-j] >>> B = [1+j, 3, -1-j]
>>> fdot(A, B) >>> fdot(A, B)
mpc(real='9.5', imag='-1.0') mpc(real='9.5', imag='-1.0')
>>> fdot(A, B, conjugate=True) >>> fdot(A, B, conjugate=True)
mpc(real='3.5', imag='-5.0') mpc(real='3.5', imag='-5.0')
""" """
if B: if B is not None:
A = zip(A, B) A = zip(A, B)
prec, rnd = ctx._prec_rounding prec, rnd = ctx._prec_rounding
real = [] real = []
imag = [] imag = []
other = 0 other = 0
hasattr_ = hasattr hasattr_ = hasattr
types = (ctx.mpf, ctx.mpc) types = (ctx.mpf, ctx.mpc)
for a, b in A: for a, b in A:
if type(a) not in types: a = ctx.convert(a) if type(a) not in types: a = ctx.convert(a)
if type(b) not in types: b = ctx.convert(b) if type(b) not in types: b = ctx.convert(b)
 End of changes. 5 change blocks. 
2 lines changed or deleted 21 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)