libtma 0.3.13-dev.9013
QE C++ library
Loading...
Searching...
No Matches
qe Namespace Reference

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 &times, 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 &times, 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.
 

Enumeration Type Documentation

◆ WeightModel

enum class qe::WeightModel
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 

Function Documentation

◆ accumulate_stanza()

arma::mat qe::accumulate_stanza ( arma::mat  codes,
int  window_back = 1,
int  window_forward = 0,
bool  binary = true,
bool  ordered = false 
)
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().

Ordering semantics
  • Undirected (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).
  • Directed (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².
Parameters
[in]codesCode matrix for one conversation (n_rows × n_codes).
[in]window_backNumber of prior rows included in the window (1 = current row only; INT_MAX = all prior rows).
[in]window_forwardNumber of future rows included in the window (ignored when ordered is true).
[in]binaryWhen true, clamp all positive entries to 1.
[in]orderedWhen true, use directed accumulation; when false, use undirected stanza-window accumulation.
Returns
Connection matrix (n_rows × connection_vector_length) where connection_vector_length is choose_two(n_codes) for undirected or n_codes² for directed.
Note
Equivalent to ref_window_df() in rENA/ena.cpp (undirected case).

◆ accumulate_unit()

arma::rowvec qe::accumulate_unit ( const arma::mat &  codes,
const std::vector< int > &  unit_rows,
std::function< arma::vec(arma::vec)>  decay_fn,
bool  ordered = false 
)
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().

Ordering semantics
The connections are always accumulated directed (ground → response, as accumulate_unit_networks() does). When 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.)
Parameters
[in]codesFull context matrix visible to this unit (n × p).
[in]unit_rows0-based indices of the rows that belong to this unit.
[in]decay_fnCallback 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]orderedWhen true, return a directed flat vector (length p²); when false, return an undirected upper-triangle flat vector (length choose_two(p)).
Returns
Flat connection vector of length choose_two(p) (undirected) or p² (directed).
Note
Equivalent to accumulate_network() in tma/code.cpp, with the R function callback replaced by std::function.

◆ accumulate_unit_networks()

UnitAccumulation qe::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 &  times,
bool  ordered = false,
WeightModel  weight = WeightModel::Binary,
bool  misses = true,
bool  diagonal = false 
)
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:

Parameters
[in]orderedtrue: directed p² networks; false: folded choose_two(p).
[in]weightPer-line weight model (see WeightModel).
[in]missesAlso accumulate missed co-occurrences and self-misses.
[in]diagonalUnordered 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.

◆ accumulate_unit_with_rows()

UnitNetworks qe::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 
)
inline

Like accumulate_unit() but also returns the per-response-row connection matrix needed by tma's accumulate_network().

Parameters
[in]codesFull context matrix visible to this unit (n × p).
[in]unit_rows0-based indices of the rows that belong to this unit.
[in]decay_fnCallback 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]orderedWhen 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.
Returns
A UnitNetworks struct containing the aggregated connection vector and the per-response-row connection matrices.
Note
Equivalent to accumulate_network() in tma/code.cpp (extended form).

◆ aggregate_row_connections() [1/2]

arma::rowvec qe::aggregate_row_connections ( const arma::mat &  row_conn,
int  n_codes,
bool  ordered,
WeightModel  weight,
bool  diagonal = false 
)
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.

Parameters
[in]row_connPer-response-row directed connection matrix (n_response_rows × p²).
[in]n_codesNumber of codes p.
[in]orderedtrue: directed p² sums; false: fold to choose_two(p) and sum.
[in]weightPer-line weight model (see WeightModel).
[in]diagonalUnordered only: keep self-connections (see finalize_row_connections()). Default false.
Returns
Flat unit connection vector of length p² (ordered) or n_connections(p, diagonal) (unordered).

◆ aggregate_row_connections() [2/2]

arma::rowvec qe::aggregate_row_connections ( const arma::mat &  row_conn,
int  n_codes,
bool  ordered = false,
bool  binary = true 
)
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.

◆ aggregate_self_connections()

arma::rowvec qe::aggregate_self_connections ( const arma::mat &  row_conn,
int  n_codes,
bool  ordered,
WeightModel  weight 
)
inline

Unit self-connection vector: finalize_self_connections() summed over the lines.

Returns
Vector of length p.

◆ apply_tensor_unit()

TensorNetworks qe::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 &  times,
bool  ordered = true,
bool  return_members = false,
bool  return_misses = false 
)
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.

Parameters
[in]tensorFlat column-major array containing window and weight values indexed by the factor dimensions.
[in]dimsSizes of each dimension of tensor.
[in]dims_senderTensor axis indices corresponding to sender factors.
[in]dims_receiverTensor 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_modeTensor axis indices corresponding to mode factors.
[in]context_lookupInteger matrix (n_context_rows × n_factors, 0-based) mapping each context row to its factor level indices.
[in]unit_rows0-based response-row indices for this unit.
[in]codesFull context code matrix (n_context_rows × n_codes).
[in]timesTimestamp associated with each context row (length n_context_rows).
[in]orderedWhen true, produce a directed full p² result; when false, produce an undirected upper-triangle result.
[in]return_membersWhen true, also record each response row's in-window ground rows and window sizes.
[in]return_missesWhen 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.
Returns
A TensorNetworks struct containing the unit-level connection counts and the per-response-row connection counts (plus misses / window members when requested).
Note
Equivalent to the inner loop of apply_tensor() in tma/code.cpp.

