[SciPy-user] vector subtraction error
Robert Kern
robert.kern at gmail.com
Sat Jan 7 15:14:13 CST 2006
Ryan Krauss 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?
This is deliberate behavior for broadcasting. Travis' book (which I expect to be
retitled shortly) describes it in "2.4 Universal Functions for arrays". The
numarray and Numeric manuals should also describe the broadcasting rules. In
particular the rule being applied here is that when arrays have different
numbers of dimensions, the smaller dimensions (in this case (n,)) get prepended
with 1s until they get are the same number as the larger. So here, you are
essentially subtracting a (n,1) array from a (1,n) array.
You should also review your .dBmag() method to find out why it is returning
arrays of different dimensions when you want to treat them as the same number of
dimensions.
--
Robert Kern
robert.kern at gmail.com
"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
More information about the SciPy-user
mailing list