[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
Wed Oct 14 13:23:47 EDT 2009


On Oct 14, 2:02 pm, Yosef Meller <yosef... at post.tau.ac.il> wrote:

> v_expr = numpy.vectorize(expr)
> v_expr(T)
>
> Is that what you wanted?


Yosef,
  thanks, the right direction -- there must be a numpy primitive for
this.
But 2 problems with vectorize:
1) an optional arg => TypeError: __call__() got an unexpected keyword
argument 'h'
2) vectorize => broadcasting => ValueError
Here's a test case: ugly, but funciter() is at least correct :)


""" funciter, vectorize ?  14oct """
import numpy as np

def spline_2p2s( t, p0, p1, m0, m1, h=1 ):
    """ Hermite 2-point, 2-slope spline
        t: a scalar / range / iterator
        p0 p1 m0 m1: scalars or arrays
        Beware: t and p0 both vecs => broadcasting =>
            ValueError: shape mismatch: objects cannot be broadcast to
a single shape
        (need guidelines, axioms on broadcasting)
    """
    def f(t):
        t2 = t*t
        t3 = t2*t
        return (
              p0 * (2*t3 - 3*t2 + 1)
            + p1 * (-2*t3 + 3*t2)
            + m0 * h * (t3 - 2*t2 + t)
            + m1 * h * (t3 - t2) )
    return funciter( f, t )

def funciter( f, T ):
    return f(T) if np.isscalar(T) \
        else np.array([ f(t) for t in T ])

#...............................................................................
if __name__ == "__main__":
    t = np.arange( 0, 1.01, .1 )
    p0 = np.array(( 0, 0 ))
    p1 = np.array(( 1, 0 ))
    m0 = np.array(( 1, 1 ))
    m1 = np.array(( 1, -1 ))

    s = spline_2p2s( t, p0, p1, m0, m1 )
    print "spline_2p2s", s.T

    spline_2p2s_vec = np.vectorize( spline_2p2s )
    s = spline_2p2s_vec( t, p0, p1, m0, m1 )
    print "spline_2p2s_vec", s.T



More information about the SciPy-User mailing list