Evaluate a 2D Legendre series on the Cartesian product of x and y with 3d array of coefficient in Python

To evaluate a 2D Legendre series on the Cartesian product of x and y with a 3D array of coefficients, use the polynomial.legendre.leggrid2d() method in NumPy. This method returns the values of the two-dimensional Legendre series at points in the Cartesian product of x and y.

Understanding the Function

The leggrid2d() function takes three parameters:

  • x, y: The coordinates for evaluation. The series is evaluated at points in the Cartesian product of x and y
  • c: A 3D array of coefficients where c[i,j] contains the coefficient of the term of multi-degree i,j

If the coefficient array has fewer than two dimensions, ones are implicitly appended to make it 2D. The result shape will be c.shape[2:] + x.shape + y.shape.

Example

Let's create a 3D coefficient array and evaluate the Legendre series:

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("Our Array...\n", c)

# Check the array properties
print("\nDimensions of our Array...\n", c.ndim)
print("\nDatatype of our Array object...\n", c.dtype)
print("\nShape of our Array object...\n", c.shape)

# Evaluate 2D Legendre series on Cartesian product
result = L.leggrid2d([1, 2], [1, 2], c)
print("\nResult...\n", result)
Our 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 of our Array...
3

Datatype of our Array object...
int64

Shape of our Array object...
(2, 2, 6)

Result...
 [[[ 36.  60.]
  [ 66. 108.]]

 [[ 40.  66.]
  [ 72. 117.]]

 [[ 44.  72.]
  [ 78. 126.]]

 [[ 48.  78.]
  [ 84. 135.]]

 [[ 52.  84.]
  [ 90. 144.]]

 [[ 56.  90.]
  [ 96. 153.]]]

How It Works

The function evaluates the 2D Legendre series using the formula:

sum(c[i,j] * L_i(x) * L_j(y))

Where L_i and L_j are Legendre polynomials of degree i and j respectively. The result has shape (6, 2, 2) because:

  • The coefficient array has shape (2, 2, 6)
  • The x and y arrays each have 2 elements
  • The result shape is c.shape[2:] + x.shape + y.shape = (6,) + (2,) + (2,) = (6, 2, 2)

Conclusion

The leggrid2d() function provides an efficient way to evaluate 2D Legendre series on Cartesian products. It handles multi-dimensional coefficient arrays and returns results with proper shape based on input dimensions.

Updated on: 2026-03-26T20:43:21+05:30

223 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements