Skip to main content

ArmadilloTensorStorage

ArmadilloTensorStorage

Tensor storage backend using Armadillo (arma::fvec, arma::fmat, arma::fcube). More…

#include <ArmadilloTensorStorage.hpp>

Inherits from TensorStorageBase< ArmadilloTensorStorage >

Public Functions

Name
ArmadilloTensorStorage(arma::fvec vector)
Construct 1D storage from an Armadillo column vector.
ArmadilloTensorStorage(arma::fmat matrix)
Construct 2D storage from an Armadillo matrix.
ArmadilloTensorStorage(arma::fcube cube)
Construct 3D storage from an Armadillo cube.
ArmadilloTensorStorage(std::vector< float > const & data, std::size_t num_rows, std::size_t num_cols)
Construct 2D storage from flat row-major data.
arma::fvec const & vector() const
Get const reference to the underlying vector (1D only).
arma::fvec & mutableVector()
Get mutable reference to the underlying vector (1D only).
arma::fmat const & matrix() const
Get const reference to the underlying matrix (2D only).
arma::fmat & mutableMatrix()
Get mutable reference to the underlying matrix (2D only).
arma::fcube const & cube() const
Get const reference to the underlying cube (3D only).
arma::fcube & mutableCube()
Get mutable reference to the underlying cube (3D only).
std::size_t ndim() const
Get the number of dimensions (1, 2, or 3).
void appendRow(std::span< float const > row_data)
Append a row to the end of the matrix.
void insertRow(std::size_t index, std::span< float const > row_data)
Insert a row at a specific position in the matrix.
void setRow(std::size_t index, std::span< float const > row_data)
Overwrite an existing row’s data in-place.
float getValueAtImpl(std::span< std::size_t const > indices) const
std::span< float const > flatDataImpl() const
std::vector< float > sliceAlongAxisImpl(std::size_t axis, std::size_t index) const
std::vector< float > getColumnImpl(std::size_t col) const
std::vector< std::size_t > shapeImpl() const
std::size_t totalElementsImpl() const
bool isContiguousImpl() const
TensorStorageType getStorageTypeImpl() const
TensorStorageCache tryGetCacheImpl() const

Additional inherited members

Public Functions inherited from TensorStorageBase< ArmadilloTensorStorage >

Name
float getValueAt(std::span< std::size_t const > indices) const
Get a single float value by multi-dimensional index.
std::span< float const > flatData() const
Get a span over the flat (row-major) data.
std::vector< float > sliceAlongAxis(std::size_t axis, std::size_t index) const
Extract a sub-tensor by fixing one axis to a single index.
std::vector< float > getColumn(std::size_t col) const
Get a single column (axis 1 slice) across all rows (axis 0).
std::vector< std::size_t > shape() const
Get the shape as a vector of sizes (one per axis).
std::size_t totalElements() const
Get the total number of elements (product of shape).
bool isContiguous() const
Whether the underlying data is contiguous in memory.
TensorStorageType getStorageType() const
Get the storage type identifier.
TensorStorageCache tryGetCache() const
Try to get cached pointers for fast-path access.

Protected Functions inherited from TensorStorageBase< ArmadilloTensorStorage >

Name
~TensorStorageBase() =default

Detailed Description

class ArmadilloTensorStorage;

Tensor storage backend using Armadillo (arma::fvec, arma::fmat, arma::fcube).

This is the default storage backend for tensors with ≤3 dimensions. Armadillo is a required project dependency, so this backend is always available.

Wraps arma::fvec (1D), arma::fmat (2D), or arma::fcube (3D) and provides:

  • Zero-copy access for mlpack interop via matrix()/cube() getters
  • Transparent column-major to row-major translation in the CRTP interface
  • Efficient column extraction leveraging Armadillo’s native col() access

Layout note

