Evaluate a 3D Legendre series at points (x,y,z) with 4D array of coefficient 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 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 first parameter consists of x, y, z coordinates where x, y, and z must have the same shape.

The second parameter is the coefficient array c, 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 3, the remaining indices enumerate multiple sets of coefficients.

Syntax

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

Parameters

x, y, z: Array-like coordinates where the series is evaluated. Must have the same shape.

c: Array of coefficients with shape compatible for 3D evaluation.

Example

Let's create a 4D coefficient array and evaluate the Legendre series at specific points ?

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("Shape:", c.shape)
print("Dimensions:", c.ndim)
print("Datatype:", c.dtype)

# Evaluate 3D Legendre series at points (1,2), (1,2), (1,2)
result = L.legval3d([1, 2], [1, 2], [1, 2], c)
print("\nResult shape:", result.shape)
print("Result values:")
print(result)
Coefficient Array:
Shape: (2, 2, 6, 2)
Dimensions: 4
Datatype: int64

Result shape: (2, 2)
Result values:
[[  552.      79447.5   ]
 [  576.      81847.125 ]]

How It Works

The legval3d() function evaluates the 3D Legendre polynomial series:

P(x,y,z) = ? c[i,j,k] × L_i(x) × L_j(y) × L_k(z)

Where L_i, L_j, L_k are Legendre basis polynomials. The 4D coefficient array allows for multiple sets of coefficients, with the extra dimensions preserved in the output shape.

Multiple Evaluation Points

You can evaluate at multiple coordinate sets simultaneously ?

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

# Smaller coefficient array for clarity
c = np.arange(8).reshape(2, 2, 2)
print("Coefficient array shape:", c.shape)

# Evaluate at multiple points
x_vals = [0, 1]
y_vals = [0, 1] 
z_vals = [0, 1]

result = L.legval3d(x_vals, y_vals, z_vals, c)
print("Result:", result)
Coefficient array shape: (2, 2, 2)
Result: [0. 7.]

Conclusion

The legval3d() method efficiently evaluates 3D Legendre polynomial series at specified coordinate points. It handles multi-dimensional coefficient arrays and preserves the shape relationships between input coordinates and output values.

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

270 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements