Evaluate a Legendre series at points x broadcast over the columns of the coefficient in Python

To evaluate a Legendre series at points x, use the polynomial.legendre.legval() method in Python NumPy. This function broadcasts x over the columns of the coefficient array, making it useful for evaluating multiple polynomials simultaneously.

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, otherwise treated as a scalar.

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

tensor: If True (default), extends coefficient array shape with ones for each dimension of x. If False, broadcasts x over columns of c for evaluation.

Example with Broadcasting Over Columns

Let's create a multidimensional coefficient array and evaluate at multiple points ?

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

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

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

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

# Evaluate Legendre series with tensor=False (broadcast over columns)
result = L.legval([1, 2], c, tensor=False)
print("\nResult (tensor=False):")
print(result)
Coefficient Array:
[[0 1]
 [2 3]]

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

Result (tensor=False):
[2. 7.]

How It Works

With tensor=False, the function evaluates:

  • First column [0, 2] at points [1, 2]
  • Second column [1, 3] at points [1, 2]

The Legendre series evaluation uses the formula: c[0] + c[1]*L?(x) where L?(x) = x for the first Legendre polynomial.

Comparison of tensor Parameter

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

c = np.array([[0, 1], [2, 3]])
x = [1, 2]

# With tensor=False (broadcast over columns)
result_false = L.legval(x, c, tensor=False)
print("tensor=False shape:", result_false.shape)
print("tensor=False result:", result_false)

# With tensor=True (default behavior)  
result_true = L.legval(x, c, tensor=True)
print("\ntensor=True shape:", result_true.shape)
print("tensor=True result:")
print(result_true)
tensor=False shape: (2,)
tensor=False result: [2. 7.]

tensor=True shape: (2, 2)
tensor=True result:
[[2. 2.]
 [5. 7.]]

Conclusion

Use legval() with tensor=False to broadcast evaluation points over coefficient columns efficiently. This is particularly useful when working with multiple polynomials stored as columns in a coefficient matrix.

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

198 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements