[Numpy-discussion] varargs for logical_or, etc

Robert Kern robert.kern@gmail....
Tue Jun 5 09:37:56 CDT 2012


On Tue, Jun 5, 2012 at 2:54 PM, Neal Becker <ndbecker2@gmail.com> wrote:
> I think it's unfortunate that functions like logical_or are limited to binary.
>
> As a workaround, I've been using this:
>
> def apply_binary (func, *args):
>    if len (args) == 1:
>        return args[0]
>    elif len (args) == 2:
>        return func (*args)
>    else:
>        return func (
>            apply_binary (func, *args[:len(args)/2]),
>            apply_binary (func, *args[(len(args))/2:]))
>
> Then for example:
>
> punc2 = np.logical_and (u % 5 == 4,
>                       apply_binary (np.logical_or, u/5 == 3, u/5 == 8, u/5 ==
> 13))


reduce(np.logical_and, args)

-- 
Robert Kern


More information about the NumPy-Discussion mailing list