"Fossies" - the Fresh Open Source Software Archive

Member "NZMATH-1.2.0/nzmath/real.py" (19 Nov 2012, 8438 Bytes) of package /linux/misc/old/NZMATH-1.2.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "real.py" see the Fossies "Dox" file reference documentation.

    1 """
    2 real -- real numbers and the real number field.
    3 
    4 The module real provides arbitrary precision real numbers and their
    5 utilities.  The functions provided are corresponding to the math
    6 standard module.
    7 """
    8 
    9 from __future__ import division
   10 import itertools
   11 import warnings
   12 
   13 import nzmath.arith1 as arith1
   14 import nzmath.rational as rational
   15 import nzmath.ring as ring
   16 from nzmath.plugins import MATHMODULE as math, FLOATTYPE as Float, \
   17      CHECK_REAL_OR_COMPLEX as check_real_or_complex
   18 
   19 
   20 class Real(ring.FieldElement):
   21     """
   22     Real is a class of real.
   23     This class is only for consistency for other Ring object.
   24     """
   25 
   26     convertable = (Float, int, long, rational.Rational)
   27 
   28     def __init__(self, value):
   29         """
   30         value will be wrapped in Float.
   31         """
   32         ring.FieldElement.__init__(self)
   33         if isinstance(value, rational.Rational):
   34             self.data = value.toFloat()
   35         else:
   36             self.data = Float(value)
   37 
   38     def __add__(self, other):
   39         if isinstance(other, Real):
   40             result = self.data + other.data
   41         elif isinstance(other, self.convertable):
   42             result = self.data + other
   43         else:
   44             return NotImplemented
   45         return self.__class__(result)
   46 
   47     def __radd__(self, other):
   48         if isinstance(other, self.convertable):
   49             result = other + self.data
   50         else:
   51             return NotImplemented
   52         return self.__class__(result)
   53 
   54     def __sub__(self, other):
   55         if isinstance(other, Real):
   56             result = self.data - other.data
   57         elif isinstance(other, self.convertable):
   58             result = self.data - other
   59         else:
   60             return NotImplemented
   61         return self.__class__(result)
   62 
   63     def __rsub__(self, other):
   64         if isinstance(other, self.convertable):
   65             result = other - self.data
   66         else:
   67             return NotImplemented
   68         return self.__class__(result)
   69 
   70     def __mul__(self, other):
   71         if isinstance(other, Real):
   72             result = self.data * other.data
   73         elif isinstance(other, self.convertable):
   74             result = self.data * other
   75         else:
   76             return NotImplemented
   77         return self.__class__(result)
   78 
   79     def __rmul__(self, other):
   80         if isinstance(other, self.convertable):
   81             result = other * self.data
   82         else:
   83             return NotImplemented
   84         return self.__class__(result)
   85 
   86     def __truediv__(self, other):
   87         if isinstance(other, Real):
   88             result = self.data / other.data
   89         elif isinstance(other, self.convertable):
   90             result = self.data / other
   91         else:
   92             return NotImplemented
   93         return self.__class__(result)
   94 
   95     __div__ = __truediv__
   96 
   97     def __rtruediv__(self, other):
   98         if isinstance(other, self.convertable):
   99             result = other / self.data
  100         else:
  101             return NotImplemented
  102         return self.__class__(result)
  103 
  104     __rdiv__ = __rtruediv__
  105 
  106     def __pow__(self, other):
  107         if isinstance(other, Real):
  108             result = math.pow(self.data, other.data)
  109         elif isinstance(other, self.convertable):
  110             result = math.pow(self.data, other)
  111         return result
  112 
  113     def __eq__(self, other):
  114         if isinstance(other, Real):
  115             return self.data == other.data
  116         elif isinstance(other, self.convertable):
  117             return self.data == other
  118         else:
  119             return NotImplemented
  120 
  121     def __hash__(self):
  122         return hash(self.data)
  123 
  124     def getRing(self):
  125         """
  126         Return the real field instance.
  127         """
  128         return theRealField
  129 
  130 
  131 class RealField(ring.Field):
  132     """
  133     RealField is a class of the field of real numbers.
  134     The class has the single instance 'theRealField'.
  135     """
  136 
  137     def __init__(self):
  138         ring.Field.__init__(self)
  139         self._one = Real(1)
  140         self._zero = Real(0)
  141 
  142     def __str__(self):
  143         return "R"
  144 
  145     def __repr__(self):
  146         return "%s()" % (self.__class__.__name__, )
  147 
  148     def __contains__(self, element):
  149         if isinstance(element, (int, long, float, Float, Real, rational.Rational)):
  150             return True
  151         else:
  152             try:
  153                 if check_real_or_complex(element):
  154                     return True
  155             except TypeError:
  156                 if hasattr(element, 'conjugate'):
  157                     return element == element.conjugate()
  158                 pass
  159         if hasattr(element, 'getRing') and element.getRing().issubring(self):
  160             return True
  161         return False
  162 
  163     def __eq__(self, other):
  164         return isinstance(other, self.__class__)
  165 
  166     def __ne__(self, other):
  167         return not self.__eq__(other)
  168 
  169     def __hash__(self):
  170         return 2
  171 
  172     # property one
  173     def _getOne(self):
  174         "getter for one"
  175         return self._one
  176 
  177     one = property(_getOne, None, None, "multiplicative unit.")
  178 
  179     # property zero
  180     def _getZero(self):
  181         "getter for zero"
  182         return self._zero
  183 
  184     zero = property(_getZero, None, None, "additive unit.")
  185 
  186     def issubring(self, aRing):
  187         if isinstance(aRing, RealField):
  188             return True
  189         elif self.issuperring(aRing):
  190             return False
  191         return aRing.issuperring(self)
  192 
  193     def issuperring(self, aRing):
  194         if isinstance(aRing, RealField):
  195             return True
  196         elif rational.theRationalField.issuperring(aRing):
  197             return True
  198         return aRing.issubring(self)
  199 
  200     def createElement(self, seed):
  201         return Float(seed)
  202 
  203     def getCharacteristic(self):
  204         """
  205         The characteristic of the real field is zero.
  206         """
  207         return 0
  208 
  209 
  210 def log1piter(xx):
  211     " iterator for log(1+x)."
  212     d = 1
  213     positive = True
  214     t = rational.Rational(xx)
  215     yield t
  216     while True:
  217         d += 1
  218         positive = not positive
  219         t *= xx
  220         if positive:
  221             yield (t / d)
  222         else:
  223             yield (-t / d)
  224 
  225 def floor(x):
  226     """
  227     floor(x) returns the integer; if x is an integer then x itself,
  228     otherwise the biggest integer less than x.
  229     """
  230     rx = rational.Rational(x)
  231     if rx.denominator == 1:
  232         return rational.Integer(rx.numerator)
  233     return rx.numerator // rx.denominator
  234 
  235 def ceil(x):
  236     """
  237     ceil(x) returns the integer; if x is an integer then x itself,
  238     otherwise the smallest integer greater than x.
  239     """
  240     rx = rational.Rational(x)
  241     if rx.denominator == 1:
  242         return rational.Integer(rx.numerator)
  243     return rx.numerator // rx.denominator + 1
  244 
  245 def tranc(x):
  246     """
  247     tranc(x) returns the integer; if x is an integer then x itself,
  248     otherwise the nearest integer to x.  If x has the fraction part
  249     1/2, then bigger one will be chosen.
  250     """
  251     rx = rational.Rational(x)
  252     if rx.denominator == 1:
  253         return rational.Integer(rx.numerator)
  254     return floor(x + rational.Rational(1, 2))
  255 
  256 def fabs(x):
  257     """
  258     returns absolute value of x.
  259     """
  260     return abs(rational.Rational(x))
  261 
  262 def fmod(x, y):
  263     """
  264     returns x - n * y, where n is the quotient of x / y, rounded
  265     towards zero to an integer.
  266     """
  267     fquot = rational.Rational(x) / y
  268     if fquot < 0:
  269         n = -floor(-fquot)
  270     else:
  271         n = floor(fquot)
  272     return x - n * y
  273 
  274 def frexp(x):
  275     """
  276     Return a tuple (m, e) where x = m * 2 ** e, 1/2 <= abs(m) < 1 and
  277     e is an integer.
  278     This function is provided as the counter-part of math.frexp, but it
  279     might not be useful.
  280     """
  281     if x == 0:
  282         return (rational.Rational(0), 0)
  283     m = rational.Rational(x)
  284     e = 0
  285     if x > 0:
  286         while m >= 1:
  287             m /= 2
  288             e += 1
  289         while m < rational.Rational(1, 2):
  290             m *= 2
  291             e -= 1
  292     else:
  293         while m <= -1:
  294             m /= 2
  295             e += 1
  296         while m > rational.Rational(-1, 2):
  297             m *= 2
  298             e -= 1
  299     return (m, e)
  300 
  301 def ldexp(x, i):
  302     """
  303     returns x * 2 ** i.
  304     """
  305     return x * 2 ** i
  306 
  307 def EulerTransform(iterator):
  308     """
  309     Return an iterator which yields terms of Euler transform of the
  310     given iterator.
  311     """
  312     stock = []
  313     b = rational.Rational(1, 2)
  314     l = -1
  315     for term in iterator:
  316         stock.append(term)
  317         for i in xrange(l, -1, -1):
  318             stock[i] += stock[i+1]
  319         yield b * stock[0]
  320         b /= 2
  321         l += 1
  322 
  323 # constants
  324 theRealField = RealField()