Evaluate a Legendre series at tuple of points x in Python

To evaluate a Legendre series at tuple of points x, use the polynomial.legendre.legval() method in Python NumPy. This function evaluates a Legendre polynomial series at given points using coefficients.

Syntax

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

Parameters

x: Array of points at which to evaluate the series. If x is a list or tuple, it is converted to an ndarray.

c: Array of coefficients ordered so that coefficients for terms of degree n are contained in c[n]. For multidimensional arrays, remaining indices enumerate multiple polynomials.

tensor: If True (default), the coefficient array shape is extended for broadcasting. If False, x is broadcast over the columns of c.

Example

Let's evaluate a Legendre series at a tuple of points ?

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

# Create an array of coefficients [1, 2, 3]
# This represents: 1*L?(x) + 2*L?(x) + 3*L?(x)
c = np.array([1, 2, 3])

print("Coefficients array:")
print(c)
print("\nArray dimensions:", c.ndim)
print("Array shape:", c.shape)
print("Array datatype:", c.dtype)

# Define tuple of points to evaluate
x = (5, 10, 15)
print("\nPoints to evaluate:", x)

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

Array dimensions: 1
Array shape: (3,)
Array datatype: int64

Points to evaluate: (5, 10, 15)

Legendre series evaluation:
[ 122.  469.5 1042. ]

How It Works

The function evaluates the series: 1×L?(x) + 2×L?(x) + 3×L?(x) where L?, L?, L? are Legendre polynomials of degrees 0, 1, and 2 respectively.

For Legendre polynomials:

  • L?(x) = 1
  • L?(x) = x
  • L?(x) = ½(3x² - 1)

Multiple Coefficient Arrays

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

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

print("2D coefficients array:")
print(c)

result = L.legval(x, c)
print("\nResult for multiple polynomials:")
print(result)
2D coefficients array:
[[1 2]
 [3 4]
 [5 6]]

Result for multiple polynomials:
[[ 9. 12.]
 [31. 44.]
 [71. 104.]]

Conclusion

The legval() function efficiently evaluates Legendre series at multiple points simultaneously. Use it with coefficient arrays to compute polynomial values at tuple or array of points for numerical analysis and approximation tasks.

Updated on: 2026-03-26T21:13:24+05:30

226 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements