Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Evaluate a Laguerre series at points x and the shape of the coefficient array extended for each dimension of x in Python
To evaluate a Laguerre series at points x, use the polynomial.laguerre.lagval() method in Python NumPy. This function allows you to evaluate Laguerre polynomials with given coefficients at specified points, with control over how multidimensional coefficient arrays are handled.
Syntax
numpy.polynomial.laguerre.lagval(x, c, tensor=True)
Parameters
The function accepts three parameters:
- x: Points at which to evaluate the series. Can be scalar, list, or array
- c: Array of coefficients ordered so that coefficients for terms of degree n are in c[n]
- tensor: Boolean controlling evaluation behavior for multidimensional arrays (default: True)
Understanding the Tensor Parameter
When tensor=True, the coefficient array shape is extended with ones for each dimension of x, allowing every column of coefficients to be evaluated at every point. When tensor=False, x is broadcast over the columns of c.
Example
import numpy as np
from numpy.polynomial import laguerre as L
# Create a multidimensional array of coefficients
c = np.arange(8).reshape(2, 4)
# Display the array
print("Coefficient Array:")
print(c)
# Check array properties
print(f"\nDimensions: {c.ndim}")
print(f"Shape: {c.shape}")
print(f"Data type: {c.dtype}")
# Evaluate Laguerre series at points [1, 2] with tensor=True
result = L.lagval([1, 2], c, tensor=True)
print(f"\nResult with tensor=True:")
print(result)
Coefficient Array: [[0 1 2 3] [4 5 6 7]] Dimensions: 2 Shape: (2, 4) Data type: int64 Result with tensor=True: [[ 0. -4.] [ 1. -4.] [ 2. -4.] [ 3. -4.]]
Comparing Tensor Modes
import numpy as np
from numpy.polynomial import laguerre as L
c = np.arange(6).reshape(2, 3)
x_points = [0, 1]
print("Coefficient array:")
print(c)
print(f"\nWith tensor=True (shape: {L.lagval(x_points, c, tensor=True).shape}):")
print(L.lagval(x_points, c, tensor=True))
print(f"\nWith tensor=False (shape: {L.lagval(x_points, c, tensor=False).shape}):")
print(L.lagval(x_points, c, tensor=False))
Coefficient array: [[0 1 2] [3 4 5]] With tensor=True (shape: (2, 3)): [[0. 1.] [1. 1.] [2. 1.]] With tensor=False (shape: (2,)): [1. 1.]
Single Point Evaluation
import numpy as np
from numpy.polynomial import laguerre as L
# Simple coefficient array
coeffs = np.array([1, 2, 3])
# Evaluate at single point
result_single = L.lagval(2, coeffs)
print(f"Laguerre series at x=2: {result_single}")
# Evaluate at multiple points
result_multiple = L.lagval([0, 1, 2], coeffs)
print(f"Laguerre series at x=[0,1,2]: {result_multiple}")
Laguerre series at x=2: -4.0 Laguerre series at x=[0,1,2]: [ 6. 2. -4.]
Conclusion
The lagval() function provides flexible evaluation of Laguerre series with multidimensional coefficient arrays. Use tensor=True to evaluate each coefficient column at all x points, or tensor=False for broadcasting behavior.