Armadillo uses column-major storage. This class presents row-major semantics to consumers through the TensorStorageBase interface:

  • [flatData()](/api_generated/Classes/classTensorStorageBase.md#function-flatdata) returns data in Armadillo’s native column-major layout (consumers needing row-major should use [getValueAt()](/api_generated/Classes/classTensorStorageBase.md#function-getvalueat) or [getColumn()](/api_generated/Classes/classTensorStorageBase.md#function-getcolumn))
  • [getValueAt](/api_generated/Classes/classTensorStorageBase.md#function-getvalueat)({r, c}) on a matrix accesses row r, column c regardless of internal layout
  • getColumn(c) efficiently extracts column c using Armadillo’s native col()

For zero-copy Armadillo access (e.g., passing to mlpack), use [matrix()](/api_generated/Classes/classArmadilloTensorStorage.md#function-matrix) or [cube()](/api_generated/Classes/classArmadilloTensorStorage.md#function-cube) directly — these return const references to the underlying Armadillo objects with no translation.

Public Functions Documentation

function ArmadilloTensorStorage

explicit ArmadilloTensorStorage(
    arma::fvec vector
)

Construct 1D storage from an Armadillo column vector.

Stored internally as arma::fvec. Shape will be [N].

function ArmadilloTensorStorage

explicit ArmadilloTensorStorage(
    arma::fmat matrix
)

Construct 2D storage from an Armadillo matrix.

Stored internally as arma::fmat. Shape will be [nrows, ncols].

function ArmadilloTensorStorage

explicit ArmadilloTensorStorage(
    arma::fcube cube
)

Construct 3D storage from an Armadillo cube.

Stored internally as arma::fcube. Shape will be [nslices, nrows, ncols].

Note on Armadillo cube layout: an fcube of size (n_rows, n_cols, n_slices) is treated here as a 3D tensor with shape [n_slices, n_rows, n_cols], where slices are the outermost dimension. This maps naturally to “time × height × width” or “batch × rows × cols” semantics.

function ArmadilloTensorStorage

ArmadilloTensorStorage(
    std::vector< float > const & data,
    std::size_t num_rows,
    std::size_t num_cols
)

Construct 2D storage from flat row-major data.

Parameters:

  • data Flat float data in row-major order
  • num_rows Number of rows
  • num_cols Number of columns

Exceptions:

  • std::invalid_argument if data.size() != num_rows * num_cols

Converts from row-major input to Armadillo’s column-major format.

function vector

arma::fvec const & vector() const

Get const reference to the underlying vector (1D only).

Exceptions:

  • std::logic_error if storage is not 1D

function mutableVector

arma::fvec & mutableVector()

Get mutable reference to the underlying vector (1D only).

Exceptions:

  • std::logic_error if storage is not 1D

function matrix

arma::fmat const & matrix() const

Get const reference to the underlying matrix (2D only).

Exceptions:

  • std::logic_error if storage is not 2D

function mutableMatrix

arma::fmat & mutableMatrix()

Get mutable reference to the underlying matrix (2D only).

Exceptions:

  • std::logic_error if storage is not 2D

function cube

arma::fcube const & cube() const

Get const reference to the underlying cube (3D only).

Exceptions:

  • std::logic_error if storage is not 3D

function mutableCube

arma::fcube & mutableCube()

Get mutable reference to the underlying cube (3D only).

Exceptions:

  • std::logic_error if storage is not 3D

function ndim

std::size_t ndim() const

Get the number of dimensions (1, 2, or 3).

function appendRow

void appendRow(
    std::span< float const > row_data
)

Append a row to the end of the matrix.

Parameters:

  • row_data Row values; size must equal number of columns

Exceptions:

  • std::logic_error if storage is not 2D
  • std::invalid_argument if row_data.size() != number of columns

Only valid for 2D storage (arma::fmat).

function insertRow

void insertRow(
    std::size_t index,
    std::span< float const > row_data
)

Insert a row at a specific position in the matrix.

Parameters:

  • index Row index to insert before (0-based; numRows inserts at end)
  • row_data Row values; size must equal number of columns

Exceptions:

  • std::logic_error if storage is not 2D
  • std::out_of_range if index > number of rows
  • std::invalid_argument if row_data.size() != number of columns

Only valid for 2D storage (arma::fmat). Existing rows at and after the insertion point are shifted down.

function setRow

void setRow(
    std::size_t index,
    std::span< float const > row_data
)

Overwrite an existing row’s data in-place.

Parameters:

  • index Row index to overwrite (0-based)
  • row_data New values; size must equal number of columns

Exceptions:

  • std::logic_error if storage is not 2D
  • std::out_of_range if index >= number of rows
  • std::invalid_argument if row_data.size() != number of columns

Only valid for 2D storage (arma::fmat).

function getValueAtImpl

float getValueAtImpl(
    std::span< std::size_t const > indices
) const

function flatDataImpl

std::span< float const > flatDataImpl() const

function sliceAlongAxisImpl

std::vector< float > sliceAlongAxisImpl(
    std::size_t axis,
    std::size_t index
) const

function getColumnImpl

std::vector< float > getColumnImpl(
    std::size_t col
) const

function shapeImpl

std::vector< std::size_t > shapeImpl() const

function totalElementsImpl

std::size_t totalElementsImpl() const

function isContiguousImpl

inline bool isContiguousImpl() const

function getStorageTypeImpl

inline TensorStorageType getStorageTypeImpl() const

function tryGetCacheImpl

TensorStorageCache tryGetCacheImpl() const

Updated on 2026-11-13 at 20:25:41 +0000