Evaluate a 3D Legendre series on the Cartesian product of x, y and z with 4d array of coefficient 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. This method evaluates a 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 coordinates where the series is evaluated. The three-dimensional series is evaluated at points in the Cartesian product of x, y and z. If any parameter is a list or tuple, it is converted to an ndarray.

c: Array of coefficients ordered so that the coefficient of the term of multidegree i,j,k is contained in c[i,j,k]. If c has more than three dimensions, the remaining indices enumerate multiple sets of coefficients.

Example

Let's create a 4D array of coefficients and evaluate the Legendre series ?

import numpy as np
from numpy.polynomial import legendre as L

# Create a 4D array of coefficients
c = np.arange(48).reshape(2, 2, 6, 2)

# Display the array
print("Coefficient Array:")
print(c)
print(f"\nDimensions: {c.ndim}")
print(f"Shape: {c.shape}")
print(f"Datatype: {c.dtype}")

# Evaluate 3D Legendre series on Cartesian product
result = L.leggrid3d([1, 2], [1, 2], [1, 2], c)
print(f"\nResult shape: {result.shape}")
print("Result:")
print(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]]]


 [[[24 25]
   [26 27]
   [28 29]
   [30 31]
   [32 33]
   [34 35]]

  [[36 37]
   [38 39]
   [40 41]
   [42 43]
   [44 45]
   [46 47]]]]

Dimensions: 4
Shape: (2, 2, 6, 2)
Datatype: int64

Result shape: (6, 2, 2, 2, 2)
Result:
[[[[[  552.   28911. ]
    [  900.   46566. ]]

   [[  972.   49765.5]
    [ 1566.   79447.5]]]


  [[[  576.   29977.5]
    [  936.   48165.75]]

   [[ 1008.   51365.25]
    [ 1620.   81847.125]]]]]

How It Works

The function computes the Legendre series expansion using the coefficient array. When c has more than three dimensions, the shape of the result follows the formula: c.shape[3:] + x.shape + y.shape + z.shape. In our example, the result shape is (6, 2, 2, 2, 2) because c.shape[3:] is (6, 2) and each coordinate array [1, 2] has shape (2,).

Key Points

− The method works with coefficient arrays of any dimension ? 3

− Input coordinates can be scalars, lists, tuples, or ndarrays

− The evaluation is performed on the Cartesian product of all coordinate points

− Higher-dimensional coefficient arrays allow evaluation of multiple series simultaneously

Conclusion

The leggrid3d() method efficiently evaluates 3D Legendre series on coordinate grids. It's particularly useful for multidimensional polynomial approximations and scientific computing applications requiring Legendre polynomial evaluations.

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

225 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements