Putting together a larger matrix from smaller matrices
sturlamolden
sturlamolden at yahoo.no
Tue Aug 25 20:39:14 CEST 2009
On 25 Aug, 17:37, Matjaz Bezovnik <mbezov... at freenet.si> wrote:
> Scott, thank you very much for the snippet.
>
> It is exactly what I looked for; simple to read and obvious as to what
> it does even a month later to a non-pythonist!
Since you were talking about matrices, observe that numpy has a matrix
subclass of ndarray, which e.g. changes the meaning of the * operator
mean indicate matrix multiplication. Thus,
>>> import numpy as np
>>> np.eye(4)
array([[ 1., 0., 0., 0.],
[ 0., 1., 0., 0.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 1.]])
>>> np.matrix(np.eye(4))
matrix([[ 1., 0., 0., 0.],
[ 0., 1., 0., 0.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 1.]])
>>> a = np.ones((4,4))
>>> a*a
array([[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.]])
>>> b = np.matrix(a)
>>> b*b
matrix([[ 4., 4., 4., 4.],
[ 4., 4., 4., 4.],
[ 4., 4., 4., 4.],
[ 4., 4., 4., 4.]])
>>> b**2
matrix([[ 4., 4., 4., 4.],
[ 4., 4., 4., 4.],
[ 4., 4., 4., 4.],
[ 4., 4., 4., 4.]])
>>> b**4
matrix([[ 64., 64., 64., 64.],
[ 64., 64., 64., 64.],
[ 64., 64., 64., 64.],
[ 64., 64., 64., 64.]])
In Matlab, you use .* vs. * and .^ vs. ^ to obtain the same effect. In
NumPy we use different classes for arrays and matrices.
Sturla Molden
More information about the Python-list
mailing list