Computer >> Máy Tính >  >> Lập trình >> Python

Tạo ma trận Vandermonde của loạt Legendre bằng Python

Để tạo ma trận Vandermonde giả của đa thức Legendre, hãy sử dụng phương thức thepolynomial.legvander () trong Python Numpy

Phương thức này trả về ma trận giả Vandermonde. Hình dạng của ma trận trả về là x.shape + (deg + 1,), trong đó Chỉ số cuối cùng là bậc của đa thức Legendre tương ứng. Loại dtype sẽ giống như x đã chuyển đổi.

Tham số, x trả về một Mảng điểm. Loại dtype được chuyển đổi thành float64 hoặc complex128 tùy thuộc vào việc liệu bất kỳ phần tử nào có phức tạp hay không. Nếu x là vô hướng, nó sẽ được chuyển đổi thành mảng 1-D. Tham số, deg là bậc của ma trận kết quả.

Các bước

Đầu tiên, hãy nhập thư viện được yêu cầu -

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

Tạo một mảng -

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

Hiển thị mảng -

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

Kiểm tra các thứ nguyên -

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

Lấy Datatype -

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ạo ma trận Vandermonde giả của đa thức Legendre, hãy sử dụng phương thức thepolynomial.legvander () trong Python -

print("\nResult...\n",L.legvander(x, 2))

Ví dụ

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

# Create an array
x = np.array([0, 1, -1, 2])

# Display the array
print("Our Array...\n",x)

# Check the Dimensions
print("\nDimensions of our Array...\n",x.ndim)

# Get the Datatype
print("\nDatatype of our Array object...\n",x.dtype)

# Get the Shape
print("\nShape of our Array object...\n",x.shape)

# To generate a pseudo Vandermonde matrix of the Legendre polynomial, use the polynomial.legvander() method in Python Numpy
print("\nResult...\n",L.legvander(x, 2))

Đầu ra

Our Array...
   [ 0 1 -1 2]

Dimensions of our Array...
1

Datatype of our Array object...
int64

Shape of our Array object...
(4,)

Result...
   [[ 1. 0. -0.5]
   [ 1. 1. 1. ]
   [ 1. -1. 1. ]
   [ 1. 2. 5.5]]