libena 0.4.18-dev.9021
QE C++ library
Loading...
Searching...
No Matches
qe Namespace Reference

Namespaces

namespace  bind
 

Classes

struct  CCDResult
 Result of a CCD window-size estimation. More...
 
struct  ConnectionKey
 Which two codes each connection column connects: the column map the layout-agnostic (_by_key) node weighting works from. More...
 
struct  GeneralizedRotationParams
 Parameters for generalized_means_rotation(). More...
 
struct  GMRStepResult
 Result of one GMR axis step. More...
 
struct  GroupPair
 A pair of 0-based row-index vectors identifying two groups within a points matrix. More...
 
struct  MovedNodes
 Result of move_nodes_to_unit_circle. More...
 
struct  NodePositions
 Aggregated result returned by every node-position solver. More...
 
struct  RotationResult
 Aggregated result returned by every rotation routine. More...
 

Functions

CCDResult ccd_window (const std::vector< arma::mat > &conversations, int max_window=20, int min_overlap=10)
 Estimate the ENA moving-window size via cross-covariance decay.
 
Between-group scatter
arma::mat between_group_scatter (const arma::mat &V, const arma::uvec &labels, arma::uword n_groups)
 Compute the between-group scatter matrix S_B.
 
Rotation direction
arma::vec gmr_direction (const arma::mat &Vx, const arma::vec &target, bool categorical, arma::uword n_groups=0)
 Compute the unit rotation direction from a covariate-adjusted contribution matrix.
 
GMR axis step
GMRStepResult gmr_step (const arma::mat &V, const arma::mat &X_model, const arma::vec &target, const arma::uvec &x1_cols, bool categorical, arma::uword n_groups, const arma::uvec &subset, int n_lambda=50, int k_folds=5, double lasso_eps=0.01)
 Compute the rotation direction for one axis of the Generalized Means Rotation.
 
Generalized Means Rotation
RotationResult generalized_means_rotation (const arma::mat &V, const GeneralizedRotationParams &params)
 Full Generalized Means Rotation (GMR) producing a complete q×q rotation matrix.
 
Correlation
arma::mat ena_correlation (arma::mat points, arma::mat centroids, double conf_level=0.95)
 Pearson correlation with Fisher-z confidence interval between ENA unit points and their centroids.
 
Node-position solvers
NodePositions node_positions (arma::mat adj_mats, arma::mat t, int num_dims)
 Multiobjective least-squares node positions for undirected ENA.
 
NodePositions directed_node_positions (arma::mat line_weights, arma::mat points, int num_dims, bool combine_pairs=false, double self_weight=DIRECTED_SELF_WEIGHT)
 Least-squares node positions for directed (ordered) ENA.
 
NodePositions node_positions_by_key (const arma::mat &line_weights, const arma::mat &points, int num_dims, const ConnectionKey &key, int n_codes, double self_weight=DIRECTED_SELF_WEIGHT)
 Layout-agnostic least-squares node positions: the solve of node_positions / directed_node_positions with node_weights_by_key.
 
SVD rotation
RotationResult ena_svd (const arma::mat &points)
 Compute a full SVD rotation of a (pre-centered) points matrix.
 
Deflation
arma::mat deflate (const arma::mat &data, const arma::vec &axis)
 Project a data matrix onto the hyperplane orthogonal to a given axis.
 
Orthogonal SVD rotation
RotationResult orthogonal_svd (const arma::mat &data, const arma::mat &weights, const std::vector< std::string > &named_labels)
 Orthonormalize named axes via QR, then complete the rotation with an SVD of the data projected onto the orthogonal complement.
 
Complete (generalized) rotation
RotationResult complete_rotation (const arma::mat &data, const arma::mat &named_axes, const std::vector< std::string > &named_labels)
 Keep named axes verbatim and fill remaining axes from an SVD of the data after parallel deflation by all named axes.
 
Means rotation
RotationResult means_rotation (const arma::mat &points, const std::vector< GroupPair > &pairs)
 Compute a means rotation from one or more group pairs.
 

Post-fit node moves

enum class  UnitCircleMode { Equal , Radial }
 How unit_circle_nodes places the nodes on the circle. More...
 
