[Numpy-discussion] Detect subclass of ndarray
Charles R Harris
charlesr.harris at gmail.com
Sat Mar 24 18:29:51 EDT 2007
On 3/24/07, Travis Oliphant <oliphant at ee.byu.edu> wrote:
>
> Alan G Isaac wrote:
> > On Sat, 24 Mar 2007, Travis Oliphant apparently wrote:
> >
> >> My opinion is that a 1-d array in matrix-multiplication
> >> should always be interpreted as a row vector. Is this not
> >> what is currently done? If not, then it is a bug in my
> >> mind.
> >>
> >
> >
> >>>> N.__version__
> >>>>
> > '1.0'
> >
> >>>> I
> >>>>
> > matrix([[ 1., 0.],
> > [ 0., 1.]])
> >
> >>>> I*N.ones(2)
> >>>>
> > matrix([[ 1., 1.]])
> >
> > If N.ones(2) were treated as a row vector,
> > matrix multiplication is not possible.
> > So the question is what should happen.
> > I would like an exception raised.
> > The current behavior is lmost certainly not desirable,
> > although it has the virute of matching ``dot``.
> >
> >
> I think that's why it is the current behavior --- by default rather than
> by design. But, dot has different use cases.
>
> I'd be fine with an error raised on matrix multiplication (as long as
> dot is not changed). In other words, I'd like to see 1-d arrays always
> interpreted the same way (as row vectors) when used in matrix
> multiplication.
The relevant bit of code is
def __mul__(self, other):
if isinstance(other, N.ndarray) or N.isscalar(other) or \
not hasattr(other, '__rmul__'):
return N.dot(self, other)
else:
return NotImplemented
How about I just replace
N.dot(self, other)
by
N.dot(self, asmatrix(other))
or maybe something like
try :
rhs = asmatrix(other)
val = dot(self, rhs)
except :
val = NotImplemented
return val
It is not clear to me why the check for __rmul__ is included in the original
code, as I believe it causes problems when the rhs is a list, i.e.
mat(eye(2))*[1,1]
currently fails.
Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20070324/48f3ff93/attachment.html>
More information about the NumPy-Discussion
mailing list