Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Trả về ma trận đồng hành tỷ lệ của mảng 1-D hệ số đa thức Legendre trong Python

Để trả về ma trận đồng hành tỷ lệ (scaled companion matrix) của mảng 1-D hệ số đa thức Legendre, sử dụng phương thức legendre.legcompanion() trong NumPy Python. Ma trận đồng hành thông thường của đa thức Legendre đã đối xứng khi c là đa thức Laguerre cơ sở, nên không áp dụng tỷ lệ.

Trả về ma trận Đồng hành tỷ lệ có kích thước (deg, deg). Tham số c là mảng 1-D các hệ số chuỗi Legendre theo thứ tự từ bậc thấp đến cao.

Các bước thực hiện

Nhập thư viện cần thiết:

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

Tạo mảng 1D hệ số:

c = np.array([1, 2, 3, 4, 5])

Hiển thị mảng:

print("Our Array...\n", c)

Kiểm tra số chiều:

print("\nDimensions of our Array...\n", c.ndim)

Lấy kiểu dữ liệu:

print("\nDatatype of our Array object...\n", c.dtype)

Lấy hình dạng:

print("\nShape of our Array object...\n", c.shape)

Tính ma trận đồng hành tỷ lệ:

print("\nResult...\n", L.legcompanion(c))

Ví dụ hoàn chỉnh

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

c = np.array([1, 2, 3, 4, 5])
print("Our Array...\n", c)
print("\nDimensions of our Array...\n", c.ndim)
print("\nDatatype of our Array object...\n", c.dtype)
print("\nShape of our Array object...\n", c.shape)
print("\nResult...\n", L.legcompanion(c))

Kết quả đầu ra

Our Array...   [1 2 3 4 5]
Dimensions of our Array... 1
Datatype of our Array object... int64
Shape of our Array object... (5,)
Result...   [[ 0.          0.57735027  0.         -0.30237158]
 [ 0.57735027  0.          0.51639778 -0.34914862]
 [ 0.          0.51639778  0.          0.10141851]
 [ 0.          0.          0.50709255 -0.45714286]]