UnitCircleMode unit_circle_mode (const std::string &name)
 Parse a mode name for the bindings: "equal" or "radial".
 
arma::mat unit_circle_nodes (const arma::mat &nodes, arma::uword dim_x, arma::uword dim_y, UnitCircleMode mode=UnitCircleMode::Equal)
 Move nodes onto a circle in the plane of two dimensions.
 
arma::mat node_centroids (const arma::mat &line_weights, const arma::mat &nodes, bool directed=false, double self_weight=DIRECTED_SELF_WEIGHT)
 Unit centroids for a set of node positions.
 
MovedNodes move_nodes_to_unit_circle (const arma::mat &line_weights, const arma::mat &nodes, arma::uword dim_x, arma::uword dim_y, UnitCircleMode mode=UnitCircleMode::Equal, bool directed=false, double self_weight=DIRECTED_SELF_WEIGHT)
 The post-fit unit-circle move of a model: moved nodes and their centroids.
 
arma::mat node_centroids_by_key (const arma::mat &line_weights, const arma::mat &nodes, const ConnectionKey &key, double self_weight=DIRECTED_SELF_WEIGHT)
 Layout-agnostic unit centroids: node_weights_by_key times nodes (as node_centroids).
 
MovedNodes move_nodes_to_unit_circle_by_key (const arma::mat &line_weights, const arma::mat &nodes, arma::uword dim_x, arma::uword dim_y, UnitCircleMode mode, const ConnectionKey &key, double self_weight=DIRECTED_SELF_WEIGHT)
 Layout-agnostic post-fit unit-circle move (as move_nodes_to_unit_circle), centroids from node_centroids_by_key.
 

Node weights

constexpr double DIRECTED_SELF_WEIGHT = 2.0
 
arma::mat node_weights (const arma::mat &line_weights)
 Per-unit node weights for undirected ENA.
 
arma::mat directed_node_weights (const arma::mat &line_weights, double self_weight=DIRECTED_SELF_WEIGHT)
 Per-unit node weights for directed (ordered) ENA.
 
ConnectionKey undirected_connection_key (int n_codes, bool diagonal=false)
 Key of the undirected layout: the column-major upper triangle (libqe connection_indices), with the diagonal (A & A, A & B, B & B, ...) when diagonal.
 
ConnectionKey directed_connection_key (int n_codes)
 Key of the ordered (directed) layout: column j*n+i is ground i -> response j, so from = i and to = j.
 
arma::mat node_weights_by_key (const arma::mat &line_weights, const ConnectionKey &key, int n_codes, double self_weight=DIRECTED_SELF_WEIGHT)
 Layout-agnostic node weights: every column counts toward each code it connects; a self-connection (from == to) toward its code self_weight times. Each row is then L1-normalised (as node_weights).
 

Enumeration Type Documentation

◆ UnitCircleMode

enum class qe::UnitCircleMode
strong

How unit_circle_nodes places the nodes on the circle.

Enumerator
Equal 

Equally spaced, 2π/n apart, the longest node fixed.

Radial 

Each node pushed out along its own ray.

Function Documentation

◆ between_group_scatter()

arma::mat qe::between_group_scatter ( const arma::mat &  V,
const arma::uvec &  labels,
arma::uword  n_groups 
)
inline

Compute the between-group scatter matrix S_B.

Computes S_B = Σ_g n_g * (μ_g − μ)(μ_g − μ)' where μ is the grand mean.

Note
Mirrors rENA's compute_SB() (commit 46776a1981a90b3a3b2861ed1010e9dbb7acf901).
Parameters
[in]Vn×p matrix of ENA points (or covariate-adjusted contribution).
[in]labelsn×1 0-based integer group labels.
[in]n_groupsNumber of distinct groups.
Returns
p×p symmetric between-group scatter matrix.

◆ ccd_window()

CCDResult qe::ccd_window ( const std::vector< arma::mat > &  conversations,
int  max_window = 20,
int  min_overlap = 10 
)
inline

Estimate the ENA moving-window size via cross-covariance decay.

For each lag l in 0..max_window, pools window-matched cross-covariance matrices across all conversation subsets, subtracts a trace-variance noise floor, and locates the first lag strictly after the peak where the corrected norm falls to half of its peak value.

