请用数学公式表示:Q = (m_in / G.size()) - ((m_boundary / (2 * G.size())) ** 2)
时间: 2023-06-25 18:06:03 浏览: 25
$$Q = \frac{m_{in}}{|G|} - \left(\frac{m_{boundary}}{2|G|}\right)^2$$
其中,$Q$ 表示模块度,$m_{in}$ 表示模块内部边的数量,$m_{boundary}$ 表示模块内部边与其他模块之间的边的数量,$|G|$ 表示网络的总边数。
相关问题
# 计算社团集合的结构模块度 Q = (m_in / G.size()) - ((m_boundary / (2 * G.size())) ** 2)
这段代码实现了计算社团集合的结构模块度。其中m_in表示社团内部边的权重之和,m_boundary表示社团与外部节点相连的边的权重之和,G.size()表示整个图中所有边的权重之和。结构模块度是用来衡量社团划分的好坏程度,数值越大表示社团划分得越好。
具体来说,第一项 (m_in / G.size()) 表示社团内部边权重占整个图边权重的比例,越大说明社团内部联系紧密;第二项 ((m_boundary / (2 * G.size())) ** 2) 表示社团与外部节点相连的边权重占整个图边权重的比例,越小说明社团与外部联系越少,即社团内部更加独立。两项相减即为结构模块度。
需要注意的是,这段代码中的m_in和m_boundary需要根据实际情况进行计算,也可能需要考虑边的方向性等因素。
下列代码出现nan int错误,请解决:float smoothCot() { float err = -1; cogs.clear(); v_end = mesh.vertices_end(); // for (v_it = mesh.vertices_begin(); v_it != v_end; ++v_it) { cog[0] = cog[1] = cog[2] = valence = 0.0; //cout << valence<<"1" << endl; for (vv_it = mesh.vv_iter(*v_it); vv_it.is_valid(); ++vv_it) { double cot_weight = 0.0; MyMesh::HalfedgeHandle heh = mesh.find_halfedge(*v_it, *vv_it); if (!mesh.is_boundary(heh)) { MyMesh::HalfedgeHandle prev_heh = mesh.prev_halfedge_handle(heh); MyMesh::HalfedgeHandle next_heh = mesh.next_halfedge_handle(heh); MyMesh::VertexHandle prev_vh = mesh.to_vertex_handle(prev_heh); MyMesh::VertexHandle next_vh = mesh.to_vertex_handle(next_heh); MyMesh::Point prev_p = mesh.point(prev_vh); MyMesh::Point curr_pi = mesh.point(*v_it); MyMesh::Point curr_pj = mesh.point(*vv_it); MyMesh::Point next_p = mesh.point(next_vh); double cot_alpha = cot(curr_pi - prev_p, curr_pj - prev_p); double cot_beta = cot(curr_pi - next_p, curr_pj - next_p); cot_weight = cot_alpha + cot_beta; //cout << cot_weight<<"2" << endl; } cog += cot_weight *( mesh.point(*vv_it)-mesh.point(*v_it)); valence += cot_weight; //cout << valence<<"3" << endl; } cogs.push_back(cog / valence); } for (v_it = mesh.vertices_begin(), cog_it = cogs.begin(); v_it != v_end; ++v_it, ++cog_it) { if (!mesh.is_boundary(*v_it)) { MyMesh::Point p = mesh.point(*v_it); //*cog_it += mesh.point(*v_it); err = max(err, (p - *cog_it).norm()); mesh.set_point(*v_it, *cog_it); } } return err; }
在这段代码中,出现nan和inf通常是由于除以0或开根号时计算负数引起的。你可以在运行代码之前添加一些检查,比如检查cot_weight是否为0,以避免除以0的情况。另外,你还可以在计算cot_alpha和cot_beta之前,添加一个检查,确保两个点之间的距离不为0,以避免计算负数的情况。具体来说,你可以修改代码如下:
```
float smoothCot() {
float err = -1;
cogs.clear();
v_end = mesh.vertices_end();
for (v_it = mesh.vertices_begin(); v_it != v_end; ++v_it) {
cog[0] = cog[1] = cog[2] = valence = 0.0;
for (vv_it = mesh.vv_iter(*v_it); vv_it.is_valid(); ++vv_it) {
double cot_weight = 0.0;
MyMesh::HalfedgeHandle heh = mesh.find_halfedge(*v_it, *vv_it);
if (!mesh.is_boundary(heh)) {
MyMesh::HalfedgeHandle prev_heh = mesh.prev_halfedge_handle(heh);
MyMesh::HalfedgeHandle next_heh = mesh.next_halfedge_handle(heh);
MyMesh::VertexHandle prev_vh = mesh.to_vertex_handle(prev_heh);
MyMesh::VertexHandle next_vh = mesh.to_vertex_handle(next_heh);
MyMesh::Point prev_p = mesh.point(prev_vh);
MyMesh::Point curr_pi = mesh.point(*v_it);
MyMesh::Point curr_pj = mesh.point(*vv_it);
MyMesh::Point next_p = mesh.point(next_vh);
double dist1 = (curr_pi - prev_p).norm();
double dist2 = (curr_pj - prev_p).norm();
double dist3 = (curr_pi - next_p).norm();
double dist4 = (curr_pj - next_p).norm();
if (dist1 > 0 && dist2 > 0 && dist3 > 0 && dist4 > 0) {
double cot_alpha = cot(curr_pi - prev_p, curr_pj - prev_p);
double cot_beta = cot(curr_pi - next_p, curr_pj - next_p);
cot_weight = cot_alpha + cot_beta;
}
}
cog += cot_weight * (mesh.point(*vv_it) - mesh.point(*v_it));
valence += cot_weight;
}
cogs.push_back(cog / valence);
}
for (v_it = mesh.vertices_begin(), cog_it = cogs.begin(); v_it != v_end; ++v_it, ++cog_it) {
if (!mesh.is_boundary(*v_it)) {
MyMesh::Point p = mesh.point(*v_it);
err = max(err, (p - *cog_it).norm());
mesh.set_point(*v_it, *cog_it);
}
}
return err;
}
```