Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Evaluate a Legendre series at points x and the shape of the coefficient array extended for each dimension of x in Python
To evaluate a Legendre series at points x, use the polynomial.legendre.legval() method in NumPy. This function allows you to evaluate Legendre polynomials with specified coefficients at given points, with control over how multidimensional coefficient arrays are handled.
Syntax
numpy.polynomial.legendre.legval(x, c, tensor=True)
Parameters
The function accepts three parameters:
- x: Points at which to evaluate the series. Can be a scalar, list, or array
-
c: Array of coefficients where
c[n]contains coefficients for terms of degree n - tensor: Boolean controlling shape behavior for multidimensional arrays (default: True)
Understanding the Tensor Parameter
When tensor=True, the coefficient array shape is extended for each dimension of x, allowing evaluation of every column of coefficients for every element of x. When tensor=False, x is broadcast over the columns of c.
Example
import numpy as np
from numpy.polynomial import legendre as L
# Create a multidimensional array of coefficients
c = np.arange(4).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)
# Evaluate Legendre series at points x with tensor=True
print("\nResult (tensor=True)...\n", L.legval([1,2], c, tensor=True))
# Compare with tensor=False
print("\nResult (tensor=False)...\n", L.legval([1,2], c, tensor=False))
Our Array... [[0 1] [2 3]] Dimensions of our Array... 2 Datatype of our Array object... int64 Shape of our Array object... (2, 2) Result (tensor=True)... [[2. 4.] [4. 7.]] Result (tensor=False)... [2. 7.]
How It Works
The Legendre series evaluation uses the formula where each coefficient c[n] multiplies the nth Legendre polynomial. For the coefficient array [[0,1], [2,3]]:
- Column 1:
0 + 2*L?(x)where L?(x) = x - Column 2:
1 + 3*L?(x)where L?(x) = x
At x=1: Column 1 gives 0+2(1)=2, Column 2 gives 1+3(1)=4
At x=2: Column 1 gives 0+2(2)=4, Column 2 gives 1+3(2)=7
Conclusion
Use polynomial.legendre.legval() to evaluate Legendre series efficiently. The tensor parameter controls how multidimensional coefficient arrays are handled, with tensor=True providing full evaluation across all dimensions.
