Let's do it by multiplying numpy without using the for statement

I don't want to use a for statement, but I don't know how to calculate with numpy

The calculation handled here is when you want to combine various combinations. Other than that, please refer to other articles.

\boldsymbol A = \left(a_0, \dots, a_I  \right) \\
\boldsymbol B = \left(b_0, \dots, b_J  \right) \\
\boldsymbol C = \left(c_0, \dots, c_K  \right) \\

From

a_i b_j c_k

If you want to calculate

A = np.arange(2)+1
B = np.arange(3)+1
C = np.arange(4)+1
print("A=", A)
print("B=", B)
print("C=", C)

D = A[:, np.newaxis, np.newaxis] * B[:, np.newaxis] * C
print("\nD=", D) 
print("\nD.shape=", D.shape) 
A= [1 2]
B= [1 2 3]
C= [1 2 3 4]

D= [[[ 1  2  3  4]
  [ 2  4  6  8]
  [ 3  6  9 12]]

 [[ 2  4  6  8]
  [ 4  8 12 16]
  [ 6 12 18 24]]]

D.shape= (2, 3, 4)

If you only have $ b_j c_k $, numpy.outer is enough.

print(np.outer(B,C))
print(B[:, np.newaxis] * C)
[[ 1  2  3  4]
 [ 2  4  6  8]
 [ 3  6  9 12]]
[[ 1  2  3  4]
 [ 2  4  6  8]
 [ 3  6  9 12]]

A little complicated example

\boldsymbol A = \left(a_0, \dots, a_I  \right) \\
\boldsymbol B = \left(b_0, \dots, b_J  \right) \\
\boldsymbol C = \left(c_0, \dots, c_K  \right) \\
\\
\boldsymbol X = \left(x_0, \dots, x_L  \right) \\
\boldsymbol Y = \left(y_0, \dots, y_J  \right) \\
\boldsymbol Z = \left(z_0, \dots, z_K  \right) \\
\\
a_i x_l \sum_{j=0}^J b_j  y_j \sum_{k=0}^K c_k z_k \\

\newcommand{\argmax}{\mathop{\rm arg~max}\limits}
\newcommand{\argmin}{\mathop{\rm arg~min}\limits}

\argmax_{j}{a_i x_l b_j  y_j \sum_{k=0}^K c_k z_k} \\

in the case of

ABC = A[:, np.newaxis, np.newaxis] * B[:, np.newaxis] * C
XYZ = X[:, np.newaxis, np.newaxis] * Y[:, np.newaxis] * Z
print("\nABC=", ABC)
print("\nXYZ=", XYZ)

O = ABC[:, np.newaxis] * XYZ
P = np.sum(O, axis=(2,3))
Q = np.argmax(np.sum(O, axis=3), axis=2)
print("O=", O)
print("P=", P)
print("Q=", Q)

I think I should do it.

Recommended Posts

Let's do it by multiplying numpy without using the for statement
Let's do it by dividing numpy without using the for statement
How to execute the sed command many times using the for statement
Do a search by image from the camera roll using Pythonista3
Let's display the map using Basemap
[Python] Multiplication table using for statement
For the first time in Numpy, I will update it from time to time
I thought it would be slow to use a for statement in NumPy, but that wasn't the case.