Differences Between Arrays and Matrices in Numpy

Robert Kern robert.kern at gmail.com
Wed Jul 29 19:42:05 EDT 2009

On 2009-07-29 18:27, Colin J. Williams wrote:
> Robert Kern wrote:
>> On 2009-07-29 15:23, Nanime Puloski wrote:
>>> What are some differences between arrays and matrices using the Numpy
>>> library? When would I want to use arrays instead of matrices and vice
>>> versa?
>> You will want to ask numpy questions on the numpy mailing list:
>> http://www.scipy.org/Mailing_Lists
>> An overview of how the matrix subclass differs from ndarray, see the
>> documentation:
>> http://docs.scipy.org/doc/numpy/reference/arrays.classes.html#matrix-objects
>> Basically, I suggest that you just use regular arrays always. There is
>> a syntactical convenience to matrix objects, but it does cause
>> incompatibilities with the majority of code that is written for
>> regular arrays. The convenience is usually not worth the cost.
> Numpy's arrays can have any dimensionality, whereas
> matrices[http://en.wikipedia.org/wiki/Matrix_%28mathematics%29],
> typically have two. A single column can represent a vector or a single
> row can represent a transposed vector.
> Does the additional cost arise because the commonly used procedures are
> accessed through numpy's array?

Most functions are written to expect that its inputs behave like ndarrays; e.g. 
a*b is elementwise multiplication rather than matrix multiplication. When you 
use the matrix subclass, you are basically confining yourself to a smallish 
ghetto of functions that knows how to deal with matrix semantics. That's a huge 
cost compared to the relatively small syntactic cost of having to write dot(a,b) 
instead of (a*b).

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

More information about the Python-list mailing list