AppDividend
Latest Code Tutorials

# NumPy linalg multi_dot: Np linalg() Method in Python

Numpy linlag multi_dot() method is used to get dot product of two or more arrays in a single function call. That means we can get dot products of more than two arrays at a single time instead of calling them again and again. So, from its work, we can say that this function can give us output in a faster way.

## Numpy linalg multi_dot()

Compute a dot product of two or more arrays in the single function call, while automatically selecting the fastest evaluation order. The multi_dot chains numpy.dot and uses optimal parenthesization of the matrices. Depending on the shapes of the matrices, the multi_dot() function can speed up the multiplication a lot.

### Syntax

```numpy.inlag.multi_dot(arrays)
```

### Parameters

The multi_dot() function takes only one argument, which is the list of the arrays whose dot products we want to calculate using this function.

### Return Value

The linag multo_dot() function returns the dot products of the given arrays.

### Programming example

Write a program to show calculating dot products of multiple arrays without using multi_dot.

```# Program to show calculating dot products of
# multiple arrays without using multi_dot
import numpy as np
from numpy.linalg import multi_dot

# Preparing some arrays with random elements
a = np.array([[1, 2], [4, 5]])
b = np.array([[5, 6], [7, 9]])
c = np.array([[6, 1], [3, 4]])

# Now we will find dot product of these three arrays
ans1 = a.dot(b).dot(c)
ans2 = np.dot(np.dot(a, b), c)

print("Dot products of these 3 arrays is: \n")
print(ans1)
print(ans2)
```

#### Output

```Dot products of these 3 arrays is:

[[186 115]
[537 331]]
[[186 115]
[537 331]]```

#### Explanation

In this program, we have declared 3, 2×2 matrices, and we calculated the dot product of these matrices without using a multidot function. For this, we have shown two different methods by which we calculated the dot products of these 3 arrays. We can see that in both cases, the answer we get is the same.

#### Program to show calculating dot products of multiple arrays using multi_dot.

See the following code.

```# Program to show calculating dot products of
# multiple arrays with using multi_dot
import numpy as np
#from numpy.linalg import multi_dot

# Preparing some arrays with random elements
a = np.array([[1, 2], [4, 5]])
b = np.array([[5, 6], [7, 9]])
c = np.array([[6, 1], [3, 4]])

# Now we will find dot product of these three arrays
ans = np.linalg.multi_dot([a, b, c])

print("Dot products of these 3 arrays is: \n")
print(ans)```

#### Output

```Dot products of these 3 arrays is:

[[186 115]
[537 331]]```

#### Explanation

In this program, we have used those arrays which were used in the previous program, and this time we have used numpy.linalg.multidot function to calculate the dot products of these arrays.

For this, we have just passed one array as a parameter which contains the arrays in an array type value.

Finally, we got the same answer which got in the previous program.

That is it for Numpy linalg multi_dot() function.

Numpy linalg det()

Numpy matmul()

Numpy convolve()

Numpy correlate()

Numpy inner()

Numpy polyfit()

This site uses Akismet to reduce spam. Learn how your comment data is processed.