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
Selected Reading
Evaluate a 2D Legendre series at points (x, y) in Python
To evaluate a 2D Legendre series at points (x, y), use the polynomial.legendre.legval2d() method in NumPy. The method returns the values of the two dimensional Legendre series at points formed from pairs of corresponding values from x and y.
Syntax
numpy.polynomial.legendre.legval2d(x, y, c)
Parameters
The function accepts the following parameters:
- x, y − The two dimensional series is evaluated at the points (x, y), where x and y must have the same shape. If x or y is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and if it isn't an ndarray it is treated as a scalar.
- c − Array of coefficients ordered so that the coefficient of the term of multidegree i,j is contained in c[i,j]. If c has dimension greater than two the remaining indices enumerate multiple sets of coefficients.
Example
Let's evaluate a 2D Legendre series at specific points ?
import numpy as np
from numpy.polynomial import legendre as L
# Create a multidimensional array of coefficients
c = np.array([[3, 4], [5, 6]])
# Display the array
print("Coefficient Array:")
print(c)
# Check the array properties
print("\nDimensions:", c.ndim)
print("Datatype:", c.dtype)
print("Shape:", c.shape)
# Evaluate the 2D Legendre series at points (1,1) and (2,2)
result = L.legval2d([1, 2], [1, 2], c)
print("\nEvaluating at points (1,1) and (2,2):")
print(result)
Coefficient Array: [[3 4] [5 6]] Dimensions: 2 Datatype: int64 Shape: (2, 2) Evaluating at points (1,1) and (2,2): [18. 45.]
How It Works
The 2D Legendre series is represented as:
P(x, y) = c[0,0]*L?(x)*L?(y) + c[0,1]*L?(x)*L?(y) + c[1,0]*L?(x)*L?(y) + c[1,1]*L?(x)*L?(y)
Where L?(t) = 1 and L?(t) = t are the first two Legendre polynomials.
Multiple Evaluation Points
import numpy as np
from numpy.polynomial import legendre as L
# Coefficient array
c = np.array([[1, 2], [3, 4]])
# Evaluate at multiple points
x_points = [0, 1, -1]
y_points = [0, 1, -1]
result = L.legval2d(x_points, y_points, c)
print("Evaluating at points (0,0), (1,1), (-1,-1):")
print(result)
Evaluating at points (0,0), (1,1), (-1,-1): [1. 10. 0.]
Conclusion
The legval2d() function efficiently evaluates 2D Legendre series at specified coordinate pairs. It's particularly useful in numerical analysis and approximation theory where Legendre polynomials are commonly employed for function approximation.
Advertisements
