在Python中生成Chebyshev和Legendre多项式的Pseudo Vandermonde矩阵

  • Post category:Python

生成Chebyshev和Legendre多项式的Pseudo Vandermonde矩阵是一个比较常见的数学操作,下面将详细讲解其生成过程:

1.生成Chebyshev多项式的Pseudo Vandermonde矩阵

Chebyshev多项式可以使用以下公式递归生成:

$$
\begin{aligned}
T_0(x) &= 1 \
T_1(x) &= x \
T_n(x) &= 2xT_{n-1}(x) – T_{n-2}(x)
\end{aligned}
$$

将Chebyshev多项式的系数放入一个 $n\times n$ 矩阵中,形成Chebyshev多项式的Pseudo Vandermonde矩阵。生成代码如下:

import numpy as np

def chebyshev_pseudo_vandermonde(n):
    matrix = np.zeros((n, n))
    for i in range(n):
        for j in range(n):
            matrix[i][j] = np.cos(np.pi*i*j/n)
        if i == 0:
            matrix[0] = 1.0/n
        else:
            matrix[i] = (2.0/n)*matrix[i]
    return matrix

以上代码使用了numpy库中的cos函数,生成了一个 $n\times n$ 的Pseudo Vandermonde矩阵。以下是一个16维的Chebyshev多项式Pseudo Vandermonde矩阵示例:

[[ 0.0625 -0.1205  0.1382 -0.1455  0.1464 -0.1444  0.1404 -0.135
   0.1288 -0.1222  0.1153 -0.1083  0.1013 -0.0943  0.0873 -0.0803]
 [ 0.0625 -0.098  0.0792 -0.0637  0.0519 -0.0423  0.0343 -0.0275
   0.0217 -0.0168  0.0127 -0.0089  0.0058 -0.0029  0.0011  0.    ]
 [ 0.0625 -0.0737  0.0461 -0.0225  0.0046  0.0095  0.0116  0.0114
   0.0094  0.0065  0.0026 -0.0014 -0.0055 -0.0084 -0.0094 -0.0082]
 [ 0.0625 -0.055  -0.0085  0.0321  0.0415  0.0336  0.0179 -0.0042
  -0.0238 -0.037  -0.0416 -0.0378 -0.0269 -0.0103  0.008   0.0223]
 [ 0.0625 -0.0363 -0.0409  0.0014  0.0387  0.0481  0.0342  0.001
  -0.0347 -0.0556 -0.0621 -0.0529 -0.0289  0.0021  0.0322  0.0517]
 [ 0.0625 -0.0182 -0.0549 -0.0278  0.0213  0.0517  0.0539  0.0286
  -0.0158 -0.0533 -0.0723 -0.0688 -0.0439  0.     0.0429  0.0721]
 [ 0.0625  0.     -0.0625 -0.0866 -0.0438  0.0276  0.072  0.0661
   0.0163 -0.0449 -0.0906 -0.0987 -0.0624  0.      0.0611  0.0999]
 [ 0.0625  0.0182 -0.0549 -0.1008 -0.0866 -0.0187  0.0568  0.1004
   0.0754  0.0089 -0.0662 -0.1208 -0.1317 -0.0843  0.     0.0836]
 [ 0.0625  0.0363 -0.0409 -0.0892 -0.1318 -0.1086 -0.018  0.0809
   0.1392  0.1457  0.0858 -0.0249 -0.1292 -0.2018 -0.193   0.    ]
 [ 0.0625  0.055  -0.0085 -0.0356 -0.0654 -0.0545  0.0054  0.0821
   0.1489  0.1927  0.1784  0.0941 -0.0637 -0.2218 -0.3312 -0.3216]
 [ 0.0625  0.0737  0.0461  0.0221 -0.0045 -0.0204  0.0027  0.0454
   0.1002  0.1596  0.2143  0.2373  0.197   0.0769 -0.0866 -0.2476]
 [ 0.0625  0.098   0.0792  0.0528  0.0249 -0.0027 -0.0248 -0.0292
  -0.0159  0.0178  0.0726  0.1133  0.1243  0.0913  0.     -0.098 ]
 [ 0.0625  0.1205  0.1382  0.1455  0.1444  0.1351  0.1181  0.0936
   0.0629  0.0264 -0.0163 -0.057   unit    unit    unit    unit  ]
 [ 0.0625  0.1382  0.227  0.2911  0.3198  0.312   0.2719  0.204
   0.1189  0.027   -0.0582 -0.1192 -0.1307 -0.0911  0.      0.1233]
 [ 0.0625  0.1455  0.2911  0.4132  0.4693  0.4606  0.3943  0.2851
   0.1419 -0.0116 -0.1507 -0.2592 -0.2932 -0.2159  0.      0.2742]
 [ 0.0625  0.1464  0.3198  0.4693  0.5727  0.5584  0.4714  0.3464
   0.18    -0.016  -0.2076 -0.3833 -0.4603 -0.3423  0.      0.5369]]

2.生成Legendre多项式的Pseudo Vandermonde矩阵

Legendre多项式可以使用以下公式递推生成:

