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 3D array of coefficient in Python
To evaluate a 2D Legendre series at points (x, y) with a 3D coefficient array, use the numpy.polynomial.legendre.legval2d() method. This method computes the values of a two-dimensional Legendre series at specified coordinate pairs.
Syntax
The basic syntax is ?
numpy.polynomial.legendre.legval2d(x, y, c)
Parameters
The function accepts the following parameters ?
- x, y ? Coordinate arrays where the series is evaluated. Must have the same shape.
-
c ? Array of coefficients where
c[i,j]contains the coefficient for the term of multi-degree (i,j). For 3D arrays, additional indices enumerate multiple coefficient sets.
Example
Let's create a 3D coefficient array and evaluate the Legendre series at specific points ?
import numpy as np
from numpy.polynomial import legendre as L
# Create a 3D array of coefficients
c = np.arange(24).reshape(2, 2, 6)
# Display the array
print("Coefficient Array:")
print(c)
# Check the array properties
print(f"\nDimensions: {c.ndim}")
print(f"Datatype: {c.dtype}")
print(f"Shape: {c.shape}")
# Evaluate the 2D Legendre series at points (1,1) and (2,2)
result = L.legval2d([1, 2], [1, 2], c)
print(f"\nResult:\n{result}")
Coefficient Array: [[[ 0 1 2 3 4 5] [ 6 7 8 9 10 11]] [[12 13 14 15 16 17] [18 19 20 21 22 23]]] Dimensions: 3 Datatype: int64 Shape: (2, 2, 6) Result: [[ 36. 108.] [ 40. 117.] [ 44. 126.] [ 48. 135.] [ 52. 144.] [ 56. 153.]]
How It Works
The 3D coefficient array has shape (2, 2, 6), meaning we have 6 different 2×2 coefficient matrices. The function evaluates each 2D Legendre series (defined by each 2×2 matrix) at the given points, returning a 6×2 result array where each row corresponds to one coefficient set.
Conclusion
The legval2d() method efficiently evaluates multiple 2D Legendre series simultaneously when working with 3D coefficient arrays. Each additional dimension in the coefficient array creates a separate series evaluation in the output.
