[Numpy-discussion] Iterative Matrix Multiplication

Chris Colbert sccolbert@gmail....
Mon Mar 1 19:15:50 CST 2010


This is how I always do it:

In [1]: import numpy as np

In [3]: tmat = np.array([[0., 1., 0., 5.],[0., 0., 1., 3.],[1., 0., 0.,
2.]])

In [4]: tmat
Out[4]:
array([[ 0.,  1.,  0.,  5.],
           [ 0.,  0.,  1.,  3.],
           [ 1.,  0.,  0.,  2.]])

In [5]: points = np.random.random((5, 3))

In [7]: hpoints = np.column_stack((points, np.ones(len(points))))

In [9]: hpoints
Out[9]:
array([[ 0.17437059,  0.38693627,  0.201047  ,  1.        ],
       [ 0.99712373,  0.16958721,  0.03050696,  1.        ],
       [ 0.30653326,  0.62037744,  0.35785282,  1.        ],
       [ 0.78936771,  0.93692711,  0.58138493,  1.        ],
       [ 0.29914065,  0.08808239,  0.72032172,  1.        ]])

In [10]: np.dot(tmat, hpoints.T).T
Out[10]:
array([[ 5.38693627,  3.201047  ,  2.17437059],
       [ 5.16958721,  3.03050696,  2.99712373],
       [ 5.62037744,  3.35785282,  2.30653326],
       [ 5.93692711,  3.58138493,  2.78936771],
       [ 5.08808239,  3.72032172,  2.29914065]])


On Mon, Mar 1, 2010 at 6:12 AM, Friedrich Romstedt <
friedrichromstedt@gmail.com> wrote:

> 2010/3/1 Charles R Harris <charlesr.harris@gmail.com>:
> > On Sun, Feb 28, 2010 at 7:58 PM, Ian Mallett <geometrian@gmail.com>
> wrote:
> >> Excellent--and a 3D rotation matrix is 3x3--so the list can remain n*3.
> >> Now the question is how to apply a rotation matrix to the array of vec3?
> >
> > It looks like you want something like
> >
> > res = dot(vec, rot) + tran
> >
> > You can avoid an extra copy being made by separating the parts
> >
> > res = dot(vec, rot)
> > res += tran
> >
> > where I've used arrays, not matrices. Note that the rotation matrix
> > multiplies every vector in the array.
>
> When you want to rotate a ndarray "list" of vectors:
>
> >>> a.shape
> (N, 3)
>
> >>> a
> [[1., 2., 3. ]
>  [4., 5., 6. ]]
>
> by some rotation matrix:
>
> >>> rotation_matrix.shape
> (3, 3)
>
> where each row of the rotation_matrix represents one vector of the
> rotation target basis, expressed in the basis of the original system,
>
> you can do this by writing:
>
> >>> numpy.dot(a, rotations_matrix)  ,
>
> as Chuck pointed out.
>
> This gives you the rotated vectors in an ndarray "list" again:
>
> >>> numpy.dot(a, rotation_matrix).shape
> (N, 3)
>
> This is just somewhat more in detail what Chuck already stated
> > Note that the rotation matrix
> > multiplies every vector in the array.
>
> my 2 cents,
> Friedrich
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20100301/e89bc0e8/attachment.html 


More information about the NumPy-Discussion mailing list