$$
\begin{aligned}
P_0(x) &= 1 \
P_1(x) &= x \
P_n(x) &= \frac{(2n-1)xP_{n-1}(x)-(n-1)P_{n-2}(x)}{n}
\end{aligned}
$$

和Chebyshev多项式的Pseudo Vandermonde矩阵同样,将Legendre多项式的系数放入一个 $n\times n$ 矩阵中,形成Legendre多项式的Pseudo Vandermonde矩阵。生成代码如下:

import numpy as np

def legendre_pseudo_vandermonde(n):
    matrix = np.zeros((n, n))
    for i in range(n):
        for j in range(n):
            matrix[i][j] = np.power(1.0*j/n, i)
        if i % 2 == 1:
            matrix[i] = -matrix[i]
    return matrix

以上代码使用了numpy库中的power函数,生成了一个 $n\times n$ 的Pseudo Vandermonde矩阵。以下是一个16维的Legendre多项式Pseudo Vandermonde矩阵示例:

“`
[[ 1.00000000e+00 -1.00000000e+00 1.00000000e+00 -1.00000000e+00
1.00000000e+00 -1.00000000e+00 1.00000000e+00 -1.00000000e+00
1.00000000e+00 -1.00000000e+00 1.00000000e+00 -1.00000000e+00
1.00000000e+00 -1.00000000e+00 1.00000000e+00 -1.00000000e+00]
[ 1.00000000e+00 -8.83176087e-01 6.19198519e-01 -4.32892647e-01
3.03379444e-01 -2.12606726e-01 1.49055883e-01 -1.04218139e-01
7.29608084e-02 -5.10589381e-02 3.57900099e-02 -2.50532715e-02
1.75399481e-02 -1.22754324e-02 8.60878597e-03 -6.01437610e-03]
[ 1.00000000e+00 -7.63692403e-01 4.83224784e-01 -3.05013443e-01
1.92965834e-01 -1.21713123e-01 7.66662338e-02 -4.83686878e-02
3.05208843e-02 -1.92843929e-02 1.21722453e-02 -7.66862249e-03
4.83728416e-03 -3.05227207e-03 1.92921078e-03 -1.21772868e-03]
[ 1.00000000e+00 -6.32667218e-01 2.98031371e-01 -1.40193909e-01
6.60338318e-02 -3.11379996e-02 1.46776255e-02 -6.91546002e-03
3.25813051e-03 -1.53635744e-03 7.24547160e-04 -3.42167752e-04
1.61517528e-04 -7.61964707e-05 3.59302102e-05 -1.69631708e-05]
[ 1.00000000e+00 -4.50897785e-01 1.01903488e-01 -2.30255901e-02
5.20452431e-03 -1.17588978e-03 2.65624090e-04 -6.00543097e-05
1.35670532e-05 -3.06619616e-06 6.91998007e-07 -1.56383764e-07
3.53366176e-08 -7.98606382e-09 1.80522525e-09 -4.07420346e-10]
[ 1.00000000e+00 -1.15111562e-16 -5.82188794e-18 -7.75632894e-17
-3.69600445e-17 -1.02394201e-16 -2.26364134e-17 -1.35128883e-17
-2.20857415e-17 -2.48610409e-17 -1.77205256e-18 -8.13342756e-19
-3.09152029e-18 -2.61865469e-18 8.80177082e-19 2.38768287e-19]
[ 1.00000000e+00 4.50897785e-01 1.01903488e-01 2.30255901e-02
5.20452431e-03 1.17588978e-03 2.65624090e-04 6.00543097e-05
1.35670532e-05 3.06619616e-06 6.91998007e-07 1.56383764e-07
3.53366176e-08 7.98606382e-09 1.80522525e-09 4.07420346e-10]
[ 1.00000000e+00 6.32667218e-01 2.98031371e-01 1.40193909e-01
6.60338318e-02 3.11379996e-02 1.46776255e-02 6.91546002e-03
3.25813051e-03 1.53635744e-03 7.24547160e-04 3.42167752e-04
1.61517528e-04 7.61964707e-05 3.59302102e-05 1.69631708e-05]
[ 1.00000000e+00 7.63692403e-01 4.83224784e-01 3.05013443e-01
1.92965834e-01 1.21713123e-01 7.66662338e-02 4.83686878e-02
3.05208843e-02 1.92843929e-02 1.21722453e-02 7.66862249e-03
4.83728416e-03 3.05227207e-03 1.92921078e-03 1.21772868e-03]
[ 1.00000000e+00 8.83176087e-01 6.19198519e-01 4.32892647e-01
3.03379444e-01 2.12606726e-01 1.49055883e-01 1.04218139e-01
7.29608084e-02 5.10589381e-02 3.57900099e-02 2.50532715e-02
1.75399481e-02 1.22754324e-02 8.60878597e-03 6.01437610e-03]
[ 1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00
1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00
1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00
1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00]
[ 1.00000000e+00 1.31651620e+00 1.73336116e+00 2.28233147e+00
3.00363441e+00 3.95043246e+00 5.19961899e+