Evaluate a Legendre series at multidimensional array of points x in Python

To evaluate a Legendre series at multi-dimensional array of points x, use the polynomial.legendre.legval() method in Python NumPy. This function evaluates Legendre polynomials at given points using coefficient arrays.

Syntax

numpy.polynomial.legendre.legval(x, c, tensor=True)

Parameters

The function accepts three parameters:

  • x − Array of points at which to evaluate the series. Can be a scalar, list, tuple, or ndarray
  • c − Array of coefficients ordered so that coefficients for degree n terms are in c[n]
  • tensor − Boolean flag controlling evaluation behavior (default: True)

Example

Let's evaluate a Legendre series at a 2D array of points ?

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

# Create an array of coefficients
c = np.array([1, 2, 3])

# Display the array
print("Coefficients array:")
print(c)

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

# Create a 2D array of evaluation points
x = np.array([[1, 2], [3, 4]])
print("\nEvaluation points x:")
print(x)

# Evaluate Legendre series at points x
result = L.legval(x, c)
print("\nResult:")
print(result)
Coefficients array:
[1 2 3]

Dimensions: 1
Datatype: int64
Shape: (3,)

Evaluation points x:
[[1 2]
 [3 4]]

Result:
[[ 6.  21.5]
 [46.  79.5]]

How It Works

The Legendre series is evaluated using the formula: c[0]*P?(x) + c[1]*P?(x) + c[2]*P?(x), where P?, P?, P? are Legendre polynomials of degrees 0, 1, 2 respectively.

For the coefficients [1, 2, 3] and point x=1:

  • P?(1) = 1, P?(1) = 1, P?(1) = 1
  • Result = 1×1 + 2×1 + 3×1 = 6

Using Different Tensor Settings

The tensor parameter controls how coefficients are applied to multidimensional input ?

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

# 2D coefficient array
c = np.array([[1, 2], [3, 4], [5, 6]])
x = np.array([1, 2])

print("2D coefficients:")
print(c)
print("\nEvaluation points:", x)

# With tensor=True (default)
result_tensor = L.legval(x, c, tensor=True)
print("\nWith tensor=True:")
print(result_tensor)

# With tensor=False
result_no_tensor = L.legval(x, c, tensor=False)
print("\nWith tensor=False:")
print(result_no_tensor)
2D coefficients:
[[1 2]
 [3 4]
 [5 6]]

Evaluation points: [1 2]

With tensor=True:
[[ 9. 12.]
 [27. 32.]]

With tensor=False:
[27. 32.]

Conclusion

The legval() function efficiently evaluates Legendre series at multidimensional points. Use tensor=True for element-wise evaluation across coefficient columns, or tensor=False for broadcasting behavior.

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

239 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements