# [Numpy-discussion] logic problem

josef.pktd@gmai... josef.pktd@gmai...
Mon Jan 18 13:17:07 CST 2010

```2010/1/18 Ernest Adrogué <eadrogue@gmx.net>:
> Hi,
>
> This is hard to explain. In this code:
>
> reduce(np.logical_or, [m1 & m2, m1 & m3, m2 & m3])
>
> where m1, m2 and m3 are boolean arrays, I'm trying to figure
> out an expression that works with an arbitrary number of
> arrays, not just 3. Any idea??

What's the shape of mi (dimension)? fixed or arbitrary number of dimension?
a loop is the most memory efficient
array broadcasting builds large arrays (and maybe has redundant
calculations), but might be a one-liner

or something like list comprehension

m = [m1, m2, ... mn]
reduce(np.logical_or, [mi & mj for (i, mi) in enumerate(m) for (j, mj)
in enumerate(m) if i<j  ])

>>> m = [np.arange(10)<5, np.arange(10)>3, np.arange(10)>8]
>>> m
[array([ True,  True,  True,  True,  True, False, False, False, False,
False], dtype=bool), array([False, False, False, False,  True,  True,
True,  True,  True,  True], dtype=bool), array([False, False, False,
False, False, False, False, False, False,  True], dtype=bool)]

>>> reduce(np.logical_or, [mi & mj for (i, mi) in enumerate(m) for (j, mj) in enumerate(m) if i<j  ])
array([False, False, False, False,  True, False, False, False, False,
True], dtype=bool)

Josef

>
> Bye.
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
```

More information about the NumPy-Discussion mailing list