我在包含对称性的算法中看到的一个常见结构是
for (int i = 0; i < n ; i++) {
for (int j = i+1; j < n ; j++) {
[compute x]
objects[i][j] += x;
objects[j][i] -= x;
}
}
Run Code Online (Sandbox Code Playgroud)
这(虽然仍然具有 O(n^2) 复杂性)减少了利用对称性所需的计算量。您能告诉我在 pyspark 代码中引入这种优化的方法是什么吗?
例如,我编写了代码,根据公式(其中r是位置)计算作用在系统中每个粒子上的每单位质量的力:
N m_j*(r_i - r_j)
F = -G * ? -----------------
i!=j |r_i - r_j|^3
Run Code Online (Sandbox Code Playgroud)
在其中,我首先对我的数据帧与自身进行叉积以获得每个成对的相互作用,然后通过 id 将它们全部聚合以获得作用在每个粒子上的总力:
def calc_F(df_clust, G=1):
# cartesian product of the dataframe with itself
renameCols = [f"`{col}` as `{col}_other`" for col in df_clust.columns]
df_cart = …Run Code Online (Sandbox Code Playgroud)