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 2D Legendre series on the Cartesian product of x and y with 1d array of coefficient in Python
To evaluate a 2D Legendre series on the Cartesian product of x and y, use the polynomial.legendre.leggrid2d() method in NumPy. This method evaluates a two-dimensional Legendre series at points formed by the Cartesian product of x and y arrays using a 1D array of coefficients.
Understanding leggrid2d()
The leggrid2d() function takes three parameters ?
- x, y ? Arrays defining evaluation points. The series is evaluated at the Cartesian product of these points
-
c ? 1D or 2D array of coefficients where
c[i,j]contains the coefficient for the term of degree (i,j)
If the coefficient array has fewer than two dimensions, ones are implicitly appended to make it 2D.
Basic Example
Let's start with a simple 1D coefficient array ?
import numpy as np
from numpy.polynomial import legendre as L
# Create a 1D array of coefficients
c = np.array([3, 5])
print("Coefficient array:", c)
print("Array dimensions:", c.ndim)
print("Array shape:", c.shape)
print("Array datatype:", c.dtype)
Coefficient array: [3 5] Array dimensions: 1 Array shape: (2,) Array datatype: int64
Evaluating on Cartesian Product
Now evaluate the 2D Legendre series at the Cartesian product of x and y points ?
import numpy as np
from numpy.polynomial import legendre as L
c = np.array([3, 5])
# Evaluate at Cartesian product of [1,2] x [1,2]
result = L.leggrid2d([1, 2], [1, 2], c)
print("Result shape:", result.shape)
print("Result values:\n", result)
Result shape: (2, 2) Result values: [[8. 8.] [8. 8.]]
Working with 2D Coefficient Arrays
For more complex polynomials, use a 2D coefficient array ?
import numpy as np
from numpy.polynomial import legendre as L
# Create a 2D coefficient array
c_2d = np.array([[1, 2], [3, 4]])
print("2D coefficient array:")
print(c_2d)
# Evaluate at different points
x_points = [0, 1]
y_points = [0, 1]
result = L.leggrid2d(x_points, y_points, c_2d)
print("\nResult for 2D coefficients:")
print(result)
2D coefficient array: [[1 2] [3 4]] Result for 2D coefficients: [[1. 3.] [3. 9.]]
How the Evaluation Works
The function evaluates the 2D Legendre polynomial ?
Complete Example
import numpy as np
from numpy.polynomial import legendre as L
# Create coefficient array
c = np.array([3, 5])
print("Original coefficient array:", c)
print("Dimensions:", c.ndim)
print("Shape:", c.shape)
# Define evaluation points
x_vals = [1, 2]
y_vals = [1, 2]
# Evaluate 2D Legendre series
result = L.leggrid2d(x_vals, y_vals, c)
print("\nEvaluation points:")
print("x values:", x_vals)
print("y values:", y_vals)
print("\nResult matrix:")
print(result)
print("Result shape:", result.shape)
Original coefficient array: [3 5] Dimensions: 1 Shape: (2,) Evaluation points: x values: [1, 2] y values: [1, 2] Result matrix: [[8. 8.] [8. 8.]] Result shape: (2, 2)
Conclusion
The leggrid2d() method efficiently evaluates 2D Legendre series on Cartesian product grids. Use 1D coefficient arrays for simple cases or 2D arrays for more complex polynomial terms. The result shape follows the pattern c.shape[2:] + x.shape + y.shape.
