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

To evaluate a 3-D polynomial on the Cartesian product of x, y, z coordinates, use the numpy.polynomial.polynomial.polygrid3d() method in Python. This method evaluates a three-dimensional polynomial at points in the Cartesian product of the input arrays.

Syntax

numpy.polynomial.polynomial.polygrid3d(x, y, z, c)

Parameters

The function accepts the following parameters ?

  • x, y, z ? 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 first.
  • c ? Array of coefficients ordered so that coefficients for terms of degree i,j,k are contained in c[i,j,k]. If c has fewer than three dimensions, ones are implicitly appended to make it 3-D.

Example

Let's create a 3D coefficient array and evaluate the polynomial at specific points ?

import numpy as np
from numpy.polynomial.polynomial import polygrid3d

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

# Display the coefficient array
print("Coefficient Array:")
print(c)

# Check array properties
print("\nDimensions:", c.ndim)
print("Datatype:", c.dtype)
print("Shape:", c.shape)

# Evaluate 3D polynomial on Cartesian product
result = polygrid3d([1, 2], [1, 2], [1, 2], c)
print("\nResult:")
print(result)
Coefficient Array:
[[[ 0  1  2  3]
  [ 4  5  6  7]]

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

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

Result:
[[[ 120.  496.]
  [ 196.  804.]]

 [[ 212.  864.]
  [ 342. 1386.]]]

How It Works

The function evaluates the polynomial at each point (x[i], y[j], z[k]) in the Cartesian product. For our example with x=[1,2], y=[1,2], z=[1,2], it evaluates at 8 points: (1,1,1), (1,1,2), (1,2,1), etc.

Simple Example with Basic Coefficients

Here's a simpler example with a smaller coefficient array ?

import numpy as np
from numpy.polynomial.polynomial import polygrid3d

# Simple 2x2x2 coefficient array
coeffs = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

print("Simple coefficient array:")
print(coeffs)

# Evaluate at single points
result = polygrid3d([1], [1], [1], coeffs)
print("\nEvaluation at (1,1,1):")
print(result)

# Evaluate at multiple points
result2 = polygrid3d([0, 1], [0, 1], [0], coeffs)
print("\nEvaluation at Cartesian product:")
print(result2)
Simple coefficient array:
[[[1 2]
  [3 4]]

 [[5 6]
  [7 8]]]

Evaluation at (1,1,1):
[[[36.]]]

Evaluation at Cartesian product:
[[[1. 5.]
  [3. 7.]]]

Conclusion

The polygrid3d() function efficiently evaluates 3D polynomials on Cartesian products of coordinate arrays. It's particularly useful for evaluating polynomials over grids or meshes in three-dimensional space.

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

220 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements