When using optimization (e.g. brentq
), the input is always an array of items. However, sometimes it is necessary to use a comparator function like >=
in object function. Then, python is not able to evaluate those values.
For example:
def f(x):
if x > 0:
return x
if x <= 0:
return -x
optimize.brentq(f,-1,1)
Then we will have the error: The truth value of an array with more than one element is ambiguous
.
In general, how to avoid this error?