[Numpy-discussion] Detect subclass of ndarray
Charles R Harris
charlesr.harris@gmail....
Sat Mar 24 12:19:21 CDT 2007
On 3/24/07, Alan G Isaac <aisaac@american.edu> wrote:
>
> >> On Fri, 23 Mar 2007, Charles R Harris apparently wrote:
> >>> the following gives the wrong result:
> >>> In [15]: I = matrix(eye(2))
> >>> In [16]: I*ones(2)
> >>> Out[16]: matrix([[ 1., 1.]])
> >>> where the output should be a column vector.
>
>
> > On 3/24/07, Alan G Isaac <aisaac@american.edu> 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.
Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20070324/3ef3ec5b/attachment.html
More information about the Numpy-discussion
mailing list