Evaluate a 3D Legendre series on the Cartesian product of x, y and z in Python

To evaluate a 3D Legendre series on the Cartesian product of x, y and z, use the polynomial.legendre.leggrid3d() method in Python NumPy. The method returns the values of the three-dimensional Legendre series at points in the Cartesian product of x, y, and z coordinates.

Syntax

numpy.polynomial.legendre.leggrid3d(x, y, z, c)

Parameters

x, y, z: The three-dimensional series is evaluated at the points in the Cartesian product of x, y and z. If x, y, or z 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 multi-degree i,j,k is contained in c[i,j,k]. If c has dimension greater than three, the remaining indices enumerate multiple sets of coefficients.

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(16).reshape(2,2,4)

# Display the array
print("Our Array...\n",c)

# Check the Dimensions
print("\nDimensions of our Array...\n",c.ndim)

# Get the Datatype
print("\nDatatype of our Array object...\n",c.dtype)

# Get the Shape
print("\nShape of our Array object...\n",c.shape)

# To evaluate a 3D Legendre series on the Cartesian product of x, y and z
print("\nResult...\n",L.leggrid3d([1,2],[1,2],[1,2],c))
Our Array...
 [[[ 0  1  2  3]
  [ 4  5  6  7]]

 [[ 8  9 10 11]
  [12 13 14 15]]]

Dimensions of our Array...
3

Datatype of our Array object...
int64

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

Result...
 [[[ 120.  868.]
  [ 196. 1404.]]

 [[ 212. 1506.]
  [ 342. 2412.]]]

How It Works

The function evaluates the 3D Legendre series at each point (x[i], y[j], z[k]) in the Cartesian product. If c has fewer than three dimensions, ones are implicitly appended to its shape to make it 3-D. The shape of the result will be c.shape[3:] + x.shape + y.shape + z.shape.

Conclusion

The leggrid3d() method efficiently evaluates 3D Legendre series on Cartesian product grids. It's particularly useful for numerical analysis and scientific computing applications involving multi-dimensional polynomial evaluations.

Updated on: 2026-03-26T20:45:34+05:30

260 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements