logical statements (in Python and Numeric)

Nathan Given ngiven at lanl.gov
Thu Jun 20 11:16:02 EDT 2002


I am graphing piecewise functions, and I have a method of getting the
proper y values in a vector that works in MATLAB... I was trying to get
it to work in Python, but it wasn't...

it has to do with a using an and statement...


here is my code...

x = arrayrange(-4.,2.+.001,.001, Float)
y = zeros(len(x),Float)

#------ Create Piecewise Function ------#
y = y + (x < -2)*(((x + 2.)**2) + 4)
y = y + (x >= -2. and x < 0.) * (x + 6.)
y = y + (x >=0) * (x ** 2. + 6.)

Now, the problem is with the second piece (where x is BETWEEN -2 and
0)...the y statement should only affect x values less then -2, the
second y statement should only affect x values between -2 and 0, but it
doesn't, it affects EVERYTHING less then zero and greater than negative
2.

How do I make this statement work properly?

thanks
--
Nathan

PS.  Here is the matlab code...

x = -4:.001:2;
y = zeros(1,length(x));

y = y + (x < -2) .* ((x+2) .^ 2 + 4);
y = y + (x >=-2 & x < 0) .* (x + 6);
y = y + (x >= 0) .* (x .^ 2 + 6);



More information about the Python-list mailing list