I want a strange dot product for matrix multiplication in numpy. For a line [1,2,3]
of matrix A
and a column [4,5,6]
for matrix B
, I wish to use the "product" min(1+4, 2+5, 3+6)
for obtaining the matrix product AB
.
In [498]: A = np.arange(12).reshape(4,3)
In [499]: B = np.arange(4,10).reshape(3,2)
In [500]: A
Out[500]:
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
In [501]: B
Out[501]:
array([[4, 5],
[6, 7],
[8, 9]])
Reference iterative solution:
In [504]: res = np.zeros((A.shape[0],B.shape[1]), A.dtype)
...: for i,row in enumerate(A):
...: for j,col in enumerate(B.T):
...: res[i,j] = np.min(row+col)
...:
In [505]: res
Out[505]:
array([[ 4, 5],
[ 7, 8],
[10, 11],
[13, 14]])
Faster version using broadcasting:
In [506]: np.min(A[:,:,None]+B[None,:,:], axis=1)
Out[506]:
array([[ 4, 5],
[ 7, 8],
[10, 11],
[13, 14]])
===
Demonstrate the equivalence to a matrix product:
In [507]: np.dot(A,B)
Out[507]:
array([[ 22, 25],
[ 76, 88],
[130, 151],
[184, 214]])
In [508]: np.sum(A[:,:,None]*B[None,:,:], axis=1)
Out[508]:
array([[ 22, 25],
[ 76, 88],
[130, 151],
[184, 214]])
You can build your own custom multiplying function using loops and numpy like the following:
import numpy as np
x = np.array([[1, 2, 3],
[4 ,5, 6],
[7 ,8, 9]])
y = np.array([[4, 0, 6],
[5, 7, 3],
[6, 5, 9]])
def custom_multiply(x, y):
return np.array([min(row*column) for row in x for column in y.T]).reshape(x.shape[0], y.shape[1])
print(custom_multiply(x, y))
Output:
[[ 4 0 6]
[16 0 15]
[28 0 24]]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.