Vectors are quite fascinating entities forming the essence of navigating around different spaces & planes used in the field of mathematics. While there can be umpteen operations that can be carried out with the vectors, in this article, we will be exploring one such operation using an in-built function within the PythonÂ *numpyÂ *library â€“ theÂ *outer( )Â *function!

*Also check: Difference Between Pandas Dataframe and Numpy Arrays*

Letâ€™s start things by importing the *numpy *library before getting on with the deployment of the *outer( ) *function using the below code.

```
import numpy as np
```

We shall cover the depths of the *outer( ) *function through the following sections.

**Syntax of***outer( )*function**Calculating Outer Product for One-Dimensional Arrays****Calculating Outer Product for N-Dimensional Arrays****Calculating Outer Product for Letters****Calculating Outer Product with***Numpy*functions

## Syntax of outer( ) function

The underlying mechanism to determine the outer product of a given set of vectors is to multiply each element of one vector with each of the others and consider each corresponding result to be part of the output vector. This is all that happens within the *outer( ) *function whose syntax is as follows,

```
numpy.outer(a, b, out=None)
```

where,

n-dimensional array for the first input vector*a â€“*n-dimensional array for the second input vector*b â€“*an optional construct set to*out â€“**none*by default, but could be used to store the results in an array of appropriate length

## Calculating Outer Product for One-Dimensional Arrays

After importing the *numpy *library let us find the outer product of vectors using a couple of one-dimensional arrays as shown below.

```
ar1 = [[12, 36, 71, 99]]
ar2 = [[2, 8, 9, 56]]
np.outer(ar1, ar2)
```

The output array shall also contain 4 columns but the significant change would be in the count of rows which shall be 4 instead of 1. What happens here is the first element of the first array, â€˜12â€™ gets multiplied with each of the elements in the second array, which explains the results of the first row of the output.

The first element of the output array â€˜24â€™ is the product of 12×2, the second element â€˜96â€™ is the product of 12×8 and so on and so forth. This iterates until the last element of the array is multiplied by 12 & then the process repeats with the next element ’36’. Now the results are recorded in a new row!

## Calculating Outer Product for N-Dimensional Arrays

In this section, let us calculate the outer product of vectors using arrays that have 3 columns & 2 rows. Happen to guess what the result might be?

```
ar3 = [[24, 25, 26],
[5, 2, 9]]
ar4 = [[6, 8, 12],
[4, 3, 10]]
np.outer(ar3, ar4)
```

A 6×6 array!

## Calculating Outer Product for Letters

The outer product of vectors ainâ€™t limited only to the numbers, but one can also use letters by assigning their datatype as an â€˜objectâ€™ as shown below.

```
ar5 = np.array(['x', 'y', 'z'], dtype = object)
ar6 = [[2, 6, 1]]
np.outer(ar5, ar6)
```

The resulting output array shall contain the letters in â€˜ar5â€™ repeated the same number of times as specified in each element of â€˜ar6â€™.

## Calculating Outer Product with Numpy functions

One can also calculate the outer product of the vectors using a combination of *ones( ) â€“ *which creates an array with â€˜1â€™s as per the dimensions specified & *linspace( ) â€“ *which creates a set of numbers by dividing the specified range into the specified number of parts.

Letâ€™s use *linspace( ) *for diving the range between 2 & 8 into four parts for demonstration in this section.

```
ar7 = np.ones(4)
ar8 = np.linspace(2, 8, 4)
np.outer(ar7, ar8)
```

## Conclusion

Now that we have reached the end of this article, hope it has elaborated on how to use the *outer( ) *function from the *numpy *library. Hereâ€™s another article that explains how to find the square root using *numpy *in Python. There are numerous other enjoyable and equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. Whilst you enjoy those, *hasta luego*!