Tuesday, November 19, 2024
Google search engine
HomeLanguagesEvaluate a Polynomial at Points x Broadcast Over the Columns of the...

Evaluate a Polynomial at Points x Broadcast Over the Columns of the Coefficient in Python using NumPy

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.]

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?