1 #ifndef BARRAY_NETWORK_H
2 #define BARRAY_NETWORK_H 1
34 std::vector< double > vertex_attr_,
46 std::vector< std::vector< double > > vertex_attr_,
64 const std::vector< size_t > & indices_,
65 const std::vector< double > & numbers_
74 #define NET_C_DATA_IDX(i) (data.indices[i])
75 #define NET_C_DATA_NUM(i) (data.numbers[i])
85 #define BARRY_ZERO_NETWORK 0.0
86 #define BARRY_ZERO_NETWORK_DENSE 0
88 template <
typename Tnet = Network>
91 template <
typename Tnet = Network>
94 template <
typename Tnet = Network>
97 template <
typename Tnet = Network>
100 template <
typename Tnet>
103 template <
typename Tnet = Network>
106 template <
typename Tnet = Network>
114 #define NETWORK_COUNTER(a) \
115 template<typename Tnet = Network>\
116 inline double (a) (const Tnet & Array, size_t i, size_t j, NetCounterData & data)
119 #define NETWORK_COUNTER_LAMBDA(a) \
120 Counter_fun_type<Tnet, NetCounterData> a = \
121 [](const Tnet & Array, size_t i, size_t j, NetCounterData & data)
123 #define NETWORKDENSE_COUNTER_LAMBDA(a) \
124 Counter_fun_type<NetworkDense, NetCounterData> a = \
125 [](const NetworkDense & Array, size_t i, size_t j, NetCounterData & data)
133 #define NETWORK_RULE(a) \
134 template<typename Tnet = Network>\
135 inline bool (a) (const Tnet & Array, size_t i, size_t j, bool & data)
138 #define NETWORK_RULE_LAMBDA(a) \
139 Rule_fun_type<Tnet, bool> a = \
140 [](const Tnet & Array, size_t i, size_t j, bool & data)
151 template<
typename Tnet = Network>
161 count_edges,
nullptr,
nullptr,
174 template<
typename Tnet = Network>
187 if (Array.row(
i).size() == 1u && Array.col(
i).size() == 0u)
192 if (Array.row(
j).size() == 0u && Array.col(
j).size() == 1u)
201 return static_cast<double>(Array.nrow());
205 tmp_count, tmp_init,
nullptr,
208 "Number of isolate vertices"
227 if (Array.rowsum(
i) == 1u && Array.colsum(
i) == 0u)
231 if (Array.rowsum(
j) == 0u && Array.colsum(
j) == 1u)
240 return static_cast<double>(Array.nrow());
244 tmp_count, tmp_init,
nullptr,
246 "Isolates",
"Number of isolate vertices"
255 template<
typename Tnet = Network>
269 if (!Array.is_empty(
j,
i,
false))
283 if (Array.nrow() != Array.ncol())
284 throw std::logic_error(
"The -mutual- counter only works on square arrays.");
286 if (Array.D_ptr() ==
nullptr)
287 throw std::logic_error(
"The array data has not been initialized");
289 if (!Array.D_ptr()->directed)
290 throw std::logic_error(
291 "The -mutual- counter only works on directed (non-symmetric) arrays."
299 tmp_count, tmp_init,
nullptr,
302 "Number of mutual ties"
311 template<
typename Tnet = Network>
320 if (Array.col(
j).size() == 1u)
323 return static_cast<double>(Array.col(
j).size() - 1.0);
328 tmp_count,
nullptr,
nullptr,
359 return static_cast<double>(Array.colsum(
j) - 1);
364 tmp_count,
nullptr,
nullptr,
375 template<
typename Tnet = Network>
385 if (Array.row(
i).size() == 1u)
388 return static_cast<double>( Array.row(
i).size() - 1.0);
393 tmp_count,
nullptr,
nullptr,
423 return static_cast<double>(Array.rowsum(
i) - 1);
428 tmp_count,
nullptr,
nullptr,
440 template<
typename Tnet = Network>
454 if (Array.row(
j).size() < Array.row(
i).size())
457 for (
auto j_row = Array.row(
j).begin(); j_row != Array.row(
j).end(); ++j_row)
458 if ((
j != j_row->first) && (
i != j_row->first) && !Array.is_empty(
i, j_row->first,
false))
463 for (
auto i_row = Array.row(
i).begin(); i_row != Array.row(
i).end(); ++i_row)
464 if ((
i != i_row->first) && (i_row->first !=
j) && !Array.is_empty(
j, i_row->first,
false))
470 if (Array.row(
i).size() > Array.col(
j).size())
473 for (
auto j_col = Array.col(
j).begin(); j_col != Array.col(
j).end(); ++j_col)
474 if ((
j != j_col->first) && (
i != j_col->first) && !Array.is_empty(
i, j_col->first,
false))
479 for (
auto i_row = Array.row(
i).begin(); i_row != Array.row(
i).end(); ++i_row)
480 if ((
i != i_row->first) && (
j != i_row->first) && !Array.is_empty(i_row->first,
j,
false))
486 if (Array.col(
i).size() > Array.col(
j).size())
489 for (
auto j_col = Array.col(
j).begin(); j_col != Array.col(
j).end(); ++j_col)
490 if ((
j != j_col->first) && (
i != j_col->first) && !Array.is_empty(j_col->first,
i,
false))
495 for (
auto i_col = Array.col(
i).begin(); i_col != Array.col(
i).end(); ++i_col)
496 if ((
i != i_col->first) && (
j != i_col->first) && !Array.is_empty(i_col->first,
j,
false))
509 if (Array.D_ptr() ==
nullptr)
510 throw std::logic_error(
"The array data has not been initialized");
512 if (!(Array.D_ptr()->directed))
513 throw std::invalid_argument(
"The ttriads counter is only valid for directed networks. This is undirected.");
520 tmp_count, tmp_init,
nullptr,
523 "Number of directed triangles"
537 const auto & dat = Array.get_data();
538 size_t N = Array.nrow();
550 for (
size_t k = 0u; k < N; ++k)
557 if ((
j != k) & (
i != k))
586 if (Array.D_ptr() ==
nullptr)
587 throw std::logic_error(
"The array data has not been initialized");
589 if (!(Array.D_ptr()->directed))
590 throw std::invalid_argument(
"The ttriads counter is only valid for directed networks. This is undirected.");
597 tmp_count, tmp_init,
nullptr,
600 "Number of directed triangles"
609 template<
typename Tnet = Network>
620 if (Array.col(
i).size() < Array.row(
j).size())
623 for (
auto i_col = Array.col(
i).begin(); i_col != Array.col(
i).end(); ++i_col)
624 if ((
i != i_col->first) && (
j != i_col->first) && !Array.is_empty(
j, i_col->first,
false))
629 for (
auto j_row = Array.row(
j).begin(); j_row != Array.row(
j).end(); ++j_row)
630 if ((
j != j_row->first) && (
i != j_row->first) && !Array.is_empty(j_row->first,
i,
false))
642 if (Array.D_ptr() ==
nullptr)
643 throw std::logic_error(
"The array data has not been initialized");
645 if (!(Array.D_ptr()->directed))
646 throw std::invalid_argument(
647 "The ctriads counter is only valid for directed networks. This is undirected."
655 tmp_count, tmp_init,
nullptr,
676 #if defined(__OPENMP) || defined(_OPENMP)
677 #pragma omp simd reduction(+:ans)
679 for (
size_t k = 0u; k < Array.nrow(); ++k)
689 if (
i != k &&
j != k)
705 if (Array.D_ptr() ==
nullptr)
706 throw std::logic_error(
"The array data has not been initialized");
708 if (!(Array.D_ptr()->directed))
709 throw std::invalid_argument(
710 "The ctriads counter is only valid for directed networks. This is undirected."
718 tmp_count, tmp_init,
nullptr,
728 template<
typename Tnet = Network>
736 1.0/(Array.nrow() * (Array.ncol() - 1.0)) / (
737 (Array.D_ptr()->directed)? 1.0 : 2.0
745 tmp_count,
nullptr,
nullptr,
748 "Proportion of present ties"
756 template<
typename Tnet = Network>
764 if (Array.col(
j).size() == 1u)
768 pow(
static_cast<double> (Array.col(
j).size()), 1.5) -
769 pow(
static_cast<double> (Array.col(
j).size() - 1), 1.5)
775 tmp_count,
nullptr,
nullptr,
793 for (
size_t k = 0u; k < Array.nrow(); ++k)
809 double res = std::pow(
static_cast<double> (ideg), 1.5) -
810 std::pow(
static_cast<double> (ideg - 1.0), 1.5);
813 throw std::domain_error(
"Resulting indeg is undefined.");
816 std::pow(
static_cast<double> (ideg), 1.5) -
817 std::pow(
static_cast<double> (ideg - 1.0), 1.5)
823 tmp_count,
nullptr,
nullptr,
833 template<
typename Tnet = Network>
841 if (Array.row(
i).size() == 1u)
845 pow(
static_cast<double>(Array.row(
i).size()), 1.5) -
846 pow(
static_cast<double>(Array.row(
i).size() - 1), 1.5)
852 tmp_count,
nullptr,
nullptr,
870 for (
size_t k = 0u; k < Array.ncol(); ++k)
888 pow(
static_cast<double>(odeg), 1.5) -
889 pow(
static_cast<double>(odeg - 1), 1.5)
895 tmp_count,
nullptr,
nullptr,
907 template<
typename Tnet = Network>
917 return std::pow(std::fabs(
927 if (Array.D_ptr() ==
nullptr)
928 throw std::logic_error(
"The array data has not been initialized");
930 if (Array.D_ptr()->vertex_attr.size() == 0u)
931 throw std::range_error(
"No attributes in the Array.");
934 throw std::range_error(
"Attribute index out of range.");
941 tmp_count, tmp_init,
nullptr,
952 template<
typename Tnet = Network>
957 double tail_head =
true
973 if (Array.D_ptr() ==
nullptr)
974 throw std::logic_error(
"The array data has not been initialized");
976 if (Array.D_ptr()->vertex_attr.size() == 0u)
977 throw std::range_error(
"No attributes in the Array.");
980 throw std::range_error(
"Attribute index out of range.");
987 tmp_count, tmp_init,
nullptr,
989 "Absdiff^(" + std::to_string(alpha) +
")"
1000 if (Array.D_ptr() ==
nullptr)
1001 throw std::logic_error(
"The array data has not been initialized");
1003 if (Array.D_ptr()->vertex_attr.size() == 0u)
1004 throw std::range_error(
"No attributes in the Array.");
1007 throw std::range_error(
"Attribute index out of range.");
1015 template<
typename Tnet = Network>
1029 tmp_count, init_single_attr<Tnet>,
nullptr,
1031 "nodeicov",
"Sum of ego attribute"
1040 template<
typename Tnet = Network>
1054 tmp_count, init_single_attr<Tnet>,
nullptr,
1056 "nodeocov",
"Sum of alter attribute"
1065 template<
typename Tnet = Network>
1080 tmp_count, init_single_attr<Tnet>,
nullptr,
1082 "nodecov",
"Sum of nodes covariates"
1090 template<
typename Tnet = Network>
1110 tmp_count, init_single_attr<Tnet>,
nullptr,
1113 "Number of homophilic ties"
1122 template<
typename Tnet = Network>
1125 std::vector< size_t > d
1131 size_t d = Array.col(
j).size();
1144 if (Array.D_ptr() ==
nullptr)
1145 throw std::logic_error(
"The array data has not been initialized");
1147 if (!Array.D_ptr()->directed)
1148 throw std::logic_error(
"-odegree- counter is only valid for directed graphs");
1151 return static_cast<double>(Array.nrow());
1157 for (
auto iter = d.begin(); iter != d.end(); ++iter)
1159 tmp_count, tmp_init,
nullptr,
1161 "Nodes indeg " + std::to_string(*iter),
1162 "Number of nodes with indigree " + std::to_string(*iter)
1172 std::vector< size_t > d
1179 for (
size_t k = 0u; k < Array.nrow(); ++k)
1195 if (Array.D_ptr() ==
nullptr)
1196 throw std::logic_error(
"The array data has not been initialized");
1198 if (!Array.D_ptr()->directed)
1199 throw std::logic_error(
"-odegree- counter is only valid for directed graphs");
1202 return static_cast<double>(Array.nrow());
1208 for (
auto iter = d.begin(); iter != d.end(); ++iter)
1210 tmp_count, tmp_init,
nullptr,
1212 "Nodes indeg " + std::to_string(*iter),
1213 "Number of nodes with indigree " + std::to_string(*iter)
1222 template<
typename Tnet = Network>
1225 std::vector<size_t> d
1231 size_t d = Array.row(
i).size();
1244 if (Array.D_ptr() ==
nullptr)
1245 throw std::logic_error(
"The array data has not been initialized");
1247 if (!Array.D_ptr()->directed)
1248 throw std::logic_error(
"-odegree- counter is only valid for directed graphs");
1251 return static_cast<double>(Array.nrow());
1258 for (
auto iter = d.begin(); iter != d.end(); ++iter)
1260 tmp_count, tmp_init,
nullptr,
1262 "Nodes w/ outdeg " + std::to_string(*iter),
1263 "Number of nodes with outdegree " + std::to_string(*iter)
1273 std::vector<size_t> d
1280 for (
size_t k = 0u; k < Array.ncol(); ++k)
1296 if (Array.D_ptr() ==
nullptr)
1297 throw std::logic_error(
"The array data has not been initialized");
1299 if (!Array.D_ptr()->directed)
1300 throw std::logic_error(
"-odegree- counter is only valid for directed graphs");
1303 return static_cast<double>(Array.nrow());
1310 for (
auto iter = d.begin(); iter != d.end(); ++iter)
1312 tmp_count, tmp_init,
nullptr,
1314 "Nodes w/ outdeg " + std::to_string(*iter),
1315 "Number of nodes with outdegree " + std::to_string(*iter)
1325 template<
typename Tnet = Network>
1328 std::vector<size_t> d
1333 size_t d = Array.row(
i).size();
1344 if (Array.D_ptr() ==
nullptr)
1345 throw std::logic_error(
"The array data has not been initialized");
1347 if (Array.D_ptr()->directed)
1348 throw std::logic_error(
"-degree- counter is only valid for undirected graphs");
1351 return static_cast<double>(Array.nrow());
1357 for (
auto iter = d.begin(); iter != d.end(); ++iter)
1360 tmp_count, tmp_init,
nullptr,
1380 template<
typename Tnet = Network>
1400 #undef NET_C_DATA_IDX
1401 #undef NET_C_DATA_NUM
Baseline class for binary arrays.
Baseline class for binary arrays.
A counter function based on change statistics.
General framework for discrete exponential models. This class allows generating discrete exponential ...
Data class used to store arbitrary size_t or double vectors.
std::vector< size_t > indices
NetCounterData(const std::vector< size_t > &indices_, const std::vector< double > &numbers_)
std::vector< double > numbers
std::vector< std::vector< double > > vertex_attr
NetworkData(std::vector< double > vertex_attr_, bool directed_=true)
Constructor using a single attribute.
NetworkData(std::vector< std::vector< double > > vertex_attr_, bool directed_=true)
Constructor using multiple attributes.
Rule for determining if a cell should be included in a sequence.
Vector of objects of class Rule.
void add_rule(Rule< Array_Type, Data_Type > rule)
Count stats for a single Array.
Compute the support of sufficient statistics.
void counter_nodematch(NetCounters< Tnet > *counters, size_t attr_id)
void counter_idegree(NetCounters< Tnet > *counters, std::vector< size_t > d)
Counts number of vertices with a given in-degree.
void counter_istar2(NetCounters< Tnet > *counters)
void counter_mutual(NetCounters< Tnet > *counters)
Number of mutual ties.
void counter_ostar2(NetCounters< Tnet > *counters)
void counter_nodeicov(NetCounters< Tnet > *counters, size_t attr_id)
void counter_ttriads(NetCounters< Tnet > *counters)
void counter_odegree(NetCounters< Tnet > *counters, std::vector< size_t > d)
Counts number of vertices with a given out-degree.
void counter_density(NetCounters< Tnet > *counters)
void counter_ctriads(NetCounters< Tnet > *counters)
void counter_degree(NetCounters< Tnet > *counters, std::vector< size_t > d)
Counts number of vertices with a given out-degree.
void counter_isolates(NetCounters< Tnet > *counters)
Number of isolated vertices.
void counter_nodecov(NetCounters< Tnet > *counters, size_t attr_id)
void counter_edges(NetCounters< Tnet > *counters)
Number of edges.
void counter_diff(NetCounters< Tnet > *counters, size_t attr_id, double alpha=1.0, double tail_head=true)
Sum of attribute difference between ego and alter to pow(alpha)
void counter_absdiff(NetCounters< Tnet > *counters, size_t attr_id, double alpha=1.0)
Sum of absolute attribute difference between ego and alter.
void counter_nodeocov(NetCounters< Tnet > *counters, size_t attr_id)
void counter_odegree15(NetCounters< Tnet > *counters)
void counter_idegree15(NetCounters< Tnet > *counters)
#define NETWORKDENSE_COUNTER_LAMBDA(a)
#define BARRY_ZERO_NETWORK_DENSE
#define NETWORK_RULE_LAMBDA(a)
#define NETWORK_COUNTER_LAMBDA(a)
BArrayDense< int, NetworkData > NetworkDense
#define NET_C_DATA_IDX(i)
BArray< double, NetworkData > Network
#define NET_C_DATA_NUM(i)
#define NETWORK_COUNTER(a)
void rules_zerodiag(NetRules< Tnet > *rules)
Number of edges.