Evaluate a Polynomial at Points x Broadcast Over the Columns of the Coefficient in Python using NumPy
Last Updated :
21 Apr, 2022
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.
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.]
Similar Reads
Evaluate a Hermite series at points x broadcast over the columns of the coefficient in Python In this article, we will be looking toward the approach to evaluating a Hermite series at points x broadcast over the columns of the coefficient in Python and NumPy. Example: Array: [1,2,3,4,5],[6,7,8,9,10]] Result: [ 73. 100. 131. 166. 205.] Explanation: Hermite series at points x broadcast over th
3 min read
Evaluate a Hermite_e series at points x broadcast over the columns of the coefficient in Python In this article, we will cover how to evaluate a Hermite_e series at points x broadcast over the columns of the coefficient in Python using NumPy. ExampleInput: [1 2 3 4 5] Output: [ -3. 1077.] Explanation: Hermite_e series at input points.hermite_e.hermeval method To evaluate a Hermite_e series at
2 min read
Evaluate a 3-D polynomial at points (x, y, z) with 4D array of coefficient using NumPy in Python In this article, we will look at how to evaluate a 3-dimensional polynomial at points (x, y, z) with a 4D array of coefficients using NumPy in Python. polynomial.polyval3d method We are using polynomial.polyval3d() function present in the NumPy module of python for the evaluation purpose of a 3-dim
2 min read
Evaluate a 2-D Hermite series at points (x,y) with 3D array of coefficient using NumPy in Python In this article, we'll look at how to evaluate a 2-dimensional Hermite series using NumPy in Python at an array of points x, and y with the 3-dimensional array. np.hermval2d method We are using the hermite.hermval2d() function from the Numpy module to evaluate a 2D Hermite series at locations (x, y)
2 min read
Evaluate a 2D Laguerre series at points (x,y) with 1D array of coefficient using NumPy in Python In this article, we will Evaluate a 2D Laguerre series at points (x,y) with a 1D array of coefficient Laguerre.lagval2d method In Python, laguerre.lagval2d() is used to evaluate a 2D Laguerre series at points (x,y). where coefficient_array is the input NumPy 1D array with coefficients and points re
2 min read
Evaluate a 2-D Hermite_e series at points (x,y) with 3D array of coefficient using NumPy in Python In this article, we will cover how to evaluate a 2-D Hermite_e series at points (x,y) with a 3D array of coefficients using NumPy in Python. np.polynomial.hermite_e.hermeval2d method The np.polynomial.hermite_e.hermeval2d from the NumPy library is used to Evaluate a 2-D Hermite_e series at points(x,
3 min read