[Numpy-discussion] Detect subclass of ndarray
Charles R Harris
Sat Mar 24 12:19:21 CDT 2007
On 3/24/07, Alan G Isaac <firstname.lastname@example.org> wrote:
> >> On Fri, 23 Mar 2007, Charles R Harris apparently wrote:
> >>> the following gives the wrong result:
> >>> In : I = matrix(eye(2))
> >>> In : I*ones(2)
> >>> Out: matrix([[ 1., 1.]])
> >>> where the output should be a column vector.
> > On 3/24/07, Alan G Isaac <email@example.com> wrote:
> >> Why should this output a column?
> >> I would prefer an exception.
> >> Add the axis if you want it:
> >> I*ones(2)[:,None]
> >> works fine.
> On Sat, 24 Mar 2007, Charles R Harris apparently wrote:
> > Because it is mathematically correct. You can't multiply
> > a vector by a 2x2 matrix and get a 1x2 matrix as the
> > result.
> I do not understand:
> what do you mean mathematically correct?
> What is the space on which this operator is being defined?
> In my opinion, it is mathematically meaningless.
> Forcing meaning into it is a bad idea.
> Just because there is a sloppy convention to call n×1
> matrices and 1×n matrices "vectors" does not mean they are
> not 2d. They are. That is *why* matrix multiplication is
> defined for them.
> As I suggested, the right thing to do when someone attemps
> matrix multiplication with an object that is not 2d is to
> raise an exception. The wrong thing to do is to pretend
> than an object without an orientation has one: that is
> inherently not explicit and will surely bite someone sooner
> or later, probably sooner.
I think it is reasonable to raise an exception in this case, but that is not
how numpy currently works, so it is a larger policy decision that I can't
make on my own. For the case under consideration it is possible to define
1-D to 2-D rules that are consistent and work, so I am thinking of adding
those to the __mul__ operator of the matrix class. However, as you say, such
rules are more likely to let some errors slip by. In essence, it is rather
like type conversion in mixed expressions: convenient, but open to error.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Numpy-discussion