In this article, we are going to see how to evaluate a polynomial at points x broadcast over the columns of the coefficient in Python using NumPy.
numpy.polyval() methodsĀ
The numpy numpy.polynomial.polynomial.polyval() method is used to evaluate a polynomial at points x broadcast over the columns of the coefficient in python. Calculate the value of a polynomial at x locations. If the parameter x is a tuple or a list, it is turned to an array; otherwise, it is regarded as a scalar. In either scenario, x or its elements must be capable of multiplication and addition among themselves as well as among the elements of c. p(x) will have the same shape as x if c is a 1-D array. If c is multidimensional, the shape of the result is determined by the tensor value. The shape will be c.shape[1:] + x.shape if tensor is true. The shape will be c.shape[1:] if tensor is false. Itās worth noting that scalars have shape (,). Because trailing zeros in coefficients are employed in the assessment, they should be avoided if efficiency is a priority.
Syntax: polynomial.polynomial.polyval(x, c, tensor=True)
parameters:
- x: array like object. If x is a list or tuple, it will be converted to an ndarray; otherwise, it will be regarded as a scalar. In any scenario, x or its elements must be able to add and multiply among themselves as well as with the elements of c.
- c: array like object. The coefficients for terms of degree n are stored in c[n], which is an array of coefficients sorted so that the coefficients for terms of degree n are contained in c[n]. The remaining indices enumerate numerous polynomials if c is multidimensional. The coefficients in the two-dimensional example can be thought of as being stored in the columns of c.
- tensor: boolean value. If True, the coefficient arrayās structure is stretched to the right with ones, one for each dimension of x. For this action, scalars have no dimension. As a result, each element of x is assessed for each column of coefficients in c. If False, for the evaluation, x is disseminated over the columns of c. When c is multidimensional, this keyword comes in handy. True is the default value.
Returns : values: The returning arrayās shape is specified above.
Example 1:
Create an array using numpy.array() method. the shape, dimension and datatype of the array is found using the .shape, .dimension and .dtype attributes. polynomial.polynomial.polyval is used to evaluate the polynomial at points x and broadcast it over the rows as tensor is set to true.
Python3
# import packages from numpy.polynomial.polynomial import polyval import numpy as np Ā Ā # Creating an array array = np.array([[ 3 , 4 ], [ 5 , 6 ]]) print (array) Ā Ā # shape of the array is print ( "Shape of the array is : " , array.shape) Ā Ā # dimension of the array print ( "The dimension of the array is : " , array.ndim) Ā Ā # Datatype of the array print ( "Datatype of our Array is : " , array.dtype) Ā Ā # evaluating a polynomial at point x print (polyval([ 2 , 2 ], array)) |
Output:
[[3 4] [5 6]] Shape of the array is : (2, 2) The dimension of the array is : 2 Datatype of our Array is : int64 [[13. 13.] [16. 16.]]
Example 2:
In this example polynomial.polynomial.polyval is used to evaluate the polynomial at points x and broadcast it over the columns as tensor is set to False.
Python3
# import packages from numpy.polynomial.polynomial import polyval import numpy as np Ā Ā # Creating an array array = np.array([[ 3 , 4 ], [ 5 , 6 ]]) print (array) Ā Ā # shape of the array is print ( "Shape of the array is : " , array.shape) Ā Ā # dimension of the array print ( "The dimension of the array is : " , array.ndim) Ā Ā # Datatype of the array print ( "Datatype of our Array is : " , array.dtype) Ā Ā # evaluating a polynomial at point x print (polyval([ 2 , 2 ], array, tensor = False )) |
Output:
[[3 4] [5 6]] Shape of the array is : (2, 2) The dimension of the array is : 2 Datatype of our Array is : int64 [13. 16.]