[Numpy-discussion] Detect subclass of ndarray

Colin J. Williams cjw at sympatico.ca
Sun Mar 25 09:56:09 EDT 2007


Colin J. Williams wrote:
> Alan G Isaac wrote:
>> On Sat, 24 Mar 2007, Charles R Harris apparently wrote: 
>>> Yes, that is what I am thinking. Given that there are only the two 
>>> possibilities, row or column, choose the only one that is compatible with 
>>> the multiplying matrix. The result will not always be a column vector, for 
>>> instance, mat([[1]])*ones(3) will be a 1x3 row vector. 
>>
>>
>> Ack!  The simple rule `post multiply means its a column vector`
>> would be horrible enough: A*ones(n)*B becomes utterly obscure.
>> Now even that simple rule is to be violated??
> 
> It depends whether ones delivers an instance of the Matrix/vector class 
> or a simple array.
> 
> I assume that, in the above A and B represent matrices.
> 
> Colin W.

Postscript:  I hadn't read the later postings when I posted the above.

PyMatrix used the convention mentioned in an earlier posting.  Simply a 
vector is considered as a single row matrix or a single column matrix.

This same approach can largely be used with numpy's mat:

*** Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit 
(Intel)] on win32. ***
 >>> import numpy as _n
 >>> _n.ones(3)
array([ 1.,  1.,  1.])
 >>> a= _n.ones(3)
 >>> a.T
array([ 1.,  1.,  1.])
 >>> _n.mat(a)
matrix([[ 1.,  1.,  1.]])
 >>> _n.mat(a).T
matrix([[ 1.],
         [ 1.],
         [ 1.]])
 >>> b= _n.mat(a).T
 >>> a * b
matrix([[ 3.]])                   #  Something has gone wrong here - it 
looks as though there is normalization under the counter.
 >>>

In any event, the problem posed by Alan Isaac can be handled with this 
approach:

A * mat(ones(3)).t * B can produce the desired result.  I haven't tested it.

Colin W.
>> Down this path lies madness.
>> Please, just raise an exception.
>>
>> Cheers,
>> Alan Isaac




More information about the NumPy-Discussion mailing list