如何在Python中编程此表达式:
min{cos(2xÏ), 1/2}
?
我尝试过:
x = np.array([1,2,3,4,5,3,2,5,7]) solution = np.min(np.cos(2*x*np.pi), 1/2)
但它不起作用,存在以下错误:
TypeError: 'float' object cannot be interpreted as an integer.
我试过你的代码 np.minimum 这样地:
np.minimum
import numpy as np x = np.array([1,2,3,4,5,3,2,5,7]) solution = np.minimum(np.cos(2*x*np.pi), 1/2) print(solution)
这就产生了这样的结果:
[ 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5]
最小值函数将检查数组的每个元素并返回一个数组。你可以看看 here