Commit edf3eb7e authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Move mf_entropy() to C++

parent 34ee3d5a
......@@ -141,6 +141,32 @@ simple_degs_t copy_simple_degs(simple_degs_t& degs)
return degs;
}
double mf_entropy(GraphInterface& gi, boost::any opv)
{
double H=0;
run_action<>()
(gi,
[&](auto& g, auto pv)
{
for (auto v : vertices_range(g))
{
double sum = 0;
for (auto p : pv[v])
sum += p;
for (double p : pv[v])
{
if (p == 0)
continue;
p /= sum;
H -= p * log(p);
}
}
},
vertex_scalar_vector_properties())(opv);
return H;
}
boost::python::tuple bethe_entropy(GraphInterface& gi, size_t B, boost::any op,
boost::any opv)
{
......@@ -159,8 +185,6 @@ boost::python::tuple bethe_entropy(GraphInterface& gi, size_t B, boost::any op,
pv[v][i] = 0;
}
H = Hmf = sH = sHmf = 0;
for (auto e : edges_range(g))
{
auto u = min(source(e, g), target(e, g));
......@@ -311,6 +335,7 @@ void export_blockmodel_state()
class_<simple_degs_t>("simple_degs_t")
.def("copy", &copy_simple_degs);
def("mf_entropy", &mf_entropy);
def("bethe_entropy", &bethe_entropy);
def("init_q_cache", init_q_cache);
......
......@@ -1878,14 +1878,8 @@ def mf_entropy(g, p):
Physics, and Computation", Oxford Univ Press, 2009.
:DOI:`10.1093/acprof:oso/9780198570837.001.0001`
"""
H = 0
for v in g.vertices():
N = p[v].a.sum()
if N == 0:
continue
pvi = asarray(p[v].a, dtype="float") / N
pvi = pvi[pvi > 0]
H -= (pvi * log(pvi)).sum()
return H
return libinference.mf_entropy(g._Graph__graph,
_prop("v", g, p))
from . overlap_blockmodel import *
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment