NumPy 生成具有给定根的Hermite_e系列

NumPy 生成具有给定根的Hermite_e系列

Hermite多项式是一组正交多项式,在各种数学应用中非常有用。它们常用于解微分方程、概率论和量子力学。 Hermite_e系列是Hermite多项式的一种变体,用于表示函数的根。在本文中,我们将讨论如何使用NumPy在Python中生成具有给定根的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-D数组。

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

以下代码示例生成一个Hermite_e级数,其根为[1, 2, 3, 4],阶数为3。

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]

NumPy 生成具有给定根的Hermite_e系列

以下代码示例生成一个Hermite_e系列,其根为[0, 1, 2, 3],阶数为4,并使用Matplotlib绘制该系列。

应用

在Python中,使用NumPy生成的Hermite系列具有各种应用。在物理学中,Hermite多项式用于描述量子谐振子的波函数,同时也在数值分析和科学计算中发挥重要作用,以及在统计学中实现近似函数,例如正态分布,因为它经常使用高精度的近似函数来实现。

结论

Hermite_e系列是科学计算和数值分析中的强大工具。在Python中,借助NumPy的帮助,生成Hermite系列变得简单。生成该系列的算法涉及设置初始系数,然后使用递推关系确定剩余系数。计算出系数后,可以使用简单的函数生成Hermite系列。该系列在物理学、数学和统计学中有许多应用。通过使用Hermite_e系列,科学家和数学家可以用高精度近似复杂函数,使其成为许多研究领域中的有价值的工具。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程