在 Python 中找到两个值中的最小值

Find minimum of two values in Python

如何在 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 试过你的代码,像这样:

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]

minimum 函数将检查数组的每个元素和 returns 数组。你可以看看here