# [SciPy-user] vector subtraction error

Ryan Krauss ryanlists at gmail.com
Sat Jan 7 15:15:53 CST 2006

```I understand what you are saying, but as far as I can tell, a vector
with the shape of (n,) is neither a row nor a column.  It can't be
transposed from one to the other:
In [9]: shape(temp2)
Out[9]: (9,)

In [10]: temp=arange(1,10)

In [11]: shape(temp)
Out[11]: (9,)

In [12]: temp2=transpose(temp)

In [13]: shape(temp2)
Out[13]: (9,)

This is entirely different from a 2d array:
In [16]: temp3=atleast_2d(temp)

In [17]: shape(temp3)
Out[17]: (1, 9)

In [18]: temp4=transpose(temp3)

In [19]: shape(temp4)
Out[19]: (9, 1)

So, is a 1D array always a row vector?  And if so, shouldn't
subtracting a 1xn from and nx1 raise an error, since it makes no sense
from a linear algebra stand point?

Ryan

On 1/7/06, Paul Barrett <pebarrett at gmail.com> wrote:
> 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
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.net
> http://www.scipy.net/mailman/listinfo/scipy-user
>
>
>

```