|
libtma 0.3.13-dev.9013
QE C++ library
|
Namespaces | |
| namespace | bind |
Classes | |
| struct | TensorNetworks |
| Result of apply_tensor_unit(). More... | |
| struct | UnitAccumulation |
| Result of accumulate_unit_networks(): a unit's finished vectors plus its raw per-line rows. More... | |
| struct | UnitNetworks |
| Result of accumulate_unit_with_rows(). More... | |
Functions | |
| arma::mat | finalize_row_connections (const arma::mat &row_conn, int n_codes, bool ordered, WeightModel weight, bool diagonal=false) |
| Finalise each per-response-row tensor connection vector. | |
| arma::rowvec | aggregate_row_connections (const arma::mat &row_conn, int n_codes, bool ordered, WeightModel weight, bool diagonal=false) |
| Aggregate per-response-row tensor connections into a unit vector. | |
| arma::rowvec | aggregate_row_connections (const arma::mat &row_conn, int n_codes, bool ordered=false, bool binary=true) |
Legacy form: binary selects Binary (true) or Product (false). | |
| arma::mat | finalize_self_connections (const arma::mat &row_conn, int n_codes, bool ordered, WeightModel weight) |
| Per-line self-connections: the diagonal of each directed row. | |
| arma::rowvec | aggregate_self_connections (const arma::mat &row_conn, int n_codes, bool ordered, WeightModel weight) |
| Unit self-connection vector: finalize_self_connections() summed over the lines. | |
| arma::mat | weight_rows (const arma::mat &rows, bool ordered, WeightModel weight) |
| Apply the weight model per line to already-finalised rows. | |
Core adjacency math | |
| arma::mat | connection_matrix (arma::rowvec ground, arma::rowvec response, double response_weight=1.0, bool ordered=true) |
| Compute the connection matrix for one ground + response event pair. | |
Traditional stanza-window accumulation (rENA model) | |
| arma::mat | accumulate_stanza (arma::mat codes, int window_back=1, int window_forward=0, bool binary=true, bool ordered=false) |
| Accumulate connection vectors for every row in one conversation using a stanza window. | |
Ground/response accumulation (tma model) | |
| arma::rowvec | accumulate_unit (const arma::mat &codes, const std::vector< int > &unit_rows, std::function< arma::vec(arma::vec)> decay_fn, bool ordered=false) |
| Accumulate connections for a single unit from its context rows. | |
Extended ground/response accumulation — returns per-row connection data | |
| UnitNetworks | accumulate_unit_with_rows (const arma::mat &codes, const std::vector< int > &unit_rows, std::function< arma::vec(int, arma::uvec)> decay_fn, bool ordered=false) |
Like accumulate_unit() but also returns the per-response-row connection matrix needed by tma's accumulate_network(). | |
Tensor-based multi-modal accumulation (tma model) | |
| int | flat_index (const std::vector< int > &indices, const std::vector< int > &dims) |
| Compute the linear index into a column-major multi-dimensional array. | |
| arma::mat | miss_matrix (const arma::rowvec &ground, const arma::rowvec &response) |
| Missed co-occurrences for one ground + response event pair. | |
| TensorNetworks | apply_tensor_unit (const arma::vec &tensor, const std::vector< int > &dims, const std::vector< int > &dims_sender, const std::vector< int > &dims_receiver, const std::vector< int > &dims_mode, const arma::imat &context_lookup, const std::vector< int > &unit_rows, const arma::mat &codes, const arma::vec ×, bool ordered=true, bool return_members=false, bool return_misses=false) |
Pure-C++ port of the tma apply_tensor() inner logic. | |
Finished unit networks (one call per unit, for every binding) | |
| UnitAccumulation | accumulate_unit_networks (const arma::vec &tensor, const std::vector< int > &dims, const std::vector< int > &dims_sender, const std::vector< int > &dims_receiver, const std::vector< int > &dims_mode, const arma::imat &context_lookup, const std::vector< int > &unit_rows, const arma::mat &codes, const arma::vec ×, bool ordered=false, WeightModel weight=WeightModel::Binary, bool misses=true, bool diagonal=false) |
| Accumulate one unit's networks end to end. | |
Per-row co-occurrence and rolling window (rENA accumulation primitives) | |
| arma::mat | row_connections (const arma::mat &codes, bool binary=true) |
| Compute the upper-triangle co-occurrence vector for each row. | |
| arma::mat | rolling_window_sum (const arma::mat &codes, int window_size=1) |
| Rolling backward window sum of the raw code matrix. | |
Weight models (per-line co-occurrence transforms, = rENA's weight.by) | |
| enum class | WeightModel { Binary , Product , Sqrt , Log1p } |
| Transform applied to each line's connection counts before the per-unit sum. More... | |
| WeightModel | weight_model_from_string (const std::string &name) |
| Parse a weight-model name (case-insensitive). | |
| WeightModel | weight_model_from_bool (bool binary) |
Map the legacy binary flag to a weight model (true → Binary, false → Product). | |
| void | apply_weight_model (arma::rowvec &v, WeightModel weight, bool ordered) |
| Apply a weight model in place to one line's connection vector. | |
|
strong |
Transform applied to each line's connection counts before the per-unit sum.
Every model applies its weight at the same stage: after a response row's connection counts are computed (and, for unordered networks, folded to the upper triangle), and before rows are summed into the unit network. This is the stage legacy rENA applies weight.by (accumulate.data.R). Because sqrt(Σ) ≠ Σ sqrt, the transform must never be applied to unit totals.
Binary — unordered: clamp each positive count to 1 (presence). Ordered: raw directed counts, unchanged — the established behaviour of every ordered/ONA model (tma ignores binary).Product — the non-binarized counts themselves ("summed cross
products" of the response row with its window).Sqrt — sqrt of each line's product count.Log1p — log(1 + x) of each line's product count. | Enumerator | |
|---|---|
| Binary | |
| Product | |
| Sqrt | |
| Log1p | |
|
inline |
Accumulate connection vectors for every row in one conversation using a stanza window.
For each focal row k in codes the function assembles a ground context from the surrounding window and calls connection_matrix().
ordered = false, default — rENA stanza model): the window spans [k - window_back, k + window_forward]; the upper-triangle outer-product (code_connections) is computed and back/forward-reference corrections are subtracted. Returns a matrix of shape n_rows × choose_two(n_codes).ordered = true): focal row k is the response; the sum of prior rows [earliest, k−1] is the ground. connection_matrix() is called with ordered = true and the result is vectorised. window_forward is ignored (future rows cannot be causal ground context). Returns a matrix of shape n_rows × n_codes².| [in] | codes | Code matrix for one conversation (n_rows × n_codes). |
| [in] | window_back | Number of prior rows included in the window (1 = current row only; INT_MAX = all prior rows). |
| [in] | window_forward | Number of future rows included in the window (ignored when ordered is true). |
| [in] | binary | When true, clamp all positive entries to 1. |
| [in] | ordered | When true, use directed accumulation; when false, use undirected stanza-window accumulation. |
ref_window_df() in rENA/ena.cpp (undirected case).
|
inline |
Accumulate connections for a single unit from its context rows.
For every response row belonging to this unit, the function computes a weighted ground vector from all preceding context rows and calls connection_matrix().
ordered is false (default), that directed sum is folded once into the upper-triangle representation (length choose_two(p)); when true, the directed p² vector is returned. (Building the undirected connection_matrix() and then folding it would count every connection twice.)| [in] | codes | Full context matrix visible to this unit (n × p). |
| [in] | unit_rows | 0-based indices of the rows that belong to this unit. |
| [in] | decay_fn | Callback with signature arma::vec(arma::vec distances) that maps a vector of distances (0 = current row, 1 = one step back, …) to a vector of scalar weights; default behaviour is a simple rectangular window. |
| [in] | ordered | When true, return a directed flat vector (length p²); when false, return an undirected upper-triangle flat vector (length choose_two(p)). |
accumulate_network() in tma/code.cpp, with the R function callback replaced by std::function.
|
inline |
Accumulate one unit's networks end to end.
Runs apply_tensor_unit() for the directed per-line rows (and misses), then finalises them in one place: folds unordered rows, applies the weight model per line, sums the lines, and pulls out the self-connection diagonal – the steps every binding would otherwise repeat. Unit vectors are additive over lines, so a unit spread over several contexts is the sum of its per-context results.
Arguments as apply_tensor_unit(), plus:
| [in] | ordered | true: directed p² networks; false: folded choose_two(p). |
| [in] | weight | Per-line weight model (see WeightModel). |
| [in] | misses | Also accumulate missed co-occurrences and self-misses. |
| [in] | diagonal | Unordered only: self-connections (and self-misses) are also columns of the connection (and miss) vectors, in the A & A, A & B, B & B, ... layout. Ordered networks always carry them. The separate self vectors are returned either way. Default false. |
|
inline |
Like accumulate_unit() but also returns the per-response-row connection matrix needed by tma's accumulate_network().
| [in] | codes | Full context matrix visible to this unit (n × p). |
| [in] | unit_rows | 0-based indices of the rows that belong to this unit. |
| [in] | decay_fn | Callback with signature arma::vec(int unit_row, arma::uvec ground_indices) that returns a weight vector of length ground_indices.n_elem. The two-argument form lets callers (e.g. the tma Rcpp wrapper) set R environment variables before invoking the actual R decay function, without any R-specific code leaking into libqe. |
| [in] | ordered | When true, networks is the directed flat vector (length p²); when false, the directed sum folded once to the undirected upper triangle (length choose_two(p)). row_networks are always directed. |
accumulate_network() in tma/code.cpp (extended form).
|
inline |
Aggregate per-response-row tensor connections into a unit vector.
Finalises each response row (fold + weight model, see finalize_row_connections()) and sums the rows — tma's R aggregation (as.unordered + colSums.ena.matrix), with the weight model applied at the same per-line stage legacy rENA applies weight.by.
This is the aggregation step tma performs in R; libqe's apply_tensor_unit intentionally returns the raw per-row matrix so callers can compose it. Kept as a standalone kernel function so the fold/weight/sum semantics live in one place shared by every binding, instead of being re-implemented in each wrapper layer.
| [in] | row_conn | Per-response-row directed connection matrix (n_response_rows × p²). |
| [in] | n_codes | Number of codes p. |
| [in] | ordered | true: directed p² sums; false: fold to choose_two(p) and sum. |
| [in] | weight | Per-line weight model (see WeightModel). |
| [in] | diagonal | Unordered only: keep self-connections (see finalize_row_connections()). Default false. |
n_connections(p, diagonal) (unordered).
|
inline |
Legacy form: binary selects Binary (true) or Product (false).
Unchanged behaviour for existing callers: ordered rows are summed raw (no binarization); unordered rows are folded and, when binary, clamped to presence before summing.
|
inline |
Unit self-connection vector: finalize_self_connections() summed over the lines.
p.
|
inline |
Pure-C++ port of the tma apply_tensor() inner logic.
For each response row in unit_rows, the function looks up window and weight values from the tensor, collects all ground rows that fall within the response row's window, and calls connection_matrix() to accumulate the connection counts.
| [in] | tensor | Flat column-major array containing window and weight values indexed by the factor dimensions. |
| [in] | dims | Sizes of each dimension of tensor. |
| [in] | dims_sender | Tensor axis indices corresponding to sender factors. |
| [in] | dims_receiver | Tensor axis indices corresponding to receiver factors; these axes are overridden to the response row's values when looking up window sizes for ground rows. |
| [in] | dims_mode | Tensor axis indices corresponding to mode factors. |
| [in] | context_lookup | Integer matrix (n_context_rows × n_factors, 0-based) mapping each context row to its factor level indices. |
| [in] | unit_rows | 0-based response-row indices for this unit. |
| [in] | codes | Full context code matrix (n_context_rows × n_codes). |
| [in] | times | Timestamp associated with each context row (length n_context_rows). |
| [in] | ordered | When true, produce a directed full p² result; when false, produce an undirected upper-triangle result. |
| [in] | return_members | When true, also record each response row's in-window ground rows and window sizes. |
| [in] | return_misses | When true, also compute missed co-occurrences (see miss_matrix()) from the same ground vector. Rows are directed p², like row_connection_counts; miss_counts is their unfolded sum. |
apply_tensor() in tma/code.cpp.
|
inline |
Apply a weight model in place to one line's connection vector.
|
inline |
Compute the connection matrix for one ground + response event pair.
| [in] | ground | Row vector of accumulated ground codes (length p). |
| [in] | response | Row vector of the focal (response) codes (length p). |
| [in] | response_weight | Scalar weight applied to the response self-connection term. Defaults to 1.0. |
| [in] | ordered | When true, produces a directed (asymmetric) matrix: ground→response cross-product plus 0.5 * response_weight * response self-connection (diagonal zeroed). When false, produces a symmetric undirected matrix: (response_weight * r⊗r) + g⊗r + r⊗g. |
calculate_adjacency_matrix() in tma/code.cpp.
|
inline |
Finalise each per-response-row tensor connection vector.
The per-line step of tma's R aggregation of apply_tensor_unit()'s row_connection_counts (tma/R/accum_multidim_c.R), before any summing: unordered rows are folded to the upper triangle (as.unordered), then the weight model is applied; ordered rows keep the directed p² layout and have the weight model applied per cell.
Returned per row so callers can expose line-level connection counts (= R's model$row.connection.counts); summing the rows gives aggregate_row_connections().
| [in] | row_conn | Per-response-row directed connection matrix (n_response_rows × p²), i.e. TensorNetworks::row_connection_counts. |
| [in] | n_codes | Number of codes p. |
| [in] | ordered | true keeps directed p² rows; false folds each row to choose_two(p). |
| [in] | weight | Per-line weight model (see WeightModel). |
| [in] | diagonal | Unordered only: keep self-connections in the folded rows (libqe fold_directed_network with diagonal: A & A, A & B, B & B, ...). Ordered rows always carry the diagonal. Default false. |
n_connections(p, diagonal) (unordered).
|
inline |
Per-line self-connections: the diagonal of each directed row.
Entry (r, i) is line r's ground→response count for code i to itself, i.e. ground i × response i: the ordered network's diagonal, which unordered networks drop when they fold to the upper triangle. Rows are taken from TensorNetworks::row_connection_counts (or row_miss_counts, giving self-misses: ground i present, response i absent), and the weight model is applied per line exactly as finalize_row_connections() applies it, so "binary" clamps unordered self-connections to 1 and leaves ordered ones raw.
| [in] | row_conn | Per-response-row directed matrix (n_response_rows × p²). |
| [in] | n_codes | Number of codes p. |
| [in] | ordered | Selects the weight model's ordered/unordered behaviour. |
| [in] | weight | Per-line weight model (see WeightModel). |
|
inline |
Compute the linear index into a column-major multi-dimensional array.
| [in] | indices | Per-dimension index values (0-based). |
| [in] | dims | Size of each dimension. |
flat_index() in tma/code.cpp.
|
inline |
Missed co-occurrences for one ground + response event pair.
The counterpart of connection_matrix()'s ground→response term: entry (i, j) is the ground weight of code i when code j is absent from the response, i.e. ground.t() * [response == 0]. Laid out like the ordered connection matrix (ground i → response j at j*p+i once vectorised), with no response self-term. Unordered networks fold it the same way as connections (finalize_row_connections() / aggregate_row_connections()).
| [in] | ground | Row vector of accumulated (weighted) ground codes (length p). |
| [in] | response | Row vector of the response codes (length p). |
|
inline |
Rolling backward window sum of the raw code matrix.
For each focal row k, sums rows [max(0, k - window_size + 1), k]. Returns a matrix of the same shape as codes — no upper-triangle transform is applied. A window_size of 0 or less is treated as 1 (current row only).
| [in] | codes | Code matrix (n_rows × n_codes). |
| [in] | window_size | Number of rows to include in the backward window (including the focal row). Values <= 0 are clamped to 1. |
codes containing the windowed sums.ref_window_lag() in rENA/ena.cpp.
|
inline |
Compute the upper-triangle co-occurrence vector for each row.
For each row, calls code_connections() and optionally binarizes the result.
| [in] | codes | Code matrix (n_rows × n_codes). |
| [in] | binary | When true, clamp all positive entries to 1. |
rows_to_co_occurrences() in rENA/ena.cpp.
|
inline |
Map the legacy binary flag to a weight model (true → Binary, false → Product).
|
inline |
Parse a weight-model name (case-insensitive).
Accepts "binary", "product", "sqrt", "log1p" and the alias "log" (rena-wasm's name for log1p).
| std::invalid_argument | for any other name. |
|
inline |
Apply the weight model per line to already-finalised rows.
For rows that are already folded (unordered) or directed (ordered) – e.g. accumulate_unit_networks()'s raw row_connection_counts – this is the weight step of finalize_row_connections() on its own, so callers that need per-line weighted rows (summing by unit to the weighted unit vector) get them without re-folding.
| [in] | rows | n_lines × n_cols raw rows. |
| [in] | ordered | Selects the weight model's ordered/unordered behaviour. |
| [in] | weight | Per-line weight model (see WeightModel). |