[SciPy-user] Zeros

Anne Archibald peridot.faceted@gmail....
Sat Mar 15 18:19:24 CDT 2008


On 15/03/2008, Hoyt Koepke <hoytak@gmail.com> wrote:
> Don't know if this helps, but it might:
>
>  In matlab, all the typical operations assume the variables are
>  matrices and tries to do matrix multiplication on them.  Array-type
>  operations are done using .*,  .^, etc.  In numpy and scipy, however,
>  there are two types, arrays and matrices, which you can easily convert
>  back and forth from using  mat(X) and M.A or array(M).  Array
>  operations are, roughly speaking, element wise, and matrix operations
>  do what you'd expect.  It takes a little getting used to, but I find
>  that distinguishing the types has a lot of advantages.
>
>  So to get the behavior you're expecting, you probably need to declare
>  a column matrix, so try mat(zeros(N)).T  (the .T to transpose it from
>  a row to a column).

I would put it differently.

Numpy arrays do not always have two or more dimensions. A
one-dimensional array is neither a row nor a column vector, it's just
an array of numbers. Also, the operations on arrays are all
elementwise, like matlab's .* and .^ operators. If you want matrix
multiplication, use the function dot().

If you're doing so many matrix operations that all those dot()s become
cumbersome, you may want to consider using the matrix() wrapper around
arrays; without affecting the underlying data, this rebinds * to do
matrix multiplication, and enforces at-least-two-dimensionality. But
like all wrappers, this is imperfect, and you can expect to stumble
over a few inconveniences (functions that inadvertently convert your
matrices to arrays, for example), so I recommend getting used to
arrays first, and only switching to matrices if absolutely necessary.

Anne


More information about the SciPy-user mailing list