[SciPy-User] idiom for iterators, expr(T) if isscalar(T) else array([expr(t) for t in T])
josef.pktd@gmai...
josef.pktd@gmai...
Thu Oct 15 12:30:43 CDT 2009
On Thu, Oct 15, 2009 at 1:20 PM, denis <denis-bz-gg@t-online.de> wrote:
> On Oct 15, 6:13 pm, josef.p...@gmail.com wrote:
>>
>> solution: add a new dimension, turn t into a "column vector"
>> then each (one element) row of `t` is multiplied with each (one
>> element) column of `p`
>>
>> t[:,None] or t[:, np.newaxis] add a new axis and increase dimension of array
>
>> Josef
>
> Nice, that turns off broadcasting t * p0 etc. which is exactly what's
this turn on broadcasting, or better makes it possible for numpy to do
the broadcasting.
> needed here --
> until the evil day when some caller passes in column vecs for p0 etc.
> and broadcasting kicks in, wrong.
>
> Is there a general way of turning off broadcasting in particular
> functions ?
> In interpolation one often has func( t, x ) where t may be a scalar or
> range, x anything,
> and wants exactly funciter() -- ugly, slow, correct.
The usual way to handle this in a function is to adjust the dimension
and shape, e.g with atleast_1d or atleast_2d, and add or change
axis if necessary.
There are a lot of examples for this interface in the numpy, scipy code.
If p0,... and t are always required to be at most one dimensional, then
you could do asarray, ravel and then add the axis with [:, None] or similar.
If it's supposed to work with a t that has many rows and columns (2d),
then you would need a third axis and it will be a little bit more complicated.
Josef
>
> Bytheway .../site-packages/numpy/doc/broadcasting.py in the dist has
> pretty good examples
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
More information about the SciPy-User
mailing list