【问题标题】:Error: TypeError: can't multiply sequence by non-int of type 'numpy.float64' <Figure size 432x288 with 0 Axes> (don't know what to do)错误:TypeError:无法将序列乘以“numpy.float64”类型的非整数 <Figure size 432x288 with 0 Axes>(不知道该怎么做)
【发布时间】:2020-04-08 21:35:09
【问题描述】:

我正在尝试绘制一个包含定积分的方程。它是与二维量子环中的子带间跃迁相关的光电离截面。我分析地制作了角度部分,我试图用数值计算径向部分。

这是我在 Python 代码中实现这一点的尝试:

from scipy.integrate import quad
import numpy as np
from scipy.special import gamma
from scipy.constants import alpha
import matplotlib.pyplot as plt

#Constants
epsilon = 13.1 #dielectric constant of the material
gamma_C = 0.5 # donor impurity linewidth 
nr = 3.2 #refractive index of semiconductor
flux = 0  # Phi in eqn 8 magnetic flux
R = 5  #radius of the qunatum ring in nm
r = np.linspace(0, 6 * R)
rho = r / R
m_0 = 0.0067*0.511 # electron effective mass
h = 4.13e-15 # Planck constant in eV
hbar =  6.58e-16 # reduced Planck constant in eV
#Photon energy
hnu = np.linspace(0, 100) #in eV


#Function that calculates the integrand
def func(rho):
    betai = np.sqrt( gama**4/4)
    betaf = np.sqrt(1+gama**4/2)
    return ((gama * rho)**(betai + betaf) *
            np.exp(-1/2*(gama * rho)**2) 
         * (gama * rho)**2/2   ) 

def cross_section(hnu, gama):
    #function that calculates the photoionisation cross section
    betai = np.sqrt( gama**4/4)
    betaf = np.sqrt(1+gama**4/2)
    Ei = gama**2*(1+betai)-gama**4/2
    Ef = gama**2*(3+betaf)-gama**4/2
    return (nr/epsilon * 4*np.pi/3 * alpha * hnu *
            (abs(R * np.sqrt(1/2**betai*gamma(betai + 1))*
            np.sqrt(1/2**betaf*gamma(betaf + 1)) *
            quad(func, 0, np.infty))**2 * 
             hbar * gamma_C/(Ef - Ei - hnu)**2 + ( hbar * gamma_C)**2))

#Plot

plt.figure();plt.clf()

for gama in [1.0, 1.5, 2.0]:
    plt.plot(hnu, cross_section(hnu, gama))

但我不断收到此错误

TypeError: can't multiply sequence by non-int of type 'numpy.float64'

任何人都知道原因以及如何避免这种情况?

【问题讨论】:

  • 跟踪堆栈跟踪,运行调试器,如果仍然卡住,则制作 MCVE。
  • 每当您报告 Python 错误时,请在问题中包含 complete 回溯(即完整的错误消息)。那里有一些有用的信息可以帮助某人(包括您)找到问题的根源。

标签: python numpy scipy


【解决方案1】:

再看一下scipy.integrate.quad 的文档字符串。特别是,请查看“退货”部分。您会看到它返回多个值。更准确地说,它返回一个值元组。实际值的数量取决于参数full_output,但它始终包含至少两个值,即数值计算的积分和误差估计值。

在这段代码中

    return (nr/epsilon * 4*np.pi/3 * alpha * hnu *
            (abs(R * np.sqrt(1/2**betai*gamma(betai + 1))*
            np.sqrt(1/2**betaf*gamma(betaf + 1)) *
            quad(func, 0, np.infty))**2 * 
             hbar * gamma_C/(Ef - Ei - hnu)**2 + ( hbar * gamma_C)**2))

您使用quad 的返回值,但那是一个元组,因此它在该表达式中无法正常工作。要修复它,只需提取quad 返回的元组的第一个值。即用quad(func, 0, np.infty)[0]替换quad(func, 0, np.infty)

    return (nr/epsilon * 4*np.pi/3 * alpha * hnu *
            (abs(R * np.sqrt(1/2**betai*gamma(betai + 1))*
            np.sqrt(1/2**betaf*gamma(betaf + 1)) *
            quad(func, 0, np.infty)[0])**2 * 
             hbar * gamma_C/(Ef - Ei - hnu)**2 + ( hbar * gamma_C)**2))

【讨论】:

  • 值的顺序如文档字符串中所述。第一个是数值计算的积分,第二个是误差的估计。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-21
  • 1970-01-01
  • 1970-01-01
  • 2019-09-08
  • 2014-12-28
  • 1970-01-01
相关资源
最近更新 更多