[SciPy-user] addition of vectors of different lengths
jdc at uwo.ca
Wed Aug 3 23:46:58 CDT 2005
Travis Oliphant <oliphant at ee.byu.edu> writes:
>> As a general question, I wonder if it would be reasonable to allow
>> ufuncs to be applied to arrays of different sizes. When one of the
>> arrays is missing data, the ufunc could be given None for that
>> argument, and decide whether to raise an exception or to use a default
>> value. Is this something that was thought about but not done for
>> some reason?
> I have not been following this entire discussion, but this kind of
> behavior would markedly change the current rules and make ufuncs slower
> in general.
Yes, I agree that it wouldn't be good to change this by default.
> Now, I suppose instead of raising a shape-mismatch error an alternative
> algorithm could be called, that would fix the speed problem, but it
> would still mean that unintentional shape mismatch bugs would be hard to
> It's an interesting propostion though.
I wonder about a couple of options. One would be to provide a
call_ufunc_with_default(add, default=0.0, a, b)
This would be the same as add(a,b), except that the result
would have its size in the ith dimension equal to the maximum
of the sizes of a and b in the ith dimension, and when values
aren't present from a or b, the value 0.0 would be passed to add.
The hope would be that this could be implemented at a low enough
level that it's quite fast.
A second possibility would be to provide a function like reshape or
resize which allowed one to extend an array by zeros in some
dimensions. This seems like a useful thing to have in general,
but (I assume that) data would have to be copied around, so
it would be slower to do
add(extend_a_with_zeros(a, b.getshape()), b)
call_ufunc_with_defaults(add, default=0.0, a, b)
More information about the SciPy-user