The code is as follows:
import numpy as np
def f(x):
assert type(x) is np.ndarray
return np.fromfunction(lambda i: np.sum(x[:i + 1]), (x.size,))
print(f(np.array((1, 2, 3))))
IndexError: failed to coerce slice entry of type numpy.ndarray to integer
I would expect the outcome to be [1, 1 + 2, 1 + 2 + 3] = [1, 3, 6]. If I change the return line of f to be:
return np.fromfunction(lambda i: np.sum(x[:int(i) + 1]), (x.size,))
I get:
TypeError: only length-1 arrays can be converted to Python scalars
I am simply mapping i to a constant and i should take the values 0, ..., x.size - 1 so I don't see whats wrong here.