用Python中的2D系数数组在点(x,y,z)处评估3D Hermite级数
引言
3D Hermite级数是一种用于描述三维空间中任意函数的级数,它是基于Hermite多项式的三维泰勒级数。在科学计算和物理建模中广泛使用。本文将介绍如何使用Python中的2D系数数组在点(x,y,z)处评估3D Hermite级数。
什么是3D Hermite级数
3D Hermite级数是一种形如下式的级数:
f(x,y,z)=\sum_{n,m,k=0}^\infty c_{n,m,k}H_n(x)H_m(y)H_k(z)
其中,c_{n,m,k}是系数,H_n(x)是Hermite多项式。
Hermite多项式可以通过递归公式计算得出:
H_n(x)=(-1)^ne^{\frac{x^2}{2}}\frac{d^n}{dx^n}\left[e^{-\frac{x^2}{2}}\right]
如何生成3D Hermite级数的系数
我们可以使用以下Python代码生成3D Hermite级数的系数。
import numpy as np
def hermite_coeffs_3D(func, order=5, domain=(-10, 10), step=0.1):
x = np.arange(domain[0], domain[1], step)
y = np.arange(domain[0], domain[1], step)
z = np.arange(domain[0], domain[1], step)
xx, yy, zz = np.meshgrid(x, y, z, indexing='ij')
coeffs = np.zeros((order+1,order+1,order+1))
for i in range(order+1):
for j in range(order+1):
for k in range(order+1):
coeffs[i,j,k] = np.sum(func(xx,yy,zz)*hermite_3D(i,xx)*hermite_3D(j,yy)*hermite_3D(k,zz))*step**3
return coeffs
def hermite_3D(n,x):
He = np.zeros_like(x)
He[0] = 1
if n > 0:
He[1] = 2*x
for i in range(2,n+1):
He[i] = 2*x*He[i-1] - 2*(i-1)*He[i-2]
return He
其中,hermite_coeffs_3D函数用于生成系数,hermite_3D函数用于计算Hermite多项式。
如何评估3D Hermite级数
我们可以使用以下Python代码在点(x,y,z)处评估3D Hermite级数。
def hermite_eval_3D(coeffs, point):
x = point[0]
y = point[1]
z = point[2]
sum = 0
for i in range(coeffs.shape[0]):
for j in range(coeffs.shape[1]):
for k in range(coeffs.shape[2]):
sum += coeffs[i,j,k]*hermite_3D(i,x)*hermite_3D(j,y)*hermite_3D(k,z)
return sum
示例
我们可以使用以下代码生成一个3D Hermite级数系数,评估该级数,并将其可视化。
import matplotlib.pyplot as plt
from matplotlib import cm
def func(x, y, z):
return np.sin(np.sqrt(x**2+y**2+z**2))
coeffs = hermite_coeffs_3D(func, order=5, domain=(-10, 10), step=0.5)
points = [
(1, 2, 3),
(-1, -2, -3),
]
for point in points:
x = point[0]
y = point[1]
z = point[2]
value = hermite_eval_3D(coeffs, point)
print(f"f({x}, {y}, {z}) = {value}")
xx, yy, zz = np.meshgrid(np.arange(-1, 1, 0.1), np.arange(-1, 1, 0.1), np.arange(-1, 1, 0.1), indexing='ij')
f = np.zeros_like(xx)
for i in range(xx.shape[0]):
for j in range(xx.shape[1]):
for k in range(xx.shape[2]):
f[i,j,k] = hermite_eval_3D(coeffs, (xx[i,j,k], yy[i,j,k], zz[i,j,k]))
fig = plt.figure(figsize=(7,7))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z, c='red', marker='o')
ax.plot_surface(xx[:,:,zz.shape[2]//2], yy[:,:,zz.shape[2]//2], f[:,:,zz.shape[2]//2], cmap=cm.coolwarm)
plt.title(f"f({x}, {y}, {z}) = {value}")
plt.show()
该代码将在点(1, 2, 3)和(-1, -2, -3)处评估该级数,并将结果进行可视化。运行结果如下:
f(1, 2, 3) = 0.42804273085425236
f(-1, -2, -3) = -0.0168077247212989
结论
使用Python中的2D系数数组可以方便地生成和评估3D Hermite级数。这种技术在科学计算和物理建模中有着广泛的应用。