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

inference: add constexpr

parent 4a8d6f3c
......@@ -131,7 +131,6 @@ public:
{
_empty_blocks.clear();
_candidate_blocks.clear();
_candidate_blocks.push_back(null_group);
for (auto r : vertices_range(_bg))
{
if (_wr[r] == 0)
......@@ -811,7 +810,7 @@ public:
if (_deg_corr)
{
get<1>(_degs[u].front())++;
if (graph_tool::is_directed(_g))
if constexpr (is_directed_::apply<g_t>::type::value)
get<0>(_degs[v].front())++;
else
get<1>(_degs[v].front())++;
......@@ -828,7 +827,7 @@ public:
if (_deg_corr)
{
get<1>(_degs[u].front())--;
if (graph_tool::is_directed(_g))
if constexpr (is_directed_::apply<g_t>::type::value)
get<0>(_degs[v].front())--;
else
get<1>(_degs[v].front())--;
......@@ -983,7 +982,7 @@ public:
if (t == u)
{
t = v;
if (!graph_tool::is_directed(_g))
if constexpr (!is_directed_::apply<g_t>::type::value)
{
assert(w % 2 == 0);
w /= 2;
......@@ -1022,7 +1021,7 @@ public:
}
}
if (graph_tool::is_directed(_g))
if constexpr (is_directed_::apply<g_t>::type::value)
{
ns_u.clear();
ns_v.clear();
......@@ -1183,7 +1182,7 @@ public:
size_t kout = out_degreeS()(v, _g, _eweight);
size_t kin = kout;
if (graph_tool::is_directed(_g))
if constexpr (is_directed_::apply<g_t>::type::value)
kin = in_degreeS()(v, _g, _eweight);
int dwr = _vweight[v];
......@@ -1195,7 +1194,7 @@ public:
auto vt = [&](auto mrp, auto mrm, auto nr)
{
assert(mrp >= 0 && mrm >=0 && nr >= 0);
if (exact)
if constexpr (exact)
return vterm_exact(mrp, mrm, nr, _deg_corr, _bg);
else
return vterm(mrp, mrm, nr, _deg_corr, _bg);
......@@ -1249,11 +1248,11 @@ public:
else
deltap[s] += _eweight[e];
}
if (!graph_tool::is_directed(_g))
if constexpr (!is_directed_::apply<g_t>::type::value)
deltal /= 2;
vector<int> deltam(num_vertices(_bg), 0);
if (graph_tool::is_directed(_g))
if (is_directed_::apply<g_t>::type::value)
{
for (auto e : in_edges_range(v, _g))
{
......@@ -1289,7 +1288,7 @@ public:
int ers = (r != null_group) ? get_beprop(r, s, _mrs, _emat) : 0;
int enrs = (nr != null_group) ? get_beprop(nr, s, _mrs, _emat) : 0;
if (!graph_tool::is_directed(_g))
if (!is_directed_::apply<g_t>::type::value)
{
if (s != nr && s != r)
{
......@@ -1727,10 +1726,10 @@ public:
// Sample node placement
size_t sample_block(size_t v, double c, double d, rng_t& rng)
{
// attempt random block
// attempt new block
size_t s;
std::bernoulli_distribution new_r(d);
if (d > 0 && new_r(rng) && (_candidate_blocks.size() - 1 < num_vertices(_g)))
if (d > 0 && new_r(rng) && (_candidate_blocks.size() < num_vertices(_g)))
{
if (_empty_blocks.empty())
add_block();
......@@ -1752,8 +1751,8 @@ public:
double p_rand = 0;
if (c > 0)
{
size_t B = _candidate_blocks.size() - 1;
if (graph_tool::is_directed(_g))
size_t B = _candidate_blocks.size();
if (is_directed_::apply<g_t>::type::value)
p_rand = c * B / double(_mrp[t] + _mrm[t] + c * B);
else
p_rand = c * B / double(_mrp[t] + c * B);
......@@ -1773,16 +1772,12 @@ public:
}
else
{
s = uniform_sample(_candidate_blocks.begin() + 1,
_candidate_blocks.end(),
rng);
s = uniform_sample(_candidate_blocks, rng);
}
}
else
{
s = uniform_sample(_candidate_blocks.begin() + 1,
_candidate_blocks.end(),
rng);
s = uniform_sample(_candidate_blocks, rng);
}
return s;
......@@ -1800,7 +1795,7 @@ public:
double get_move_prob(size_t v, size_t r, size_t s, double c, double d,
bool reverse, MEntries& m_entries)
{
size_t B = _candidate_blocks.size() - 1;
size_t B = _candidate_blocks.size();
if (reverse)
{
......@@ -1828,13 +1823,13 @@ public:
size_t kout = 0, kin = 0;
degs_op(v, _vweight, _eweight, _degs, _g,
[&] (size_t din, size_t dout, auto c)
[&] ([[maybe_unused]] size_t din, size_t dout, auto c)
{
kout += dout * c;
if (graph_tool::is_directed(this->_g))
if constexpr (is_directed_::apply<g_t>::type::value)
kin += din * c;
});
if (!graph_tool::is_directed(_g))
if constexpr (!is_directed_::apply<g_t>::type::value)
kin = kout;
m_entries.get_mes(_emat);
......@@ -1855,7 +1850,7 @@ public:
int mst = mts;
int mtm = mtp;
if (graph_tool::is_directed(_g))
if constexpr (is_directed_::apply<g_t>::type::value)
{
mst = 0;
const auto& me = m_entries.get_me(s, t, _emat);
......@@ -1868,7 +1863,7 @@ public:
{
int dts = m_entries.get_delta(t, s);
int dst = dts;
if (graph_tool::is_directed(_g))
if constexpr (is_directed_::apply<g_t>::type::value)
dst = m_entries.get_delta(s, t);
mts += dts;
......@@ -1887,7 +1882,7 @@ public:
}
}
if (graph_tool::is_directed(_g))
if constexpr (is_directed_::apply<g_t>::type::value)
{
p += ew * ((mts + mst + c) / (mtp + mtm + c * B));
}
......@@ -1907,7 +1902,7 @@ public:
sum_prob(e, target(e, _g));
}
if (graph_tool::is_directed(_g))
if constexpr (is_directed_::apply<g_t>::type::value)
{
for (auto e : in_edges_range(v, _g))
{
......@@ -2131,7 +2126,7 @@ public:
auto& m = uc.second;
if (m > 1)
{
if (u == v && !graph_tool::is_directed(_g))
if (u == v && !is_directed_::apply<g_t>::type::value)
{
assert(m % 2 == 0);
S += lgamma_fast(m/2 + 1) + m * log(2) / 2;
......@@ -2181,7 +2176,7 @@ public:
}
else
{
if (!graph_tool::is_directed(this->_g))
if constexpr (!is_directed_::apply<g_t>::type::value)
{
if (Add)
degs[1] = {kin, kout + 2};
......@@ -2210,7 +2205,7 @@ public:
[&] (size_t kin, size_t kout, auto)
{
degs[0] = {kin, kout};
if (!graph_tool::is_directed(this->_g))
if constexpr (!is_directed_::apply<g_t>::type::value)
{
if (Add)
degs[1] = {kin, kout + 1};
......@@ -2250,7 +2245,7 @@ public:
{
degs[2] = {kin, kout};
if (!graph_tool::is_directed(this->_g))
if constexpr (!is_directed_::apply<g_t>::type::value)
{
if (Add)
degs[3] = {kin, kout + 1};
......
......@@ -34,7 +34,7 @@ template <class Edge, class Graph>
inline typename graph_traits<Graph>::vertex_descriptor
get_source(const Edge& e, const Graph &g)
{
if (graph_tool::is_directed(g))
if constexpr (is_directed_::apply<Graph>::type::value)
return source(e, g);
return std::min(source(e, g), target(e, g));
}
......@@ -43,7 +43,7 @@ template <class Edge, class Graph>
inline typename graph_traits<Graph>::vertex_descriptor
get_target(const Edge& e, const Graph &g)
{
if (graph_tool::is_directed(g))
if constexpr (is_directed_::apply<Graph>::type::value)
return target(e, g);
return std::max(source(e, g), target(e, g));
}
......@@ -221,7 +221,7 @@ public:
// update the half-edge lists
for (auto e : out_edges_range(v, g))
remove_edge(e, b, g);
if (graph_tool::is_directed(g))
if constexpr (is_directed_::apply<Graph>::type::value)
{
for (auto e : in_edges_range(v, g))
remove_edge(e, b, g);
......@@ -236,7 +236,7 @@ public:
//update the half-edge lists
for (auto e : out_edges_range(v, g))
insert_edge(e, eweight[e], b, g);
if (graph_tool::is_directed(g))
if constexpr (is_directed_::apply<Graph>::type::value)
{
for (auto e : in_edges_range(v, g))
insert_edge(e, eweight[e], b, g);
......
......@@ -131,7 +131,7 @@ public:
{
_r_out_field.resize(B, _null);
_nr_out_field.resize(B, _null);
if (is_directed_::apply<Graph>::type::value)
if constexpr (is_directed_::apply<Graph>::type::value)
{
_r_in_field.resize(B, _null);
_nr_in_field.resize(B, _null);
......@@ -146,7 +146,7 @@ public:
{
_r_out_field.resize(B, _null);
_nr_out_field.resize(B, _null);
if (is_directed_::apply<Graph>::type::value)
if constexpr (is_directed_::apply<Graph>::type::value)
{
_r_in_field.resize(B, _null);
_nr_in_field.resize(B, _null);
......@@ -160,7 +160,7 @@ public:
size_t& get_field_rnr(size_t s, size_t t)
{
auto& out_field = First ? _r_out_field : _nr_out_field;
if (is_directed_::apply<Graph>::type::value)
if constexpr (is_directed_::apply<Graph>::type::value)
{
auto& in_field = (First ? _r_in_field : _nr_in_field);
return (Source || s == t) ? out_field[t] : in_field[s];
......@@ -192,7 +192,7 @@ public:
f = _entries.size();
_entries.emplace_back(s, t);
_delta.emplace_back();
if (sizeof...(delta) > 0)
if constexpr (sizeof...(delta) > 0)
_edelta.emplace_back();
}
......@@ -315,7 +315,7 @@ void modify_entries(Vertex v, Vertex r, Vertex nr, Vprop& _b, Graph& g,
typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
auto& eself_weight = m_entries._self_eweight;
int self_weight = 0;
if (!graph_tool::is_directed(g) && sizeof...(Eprops) > 0)
if constexpr (!is_directed_::apply<Graph>::type::value && sizeof...(Eprops) > 0)
{
tuple_apply([&](auto&... vals)
{
......@@ -350,7 +350,7 @@ void modify_entries(Vertex v, Vertex r, Vertex nr, Vprop& _b, Graph& g,
(nr, s, ew, make_vadapter(eprops, e)...);
}
if ((u == v || is_loop(v)) && !graph_tool::is_directed(g))
if ((u == v || is_loop(v)) && !is_directed_::apply<Graph>::type::value)
{
self_weight += ew;
tuple_op(eself_weight, [&](auto&& x, auto&& val){ x += val; },
......@@ -358,9 +358,9 @@ void modify_entries(Vertex v, Vertex r, Vertex nr, Vprop& _b, Graph& g,
}
}
if (self_weight > 0 && self_weight % 2 == 0 && !graph_tool::is_directed(g))
if (self_weight > 0 && self_weight % 2 == 0 && !is_directed_::apply<Graph>::type::value)
{
if (sizeof...(Eprops) > 0)
if constexpr (sizeof...(Eprops) > 0)
{
tuple_apply([&](auto&... vals)
{
......@@ -378,16 +378,16 @@ void modify_entries(Vertex v, Vertex r, Vertex nr, Vprop& _b, Graph& g,
}
else
{
if (Add)
if constexpr (Add)
m_entries.template insert_delta_rnr<false, true, false>
(nr, nr, self_weight / 2);
if (Remove)
if constexpr (Remove)
m_entries.template insert_delta_rnr<true, true, true>
(r, r, self_weight / 2);
}
}
if (graph_tool::is_directed(g))
if constexpr (is_directed_::apply<Graph>::type::value)
{
for (auto e : in_edges_range(v, g))
{
......@@ -399,10 +399,10 @@ void modify_entries(Vertex v, Vertex r, Vertex nr, Vprop& _b, Graph& g,
vertex_t s = _b[u];
int ew = eweights[e];
if (Remove)
if constexpr (Remove)
m_entries.template insert_delta_rnr<true, false, false>
(s, r, ew, make_vadapter(eprops, e)...);
if (Add)
if constexpr (Add)
{
if (s != r)
m_entries.template insert_delta_rnr<false, false, true>
......@@ -496,7 +496,7 @@ double entries_dS(MEntries& m_entries, Eprop& mrs, EMat& emat, BGraph& bg)
if (me != emat.get_null_edge())
ers = mrs[me];
assert(int(ers) + d >= 0);
if (exact)
if constexpr (exact)
dS += eterm_exact(r, s, ers + d, bg) - eterm_exact(r, s, ers, bg);
else
dS += eterm(r, s, ers + d, bg) - eterm(r, s, ers, bg);
......
......@@ -56,39 +56,38 @@ struct entropy_args_t
// exact microcanonical deg-corr entropy
template <class Graph>
inline double eterm_exact(size_t r, size_t s, size_t mrs, const Graph& g)
inline double eterm_exact(size_t r, size_t s, size_t mrs, const Graph&)
{
double val = lgamma_fast(mrs + 1);
if (graph_tool::is_directed(g) || r != s)
if (is_directed_::apply<Graph>::type::value || r != s)
{
return -val;
}
else
{
#ifndef __clang__
constexpr double log_2 = log(2);
#else
const double log_2 = log(2);
constexpr
#endif
double log_2 = log(2);
return -val - mrs * log_2;
}
}
template <class Graph>
inline double vterm_exact(size_t mrp, size_t mrm, size_t wr, bool deg_corr,
const Graph& g)
const Graph&)
{
if (deg_corr)
{
if (graph_tool::is_directed(g))
if constexpr (is_directed_::apply<Graph>::type::value)
return lgamma_fast(mrp + 1) + lgamma_fast(mrm + 1);
else
return lgamma_fast(mrp + 1);
}
else
{
if (graph_tool::is_directed(g))
if constexpr (is_directed_::apply<Graph>::type::value)
return (mrp + mrm) * safelog_fast(wr);
else
return mrp * safelog_fast(wr);
......@@ -97,14 +96,14 @@ inline double vterm_exact(size_t mrp, size_t mrm, size_t wr, bool deg_corr,
// "edge" term of the entropy
template <class Graph>
inline double eterm(size_t r, size_t s, size_t mrs, const Graph& g)
inline double eterm(size_t r, size_t s, size_t mrs, const Graph& )
{
if (!graph_tool::is_directed(g) && r == s)
if (!is_directed_::apply<Graph>::type::value && r == s)
mrs *= 2;
double val = xlogx_fast(mrs);
if (graph_tool::is_directed(g) || r != s)
if (is_directed_::apply<Graph>::type::value || r != s)
return -val;
else
return -val / 2;
......@@ -113,11 +112,11 @@ inline double eterm(size_t r, size_t s, size_t mrs, const Graph& g)
// "vertex" term of the entropy
template <class Graph>
inline double vterm(size_t mrp, size_t mrm, size_t wr, bool deg_corr,
Graph& g)
Graph& )
{
double one = 0.5;
if (graph_tool::is_directed(g))
if constexpr (is_directed_::apply<Graph>::type::value)
one = 1;
if (deg_corr)
......
......@@ -68,14 +68,14 @@ public:
{
if (!self_loops)
continue;
if (!graph_tool::is_directed(g))
if constexpr (!is_directed_::apply<Graph>::type::value)
w /= 2;
}
insert(v, u, w, e);
}
if (graph_tool::is_directed(g))
if constexpr (is_directed_::apply<Graph>::type::value)
{
for (auto e : in_edges_range(v, g))
......@@ -112,14 +112,14 @@ public:
{
if (!self_loops)
continue;
if (!graph_tool::is_directed(g))
if constexpr (!is_directed_::apply<Graph>::type::value)
w /= 2;
}
us.emplace_back(u, 0);
probs.push_back(w);
}
if (graph_tool::is_directed(g))
if constexpr (is_directed_::apply<Graph>::type::value)
{
for (auto e : in_edges_range(v, g))
{
......
......@@ -86,7 +86,10 @@ double q_rec_memo(int n, int k)
double log_q_approx_big(size_t n, size_t k)
{
constexpr double C = M_PI * sqrt(2/3.);
#ifndef __clang__
constexpr
#endif
double C = M_PI * sqrt(2/3.);
double S = C * sqrt(n) - log(4 * sqrt(3) * n);
if (k < n)
{
......
Supports Markdown
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