[SciPy-user] vector subtraction error

Ryan Krauss ryanlists at gmail.com
Sat Jan 7 16:21:34 EST 2006


In response to Robert's question, I will admit that I am somewhat
responsible for my own problem.  I do not believe it is the dBmag
function, but the inputs to it that are different.  The reason is most
likely that buried somewhere in my code I had a check for a problem
like this that got applied to experimental data and not to the model I
am trying to fit to it.  I have written a function called colwise as a
solution to these kinds of problems that makes sure that the largest
dimension of any matrix is the # of rows.  I just don't want to have
to sprinkle these colwise transformations all over (I believe it also
calls atleast_2d and that may not be appropriate all the time).

Ryan

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




More information about the SciPy-User mailing list