# high-speed calculation of vectors and matrices

Asked 5 months ago, Updated 5 months ago, 17 views

Please tell me how to do the following calculations at high speed.

``````import numpy as np
# It's actually about 100,000 long.
a = np.array([1, 2, 3], [4, 5, 6]])
# It's about 100,000 feet long.
b=np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 2, 3], [4, 5, 6], [7, 8, 9]] ])
res = [ ]
for i in range (a.shape):
for jin range (b.shape):
c=np.dot(a[i], b[i,j])
res.append(c)
``````

Please let me know if there is a way to calculate the product of an array (matrix) containing vectors and an array (triple tensor) containing matrices at high speed without using the for statement above.

python python3 numpy

2022-09-30 14:13

The python for statement is slow, so I think you want to process it with a properly accelerated (should) numpy.

I have no experience in calculating tensors, but `numpy` has `numpy.tensordot` and `numpy.einsum` which can calculate tensor product and tensor dot product, so if you use it well, it will be easy to write.

Looking at the code you wrote, it seems that you want to calculate the dot product, so I tried it.(The code for the question is `IndexError`, so there is no proof, but does the following match the intent of the question?)
See if you can speed it up.

``````import numpy as np
# It's actually about 100,000 long.
a = np.array([1, 2, 3], [4, 5, 6]])
# It's about 100,000 feet long.
b=np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [2, 3, 4], [5, 6, 7], [8, 9, 10]] ])

c=np.tensordot(a,b.T,1)
x,y,z = c.shape

tmp = c.reshape (1, x*y*z)
res=tmp.tolist()
``````

Also, I referred to the following.

Alternatively, using a library with parallel processing like `disk` or using a GPU using `cupy` might speed up.

2022-09-30 14:13

Using multiprocessing to parallelize the processing of the innermost for loop will increase the processing speed.

When parallelized, you don't know in which order the calculations will be returned, so

``````res.append(c)
``````

Then I don't know if it's in order.

I think it is necessary to devise a code that specifies where to put the results in the array res.

``````res[i*N+j]=np.dot(a[i],b[j])
``````

2022-09-30 14:13

## If you have any answers or tips

Popular Tags
python x 4628
android x 1593
java x 1493
javascript x 1425
c x 924
c++ x 877
ruby-on-rails x 696
php x 692
python3 x 683
html x 656