Parameters
[in]conversationsOne code matrix per conversation (N_k rows × C codes, rows in sequence). All matrices must share the same number of columns.
[in]max_windowMaximum lag to evaluate. Default 20.
[in]min_overlapMinimum overlapping rows (N_k - l) required for a conversation to contribute at lag l. Default 10.
Returns
A CCDResult. When no conversation contributes at any positive lag (all-NaN or non-positive corrected covariance), the window size defaults to 1 and peak_lag to 0.

◆ complete_rotation()

RotationResult qe::complete_rotation ( const arma::mat &  data,
const arma::mat &  named_axes,
const std::vector< std::string > &  named_labels 
)
inline

Keep named axes verbatim and fill remaining axes from an SVD of the data after parallel deflation by all named axes.

Unlike orthogonal_svd(), this routine preserves the caller-supplied named_axes exactly (no QR re-orthonormalization). The complementary axes are obtained by deflating data simultaneously by all named axes and then running ena_svd() on the result.

The deflation is parallel: every projection is subtracted from the original data, not from a progressively deflated copy:

defA = data - data * (named_axes * named_axes^T)

This matches defA <- A - A %*% v1 %*% t(v1) - A %*% v2 %*% t(v2) in rENA line-for-line. For mutually orthogonal axes the result is identical to sequential deflation; for non-orthogonal axes it differs.

Parameters
datan x p data matrix.
named_axesp x k matrix of named rotation axes. Each column must be unit-norm; orthonormality between columns is NOT required.
named_labelsLength-k vector of labels for the first k axes. Trailing axes receive labels "SVD{k+1}" .. "SVDp". Conventional labels for generalized rotation are "GMR1", "GMR2", then "SVD{k+1}" onward, but labels are entirely caller-supplied.
Returns
A RotationResult where columns 0..k-1 of rotation equal named_axes verbatim and columns k..p-1 come from the SVD of the deflated data. Columns from the deflated data's null space (rank-deficient data) are orthogonalised against the named axes and each other, so the result is orthonormal whenever the named axes are.
Note
Equivalent to the tail of ena.rotate.by.generalized in rENA (canonical version: commit 2c079126 on rENA origin/main).
Exceptions
std::runtime_errorif data.n_cols != named_axes.n_rows or named_labels.size() != named_axes.n_cols.

◆ deflate()

arma::mat qe::deflate ( const arma::mat &  data,
const arma::vec &  axis 
)
inline

Project a data matrix onto the hyperplane orthogonal to a given axis.

Computes data - (data * axis) * axis^T, effectively removing the component of every row of data that lies along axis.

Parameters
datan x p data matrix.
axisUnit-norm column vector of length p. The caller is responsible for normalizing axis before calling this function; no normalization is performed internally.
Returns
n x p matrix with the projection onto axis subtracted out.

◆ directed_connection_key()

ConnectionKey qe::directed_connection_key ( int  n_codes)
inline

Key of the ordered (directed) layout: column j*n+i is ground i -> response j, so from = i and to = j.

◆ directed_node_positions()

NodePositions qe::directed_node_positions ( arma::mat  line_weights,
arma::mat  points,
int  num_dims,
bool  combine_pairs = false,
double  self_weight = DIRECTED_SELF_WEIGHT 
)
inline

Least-squares node positions for directed (ordered) ENA.

Each row of line_weights is an n_nodes × n_nodes directed weight matrix stored in row-major order. The per-unit node weight for node x accumulates the full row weight plus all column weights directed at x from other nodes. Each row is L1-normalised before solving (directed_node_weights).

When combine_pairs is false (the standard directed case) the system:

(W^T W) X = W^T P

is solved directly with W = normalised weight matrix and P = points.

When combine_pairs is true, adjacent row pairs (ground row k and response row k+1) are summed before solving:

W_combined[k/2] = W[k] + W[k+1]
P_combined[k/2] = P[k] + P[k+1]

The system is then solved on the combined matrices, but centroids are projected back using the original (un-combined) weight matrix so that every unit retains its own centroid.

Parameters
[in]line_weightsDirected adjacency vectors, n_units × (n_nodes²).
[in]pointsRotated unit points, n_units × n_dims.
[in]num_dimsNumber of dimensions to solve for.
[in]combine_pairsIf true, sum adjacent row pairs before solving (ground + response model). Default false.
[in]self_weightSelf-connection multiplier (see directed_node_weights). Default 2.
Returns
A NodePositions struct containing nodes, centroids, weights, and points.
Note
Equivalent to directed_node_positions() in rENA/ena.cpp when combine_pairs is false.
Equivalent to directed_node_positions_with_ground_response_added() in rENA/ena.cpp when combine_pairs is true.
Warning
When combine_pairs is true, line_weights must have an even number of rows (row_count must be even); odd row counts result in an out-of-bounds access when forming the combined matrices.

◆ directed_node_weights()

arma::mat qe::directed_node_weights ( const arma::mat &  line_weights,
double  self_weight = DIRECTED_SELF_WEIGHT 
)
inline

Per-unit node weights for directed (ordered) ENA.

A node's weight is the sum of its column plus the sum of its row of the unit's adjacency matrix: every directed connection's weight goes in full to both of its codes, and a self-connection (the diagonal, ground i × response i) goes to its code self_weight times – twice by default, as it is in both the node's row and its column. Each row is then L1-normalised as in node_weights. This is the weighting directed_node_positions solves with, and the weighting its centroids use.

Parameters
[in]line_weightsDirected adjacency vectors, n_units × (n_codes²).
[in]self_weightMultiplier for a self-connection's contribution to its code: 2 (default, row + column sum) or e.g. 1 (counted once). Must be finite and >= 0.
Returns
n_units × n_codes weights.

◆ ena_correlation()

arma::mat qe::ena_correlation ( arma::mat  points,
arma::mat  centroids,
double  conf_level = 0.95 
)
inline

Pearson correlation with Fisher-z confidence interval between ENA unit points and their centroids.

All unique pairs of units are formed; for each pair the per-dimension difference vectors are computed, then Pearson r is obtained between the points differences and the centroids differences. The confidence interval is derived via Fisher's z-transform using the pure-C++ normal_quantile function so the function works outside R without Rcpp::qnorm.

Parameters
[in]pointsRotated ENA unit points, n_units × n_dims.
[in]centroidsCorresponding centroid coordinates, n_units × n_dims.
[in]conf_levelConfidence level for the interval (default 0.95).
Returns
An n_dims × 3 matrix whose columns are [r, ci_lower, ci_upper].
Note
Equivalent to ena_correlation() in rENA/ena.cpp.

◆ ena_svd()

RotationResult qe::ena_svd ( const arma::mat &  points)
inline

Compute a full SVD rotation of a (pre-centered) points matrix.

Produces the right-singular vectors V as the rotation matrix and scales each singular value to an eigenvalue (sdev^2) compatible with rENA's prcomp() output.

Parameters
pointsn x p data matrix. The caller is responsible for centering upstream; this function applies no centering.
Returns
A RotationResult where:
  • rotation = V (p x p full SVD; trailing columns span the null space when the matrix is rank-deficient).
  • eigenvalues[j] = s[j]^2 / max(1, n - 1) (== sdev^2 in R).
  • column_names = {"SVD1", "SVD2", ..., "SVDp"}.
Note
Equivalent to prcomp(points, retx=FALSE, scale=FALSE, center=FALSE, tol=0) in rENA/R.
Sign convention: none. Signs follow the underlying LAPACK SVD, matching rENA's long-standing behavior. A deterministic sign rule (e.g. svd_flip) may be added later as an opt-in flag.

◆ generalized_means_rotation()

RotationResult qe::generalized_means_rotation ( const arma::mat &  V,
const GeneralizedRotationParams &  params 
)
inline

Full Generalized Means Rotation (GMR) producing a complete q×q rotation matrix.

C++/Armadillo implementation of ena.rotate.by.generalized(), matching the authoritative R implementation in commit 46776a1981a90b3a3b2861ed1010e9dbb7acf901 by Zhiqiang Cai.

Steps performed:

  1. X axis via GMR — calls gmr_step() on V with the X-axis parameters.
  2. Deflate V — computes defA = V − V·x·x' for subsequent Y-axis work.
  3. Secondary axis x1 — when the X target is categorical with ≥ 2 groups, computes x1 = normalize(mean(V[group0]) − mean(V[group1])) using the original V (not defA) and the full-length target (all n rows). x1 is used only to orthogonalise y_vector; it does not deflate defA. There is no SVD fallback: x1 stays empty if the condition is not met.
  4. Y axis — either GMR on defA (when params.has_y is true) or the leading SVD direction of defA.
  5. Orthogonalise y_vector — mirrors R: y_vector <- y_vector - (t(y_vector) %*% x_vector) * x_vector and, when x1 is available, y_vector <- y_vector - (t(y_vector) %*% x1) * x1, followed by safe normalisation.
  6. Complete with SVD — double-deflates V by both x_vector and y_vector, then calls complete_rotation() to fill remaining dimensions.
Note
Mirrors rENA's ena.rotate.by.generalized() (commit 46776a1981a90b3a3b2861ed1010e9dbb7acf901).
Parameters
[in]Vn×q matrix of ENA points (line weights; caller normalises/ centres upstream).
[in]paramsFully populated GeneralizedRotationParams struct.
Returns
RotationResult with:
  • rotation q×q full rotation matrix (column j = axis j).
  • eigenvalues q×1 variance per SVD-filled axis (named axes carry 0).
  • column_names "GMR1", "GMR2" or "SVD2", "SVD3", …, "SVD{q}".
Exceptions
std::runtime_errorIf gmr_direction() produces a degenerate direction for the X or Y axis.
std::runtime_errorIf y_vector has zero norm after orthogonalisation (can happen when defA is rank-deficient or perfectly aligned with x_vector).

◆ gmr_direction()

arma::vec qe::gmr_direction ( const arma::mat &  Vx,
const arma::vec &  target,
bool  categorical,
arma::uword  n_groups = 0 
)
inline

Compute the unit rotation direction from a covariate-adjusted contribution matrix.

Numeric target: Centres the target, then computes β = (t_c' t_c)^{-1} t_c' Vx (OLS slope from lm(Vx ~ target)). Mirrors R: model = lm(Vx ~ target); beta = coefficients[2,].

Categorical target: Computes the between-group scatter matrix S_B and returns its leading eigenvector (the direction of maximum between-group variance). Mirrors R: sb = compute_SB(Vx, target); r = svd(sb)$v[,1].

Note
Mirrors rENA's gmr() direction computation (commit 46776a1981a90b3a3b2861ed1010e9dbb7acf901).
Parameters
[in]Vxn×q covariate-adjusted contribution matrix.
[in]targetn×1 target variable (numeric float, or 0-based integer codes).
[in]categoricaltrue if the target is categorical (uses between-group scatter); false for a numeric target (uses OLS slope).
[in]n_groupsNumber of distinct groups; only used when categorical is true.
Returns
Unit vector (p×1) representing the rotation direction.
Exceptions
std::runtime_errorIf categorical is true and n_groups < 2.
std::runtime_errorIf the numeric target has zero variance after centering.
Warning
Throws std::runtime_error if the resulting direction has zero norm, which can occur when the Lasso-adjusted contribution collapses to zero.

◆ gmr_step()

GMRStepResult qe::gmr_step ( const arma::mat &  V,
const arma::mat &  X_model,
const arma::vec &  target,
const arma::uvec &  x1_cols,
bool  categorical,
arma::uword  n_groups,
const arma::uvec &  subset,
int  n_lambda = 50,
int  k_folds = 5,
double  lasso_eps = 0.01 
)
inline

Compute the rotation direction for one axis of the Generalized Means Rotation.

Applies optional row subsetting, performs Lasso-based covariate adjustment (or a simple OLS projection when all columns are target columns), then delegates to gmr_direction() to produce a unit direction vector.

When x1_cols.n_elem >= p (all columns are target columns and there are no penalized covariates), the function falls back to a simple OLS projection: Vx = fitted values from lm(V ~ t).

Note
Mirrors rENA's gmr() step logic (commit 46776a1981a90b3a3b2861ed1010e9dbb7acf901).
Parameters
[in]Vn×q matrix of ENA points (full data; subset applied internally).
[in]X_modeln×p model matrix (caller-built; target cols + covariate cols).
[in]targetn×1 target variable (raw float or 0-based integer codes).
[in]x1_cols0-based indices of target columns in X_model (unpenalized in Lasso).
[in]categoricaltrue if the target is categorical.
[in]n_groupsNumber of distinct groups (only used when categorical is true).
[in]subsetOptional 0-based row indices to use; empty means use all rows.
[in]n_lambdaLength of the Lasso lambda path (forwarded to lasso_x1_contribution).
[in]k_foldsNumber of cross-validation folds (forwarded to lasso_x1_contribution).
[in]lasso_epslambda_min = lasso_eps * lambda_max (forwarded to lasso_x1_contribution).
Returns
GMRStepResult containing the unit direction vector.
Exceptions
std::runtime_errorPropagated from gmr_direction() if the direction is degenerate or the target has zero variance.

◆ means_rotation()

RotationResult qe::means_rotation ( const arma::mat &  points,
const std::vector< GroupPair > &  pairs 
)
inline

Compute a means rotation from one or more group pairs.

For each GroupPair, computes the normalized mean-difference vector on the progressively deflated data and stacks the resulting unit-norm axes into a weights matrix. The final rotation is completed by orthogonal_svd(). The input points is mean-centered before any axes are computed, matching rENA's scale(data, scale=FALSE, center=TRUE).

Parameters
pointsn x p matrix of network positions (raw, not pre-centered).
pairsOrdered list of GroupPair objects. Each pair contributes one "MR" axis in sequence. Must be non-empty.
Returns
A RotationResult with column_names = {"MR1", ..., "MRm", "SVD{m+1}", ..., "SVDp"} where m = pairs.size().
Note
Equivalent to ena.rotate.by.mean() in rENA/R/ena.rotate.by.mean.R. The progressive deflation (each axis is computed on the data after removing all previous axes) is the key numerical step.
Warning
There is NO guard against a zero-norm mean-difference vector. If the two group means are identical on the current deflated data, the axis will contain NaN values (division by zero). This mirrors a latent bug in rENA and is preserved verbatim for numerical fidelity; a safe opt-in guard will be added in a future release.
Exceptions
std::runtime_errorif pairs is empty.

◆ move_nodes_to_unit_circle()

MovedNodes qe::move_nodes_to_unit_circle ( const arma::mat &  line_weights,
const arma::mat &  nodes,
arma::uword  dim_x,
arma::uword  dim_y,
UnitCircleMode  mode = UnitCircleMode::Equal,
bool  directed = false,
double  self_weight = DIRECTED_SELF_WEIGHT 
)
inline

The post-fit unit-circle move of a model: moved nodes and their centroids.

unit_circle_nodes on the model's nodes, then node_centroids for the moved nodes. Unit points are never changed, so a model keeps its points and takes the returned nodes and centroids.

Parameters
[in]line_weightsThe model's line weights (see node_centroids).
[in]nodesThe model's node positions, n_codes × n_dims.
[in]dim_x0-based column of the plane's x axis.
[in]dim_y0-based column of the plane's y axis.
[in]modeEqual (default) or Radial.
[in]directedOrdered (directed) networks. Default false.
[in]self_weightDirected self-connection multiplier, the fit's. Default 2.
Returns
A MovedNodes with the moved nodes and recomputed centroids.
Exceptions
std::invalid_argumentas unit_circle_nodes and node_centroids.

◆ move_nodes_to_unit_circle_by_key()

MovedNodes qe::move_nodes_to_unit_circle_by_key ( const arma::mat &  line_weights,
const arma::mat &  nodes,
arma::uword  dim_x,
arma::uword  dim_y,
UnitCircleMode  mode,
const ConnectionKey &  key,
double  self_weight = DIRECTED_SELF_WEIGHT 
)
inline

Layout-agnostic post-fit unit-circle move (as move_nodes_to_unit_circle), centroids from node_centroids_by_key.

◆ node_centroids()

arma::mat qe::node_centroids ( const arma::mat &  line_weights,
const arma::mat &  nodes,
bool  directed = false,
double  self_weight = DIRECTED_SELF_WEIGHT 
)
inline

Unit centroids for a set of node positions.

The model's node weights (node_weights, or directed_node_weights when directed) times nodes: the centroids node_positions and directed_node_positions return, for any nodes. Given a fit's own nodes it reproduces the fit's centroids; given moved nodes, it gives the centroids that match them.

Line weights rather than raw connection counts: the two give the same weights for the default (binary) weight model, since each unit's line weights are its counts scaled, and the line weights are what the fit used for any weight model.

Parameters
[in]line_weightsThe model's line weights, n_units × choose(n_codes, 2), or n_units × n_codes² when directed.
[in]nodesNode positions, n_codes × n_dims.
[in]directedOrdered (directed) networks. Default false.
[in]self_weightDirected self-connection multiplier (see directed_node_weights); use the fit's. Default 2.
Returns
n_units × n_dims centroids.
Exceptions
std::invalid_argumentif the line weights' column count does not match the number of nodes, or either input contains NaN or Inf.

◆ node_centroids_by_key()

arma::mat qe::node_centroids_by_key ( const arma::mat &  line_weights,
const arma::mat &  nodes,
const ConnectionKey &  key,
double  self_weight = DIRECTED_SELF_WEIGHT 
)
inline

Layout-agnostic unit centroids: node_weights_by_key times nodes (as node_centroids).

◆ node_positions()

NodePositions qe::node_positions ( arma::mat  adj_mats,
arma::mat  t,
int  num_dims 
)
inline

Multiobjective least-squares node positions for undirected ENA.

Half of each adjacency-vector entry (line weight) is distributed to each of its two endpoint nodes, building a per-unit weight matrix (node_weights). Each row is then L1-normalised. An overdetermined system is solved per dimension:

(W^T W) X = W^T T

where W is the normalised weight matrix and T contains the rotated unit points.

Parameters
[in]adj_matsUpper-triangular adjacency vectors stacked row-wise, n_units × tri_size.
[in]tRotated unit points, n_units × n_dims.
[in]num_dimsNumber of dimensions to solve for.
Returns
A NodePositions struct containing nodes, centroids, weights, and points.
Note
Equivalent to lws_lsq_positions() in rENA/ena.cpp.

◆ node_positions_by_key()

NodePositions qe::node_positions_by_key ( const arma::mat &  line_weights,
const arma::mat &  points,
int  num_dims,
const ConnectionKey &  key,
int  n_codes,
double  self_weight = DIRECTED_SELF_WEIGHT 
)
inline

Layout-agnostic least-squares node positions: the solve of node_positions / directed_node_positions with node_weights_by_key.

Parameters
[in]line_weightsn_units × n_columns, any layout.
[in]pointsRotated unit points, n_units × n_dims.
[in]num_dimsNumber of dimensions to solve for.
[in]keyColumn map (see ConnectionKey).
[in]n_codesNumber of codes (nodes).
[in]self_weightSelf-connection multiplier. Default 2.
Returns
A NodePositions struct.

◆ node_weights()

arma::mat qe::node_weights ( const arma::mat &  line_weights)
inline

Per-unit node weights for undirected ENA.

Half of each line weight goes to each of its two codes; each row is then L1-normalised (a row whose L1 norm is below 0.0001 is divided by 0.0001, so an all-zero row stays zero). This is the weighting node_positions solves with, and the weighting its centroids use.

Parameters
[in]line_weightsUpper-triangular adjacency vectors stacked row-wise (column order A & B, A & C, B & C, ...), n_units × choose(n_codes, 2).
Returns
n_units × n_codes weights.

◆ node_weights_by_key()

arma::mat qe::node_weights_by_key ( const arma::mat &  line_weights,
const ConnectionKey &  key,
int  n_codes,
double  self_weight = DIRECTED_SELF_WEIGHT 
)
inline

Layout-agnostic node weights: every column counts toward each code it connects; a self-connection (from == to) toward its code self_weight times. Each row is then L1-normalised (as node_weights).

One rule for every layout. With the undirected key it equals node_weights; with the directed key, directed_node_weights at the same self_weight; with the undirected-with-diagonal key, the undirected weighting plus self-connections that count as they do in ordered models.

Parameters
[in]line_weightsn_units × n_columns.
[in]keyColumn map, n_columns long (see ConnectionKey).
[in]n_codesNumber of codes (nodes).
[in]self_weightSelf-connection multiplier; default 2 (a node's row plus column sum, as directed_node_weights).
Returns
n_units × n_codes weights.
Exceptions
std::invalid_argumentif the key's length does not match the columns, an index is out of range, or self_weight is negative or not finite.

◆ orthogonal_svd()

RotationResult qe::orthogonal_svd ( const arma::mat &  data,
const arma::mat &  weights,
const std::vector< std::string > &  named_labels 
)
inline

Orthonormalize named axes via QR, then complete the rotation with an SVD of the data projected onto the orthogonal complement.

The named axes in weights are orthonormalized via a full QR decomposition. The first k columns of the resulting Q become the "fixed" part of the rotation; the remaining p - k columns define a complementary subspace onto which data is projected, and a further SVD fills those trailing rotation columns.

Parameters
datan x p data matrix (caller-centered if required).
weightsp x k matrix of named axis directions. Column norms need not be 1; QR handles normalization internally.
named_labelsLength-k vector of labels for the first k rotation axes. Trailing axes receive labels "SVD{k+1}" .. "SVDp".
Returns
A RotationResult containing the combined p x p rotation, eigenvalues for the SVD-filled trailing axes (indices k..p-1; the first k eigenvalues are zero), and axis labels.
Note
Equivalent to orthogonal_svd() in rENA/R/ena.rotate.by.mean.R:
Q = qr.Q(qr(weights), complete = TRUE) # p x p
X_bar = data %*% Q[, (k+1):p] # n x (p - k)
V = prcomp(X_bar)$rotation
out = cbind(Q[, 1:k], Q[, (k+1):p] %*% V)
The named axes in the OUTPUT are the orthonormalized Q columns, NOT the original weights columns. Use complete_rotation() if you need to preserve input axes verbatim.
Exceptions
std::runtime_errorif data.n_cols != weights.n_rows or named_labels.size() != weights.n_cols.

◆ undirected_connection_key()

ConnectionKey qe::undirected_connection_key ( int  n_codes,
bool  diagonal = false 
)
inline

Key of the undirected layout: the column-major upper triangle (libqe connection_indices), with the diagonal (A & A, A & B, B & B, ...) when diagonal.

◆ unit_circle_mode()

UnitCircleMode qe::unit_circle_mode ( const std::string &  name)
inline

Parse a mode name for the bindings: "equal" or "radial".

Exceptions
std::invalid_argumentfor any other name.

◆ unit_circle_nodes()

arma::mat qe::unit_circle_nodes ( const arma::mat &  nodes,
arma::uword  dim_x,
arma::uword  dim_y,
UnitCircleMode  mode = UnitCircleMode::Equal 
)
inline

Move nodes onto a circle in the plane of two dimensions.

The circle's radius is the length of the longest node in that plane. Nodes of length zero there are left where they are, and every other dimension is unchanged.

  • UnitCircleMode::Radial: each node moves out along its own ray.
  • UnitCircleMode::Equal: the nodes keep their order around the origin and are spaced 2π/n apart, counter-clockwise, starting from the longest node, which stays where it is (the first one when lengths tie). The order is that of rENA's original R implementation: nodes with y >= 0 by decreasing x, then nodes with y < 0 by increasing x, ties in their original order.

A post-fit transform for display; it does not refit anything. Use node_centroids to recompute the centroids for the moved nodes.

Parameters
[in]nodesNode positions, n_codes × n_dims.
[in]dim_x0-based column of the plane's x axis.
[in]dim_y0-based column of the plane's y axis.
[in]modeEqual (default) or Radial.
Returns
A copy of nodes with columns dim_x and dim_y moved.
Exceptions
std::invalid_argumentif a dimension is out of range, the two are the same, or nodes contains NaN or Inf.
Note
Equivalent to rENA's move_nodes_to_unit_circle() (Radial) and move_nodes_to_unit_circle_with_equal_space() (Equal), up to their centroid step.

Variable Documentation

◆ DIRECTED_SELF_WEIGHT

constexpr double qe::DIRECTED_SELF_WEIGHT = 2.0
constexpr

Default self_weight of the directed node weights: a node's weight is the sum of its column plus the sum of its row of the unit's adjacency matrix (in + out), so a self-connection, in both, counts twice (directedENA, Zhiqiang Cai, 2021-10-29).