- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# Evaluate a polynomial and every column of coefficients in r is evaluated for every element of x in Python

To evaluate a polynomial specified by its roots at points x, use the polynomial.polyvalfromroots() method in Python Numpy. The 1st parameter is x. If x is a list or tuple, it is converted to an ndarray, otherwise it is left unchanged and treated as a scalar. In either case, x or its elements must support addition and multiplication with themselves and with the elements of r.

The 2nd parameter, r is an array of roots. If r is multidimensional the first index is the root index, while the remaining indices enumerate multiple polynomials. For instance, in the two dimensional case the roots of each polynomial may be thought of as stored in the columns of r.

The 3rd parameter is tensor. If True, the shape of the roots array is extended with ones on the right, one for each dimension of x. Scalars have dimension 0 for this action. The result is that every column of coefficients in r is evaluated for every element of x. If False, x is broadcast over the columns of r for the evaluation. This keyword is useful when r is multidimensional. The default value is True.

## Steps

At first, import the required libraries −

from numpy.polynomial.polynomial import polyvalfromroots import numpy as np

Create an array of multidimensional coefficients −

c = np.arange(-2, 2).reshape(2,2)

Display the array −

print("Our Array...\n",c)

Check the Dimensions −

print("\nDimensions of our Array...\n",c.ndim)

Get the Datatype −

print("\nDatatype of our Array object...\n",c.dtype)

Get the Shape −

print("\nShape of our Array object...\n",c.shape)

To evaluate a polynomial specified by its roots at points x, use the polynomial.polyvalfromroots() method in Python Numpy −

print("\nResult...\n",polyvalfromroots([-2, 1], c, tensor=True))

## Example

from numpy.polynomial.polynomial import polyvalfromroots import numpy as np # Create an array of multidimensional coefficients c = np.arange(-2, 2).reshape(2,2) # Display the array print("Our Array...\n",c) # Check the Dimensions print("\nDimensions of our Array...\n",c.ndim) # Get the Datatype print("\nDatatype of our Array object...\n",c.dtype) # Get the Shape print("\nShape of our Array object...\n",c.shape) # To evaluate a polynomial specified by its roots at points x, use the polynomial.polyvalfromroots() method in Python Numpy print("\nResult...\n",polyvalfromroots([-2, 1], c, tensor=True))

## Output

Our Array... [[-2 -1] [ 0 1]] Dimensions of our Array... 2 Datatype of our Array object... int64 Shape of our Array object... (2, 2) Result... [[-0. 3.] [ 3. 0.]]

- Related Articles
- How to minus every element of a vector with every element of another vector in R?
- Evaluate a polynomial at points x and x is broadcast over the columns of r for the evaluation in Python
- Evaluate a polynomial when coefficients are multi-dimensional in Python
- How to create a table with sub totals for every row and column in R?
- Evaluate a polynomial at points x in Python
- AND every element of a masked array by a given scalar value in Python
- AND a given scalar value with every element of a masked array in Python
- Python – Extract Kth element of every Nth tuple in List
- Evaluate a polynomial at points x and the shape of the coefficient array extended for each dimension of x in Python
- Find the standard deviation for every n number of observations in an R data frame column.
- Change the decimal point of every value in an R data frame column.
- Which of the following statement is incorrect?(a) foe every hormone there is a gene (b) for every protein there is a gene (c) for production of every enzyme there is a gene (d) for every type of fat there is a gene
- Evaluate a 2-D polynomial on the Cartesian product of x and y in Python
- Evaluate a polynomial at points x with multidimensioanl array of roots in Python
- XOR every element of a masked array by a given scalar value in Python