Numarray question

Discussion in 'Python' started by Matt Feinstein, May 13, 2005.

  1. If I try

    >>> 2 < array([1,2,3])


    I get:

    array([0, 0, 1], type=Bool)

    which is pretty slick, However if I set

    >>> q = 2 < array([1,2,3])
    >>> q and q


    I get a runtime error: "An array doesn't make sense as a truth value."

    So.. why not? It seems to me that if I could vectorize logical
    expressions (a la Matlab), it would be a lot easier to write functions
    that will take either scalar or vector input values. Am I missing
    something?

    Matt Feinstein

    --
    There is no virtue in believing something that can be proved to be true.
    Matt Feinstein, May 13, 2005
    #1
    1. Advertising

  2. Matt Feinstein

    Robert Kern Guest

    Matt Feinstein wrote:
    > If I try
    >
    >
    >>>>2 < array([1,2,3])

    >
    >
    > I get:
    >
    > array([0, 0, 1], type=Bool)
    >
    > which is pretty slick, However if I set
    >
    >
    >>>>q = 2 < array([1,2,3])
    >>>>q and q

    >
    >
    > I get a runtime error: "An array doesn't make sense as a truth value."
    >
    > So.. why not? It seems to me that if I could vectorize logical
    > expressions (a la Matlab), it would be a lot easier to write functions
    > that will take either scalar or vector input values. Am I missing
    > something?


    logical_and(), logical_or(), etc. These will handle arrays of truth
    values rather than treating the array as a single truth value like "and"
    and "or" will do. The reason why the latter is left undefined is because
    there isn't much solid agreement about what the semantics should be. In
    the face of ambiguity, numarray refuses the temptation to guess and
    forces the user to explicitly request certain behavior via alltrue() or
    sometrue() or whatever strikes their fancy.

    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
    Robert Kern, May 13, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SunX

    numarray question

    SunX, Jul 18, 2004, in forum: Python
    Replies:
    3
    Views:
    287
    Colin J. Williams
    Jul 20, 2004
  2. grv

    numarray speed question

    grv, Aug 4, 2004, in forum: Python
    Replies:
    11
    Views:
    712
    grv575
    Aug 31, 2004
  3. ChinStrap

    Numarray newbie question

    ChinStrap, Mar 29, 2005, in forum: Python
    Replies:
    5
    Views:
    292
    Colin J. Williams
    Mar 31, 2005
  4. Matt Feinstein

    Matplotlib question-- Numeric or numarray?

    Matt Feinstein, Apr 8, 2005, in forum: Python
    Replies:
    1
    Views:
    397
    John Hunter
    Apr 8, 2005
  5. a numarray question

    , Feb 15, 2006, in forum: Python
    Replies:
    3
    Views:
    281
Loading...

Share This Page