使用 Python 中的 NumPy 生成具有給定根的 Hermite_e 級數


厄米特多項式是一組正交多項式,在各種數學應用中非常有用。它們常用於求解微分方程、機率論和量子力學。Hermite_e 級數是厄米特多項式的變體,用於根據其根表示函式。在本文中,我們將討論如何使用 Python 中的 NumPy 生成具有給定根的 Hermite_e 級數。

安裝和語法

NumPy 是一個 Python 庫,它提供對數值運算的支援,可以使用 pip 安裝,並使用語句“import numpy”匯入到 Python 中。

pip install numpy

要使用 NumPy 生成具有給定根的 Hermite_e 級數,可以使用以下語法:

numpy.polynomial.hermite_e.hermegauss(roots, deg)

roots − 包含 Hermite_e 級數根的 1 維陣列。

deg − Hermite_e 級數的次數。

演算法

以下是使用 NumPy 生成具有給定根的 Hermite_e 級數的演算法:

  • 匯入 NumPy 庫。

  • 定義一個包含 Hermite_e 級數根的陣列。

  • 定義 Hermite_e 級數的次數。

  • 使用根和次數作為引數呼叫 numpy.polynomial.hermite_e.hermegauss() 函式。

  • 該函式返回兩個陣列,一個包含 Hermite_e 級數的權重,另一個包含節點。

  • 使用權重和節點構建 Hermite_e 級數。

示例 1

以下程式碼示例生成一個具有根 [-1, 0, 1] 和次數 2 的 Hermite_e 級數。

import numpy as np
roots = np.array([-1, 0, 1])
deg = 2
weights, nodes = np.polynomial.hermite_e.hermegauss(deg)
print(weights)
print(nodes)

輸出

[-1.  1.]
[1.25331414 1.25331414]

示例 2

以下程式碼示例生成一個具有根 [1, 2, 3, 4] 和次數 3 的 Hermite_e 級數。

import numpy as np

# array of roots
roots = np.array([0, 1, 2, 3])

# initialize coefficients array with zeros
coeffs = np.zeros((len(roots), 2 * len(roots) - 1))

# setting up initial values of coefficients
coeffs[:, 0] = roots # setting f(x) values to be the roots
coeffs[1:, 1] = np.diff(coeffs[:, 0]) / np.diff(roots) # setting f'(x) values using finite difference method

# setting up the remaining coefficients using recurrence relation
for j in range(2, 2 * len(roots)):
   for i in range(len(roots)):
      if j % 2 == 0 and i >= j // 2:
         # even-indexed coefficients
         coeffs[i, j // 2] = coeffs[i, j // 2 - 1] * (j - 1) / (j // 2)
      elif j % 2 == 1 and i >= (j + 1) // 2:
         # odd-indexed coefficients
         coeffs[i, (j + 1) // 2 - 1] = (coeffs[i, j // 2] - coeffs[i - 1, j // 2]) / (roots[i] - roots[i - j // 2])

# generating the Hermite series using the calculated coefficients
def hermite_e_series(x):
   res = np.zeros_like(x)
   for i in range(len(roots)):
      term = np.ones_like(x)
      for j in range(i):
         term *= (x - roots[j])
      res += coeffs[i, i] * term
   return res
 

x = np.linspace(-1, 4, 1000)
y = hermite_e_series(x)

# get the first 10 coefficients
print(y[:10])

# plot the function
import matplotlib.pyplot as plt
plt.plot(x, y)
plt.show()

輸出

[-5.5  -5.44884884 -5.39799735 -5.34744457 -5.29718957 -5.24723141                                                                                                       
 -5.19756916 -5.14820186 -5.09912858 -5.05034838]

以下程式碼示例生成一個具有根 [0, 1, 2, 3] 和次數 4 的 Hermite_e 級數,並使用 Matplotlib 繪製該級數。

應用

在 Python 中使用 NumPy 生成的厄米特級數具有各種應用。在物理學中,厄米特多項式用於描述量子諧振子的波函式,同時在數值分析和科學計算中也證明非常有用,以及在統計學中實現近似函式(如正態分佈),因為它通常以高精度實現近似函式。

結論

Hermite_e 級數是科學計算和數值分析中一個強大的工具。藉助 Python 中的 NumPy,生成厄米特級數已成為一項簡單的任務。生成該級數的演算法包括設定初始係數,然後使用遞推關係確定其餘係數。一旦計算出係數,就可以使用簡單函式生成厄米特級數。該級數在物理學、數學和統計學中具有眾多應用。透過使用 Hermite_e 級數,科學家和數學家可以高精度地逼近複雜函式,使其成為許多研究領域的寶貴工具。

更新於: 2023-08-22

159 次檢視

啟動你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.