gpt4 book ai didi

c++ - 逻辑帮助 : comparing values and taking the smallest distance, 同时从 "available to compare"列表中删除它

转载 作者:太空宇宙 更新时间:2023-11-04 12:07:44 26 4
gpt4 key购买 nike

好的,我的任务是使用一种方法 (IU) 比较此光子列表并将其与另一种方法 (TSP) 进行比较。我需要获取第一个 IU 光子并将距离与 所有 的 TSP 光子进行比较,找到最小距离,然后将它们“配对”(即将它们都设置在具有相同索引的数组中)。然后,我需要获取 IU 列表中的下一个光子,并将其与所有 TSP 光子进行比较,减去已经选择的光子。我知道我需要使用各种 bool 数组,并保留一个计数器。我似乎无法完全理解它。

下面的代码不是标准的 C++ 语法,因为它是为与 ROOT(CERN 数据分析软件)交互而编写的。如果您对语法有任何疑问以更好地理解代码,请提问。我会很乐意回答。

我已经声明了数组和变量。您看到的类型称为 EEmcParticleCandidate,它是一种从信息树中读取的类型,我有一整套类和 header 来指示其行为方式。

谢谢。

  Bool_t used[2];
if (num[0]==2 && num[1]==2) {
TIter photonIterIU(mPhotonArray[0]);
while(IU_photon=(EEmcParticleCandidate_t*)photonIterIU.Next()){
if (IU_photon->E > thresh2) {
distMin=1000.0;
index = 0;
IU_PhotonArray[index] = IU_photon;
TIter photonIterTSP(mPhotonArray[1]);
while(TSP_photon=(EEmcParticleCandidate_t*)photonIterTSP.Next()) {
if (TSP_photon->E > thresh2) {
Float_t Xpos_IU = IU_photon->position.fX;
Float_t Ypos_IU = IU_photon->position.fY;
Float_t Xpos_TSP = TSP_photon->position.fX;
Float_t Ypos_TSP = TSP_photon->position.fY;
distance_1 = find distance //formula didnt fit here //
if (distance_1 < distMin){
distMin = distance_1;;
for (Int_t i=0;i<2;i++){
used[i] = false;
} //for
used[index] = true;
TSP_PhotonArray[index] = TSP_photon;
index++;
} //if
} //if thresh
} // while TSP
} //if thresh
} // while IU

这就是我目前所拥有的...正在进行中,我意识到所有的牙套都没有闭合。这只是一道简单的逻辑题。

最佳答案

这可能需要几次迭代。

作为粒子物理学家,您应该了解将事物分解成其组成部分的重要性。让我们从遍历所有 TSP 光子开始。看起来好像相关代码在这里:

TIter photonIterTSP(mPhotonArray[1]);
while(TSP_photon=(EEmcParticleCandidate_t*)photonIterTSP.Next()) {
...
if(a certain condition is met)
TSP_PhotonArray[index] = TSP_photon;
}

因此 TSP_photon 是一个指针,您将把它复制到数组 TSP_PhotonArray 中(如果光子的能量超过固定阈值),然后您将转到跟踪哪些指针已经被复制了很多麻烦。有更好的方法,但现在我们只考虑寻找最佳匹配的问题:

distMin=1000.0;

while(TSP_photon= ... ) {
distance_1 = compute_distance_somehow();
if (distance_1 < distMin) {
distMin = distance_1;
TSP_PhotonArray[index] = TSP_photon; // <-- BAD
index++; // <-- VERY BAD
}
}

这是错误的。 假设您找到了一个距离最小的 TSP_photon。您还没有检查所有 TSP 光子,所以这可能不是最好的,但您无论如何都存储指针,并增加索引。然后,如果您发现另一个更好的匹配项,您也会存储那个匹配项。从概念上讲,它应该是这样的:

distMin=1000.0;
best_photon_yet = NULL;
while(TSP_photon= ... ) {
distance_1 = compute_distance_somehow();
if (distance_1 < distMin) {
distMin = distance_1;
best_pointer_yet = TSP_photon;
}
}
// We've now finished searching the whole list of TSP photons.
TSP_PhotonArray[index] = best_photon_yet;
index++;

对这个答案发表评论,告诉我这是否有意义;如果是,我们可以继续,如果不是,我会尽力澄清。

关于c++ - 逻辑帮助 : comparing values and taking the smallest distance, 同时从 "available to compare"列表中删除它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11295065/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com