5.4.2. nvector.util.mdot¶
-
mdot
(a, b)[source]¶ Returns multiple matrix multiplications of two arrays i.e. dot(a, b)[i,j,k] = sum(a[i,:,j] * b[:,j,k])
- Parameters
- aarray_like
First argument.
- barray_like
Second argument.
See also
Notes
if a and b have the same shape this is the same as
np.concatenate([np.dot(a[…,i], b[…,i])[:, :, None] for i in range(n)], axis=2)
Examples
- 3 x 3 x 2 times 3 x 3 x 2 array -> 3 x 2 x 2 array
>>> import numpy as np >>> import nvector as nv >>> a = 1.0 * np.arange(18).reshape(3,3,2) >>> b = - a >>> t = np.concatenate([np.dot(a[...,i], b[...,i])[:, :, None] ... for i in range(2)], axis=2) >>> tm = nv.mdot(a, b) >>> tm.shape (3, 3, 2) >>> np.allclose(t, tm) True
- 3 x 3 x 2 times 3 x 1 array -> 3 x 1 x 2 array
>>> t1 = np.concatenate([np.dot(a[...,i], b[:,0,0][:,None])[:,:,None] ... for i in range(2)], axis=2)
>>> tm1 = nv.mdot(a, b[:,0,0].reshape(-1,1)) >>> tm1.shape (3, 1, 2) >>> np.allclose(t1, tm1) True
- 3 x 3 times 3 x 3 array -> 3 x 3 array
>>> tt0 = nv.mdot(a[...,0], b[...,0]) >>> tt0.shape (3, 3) >>> np.allclose(t[...,0], tt0) True
- 3 x 3 times 3 x 1 array -> 3 x 1 array
>>> tt0 = nv.mdot(a[...,0], b[:,:1,0]) >>> tt0.shape (3, 1) >>> np.allclose(t[:,:1,0], tt0) True
- 3 x 3 times 3 x 1 x 2 array -> 3 x 1 x 2 array
>>> tt0 = nv.mdot(a[..., 0], b[:, :2, 0][:, None]) >>> tt0.shape (3, 1, 2) >>> np.allclose(t[:,:2,0][:,None], tt0) True