matrix multiplication

Terry Reedy tjreedy at udel.edu
Sun Feb 25 18:52:14 EST 2018


On 2/25/2018 12:45 PM, Seb wrote:
> Hello,
> 
> The following is an example of an Nx3 matrix (`uvw`) representing N
> vectors that need to be multiplied by a 3x3 matrix (generated by
> `randint_mat` function) and store the result in `uvw_rots`:
> 
> ---<--------------------cut here---------------start------------------->---
> import numpy as np
> 
> 
> def randint_mat(x):
>      return np.random.randint(x, size=(3, 3))
> 
> 
> np.random.seed(123)
> uvw = np.random.randn(1000, 3)
> maxint = np.random.randint(1, 10, size=uvw.shape[0])
> 
> uvw_rots = np.empty_like(uvw)
> for i, v in enumerate(maxint):
>      mati = randint_mat(v)
>      uvw_roti = np.dot(uvw[i], mati)
>      uvw_rots[i] = uvw_roti
> ---<--------------------cut here---------------end--------------------->---
> 
> Is there a better (faster) approach than looping through the rows of uvw
> as shown?

numpy has a matrix multiply function and now the '@' matrix multiply 
operator.


-- 
Terry Jan Reedy




More information about the Python-list mailing list