numpy.power() breaks down

Viewed 89

I am plotting a few exponential functions with different bases against the factorial function.

When going up to x=15 everything looks nice and we have a tight race:

enter image description here

However when going up to x=50, all the exponential functions seem to be upset that the factorial won the race and they all break down:

enter image description here

I doubt that it would be an overflow as I use dtype=np.longlong and the function values only reach about 10^60. Moreover, the factorial function is still doing fine.

Any idea of what could be going on?

Here is the code:

import numpy as np
import matplotlib.pyplot as plt


def compare_exponential_factorial(bases, x):

    xs = np.arange(0, x, dtype=np.longlong)

    fact = []
    for x in xs:
        fact.append(np.math.factorial(x))

    fig, ax = plt.subplots(1,1, figsize=(8,6))
    ax.semilogy(xs, fact, label="$f_1 = x!$", color="r")

    exps = []
    for i, b in enumerate(bases):
        exp = np.power(b, xs, dtype=np.longlong)
        exps.append(exp)

        ax.plot(xs, exp, label=f"$f_{i + 2} = {b}^x$", color="b", alpha=(i + 1) / len(bases))

    ax.set_xlabel("x")
    ax.set_title("Epic race between Exponentials and Factorial functions", fontsize=14)
    ax.legend(loc='best')
    plt.show()

if __name__ == "__main__":
    compare_exponential_factorial(bases=np.array([2, 3, 4, 5, 8, 10, 15], dtype=np.longlong), x=50)


0 Answers
Related