[英]The truth value of an array with more than one element is ambiguous error? python
from numpy import *
from pylab import *
from math import *
def TentMap(a,x):
if x>= 0 and x<0.5:
return 2.*a*x
elif x>=0.5 and x<=1.:
return 2.*a*(1.-x)
# We set a = 0.98, a typical chaotic value
a = 0.98
N = 1.0
xaxis = arange(0.0,N,0.01)
Func = TentMap
subplot(211)
title(str(Func.func_name) + ' at a=%g and its second iterate' %a)
ylabel('X(n+1)') # set y-axis label
plot(xaxis,Func(a,xaxis), 'g', antialiased=True)
subplot(212)
ylabel('X(n+1)') # set y-axis label
xlabel('X(n)') # set x-axis label
plot(xaxis,Func(a,Func(a,xaxis)), 'bo', antialiased=True)
My TentMap
function isn't working properly.我的TentMap
功能无法正常工作。 I keep getting the error The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我不断收到错误The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I don't understand how I'm supposed to use those. The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我不明白我应该如何使用它们。 Basically, the TentMap
function takes a value X and returns a certain value depending on what X is.基本上, TentMap
函数接受一个值 X 并根据 X 是什么返回某个值。 So if 0<=x<0.5 then it returns 2 a x and if 0.5<=x<=1 then it returns 2 a (1-x).因此,如果 0<=x<0.5 则返回 2 a x,如果 0.5<=x<=1 则返回 2 a (1-x)。
If you compare a numpy array with a number, you'll get another array:如果您将一个 numpy 数组与一个数字进行比较,您将得到另一个数组:
>>> from numpy import arange
>>> xaxis = arange(0.0, 0.04, 0.01)
>>> xaxis
array([ 0. , 0.01, 0.02, 0.03])
>>> xaxis <= .02
array([ True, True, True, False], dtype=bool)
The problem starts when you want to and
this with something else, or use it in a boolean context:当您想要将它and
其他东西一起使用,或者在布尔上下文中使用它时,问题就开始了:
>>> xaxis <= .02 and True
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
>>> bool(xaxis <= .02)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
And that's what you're trying to do in the constructor of your TentMap
.这就是您在TentMap
的构造函数中尝试做的事情。 Are you sure you don't need to use a
where you're using x
?您确定不需要在使用x
a
地方使用 a 吗?
You can use np.vectorize
to get around this error which occurs when using and
with a scalar value and arrray.您可以使用np.vectorize
来解决在使用标量值and
arrray 时发生的错误。 The call looks like电话看起来像
np.vectorize(TentMap)(a,xaxis)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.