使用 NumPy 在 Python 中将一个 Hermite 级数除以另一个


Hermite 级数是数学技术之一,用于表示 Hermite 多项式的无限级数。Hermite 多项式被称为正交多项式序列,是 Hermite 微分方程的解。

将一个 Hermite 级数除以另一个

Hermite 级数由以下等式给出。

f(x) = Σn=0^∞ cn Hn(x)

其中

  • Hn(x) 是第 n 个 Hermite 多项式

  • cn 是展开式中的第 n 个系数。

系数 cn 可以使用以下公式确定

cn = (1/$\mathrm{\surd}$(2^n n!))$\mathrm{\lmoustache}$ f(x) Hn(x) e^(−x^2/2) dx

示例

首先,让我们使用 numpy 库中的函数(即 **polynomial.hermite.poly2herm()**)创建 Hermite 级数。

import numpy as np
from numpy.polynomial import hermite as H
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 (polynomial to hermite)...\n",H.poly2herm(c))

输出

Our Array...
 [1 2 3 4 5]

Dimensions of our Array...
 1

Datatype of our Array object...
 int32

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

Result (polynomial to hermite)...
 [6.25   4.     4.5    0.5    0.3125]

示例

我们在 Numpy 库中有一个名为 divide() 的函数,用于将 Hermite 级数除以另一个 Hermite 级数。以下是一个示例。

import numpy as np
from numpy.polynomial import hermite as H
h = np.array([1, 2, 3, 4, 5])
print("Our Array...\n",h)
print("\nDimensions of our Array...\n",h.ndim)
print("\nDatatype of our Array object...\n",h.dtype)
print("\nShape of our Array object...\n",h.shape)
print("\nResult (polynomial to hermite)...\n",H.poly2herm(h))
divide_hermite = np.divide(h,h)
print("The output of dividing the hermite series by another:",divide_hermite)

输出

Our Array...
 [1 2 3 4 5]

Dimensions of our Array...
 1

Datatype of our Array object...
 int32

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

Result (polynomial to hermite)...
 [6.25   4.     4.5    0.5    0.3125]
The output of dividing the hermite series by another: [1. 1. 1. 1. 1.]

示例

让我们再看一个使用 numpy 库的 divide() 函数将一个 Hermite 级数除以另一个 Hermite 级数的示例。

import numpy as np
from numpy.polynomial import hermite as H
h1 = np.array([1, 2, 3, 4, 5])
h2 = np.arange(2,12,2)
print("Array1...\n",h1)
print("Array2...\n",h2)
print("\nDimensions of Array1...\n",h1.ndim)
print("\nDimensions of Array2...\n",h2.ndim)
print("\nDatatype of Array1 object...\n",h1.dtype)
print("\nDatatype of Array2 object...\n",h2.dtype)
print("\nShape of Array1 object...\n",h1.shape)
print("\nShape of Array2 object...\n",h2.shape)
print("\nResult (polynomial to hermite)...\n",H.poly2herm(h1))
print("\nResult (polynomial to hermite)...\n",H.poly2herm(h2))
divide_hermite = np.divide(h1,h2)
print("The output of dividing the hermite series by another:",divide_hermite)

输出

Array1...
 [1 2 3 4 5]
Array2...
 [ 2  4  6  8 10]

Dimensions of Array1...
 1

Dimensions of Array2...
 1

Datatype of Array1 object...
 int32

Datatype of Array2 object...
 int32

Shape of Array1 object...
 (5,)

Shape of Array2 object...
 (5,)

Result (polynomial to hermite)...
 [6.25   4.     4.5    0.5    0.3125]

Result (polynomial to hermite)...
 [12.5    8.     9.     1.     0.625]
The output of dividing the hermite series by another: [0.5 0.5 0.5 0.5 0.5]

示例

在以下示例中,我们使用 divide() 函数将一个一维数组除以一个二维 Hermite 级数数组,由于两个级数之间的系数维度不匹配,因此会引发错误。

import numpy as np
from numpy.polynomial import hermite as H
h1 = np.array([1, 2, 3, 4, 5])
h2 = np.arange(2,12).reshape(5,2)
print("Array1...\n",h1)
print("Array2...\n",h2)
print("\nDimensions of Array1...\n",h1.ndim)
print("\nDimensions of Array2...\n",h2.ndim)
print("\nDatatype of Array1 object...\n",h1.dtype)
print("\nDatatype of Array2 object...\n",h2.dtype)
print("\nShape of Array1 object...\n",h1.shape)
print("\nShape of Array2 object...\n",h2.shape)
print("\nResult (polynomial to hermite)...\n",H.poly2herm(h1))
print("\nResult (polynomial to hermite)...\n",H.poly2herm(h2))
divide_hermite = np.divide(h1,h2)
print("The output of dividing the hermite series by another:",divide_hermite)

输出

Array1...
 [1 2 3 4 5]
Array2...
 [[ 2  3]
 [ 4  5]
 [ 6  7]
 [ 8  9]
 [10 11]]

Dimensions of Array1...
 1

Dimensions of Array2...
 2

Datatype of Array1 object...
 int32

Datatype of Array2 object...
 int32

Shape of Array1 object...
 (5,)

Shape of Array2 object...
 (5, 2)

Result (polynomial to hermite)...
 [6.25   4.     4.5    0.5    0.3125]
Traceback (most recent call last):
  File "F:\test.py", line 14, in <module>
    print("\nResult (polynomial to hermite)...\n",H.poly2herm(h2))
                                                  ^^^^^^^^^^^^^^^
  File "C:\Users\Krishna\AppData\Roaming\Python\Python311\site-packages\numpy\polynomial\hermite.py", line 134, in poly2herm
    [pol] = pu.as_series([pol])
            ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Krishna\AppData\Roaming\Python\Python311\site-packages\numpy\polynomial\polyutils.py", line 134, in as_series
    raise ValueError("Coefficient array is not 1-d")
ValueError: Coefficient array is not 1-d

更新于: 2023年11月2日

67 次浏览

开启您的 职业生涯

通过完成课程获得认证

开始学习
广告