[SciPy-dev] matrix and ravel()

Travis Oliphant oliphant at ee.byu.edu
Tue Oct 11 21:49:58 CDT 2005

Robert Kern wrote:

>In [1]: A = array([[1,2],[3,4]])
>In [2]: B = mat(A)
>In [3]: ravel(A)
>Out[3]: array([1, 2, 3, 4])
>In [4]: ravel(B)
>Out[4]: matrix([[1, 2, 3, 4]])
>This appears to be a side effect of the new __array_finalize__ magic.
>I'm not sure that it's desirable. I'm also not sure that it's desirable
>to change it.
Not really a side-effect.  An intended feature.   Matrices are supposed 
to be matrices (i.e. 2-d arrays).  They cannot be 1-d arrays no matter 
how hard you try....  If you want to ravel a matrix and get a 1-d array 
out,  you are not using matrices right....  Use a 1-d array (i.e. the .A 

It does bring up an issue, though, and may require some re-thinking.

asarray(a)  will return a  if a is any sub-class of a (big)ndarray.


asarray(a) will return a matrix if a is a matrix.


a = asarray(a)
return a*a

will do matrix multiplication if a matrix is passed in,
but element-by-element multiplication if another array is passed in.

asndarray(a) will always return a base-class array (even 0-d arrays). 

One option, I suppose is to change the meaning of asarray to asndarray, 
(thus destroying the ability for matrices to perservere through many 
operations) --- and introduce another function like  asanyarray or 
something to mean any sub-class is O.K. too.

Comments welcome.


More information about the Scipy-dev mailing list