예제 #1
0
def cosh(args):
    x = 0
    e = get_e()

    if len(args) == 1 or (len(args) == 2 and args[1] == 0):
        x = args[0]
        return (power_function([e, x]) + power_function([e, -x])) / 2
    elif len(args) == 2 and args[1] == 1:
        x = to_radians(args[0])
        return (power_function([e, x]) + power_function([e, -x])) / 2
    else:
        raise Exception("Invalid input: cosh takes 1 input which is the "
                        "number in radians, or 2 inputs which is the number "
                        "and whether the number is in \" radians\" or \" "
                        "degrees\".")
예제 #2
0
def get_pi():
    global pi

    if pi == 0:
        series_sum = 0

        # We iterate for 2 iterations, as Chudnosvky converges very
        # quickly
        for i in range(2):
            series_sum += (pf.power_function([-1, i]) * af.factorial(6 * i)
                           * (13591409 + (545140134 * i))) \
                          / (af.factorial(3 * i)
                             * pf.power_function([math.factorial(i), 3])
                             * pf.power_function([640320, 3 * i + 1.5]))

        pi = 1 / (12 * series_sum)

    return pi
예제 #3
0
def standard_deviation(args):
    # helper function that returns a list storing each value - mean
    def helper(arg_list, length):
        # compute mean
        mean = total_count(arg_list) / length
        # a new list stores the values of each element - mean
        args_new = []
        for i in range(length):
            args_new.append(arg_list[i] - mean)

        for i in range(length):
            args_new[i] = power_function([args_new[i], 2])
        return args_new

    count = len(args)
    if count > 1:
        # Get the last element in the list
        flag = args[-1]
        # population stdev
        if flag == 0.0:
            if count == 2:
                return 0
            else:
                args = args[:-1]
                count -= 1
                args_sqr = helper(args, count)
                return power_function([total_count(args_sqr) / count, 0.5])
        # sample stdev
        if flag == 1.0:
            # Sample stdev requires at least 2 numbers
            if count == 2:
                raise TypeError('Sample Standard Deviation requires at least '
                                '2 numbers !')
            else:
                args = args[:-1]
                count -= 1
                args_sqr = helper(args, count)
                return power_function(
                    [total_count(args_sqr) / (count - 1), 0.5])
        else:
            raise TypeError('Please specify the type of standard deviation !')
    else:
        raise TypeError('Invalid Argument !')
예제 #4
0
    def helper(arg_list, length):
        # compute mean
        mean = total_count(arg_list) / length
        # a new list stores the values of each element - mean
        args_new = []
        for i in range(length):
            args_new.append(arg_list[i] - mean)

        for i in range(length):
            args_new[i] = power_function([args_new[i], 2])
        return args_new
예제 #5
0
def get_ln10():
    global ln10

    if ln10 == 0:
        # if the ln(10) isn't already calculated, then calculate it for
        # the first time and store it for the duration of runtime
        sum = 0
        for i in range(1000):
            sum += (1 / (2 * i + 1)) * pf.power_function(
                [(10 - 1) / (10 + 1), 2 * i + 1])

        ln10 = 2 * sum

    return ln10
예제 #6
0
def sine(args):
    if len(args) == 1 or (len(args) == 2 and args[1] == 0):
        num = args[0]
    elif len(args) == 2 and args[1] == 1:
        num = to_radians(args[0])
    else:
        raise Exception("Invalid input: sine takes 1 input which is the "
                        "number in degrees, or 2 inputs which is the number "
                        "and whether the number is in \" radians\" or \" "
                        "degrees\".")
    res = 0
    pi = get_pi()
    twopi = 2 * pi

    # program will crush if num too big, convert to interval 0 -> 2pi
    num = num % twopi

    # using for loop to create summation i=0 to i=79
    for i in range(80):
        res += (power_function([-1, i])) \
               * (power_function([num, (2 * i + 1)])) \
               / (factorial(2 * i + 1))
    return res
예제 #7
0
def natural_log(x):
    a = x
    b = 0

    # we will rewrite ln(x) where x = a * 10^b, with a < 1
    # we can then take the ln(x) = ln(a) + b * ln(10)
    while a > 1:
        a /= 10
        b += 1

    # Variable used to store the calculated sum
    s = 0

    # Number of iterations currently hardcoded at 100, but we can change
    # this to an accuracy based metric by calculating the accuracy of
    # e^s = x to a certain bound.
    for i in range(100):
        s += (1 / (2 * i + 1)) * power_function([(a - 1) / (a + 1), 2 * i + 1])

    return (2 * s) + b * get_ln10()