# [Numpy-discussion] Can this be done efficiently with array functions?

Chris Barker chrishbarker at home.net
Fri May 4 17:42:34 EDT 2001

```Janko and Paul,

Thanks for your suggestions. I had come up with something like Janko's
solution, but not as cleanly. It seems that Paul's suggestion is
essentially the same thing. Maybe I'll do some tests to see which is
faster.

By the way, I tried Janko's code, and got an error on "a == 0" (I'm
running Python 2.0). I looked at it again, and then looked at the
release notes for Python 2.1, and saw that rich comparisons are
working..Glory be! I'll be upgrading real soon, just for that!

thanks again,
-Chris

PS: anyone have a response for my Int32 question? Why isn't it a "long"
on Linux or windows on Intel?

Janko Hauser wrote:
> truely_max=100
>
> a=array([[10, 0, 5, 0,],
>          [ 0, 0, 0, 0,],
>          [ 0, 5,15, 0,],
>          [ 0, 0, 0, 0,],
>          [ 0, 3, 1,25,],
>          [ 0, 0, 0, 0,],
>          [ 4, 7, 2,12,],
>          [ 0, 0, 0, 0,]])
>
> am=minimum.reduce(where(a==0,truely_max,a),1)
> am=where(am==truely_max,0,am)
> print am
> [5 0 5 0 1 0 2 0]

> "Paul F. Dubois" wrote:

> but there doesn't seem to be
> a minimum.reduce() in MA.
>
> --> I'll add this to my list.

That will be great if you get a chance.

>     Meanwhile you could do something like this,
>     where x is your array:

>     Numeric.minimum.reduce(y.filled(100000), 1)
> ---

> --> There are two problems. There is a hit, even in the case of no mask.
>     It is hard to quantify because it depends on what operations you do.
>     Much of that could be eliminated if MA was in C but it is Python.
>     It would have driven me mad to write it in C, however.

WOW, I'm impressed, I had assumed that some C was involved. Nice job.

>     More seriously, there are a few semantic issues where it isn't clear
>     what you mean if a mask is present.

I'm sure these could be resolved. It would be nice if we could have in
mind to move in that direction. I'm not going ot be able to do any of
the coding, so that's the last you'll here from me about it.

-Chris

--
Christopher Barker,
Ph.D.
ChrisHBarker at home.net                 ---           ---           ---
http://members.home.net/barkerlohmann ---@@       -----@@       -----@@
------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------

```