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 list of points x in Python
To evaluate a Legendre series at specific points, use the polynomial.legendre.legval() method in NumPy. This function allows you to compute Legendre polynomial values efficiently at single points or arrays of points.
Syntax
numpy.polynomial.legendre.legval(x, c, tensor=True)
Parameters
x: Points at which to evaluate the Legendre series. Can be a scalar, list, or array.
c: Array of coefficients where c[n] contains the coefficient for the degree-n term.
tensor: If True (default), evaluates every coefficient column for every element of x. If False, broadcasts x over coefficient columns.
Example
Let's evaluate a Legendre series with coefficients [1, 2, 3] at multiple points:
import numpy as np
from numpy.polynomial import legendre as L
# Create an array of coefficients
c = np.array([1, 2, 3])
# Display the coefficient array
print("Coefficients:", c)
print("Dimensions:", c.ndim)
print("Shape:", c.shape)
# Define evaluation points
x = [5, 10, 15]
# Evaluate the Legendre series at points x
result = L.legval(x, c)
print("\nEvaluating at points", x)
print("Result:", result)
Coefficients: [1 2 3] Dimensions: 1 Shape: (3,) Evaluating at points [5, 10, 15] Result: [ 122. 469.5 1042. ]
How It Works
The Legendre series is evaluated using the formula:
P(x) = c[0] × L?(x) + c[1] × L?(x) + c[2] × L?(x) + ...
Where L?(x) = 1, L?(x) = x, L?(x) = (3x² - 1)/2, etc.
Single Point Evaluation
import numpy as np
from numpy.polynomial import legendre as L
# Coefficients for polynomial: 1 + 2x + 3L?(x)
coefficients = np.array([1, 2, 3])
# Evaluate at a single point
point = 2.0
result = L.legval(point, coefficients)
print(f"L({point}) = {result}")
L(2.0) = 25.0
Conclusion
Use legval() to efficiently evaluate Legendre series at single or multiple points. The function handles the complex Legendre polynomial calculations automatically, making it ideal for numerical analysis and scientific computing applications.
