Evaluate a 3D Legendre series at points (x, y, z) in Python

To evaluate a 3D Legendre series at points (x, y, z), use the polynomial.legendre.legval3d() method in Python NumPy. The method returns the values of the multidimensional polynomial on points formed with triples of corresponding values from x, y, and z.

If the coefficient array c has fewer than 3 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. The coordinates x, y, and z must have the same shape, and if any are lists or tuples, they are first converted to ndarrays.

Syntax

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

Parameters

x, y, z: The three-dimensional series is evaluated at points (x, y, z), where x, y, and z must have the same shape. If any of x, y, or z is a list or tuple, it is first 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 dimension greater than 3, the remaining indices enumerate multiple sets of coefficients.

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 array properties
print(f"\nDimensions: {c.ndim}")
print(f"Datatype: {c.dtype}")
print(f"Shape: {c.shape}")

# Evaluate 3D Legendre series at points (1,2), (1,2), (1,2)
result = L.legval3d([1, 2], [1, 2], [1, 2], c)
print(f"\nResult: {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: [  276.   39723.75]

Understanding the Result

The function evaluates the 3D Legendre series at two points: (1,1,1) and (2,2,2), returning an array with two values. Each value represents the polynomial evaluation at the corresponding point coordinates.

Multiple Points Example

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

# Create simpler 3D coefficients
c = np.ones((2, 2, 3))  # 2x2x3 array of ones

# Evaluate at multiple points
x_points = [0, 1, -1]
y_points = [0, 1, -1] 
z_points = [0, 1, -1]

result = L.legval3d(x_points, y_points, z_points, c)
print(f"Coefficients shape: {c.shape}")
print(f"Evaluation points: {list(zip(x_points, y_points, z_points))}")
print(f"Results: {result}")
Coefficients shape: (2, 2, 3)
Evaluation points: [(0, 0, 0), (1, 1, 1), (-1, -1, -1)]
Results: [6. 6. 6.]

Conclusion

The legval3d() function efficiently evaluates 3D Legendre polynomial series at specified coordinate points. It's particularly useful for multidimensional polynomial approximations and scientific computing applications requiring three-dimensional polynomial evaluations.

Updated on: 2026-03-26T20:42:07+05:30

208 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements