libqe 0.2.5
QE C++ library
Loading...
Searching...
No Matches
adjacency.hpp
Go to the documentation of this file.
1
8#ifndef LIBQE_ADJACENCY_HPP
9#define LIBQE_ADJACENCY_HPP
10
11#include <armadillo>
12#include <string>
13#include <vector>
14#include <cmath>
15
16namespace qe {
17
18// ---------------------------------------------------------------------------
21// ---------------------------------------------------------------------------
22
29inline int choose_two(int n) {
30 return (n * (n - 1)) / 2;
31}
32
40inline int n_connections(int n, bool diagonal = false) {
41 return diagonal ? (n * (n + 1)) / 2 : choose_two(n);
42}
43
63inline arma::umat connection_indices(int len, int row = -1, bool diagonal = false) {
64 int vS = n_connections(len, diagonal);
65 int s = 0;
66
67 arma::umat vR(2, vS, arma::fill::zeros);
68 arma::umat vRone(1, vS, arma::fill::zeros);
69
70 for (int i = 1; i <= len; i++) {
71 for (int j = 0; j < (diagonal ? i : i - 1); j++) {
72 vR(0, s) = j;
73 vR(1, s) = i - 1;
74 if (row == 0) vRone[s] = j;
75 else if (row == 1) vRone[s] = i - 1;
76 s++;
77 }
78 }
79 return (row == -1) ? vR : vRone;
80}
81
83
84// ---------------------------------------------------------------------------
87// ---------------------------------------------------------------------------
88
99inline arma::rowvec code_connections(arma::mat v) {
100 int vL = v.size();
101 int vS = choose_two(vL);
102 arma::rowvec out(vS, arma::fill::zeros);
103 int s = 0;
104 for (int i = 2; i <= vL; i++) {
105 for (int j = 0; j < i - 1; j++) {
106 out[s] = v[j] * v[i - 1];
107 s++;
108 }
109 }
110 return out;
111}
112
127inline arma::rowvec fold_directed_network(arma::vec v, bool diagonal = false) {
128 int n = static_cast<int>(std::round(std::sqrt(static_cast<double>(v.size()))));
129
130 arma::mat m(v);
131 m.reshape(n, n);
132 arma::mat folded = m + m.t();
133 if (diagonal) folded.diag() = m.diag(); // a self-connection is not a pair
134
135 arma::colvec flat = folded.as_col();
136 arma::uvec up_inds = arma::trimatu_ind(arma::size(folded), diagonal ? 0 : 1);
137 return arma::conv_to<arma::rowvec>::from(flat.elem(up_inds));
138}
139
152inline arma::rowvec network_to_vector(arma::mat x, bool full = true, bool diagonal = false) {
153 if (full) return arma::vectorise(x).t();
154 if (diagonal) {
155 arma::uvec up_inds = arma::trimatu_ind(arma::size(x), 0);
156 return arma::vectorise(x(up_inds)).t();
157 }
158 arma::mat combined = arma::trimatu(x, 1) + arma::trimatl(x, -1);
159 arma::uvec up_inds = arma::trimatu_ind(arma::size(combined), 1);
160 return arma::vectorise(combined(up_inds)).t();
161}
162
164
165// ---------------------------------------------------------------------------
168// ---------------------------------------------------------------------------
169
182inline std::vector<std::string> connection_names(std::vector<std::string> v,
183 bool diagonal = false) {
184 int vL = v.size();
185 int vS = n_connections(vL, diagonal);
186 std::vector<std::string> out(vS);
187 int s = 0;
188 for (int i = 1; i <= vL; i++) {
189 for (int j = 0; j < (diagonal ? i : i - 1); j++) {
190 out[s] = v[j] + " & " + v[i - 1];
191 s++;
192 }
193 }
194 return out;
195}
196
198
199} // namespace qe
200
201#endif // LIBQE_ADJACENCY_HPP
Definition adjacency.hpp:16
int n_connections(int n, bool diagonal=false)
Number of undirected connection columns for n codes.
Definition adjacency.hpp:40
int choose_two(int n)
Number of unordered pairs from n items (n choose 2).
Definition adjacency.hpp:29
arma::rowvec fold_directed_network(arma::vec v, bool diagonal=false)
Fold a directed n² vector into an undirected upper-triangle vector.
Definition adjacency.hpp:127
std::vector< std::string > connection_names(std::vector< std::string > v, bool diagonal=false)
Generate "A & B" pair names for every upper-triangle position.
Definition adjacency.hpp:182
arma::rowvec network_to_vector(arma::mat x, bool full=true, bool diagonal=false)
Flatten an adjacency matrix to a vector.
Definition adjacency.hpp:152
arma::umat connection_indices(int len, int row=-1, bool diagonal=false)
Upper-triangle index pairs for a symmetric matrix of side len.
Definition adjacency.hpp:63
arma::rowvec code_connections(arma::mat v)
Compute pairwise products and return as a flat upper-triangle vector.
Definition adjacency.hpp:99