定义一个段函数数组

时间:2018-06-21 10:54:37

标签: python numpy

我想定义一个数组f,它是y的分段函数,如何实现?

import numpy as np   

y0 = 1.0
y1 = 2.0
Ly = y1-y0
NY = 10

y =  np.array([y0+Ly*float(i)/(NY-1) for i in range(NY)])
print('y=',y)

# I wnat f = -2.0*y, when y1<= y < Ly/2.0
# I want f = 2.0*y, when Ly/2.0<= y < Ly
#print('f=',f)

1 个答案:

答案 0 :(得分:0)

您可以这样做

f1 = np.array([-2.0*y[i] for i in range(NY/2)])
f2 = np.array([2.0*y[NY/2+i] for i in range(NY/2)])

f = np.concatenate((f1,f2),axis = 0)

print('f=',f)