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 when coefficients are multi-dimensional in Python
To evaluate a Legendre series at points x with multi-dimensional coefficients, use the polynomial.legendre.legval() method in Python NumPy. This method handles arrays of coefficients where each column represents a separate polynomial.
Syntax
numpy.polynomial.legendre.legval(x, c, tensor=True)
Parameters
The method accepts three parameters ?
- x − Points at which to evaluate the series. Can be scalar, list, or array
- c − Array of coefficients. For multi-dimensional arrays, columns represent different polynomials
- tensor − If True (default), evaluates every column for every point in x
Example
Let's create a multi-dimensional coefficient array and evaluate the Legendre series ?
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=[1,2]
print("\nResult...\n",L.legval([1,2],c))
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... [[2. 4.] [4. 7.]]
How It Works
The coefficient array c has shape (2, 2), representing two polynomials:
- First polynomial: coefficients [0, 2] − represents 0 + 2x
- Second polynomial: coefficients [1, 3] − represents 1 + 3x
For Legendre polynomials, the evaluation uses the basis functions L?(x) = 1 and L?(x) = x.
Understanding Multi-dimensional Results
The output shape is (2, 2) because we evaluate 2 polynomials at 2 points ?
import numpy as np
from numpy.polynomial import legendre as L
# Same coefficient array
c = np.array([[0, 1], [2, 3]])
# Evaluate at single point
result_single = L.legval(1, c)
print("At x=1:", result_single)
# Evaluate at multiple points
result_multi = L.legval([0, 1, 2], c)
print("At x=[0,1,2]:")
print(result_multi)
At x=1: [2. 4.] At x=[0,1,2]: [[0. 1.] [2. 4.] [4. 7.]]
Conclusion
Use polynomial.legendre.legval() to evaluate Legendre series with multi-dimensional coefficients. Each column in the coefficient array represents a separate polynomial, and the method efficiently evaluates all polynomials at the specified points.
