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 at points (x, y) with 1D array of coefficient in Python
To evaluate a 2D Legendre series at points (x, y), use the polynomial.legendre.legval2d() method in NumPy. This method returns the values of the two-dimensional Legendre series at points formed from pairs of corresponding values from x and y arrays.
Syntax
numpy.polynomial.legendre.legval2d(x, y, c)
Parameters
The function takes the following parameters:
- x, y: The two dimensional series is evaluated at points (x, y). Both must have the same shape. If x or y is a list or tuple, it is converted to an ndarray.
- c: Array of coefficients ordered so that the coefficient of the term of multi-degree i,j is contained in c[i,j].
Example
Let's create a 1D array of coefficients and evaluate a 2D Legendre series at specific points ?
import numpy as np
from numpy.polynomial import legendre as L
# Create a 1D array of coefficients
c = np.array([3, 5])
# Display the array
print("Our Array:")
print(c)
# Check the dimensions
print("\nDimensions of our Array:")
print(c.ndim)
# Get the datatype
print("\nDatatype of our Array:")
print(c.dtype)
# Get the shape
print("\nShape of our Array:")
print(c.shape)
# Evaluate 2D Legendre series at points (1,1) and (2,2)
result = L.legval2d([1, 2], [1, 2], c)
print("\nResult:")
print(result)
Our Array: [3 5] Dimensions of our Array: 1 Datatype of our Array: int64 Shape of our Array: (2,) Result: [21. 34.]
How It Works
The legval2d() function evaluates the 2D Legendre polynomial using the coefficient array. For a 1D coefficient array [3, 5], it treats this as coefficients for terms of different degrees in the Legendre series expansion.
Multiple Points Example
You can evaluate the series at multiple coordinate pairs simultaneously ?
import numpy as np
from numpy.polynomial import legendre as L
# Create coefficient array
coefficients = np.array([2, 4, 1])
# Evaluate at multiple points
x_points = [0, 1, 2]
y_points = [0, 1, 2]
result = L.legval2d(x_points, y_points, coefficients)
print("Coefficients:", coefficients)
print("X points:", x_points)
print("Y points:", y_points)
print("Results:", result)
Coefficients: [2 4 1] X points: [0, 1, 2] Y points: [0, 1, 2] Results: [ 6. 13. 28.]
Conclusion
The legval2d() function efficiently evaluates 2D Legendre series at specified coordinate points. It accepts 1D coefficient arrays and coordinate pairs, returning the computed polynomial values at each point.
