Пример #1
0
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
Пример #2
0
 def check(self, *args):
     from amath.testing.types import isReal, isComplex, isNatural, isWhole, intQ, isNumber, isValue
     if len(args) != len(self.vars):
         raise TypeError("check takes exactly {0} arguments ({1} given)".format(len(self.vars), len(args)))
     i = 0
     for var in self.vars:
         tp = self.vars[var]
         value = args[i]
         if tp == "Value":
             if not isValue(value):
                 raise TypeError("{0} must be a value".format(var))
         elif tp == "Number":
             if not isNumber(value):
                 raise TypeError("{0} must be a number".format(var))
         elif tp == "Imaginary":
             if not isComplex(value):
                 raise TypeError("{0} must be an Imaginary number".format(var))
         elif tp == "Real":
             if not isReal(value):
                 raise TypeError("{0} must be a Real number".format(var))
         elif tp == "Integer":
             if not intQ(value):
                 raise TypeError("{0} must be an Integer".format(var))
         elif tp == "Whole":
             if not isWhole(value):
                 raise TypeError("{0} must be a Whole number".format(var))
         elif tp == "Natural":
             if not isNatural(value):
                 raise TypeError("{0} must be a Natural number".format(var))
         else:
             raise Exception("Internal Error")
         i += 1
Пример #3
0
def digits(x):
    """
    Find the number of digits in x

    :param x: Number
    :return: Number of digits in x

    >>> digits(5)
    1
    >>> digits(234)
    3
    >>> digits(3.0)
    1
    >>> digits(3.4)
    2
    """
    from amath.testing.types import intQ, isFraction

    if isFraction(x):  # if x is a Fraction
        return x.digits()  # run the specified digits function
    if intQ(x):
        x = int(x)
    if int(x) == x:
        return len(str(abs(x)))  # for int
    else:
        return len(str(abs(x))) - 1  # for float
Пример #4
0
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
Пример #5
0
 def check(self, *args):
     from amath.testing.types import isReal, isComplex, isNatural, isWhole, intQ, isNumber, isValue
     if len(args) != len(self.vars):
         raise TypeError(
             "check takes exactly {0} arguments ({1} given)".format(
                 len(self.vars), len(args)))
     i = 0
     for var in self.vars:
         tp = self.vars[var]
         value = args[i]
         if tp == "Value":
             if not isValue(value):
                 raise TypeError("{0} must be a value".format(var))
         elif tp == "Number":
             if not isNumber(value):
                 raise TypeError("{0} must be a number".format(var))
         elif tp == "Imaginary":
             if not isComplex(value):
                 raise TypeError(
                     "{0} must be an Imaginary number".format(var))
         elif tp == "Real":
             if not isReal(value):
                 raise TypeError("{0} must be a Real number".format(var))
         elif tp == "Integer":
             if not intQ(value):
                 raise TypeError("{0} must be an Integer".format(var))
         elif tp == "Whole":
             if not isWhole(value):
                 raise TypeError("{0} must be a Whole number".format(var))
         elif tp == "Natural":
             if not isNatural(value):
                 raise TypeError("{0} must be a Natural number".format(var))
         else:
             raise Exception("Internal Error")
         i += 1
Пример #6
0
def digits(x):
    # from amath.DataTypes.Fraction import Fraction
    from amath.testing.types import intQ

    if type(x) is not int:
        if type(x) is not float:
            if type(x) is not mpq:
                try:
                    x = float(x)
                except ValueError:
                    raise TypeError(str(x) + " is not a number")  # x is not a number
    if isinstance(x, mpq):  # if x is a Fraction
        return x.digits()  # run the specified digits function
    if intQ(x):
        x = int(x)
    if int(x) == x:
        return len(str(abs(x)))  # for int
    else:
        return len(str(abs(x))) - 1  # for float
Пример #7
0
def digits(x):
    from amath.DataTypes.Fraction import Fraction
    from amath.testing.types import intQ

    if type(x) is not int:
        if type(x) is not float:
            if type(x) is not Fraction:
                try:
                    x = float(x)
                except ValueError:
                    raise TypeError(str(x) + " is not a number")  # x is not a number
    if isinstance(x, Fraction):  # if x is a Fraction
        return x.digits()  # run the specified digits function
    if intQ(x):
        x = int(x)
    if int(x) == x:
        return len(str(abs(x)))  # for int
    else:
        return len(str(abs(x))) - 1  # for float
Пример #8
0
    def __new__(cls, v, rep=None):
        from amath.testing.types import intQ, isReal
        self = object.__new__(cls)
        if rep is not None:
            if not intQ(v):
                raise TypeError("If repeating decimal is defined, value must have an integer value")
            if not isReal(rep):
                raise TypeError("Repeating decimal must be a float or a string")

        else:
            if not isReal(v):
                raise TypeError("value must be a float or a string")
            self.value = int(v)
            v = str(v)
            index = v.find('.')
            if index == -1:
                raise ValueError("Value must be a float or a string, not an int")
            self.rep = v[index + 1:]

        return self
Пример #9
0
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
Пример #10
0
    def __new__(cls, v, rep=None):
        from amath.testing.types import intQ, isReal
        self = object.__new__(cls)
        if rep is not None:
            if not intQ(v):
                raise TypeError(
                    "If repeating decimal is defined, value must have an integer value"
                )
            if not isReal(rep):
                raise TypeError(
                    "Repeating decimal must be a float or a string")

        else:
            if not isReal(v):
                raise TypeError("value must be a float or a string")
            self.value = int(v)
            v = str(v)
            index = v.find('.')
            if index == -1:
                raise ValueError(
                    "Value must be a float or a string, not an int")
            self.rep = v[index + 1:]

        return self