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 in Python
To evaluate a Legendre series at specific points x in Python, use the polynomial.legendre.legval() method from NumPy. This function computes the value of a Legendre polynomial series at given points using the provided coefficients.
Syntax
numpy.polynomial.legendre.legval(x, c, tensor=True)
Parameters
x: Points at which to evaluate the Legendre series. Can be a scalar, list, tuple, or ndarray.
c: Array of coefficients ordered so that coefficients for terms of degree n are in c[n]. For multidimensional arrays, remaining indices enumerate multiple polynomials.
tensor: If True (default), the coefficient array shape is extended for broadcasting. If False, x is broadcast over columns of c.
Basic Example
Let's evaluate a simple Legendre series with coefficients [1, 2, 3] at point x = 1 ?
import numpy as np
from numpy.polynomial import legendre as L
# Create an array of coefficients
c = np.array([1, 2, 3])
print("Coefficients:", c)
print("Shape:", c.shape)
print("Dimensions:", c.ndim)
# Evaluate Legendre series at x = 1
result = L.legval(1, c)
print("Result at x=1:", result)
Coefficients: [1 2 3] Shape: (3,) Dimensions: 1 Result at x=1: 6.0
Multiple Points Evaluation
You can evaluate the same series at multiple points simultaneously ?
import numpy as np
from numpy.polynomial import legendre as L
c = np.array([1, 2, 3])
x_points = np.array([0, 0.5, 1, 1.5])
result = L.legval(x_points, c)
print("X points:", x_points)
print("Results:", result)
X points: [0. 0.5 1. 1.5] Results: [1. 2.75 6. 11.75]
How It Works
For coefficients [1, 2, 3], the Legendre series is: 1×P?(x) + 2×P?(x) + 3×P?(x), where P?(x)=1, P?(x)=x, and P?(x)=(3x²-1)/2.
Multidimensional Coefficients
You can work with multidimensional coefficient arrays to evaluate multiple series ?
import numpy as np
from numpy.polynomial import legendre as L
# Two different series with coefficients in columns
c = np.array([[1, 2], [2, 3], [3, 1]]) # 3x2 array
x = np.array([0, 1])
result = L.legval(x, c)
print("Coefficient array shape:", c.shape)
print("Results shape:", result.shape)
print("Results:\n", result)
Coefficient array shape: (3, 2) Results shape: (2, 2) Results: [[-0.5 0.5] [ 6. 6. ]]
Conclusion
The legval() function efficiently evaluates Legendre polynomial series at specified points. Use it for single or multiple point evaluations, and leverage multidimensional coefficients for evaluating multiple series simultaneously.
