Function 数组作为函数的输入
我定义了一个公式,希望将现有数组数据作为该函数的输入,并在数组中返回结果 代码Function 数组作为函数的输入,function,numpy,Function,Numpy,我定义了一个公式,希望将现有数组数据作为该函数的输入,并在数组中返回结果 代码 x1=np.array(x1)# <---- existing input data x_test=[0,5,10,15] <---- just some test data x_test=np.array(x_test) def P_0(x): return 295*(math.cos((0.9952*x+2.25952)*math.pi/180))**2-(295*(
x1=np.array(x1)# <---- existing input data
x_test=[0,5,10,15] <---- just some test data
x_test=np.array(x_test)
def P_0(x):
return 295*(math.cos((0.9952*x+2.25952)*math.pi/180))**2-(295*(math.cos((0.9952*x+1.74)*math.pi/180))**2)
results=P_0(x_test)
尝试更改:
def P_0(x):
return 295*(np.cos((0.9952*x+2.25952)*np.pi/180))**2-(295*(np.cos((0.9952*x+1.74)*np.pi/180))**2)
我刚刚将math.pi
替换为np.pi
,math.sin
替换为np.sin
和math.cos
替换为np.cos
如果您在执行numpy数组操作时使用numpy中的函数总是更好的@kevin yes大多数情况下都是这样。
math。
函数用于标量,尽管它们可能用于单个元素数组。
def P_0(x):
return 295*(np.cos((0.9952*x+2.25952)*np.pi/180))**2-(295*(np.cos((0.9952*x+1.74)*np.pi/180))**2)