def gamma(x): # type: (float) -> float t = False y = 0.0 try: y = _b.gamma(x) # call c-api except: t = True from amath.DataTypes import Infinity from amath.testing.types import isinf, isnan, intQ x = int(x) if x >= 170 or t: # to not overflow float or if in _basic failure if intQ(x): # x must be an int from amath.stats.stats import product return product(lambda k: k, 1, x) // x elif isinf(x): if x > 0: return Infinity(True) else: from amath.Errors import Indeterminate raise Indeterminate() else: raise TypeError("For values over 170, x must be a integer") else: if isinf(y) or isnan(y): return Infinity(None) elif isinstance(x, int) or int(x) == x: return int(y) else: return y
def gamma(x): # type: (float) -> float t = False y = 0.0 try: y = _b.gamma(x) # call c-api except: t = True from amath.DataTypes import Infinity, Function from amath.testing.types import isinf, isnan, intQ x = int(x) if x >= 170 or t: # to not overflow float or if in _basic failure if intQ(x): # x must be an int from amath.stats.stats import product return product(lambda k: k, 1, x) // x elif isinf(x): if x > 0: return Infinity(True) else: from amath.Errors import Indeterminate raise Indeterminate() else: raise TypeError("For values over 170, x must be a integer") else: if isinf(y) or isnan(y): return Infinity(None) elif isinstance(x, int) or int(x) == x: return int(y) else: return y
def __rpow__(self, other): from amath.testing.types import isinf if not isinf(other): if other == 0: if self.n is None: raise Indeterminate( "Indeterminate expression 0**ComplexInfinity encountered" ) elif self.n: return 0 else: return Infinity(None) elif (other == 1) or (other == -1): raise Indeterminate( "Indeterminate expression ({0})**{1} encountered".format( str(other), repr(self))) elif other < 0: if self.n is None: raise Indeterminate( "Indeterminate expression " "({0})**ComplexInfinity encountered".format( str(other))) elif self.n: return Infinity(None) else: return 0 else: if self.n is None: raise Indeterminate( "Indeterminate expression {0}**ComplexInfinity encountered" .format(str(other))) elif self.n: return Infinity(True) else: return 0
def __rpow__(self, other): from amath.testing.types import isinf if not isinf(other): if other == 0: if self.n is None: raise Indeterminate("Indeterminate expression 0**ComplexInfinity encountered") elif self.n: return 0 else: return Infinity(None) elif (other == 1) or (other == -1): raise Indeterminate("Indeterminate expression ({0})**{1} encountered".format(str(other), repr(self))) elif other < 0: if self.n is None: raise Indeterminate("Indeterminate expression " "({0})**ComplexInfinity encountered".format(str(other))) elif self.n: return Infinity(None) else: return 0 else: if self.n is None: raise Indeterminate("Indeterminate expression {0}**ComplexInfinity encountered".format(str(other))) elif self.n: return Infinity(True) else: return 0
def __pow__(self, power, modulo=None): from amath.testing.types import isinf if not isinf(power): if power == 0: if self.n is None: raise Indeterminate("Indeterminate expression ComplexInfinity**0 encountered") elif self.n: raise Indeterminate("Indeterminate expression inf**0 encountered") else: raise Indeterminate("Indeterminate expression (-inf)**0 encountered") elif power < 0: return 0 else: return Infinity(self.n) else: if power > 0: return Infinity(None) elif power < 0: return 0 elif isinstance(power, Infinity): if self.n is None: raise Indeterminate("Indeterminate expression ComplexInfinity**ComplexInfinity encountered") elif self.n: raise Indeterminate("Indeterminate expression inf**ComplexInfinity encountered") else: raise Indeterminate("Indeterminate expression (-inf)**ComplexInfinity encountered")
def __mul__(self, other): from amath.testing.types import isinf if isinstance(other, Infinity): if self.n is None: return Infinity(None) elif other.n is None: return Infinity(None) elif self.n: if other.n: return Infinity(True) else: return Infinity(False) elif not self.n: if other.n: return Infinity(False) else: return Infinity(True) elif isinf(other): if self.n is None: return Infinity(None) elif self.n: if other > 0: return Infinity(True) else: return Infinity(False) else: if other > 0: return Infinity(False) else: return Infinity(True) elif isinstance(other, complex): if other.imag < 0: return self.__neg__() elif other.imag > 0: return Infinity(self.n) else: return Infinity(self.n) else: if self.n is None: if other == 0: raise Indeterminate("Indeterminate expression 0 * ComplexInfinity encountered") else: return Infinity(None) elif self.n: if other == 0: raise Indeterminate("Indeterminate expression 0 * inf encountered") elif other > 0: return Infinity(True) else: return Infinity(False) else: if other == 0: raise Indeterminate("Indeterminate expression 0 * (-inf) encountered") elif other > 0: return Infinity(False) else: return Infinity(True)
def gamma(x): # type: (float) -> float t = False y = 0.0 try: y = _b.gamma(x) # call c-api except: t = True from amath.testing.types import isinf, isnan, intQ if x >= 170 or t: # to not overflow float or if in _basic failure try: from amath.constants import e, pi s = GammaDk[0] for i in range(1, GammaN + 1): s += GammaDk[i] / (x + i - 1.0) return s * 2 * sqrt(e / pi) * pow( (x - 0.5 + GammaR) / e, x - 0.5) except OverflowError: if intQ(x): # x must be an int from amath.stats.stats import product return product(lambda k: k, 1, x) // x elif isinf(x): if x > 0: from amath.DataTypes import Infinity return Infinity(True) else: from amath.Errors import Indeterminate raise Indeterminate() else: raise else: if isinf(y) or isnan(y): from amath.DataTypes import Infinity return Infinity(None) elif isinstance(x, int) or int(x) == x: return int(y) else: return y
def __cmp__(self, other): """ Compares with another value. Used by >, <, ==, and != :param other: :return: >>> Infinity(True) > 5 True >>> Infinity(True) < 5 False >>> Infinity(False) > Infinity(True) False >>> Infinity(None) == 0 Traceback (most recent call last): ... Failure: Cannot be compared >>> Infinity(True) == float("inf") True >>> float("inf") > Infinity(None) Traceback (most recent call last): ... Failure: Cannot be compared >>> """ from amath.testing.types import isinf if isinf(other): if isinstance(other, Infinity): if other.n is None: raise elif other.n < self.n: return 1 else: return -1 if other == float("inf"): if self.n: return 0 elif self.n is False: return -1 elif other == float("-inf"): if self.n: return 1 elif self.n is False: return 0 if self.n: return 1 elif self.n is False: return -1 else: raise Failure("Cannot be compared")
def __cmp__(self, other): """ Compares with another value. Used by >, <, ==, and != :param other: :return: >>> Infinity(True) > 5 True >>> Infinity(True) < 5 False >>> Infinity(False) > Infinity(True) False >>> Infinity(None) == 0 Traceback (most recent call last): ... Failure: Cannot be compared >>> Infinity(True) == float("inf") True >>> float("inf") > Infinity(None) Traceback (most recent call last): ... Failure: Cannot be compared >>> """ from amath.testing.types import isinf if isinf(other): if isinstance(other, Infinity): if other.n is None: raise elif other.n < self.n: return 1 else: return -1 if other == float("inf"): if self.n: return 0 elif self.n is False: return -1 elif other == float("-inf"): if self.n: return 1 elif self.n is False: return 0 if self.n: return 1 elif self.n is False: return -1 else: raise Failure("Cannot be compared")
def __pow__(self, power, modulo=None): from amath.testing.types import isinf if not isinf(power): if power == 0: if self.n is None: raise Indeterminate( "Indeterminate expression ComplexInfinity**0 encountered" ) elif self.n: raise Indeterminate( "Indeterminate expression inf**0 encountered") else: raise Indeterminate( "Indeterminate expression (-inf)**0 encountered") elif power < 0: return 0 else: return Infinity(self.n) else: if power > 0: return Infinity(None) elif power < 0: return 0 elif isinstance(power, Infinity): if self.n is None: raise Indeterminate( "Indeterminate expression ComplexInfinity**ComplexInfinity encountered" ) elif self.n: raise Indeterminate( "Indeterminate expression inf**ComplexInfinity encountered" ) else: raise Indeterminate( "Indeterminate expression (-inf)**ComplexInfinity encountered" )
def __mul__(self, other): from amath.testing.types import isinf if isinstance(other, Infinity): if self.n is None: return Infinity(None) elif other.n is None: return Infinity(None) elif self.n: if other.n: return Infinity(True) else: return Infinity(False) elif not self.n: if other.n: return Infinity(False) else: return Infinity(True) elif isinf(other): if self.n is None: return Infinity(None) elif self.n: if other > 0: return Infinity(True) else: return Infinity(False) else: if other > 0: return Infinity(False) else: return Infinity(True) elif isinstance(other, complex): if other.imag < 0: return self.__neg__() elif other.imag > 0: return Infinity(self.n) else: return Infinity(self.n) else: if self.n is None: if other == 0: raise Indeterminate( "Indeterminate expression 0 * ComplexInfinity encountered" ) else: return Infinity(None) elif self.n: if other == 0: raise Indeterminate( "Indeterminate expression 0 * inf encountered") elif other > 0: return Infinity(True) else: return Infinity(False) else: if other == 0: raise Indeterminate( "Indeterminate expression 0 * (-inf) encountered") elif other > 0: return Infinity(False) else: return Infinity(True)