[SciPy-User] idiom for iterators, expr(T) if isscalar(T) else array([expr(t) for t in T])

denis denis-bz-gg at t-online.de
Mon Oct 12 10:42:21 EDT 2009


Some vectors are iterators: I want
    expr(T) if isscalar(T) \
    else array([expr(t) for t in T])
Is there an idiom for this ?

One can
    try:
        expr(T)
    except ValueError:
    # ValueError: shape mismatch: objects cannot be broadcast to a
single shape
        array([ expr(t) for t in T ])

but this is ugly, and probably wrong in some cases even with no
ValueError
I'm afraid the broadcasting rules are just too complex for me /
I'm too simple for the rules.

For a real example, see spline_2p2s in the thread "Forced derivative
interpolation".

Thanks, cheers
  -- denis



More information about the SciPy-User mailing list