In the Numpy module, we have discussed many functions used to operate on the multidimensional array. Sometimes, we come to a situation where we need to find the polynomial of certain values. So, there is a function in the numpy module, i.e., the Numpy polyval() function. In this tutorial, we will discuss the concept of the **Numpy Polyval() function**, which is used to evaluate the polynomial at the given specific values.

Contents

## What is Numpy polyval() function?

**The Numpy polyval() function helps us to evaluate the polynomial at the given specific values. **It has two parameters in it, i.e., P and X. If we have polynomial P of length L. Then, it returns the value as:

**p[0]*x**(L-1) + p[1]*x**(L-2) + ..... + p[L-2]*x + p[L-1]**

## Syntax of Numpy polyval()

The syntax of the numpy polyval() is:

`numpy.polyval(p, x)`

## Parameters of Numpy polyval()

**P:**It is an array-like or poly1d input. In this, the polynomial coefficients are written as the decreasing order of powers. If the second parameter, i.e., x, is set to True then, array values are the polynomial equation’s roots.- x: It is an array-like or poly1d input. It is an integer number, an array of numbers, for evaluating ‘p.’

## Return Value of Numpy polyval()

The return value of the function is the evaluated value of the polynomial.

## Examples of Numpy polyval() function

Here, we will be discussing how we can write the polyval() from the numpy package of python.

### 1. Using poly1d

In this example, we will import the numpy and pandas package from python. **We will apply the poly1d() function to know the expression that is to be evaluated.** Hence, Let us look at the example for understanding the concept in detail.

```
#import numpy package
import numpy as np
#import pandas package
import pandas as pd
#applying poly1d function
p1 = np.poly1d([3, 6, 2])
p2 = np.poly1d([4, 9, 2, 1])
print ("P1 : \n", p1)
print ("\n p2 : \n", p2)
```

**Output:**

```
P1 :
2
3 x + 6 x + 2
p2 :
3 2
4 x + 9 x + 2 x + 1
```

**Explanation:**

- Firstly, we will be importing the numpy library with an alias name as np.
- Then, we will be importing the pandas library with an alias name as pd.
- After that, we will apply the poly1d() function and store the output in any variable, helping us know the expression to be evaluated.
- At last, we will print the output.
- Hence, you can see the output as the expression gets printed.

### 2. Putting the values in the output of poly1d function

In this example, we will import the numpy and pandas package from python. We will apply the** poly1d() function to get to know the expression that is to be evaluated.** After that, we will put the integer value for evaluating the expression and seeing the output. Let us look at the example for understanding the concept in detail.

```
#import numpy package
import numpy as np
#import pandas package
import pandas as pd
#applying poly1d function
p1 = np.poly1d([3, 6, 2])
p2 = np.poly1d([4, 9, 2, 1])
print ("P1 : \n", p1(2))
print ("\n p2 : \n", p2(2))
```

**NOTE : The expression will be evaluated as
3*2*2 + 6*2 + 2 = 26
4*2*2*2 + 9*2*2 + 2*2 + 1 = 73**

**Output:**

```
P1 : 26
p2 : 73
```

**Explanation:**

- Firstly, we will be importing the numpy library with an alias name as np.
- Then, we will be importing the pandas library with an alias name as pd.
- After that, we will apply the poly1d() function and store the output in any variable, helping us know the expression to be evaluated.
- After that, while printing the values of the variable passed, we will pass the integer value inside them so that the expression gets evaluated.
- At last, we will print the output.
- Hence, you can see the output as the expression gets evaluated and final and gets printed.

### 3. Applying polyval() function

In this example, we will be importing the numpy and pandas package from python. Then, we will apply the numpy polyval() function with both the parameters inside it. With the help of this function, **we will directly able to evaluate the expression.** At last, we will print the output. Let us look at the example for understanding the concept in detail.

```
#import numpy package
import numpy as np
#import pandas package
import pandas as pd
a = np.polyval([3, 5], 2)
b = np.polyval([4, 9, 2, 1], 2)
print ("\n\nUsing polyval() function")
print ("p1 at x = 2 : ", a)
print ("p2 at x = 2 : ", b)
```

**Output:**

```
Using polyval() function
p1 at x = 2 : 11
p2 at x = 2 : 73
```

**Explanation:**

- Firstly, we will be importing the numpy library with an alias name as np.
- Then, we will be importing the pandas library with an alias name as pd.
- After that, we will apply the polyval() function with both the parameters as the array and the integer value passed inside it.
- So that it directly gives the result as the output.
- At last, we will print the output.
- Hence, you can see the output as the expression gets evaluated directly.

### 4. Using poly1d() inside numpy polyval() function

In this example, we will be importing the numpy and pandas package from python. We will be using the poly1d() function for finding the expression inside the polyval(), which will directly print the output after the evaluation of the expression. At last, we will print the output. Let us look at the example for understanding the concept in detail.

```
#import numpy package
import numpy as np
#import pandas package
import pandas as pd
a = np.polyval(np.poly1d([4, 2, 5,3]), np.poly1d(3))
print ("\nc : ", a)
```

**Output:**

`c : 144`

**Explanation:**

- Firstly, we will be importing the numpy library with an alias name as np.
- Then, we will be importing the pandas library with an alias name as pd.
- After that, we will apply the poly1d() function inside the polyval() function ad try to print the evaluated expression as a result.
- At last, we will print the output.
- Hence, you can see the output as a result.

## See Also

## Conclusion

In this tutorial, we have learned about the concept of the numpy polyval() function. We have discussed what the polyval() function is and why we use it. After that, We have also discussed the syntax and how we can use the function. We have explained all the ways with the help of examples explained in detail. You can use any of the functions according to your choice and your requirement in the program.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.