Để đánh giá chuỗi Legendre tại điểm x, dùng phương thức polynomial.legendre.legval() trong Python NumPy. Tham số 1 là x. Nếu x là list hoặc tuple, nó chuyển thành ndarray, ngược lại giữ nguyên và coi như scalar. Trong cả hai trường hợp, x hoặc các element phải hỗ trợ addition và multiplication với chính nó và với element của c.
Tham số 2, C, mảng hệ số có thứ tự sao cho hệ số cho terms degree n chứa trong c[n]. Nếu c đa chiều, các index còn lại enumerate multiple polynomials. Trong trường hợp 2D, hệ số có thể nghĩ như stored trong các cột của c.
Tham số 3, tensor, nếu True, shape của coefficient array mở rộng với 1 ở bên phải, một cho mỗi dimension của x. Scalar có dimension 0 cho action này. Kết quả là mọi cột hệ số trong c được đánh giá cho mọi element của x. Nếu False, x broadcast qua các cột của c để evaluate. Keyword này hữu ích khi c đa chiều. Default là True.
Các bước
Đầu tiên, import thư viện cần thiết:
import numpy as np from numpy.polynomial import legendre as L
Tạo mảng hệ số:
c = np.array([1, 2, 3])
Hiển thị mảng:
print("Our Array...\n", c)
Kiểm tra Dimensions:
print("\nDimensions of our Array...\n", c.ndim)
Lấy Datatype:
print("\nDatatype of our Array object...\n", c.dtype)
Lấy Shape:
print("\nShape of our Array object...\n", c.shape)
Ở đây, x là tuple:
x = (5, 10, 15)
Đánh giá chuỗi Legendre tại điểm x, dùng polynomial.legendre.legval():
print("\nResult...\n", L.legval(x, c))
Ví dụ
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("Our Array...\n", c)
# Check the Dimensions
print("\nDimensions of our Array...\n", c.ndim)
# Get the Datatype
print("\nDatatype of our Array object...\n", c.dtype)
# Get the Shape
print("\nShape of our Array object...\n", c.shape)
# Here, x is a tuple
x = (5, 10, 15)
# To evaluate a Legendre series at points x, use the polynomial.legendre.legval() method in Python Numpy
print("\nResult...\n", L.legval(x, c))
Kết quả
Our Array... [1 2 3] Dimensions of our Array... 1 Datatype of our Array object... int64 Shape of our Array object... (3,) Result... [ 122. 469.5 1042. ]