Evaluate a Legendre series at array of points x in Python

To evaluate a Legendre series at an array of points x, use the polynomial.legendre.legval() method in Python NumPy. This method takes coefficients of a Legendre polynomial and evaluates it at specified points.

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. The elements must support addition and multiplication operations.

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

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

Basic Example

Let's evaluate a Legendre series with coefficients [1, 2, 3] at various points ?

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

# Create coefficient array
c = np.array([1, 2, 3])
print("Coefficients:", c)

# Evaluate at single point
result_single = L.legval(2, c)
print("At x=2:", result_single)

# Evaluate at array of points
x = np.array([0, 1, 2])
result_array = L.legval(x, c)
print("At x=[0,1,2]:", result_array)
Coefficients: [1 2 3]
At x=2: 21.5
At x=[0,1,2]: [ 3.   6.  21.5]

Multidimensional Array Example

Here's how to evaluate the series at a 2D array of points ?

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

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

# Display array properties
print("Coefficients:", c)
print("Dimensions:", c.ndim)
print("Shape:", c.shape)
print("Datatype:", c.dtype)

# Evaluate at 2D array
x = np.array([[1, 2], [3, 4]])
print("\nInput points x:")
print(x)

result = L.legval(x, c)
print("\nResult:")
print(result)
Coefficients: [1 2 3]
Dimensions: 1
Shape: (3,)
Datatype: int64

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

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

How It Works

A Legendre series with coefficients [1, 2, 3] represents: 1×P?(x) + 2×P?(x) + 3×P?(x), where P?, P?, P? are Legendre polynomials. The method evaluates this polynomial at each point in the input array.

Conclusion

Use legval() to efficiently evaluate Legendre series at single points or arrays. The method handles multidimensional inputs and provides flexible broadcasting options through the tensor parameter.

Updated on: 2026-03-26T20:41:49+05:30

339 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements