[SciPy-user] vector subtraction error

Paul Barrett pebarrett at gmail.com
Sat Jan 7 14:54:59 CST 2006


On 1/7/06, Ryan Krauss <ryanlists at gmail.com> wrote:
>
> I am still using old scipy, so maybe this is no longer an issue in the
> new NumPy, but I seem to do this to myself a fair ammount.  I think I
> have to 1-d vectors and I need to subtract them, but some how there
> shapes are (n,) and (n,1)  and when I subtract them I get something
> that is shape (n,n):
>
> (Pdb) shape(cb.dBmag())
> Out[3]: (4250,)
> (Pdb) shape(curb.dBmag())
> Out[3]: (4250, 1)
> (Pdb) temp=cb.dBmag()-curb.dBmag()
> (Pdb) shape(temp)
> Out[3]: (4250, 4250)
>
> Would there be a terrible performance cost to check for this when
> array subtraction is called?  Would this be different in the new
> NumPy?
>

You are seeing the array broadcasting behavior of Numeric/Numarray/Numpy,
which behaves like an outer product when operating on row and column
vectors. The output array that you are seeing is the result of this
behavior, since you are subtracting a column vector from a row vector. You
probably want to reshape the column vector into a row vector and then
subtract.

Note that this behaviour will never change. It is a feature of Numpy.

 -- Paul

--
Paul Barrett, PhD                     Johns Hopkins University
Assoc. Research Scientist     Dept of Physics and Astronomy
Phone: 410-516-5190            Baltimore, MD 21218
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.scipy.net/pipermail/scipy-user/attachments/20060107/d6d2affe/attachment.htm


More information about the SciPy-user mailing list