[Edu-sig] Truth values and comparisons
ajsiegel at optonline.net
Mon Oct 30 14:28:58 CET 2006
Looking for a little education on edu-sig.
On one hand I am feeling like a big boy, having announced today on the
vpython list that I think I have adequately accomplished the necessary
fixes to the vpython C++ code to accomplish compatibility with the newly
released numpy 1.0, and in a way that I think in the end (it was a
round-about process for me) is straightforward and unlikely to
introduce bugs into the existing stable codebase.
OTOH, I am learning there is some very basic Python I do not understand.
Now having a vpython that runs against numpy, I am learning about some
of the changes in the numpy Python API versus that of Numeric and
numarray. One basic incompatibility is in that the older libraries did
something to give a result in comparing 2 arrays that numpy backs away
from - complaining about ambiguity.
For example, in the vpython faces_heightfield.py demo there is a line:
>>>normal[i] = vertex_map[tp] and norm( vertex_map[ tp ] )
which ran fine under Numeric and numarray, and now fails with a message:
The truth value of an array with more than one element is ambiguous.
Use a.any() or a.all()
For the purposes of the exercise assume that both "vertex_map[tp]" and
"norm( vertex_map[ tp ]"
each return one dimensional, 3 element numpy arrays. Not exactly true,
but I think the result would be the same if it were.
In the original code, what is the "and" doing, and in what way might the
"any" or "all" built-ins be used to get the result intended by it?
More information about the Edu-sig