如何使用(1000,1)和索引n值创建(1000,500)数组?

时间:2019-03-07 16:45:20

标签: python arrays numpy multidimensional-array

我需要将变量phi,En和Cn设置为适当大小的数组。通过从Matlab到python的转换,我能够在Matlab中成功做到这一点。我将如何进行此计算。我基本上需要在n = 1时将整个x数组相乘,再次在n = 2,...,n = 500时,还要获得En和Cn的正确大小的数组。

def Gaussan_wave_packet():

    quantum_number = 500
    x = np.linspace(0,100,1000).astype(complex)
    x0 = 50, a = 10, l = 1
    A = (1/(4*a**2))**(1/4.0)
    m = 0.511*10**6 #mass
    hbar = 6.58211951*10**(-16)
    L = x[-1]

    #Gaussian wave packet
    psi_x0 = np.exp((-(x - x0)**2)/(4*a**2))*np.exp(1j*l*x)

    #Normalize wave function
    A = (1/(np.sqrt(np.trapz((np.conj(psi_x0)*psi_x0),x))))
    psi_x0_normalized = np.outer(psi_x0,A) # Makes a (1000,1) array

    phi_result  = np.array([])
    En_result = np.array([])
    Cn_result = np.array([])

    for n in range(0,quantum_number):

        phi = ( np.sqrt( 2/L ) * np.sin( ( n * x * np.pi )/L ) ) # Needs to be (1000,500)
        En = ( ( np.power(n,2))*(np.pi**2)*(hbar**2))/(2*m*L**2) # Needs to be (1,500)
        Cn = np.trapz( ( np.conj(phi) * psi_x0_normalized ), x ) # Needs to be (1,500)

1 个答案:

答案 0 :(得分:1)

您可以对np.multiply(a,b)使用按元素乘法。 并重塑x以便使用隐式扩展并避免for循环:

n = np.arange(quantum_number)
phi =  np.sqrt(2/L) * np.sin((np.multiply(n,x.reshape(1000,1)*np.pi)/L ))

您可以将相同的逻辑应用于EnCn

等效的matlab为:

n = 0:(quantum_number-1);
phi = (2/L)^0.5*sin(n.*x.'*pi/L);