简体   繁体   中英

What does y=np.zeros(len(x)) do in this function definition?

Learning data visualisation with python and numpy, provided with an example in a Jupyter notebook

What does y=np.zeros(len(x)) do in this definition? Set y = 0?

x=np.linspace(-2*np.pi,2*np.pi,100)


# i-th order sine series decomposition
from math import factorial
def sineseries(x,order):
    y=np.zeros(len(x))
    for i in range(order):
        y=y+(-1.)**i/factorial(2*i+1)*x**(2*i+1)
    return y

for i in range(20):
    plt.figure()
    plt.plot(x,sineseries(x,i),'.')
    plt.plot(x,np.sin(x),'k')

It creates an array with len(x) zeros.

For example if x has 3 elements then the resulting array will be [0, 0, 0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM