Apply dynamical function to each point in phase space (represented by a 2D matrix)

后端 未结 2 958
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-20 05:08

I have a matrix of integers, phase_space of shape (n,n), where each entry represents the number of points in that location in space. I also have tw

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-20 05:33

    You can use pandas.DataFrame.groupby() to accumulate all moves with same coordinates in phase_space:

    new_phase_space + (pd.DataFrame(phase_space)
               .stack()
               .groupby([u_x.ravel(), u_y.ravel()])
               .sum()
               .unstack(fill_value=0)
               .values
    )
    

    Output:

    array([[2., 2., 4.],
           [2., 0., 4.],
           [0., 4., 0.]])
    

提交回复
热议问题