◆ apply_weight_model()

void qe::apply_weight_model ( arma::rowvec &  v,
WeightModel  weight,
bool  ordered 
)
inline

Apply a weight model in place to one line's connection vector.

◆ connection_matrix()

arma::mat qe::connection_matrix ( arma::rowvec  ground,
arma::rowvec  response,
double  response_weight = 1.0,
bool  ordered = true 
)
inline

Compute the connection matrix for one ground + response event pair.

Parameters
[in]groundRow vector of accumulated ground codes (length p).
[in]responseRow vector of the focal (response) codes (length p).
[in]response_weightScalar weight applied to the response self-connection term. Defaults to 1.0.
[in]orderedWhen 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.
Returns
A p × p connection matrix.
Note
Equivalent to calculate_adjacency_matrix() in tma/code.cpp.

◆ finalize_row_connections()

arma::mat qe::finalize_row_connections ( const arma::mat &  row_conn,
int  n_codes,
bool  ordered,
WeightModel  weight,
bool  diagonal = false 
)
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().

Parameters
[in]row_connPer-response-row directed connection matrix (n_response_rows × p²), i.e. TensorNetworks::row_connection_counts.
[in]n_codesNumber of codes p.
[in]orderedtrue keeps directed p² rows; false folds each row to choose_two(p).
[in]weightPer-line weight model (see WeightModel).
[in]diagonalUnordered 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.
Returns
Matrix of shape n_response_rows × p² (ordered) or n_response_rows × n_connections(p, diagonal) (unordered).

◆ finalize_self_connections()

arma::mat qe::finalize_self_connections ( const arma::mat &  row_conn,
int  n_codes,
bool  ordered,
WeightModel  weight 
)
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.

Parameters
[in]row_connPer-response-row directed matrix (n_response_rows × p²).
[in]n_codesNumber of codes p.
[in]orderedSelects the weight model's ordered/unordered behaviour.
[in]weightPer-line weight model (see WeightModel).
Returns
Matrix of shape n_response_rows × p.

◆ flat_index()

int qe::flat_index ( const std::vector< int > &  indices,
const std::vector< int > &  dims 
)
inline

Compute the linear index into a column-major multi-dimensional array.

Parameters
[in]indicesPer-dimension index values (0-based).
[in]dimsSize of each dimension.
Returns
The scalar column-major flat index.
Note
Equivalent to flat_index() in tma/code.cpp.

◆ miss_matrix()

arma::mat qe::miss_matrix ( const arma::rowvec &  ground,
const arma::rowvec &  response 
)
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()).

Parameters
[in]groundRow vector of accumulated (weighted) ground codes (length p).
[in]responseRow vector of the response codes (length p).
Returns
A p × p missed-co-occurrence matrix.

◆ rolling_window_sum()

arma::mat qe::rolling_window_sum ( const arma::mat &  codes,
int  window_size = 1 
)
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).

Parameters
[in]codesCode matrix (n_rows × n_codes).
[in]window_sizeNumber of rows to include in the backward window (including the focal row). Values <= 0 are clamped to 1.
Returns
Matrix of the same shape as codes containing the windowed sums.
Note
Equivalent to ref_window_lag() in rENA/ena.cpp.

◆ row_connections()

arma::mat qe::row_connections ( const arma::mat &  codes,
bool  binary = true 
)
inline

Compute the upper-triangle co-occurrence vector for each row.

For each row, calls code_connections() and optionally binarizes the result.

Parameters
[in]codesCode matrix (n_rows × n_codes).
[in]binaryWhen true, clamp all positive entries to 1.
Returns
Matrix of shape n_rows × choose_two(n_codes).
Note
Equivalent to rows_to_co_occurrences() in rENA/ena.cpp.

◆ weight_model_from_bool()

WeightModel qe::weight_model_from_bool ( bool  binary)
inline

Map the legacy binary flag to a weight model (true → Binary, false → Product).

◆ weight_model_from_string()

WeightModel qe::weight_model_from_string ( const std::string &  name)
inline

Parse a weight-model name (case-insensitive).

Accepts "binary", "product", "sqrt", "log1p" and the alias "log" (rena-wasm's name for log1p).

Exceptions
std::invalid_argumentfor any other name.

◆ weight_rows()

arma::mat qe::weight_rows ( const arma::mat &  rows,
bool  ordered,
WeightModel  weight 
)
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.

Parameters
[in]rowsn_lines × n_cols raw rows.
[in]orderedSelects the weight model's ordered/unordered behaviour.
[in]weightPer-line weight model (see WeightModel).