[Python] Is there a way to specify which element of arrays I want to add via indexation?

Discussão em 'Python' iniciado por Stack, Fevereiro 13, 2025.

  1. Stack

    Stack Membro Participativo

    massetest = np.array([12,15])
    xtest = np.array([34,30])
    ytest = np.array([1,0])
    ztest = np.array([7,1])
    rtest = np.array([xtest,ytest,ztest])
    rtest = rtest.T

    def integrateur(r,v,a,step):
    v = v + a * step/2
    r = r + v *step
    a = acceleration(massetest,rtest,1)
    v = v + a * (step/2)
    v = v+ a * (step/2)
    return r, v, a

    v_r = np.ones((2,3))
    acc = np.ones((2,3))

    def acceleration(masse,r,G):
    x = r[:,0:1]
    y = r[:,1:2]
    z = r[:,2:3]

    dpos_x = x.T - x
    dpos_y = y.T - y
    dpos_z = z.T - z

    nc = ( dpos_x**2 + dpos_y**2 + dpos_z**2)**(1.5)

    ax = G*(dpos_x * masse)/nc
    ay = G*(dpos_y * masse)/nc
    az = G*(dpos_z * masse)/nc

    a = np.array([ax,ay,az])
    return a


    a's shape is (3,2,2)
    v's shape is (2,3)

    I realize that I can't add them because of their shape but I can't seem to find a work around.

    This is the error I get:

    ValueError: operands could not be broadcast together with shapes (2,3) (3,2,2)


    I tried to add only certain parts of a with v but I can't really access the ones I need and well I don't really end up with the values that are needed and that creates errors of their own :( kinda lost ngl.

    I am trying to compute an n-body simulation but I am currently trialing with only 2 particules using the leap-frog integration method which is supposed to conserve the system's total energy.

    Continue reading...

Compartilhe esta Página