L
L
leo97972019-11-20 11:04:13
Python
leo9797, 2019-11-20 11:04:13

Why doesn't the formula work?

There is such a formula:
5dd4f299504e8743339528.png
Using this formula, I try to calculate my values ​​using the numpy library:
I got the following code:

N = np.arange(proc+1)
np.sum(np.power(R, N) / (math.factorial(N)))

I end up with the following error:
TypeError: only size-1 arrays can be converted to Python scalars

Does anyone know how to solve this error?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Dugin, 2019-11-20
@leo9797

To apply factorial to arrays, you need to vectorize the function:

>>> import numpy as np
>>> np.math.factorial(np.arange(6))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: only size-1 arrays can be converted to Python scalars

>>> factorial = np.vectorize(np.math.factorial)
>>> factorial(np.arange(6))
array([  1,   1,   2,   6,  24, 120])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question