Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
momentum::ResizeableMatrix< T > Class Template Reference

A dense column-major matrix whose underlying storage capacity grows monotonically and is reused across resizes, unlike Eigen::Matrix<T, Dynamic, Dynamic> which reallocates on every resize() / conservativeResize() call. More...

#include <resizeable_matrix.h>

Public Types

using MatrixType = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>

Public Member Functions

 ResizeableMatrix ()=default
 ResizeableMatrix (Eigen::Index rows, Eigen::Index cols=1)
 Constructs a matrix of the given logical size and zero-initializes all entries.
void resizeAndSetZero (Eigen::Index rows, Eigen::Index cols=1)
 Sets the logical size to rows x cols and zeroes every element.
Eigen::Index rows () const
Eigen::Index cols () const
Eigen::Map< MatrixType, Eigen::Aligned128 > mat ()
 Returns an aligned, mutable Eigen::Map view of the logical rows() x cols() matrix.
Eigen::Map< const MatrixType, Eigen::Aligned128 > mat () const
 Const overload of mat().
Eigen::Map< Eigen::Matrix< T, Eigen::Dynamic, 1 >, Eigen::Aligned128 > vec ()
 Returns an aligned, mutable Eigen::Map view interpreting the storage as a length-rows() column vector.
Eigen::Map< const Eigen::Matrix< T, Eigen::Dynamic, 1 >, Eigen::Aligned128 > vec () const
 Const overload of vec().

Static Public Attributes

static constexpr std::size_t kAlignment = 128
 Alignment in bytes for the underlying storage.

Detailed Description

template<typename T>
class momentum::ResizeableMatrix< T >

A dense column-major matrix whose underlying storage capacity grows monotonically and is reused across resizes, unlike Eigen::Matrix<T, Dynamic, Dynamic> which reallocates on every resize() / conservativeResize() call.

Motivation: solver inner loops repeatedly build matrices (e.g. blockwise Jacobians) whose logical dimensions vary slightly from iteration to iteration. Using a plain Eigen matrix triggers a heap allocation each time the size changes; this class amortizes those costs by only growing the backing buffer when the requested element count exceeds the current capacity. Storage shrinks only when the object is destroyed.

Capacity vs. logical size: rows() / cols() report the logical size requested by the last resizeAndSetZero(). The backing std::vector capacity may be larger and is never returned to the allocator while the object lives.

Layout: data is stored contiguously in column-major order (matching Eigen's default) and is aligned to kAlignment bytes so that the returned Eigen::Map<..., Eigen::Aligned128> can use aligned SIMD loads/stores.

Thread safety: not thread-safe. Concurrent calls to any non-const method, or a non-const method concurrent with any other access, are data races. Multiple readers via const accessors are safe provided no writer is active.

Definition at line 40 of file resizeable_matrix.h.

Member Typedef Documentation

◆ MatrixType

template<typename T>
using momentum::ResizeableMatrix< T >::MatrixType = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>

Definition at line 46 of file resizeable_matrix.h.

Constructor & Destructor Documentation

◆ ResizeableMatrix() [1/2]

template<typename T>
momentum::ResizeableMatrix< T >::ResizeableMatrix ( )
default

◆ ResizeableMatrix() [2/2]

template<typename T>
momentum::ResizeableMatrix< T >::ResizeableMatrix ( Eigen::Index rows,
Eigen::Index cols = 1 )
inlineexplicit

Constructs a matrix of the given logical size and zero-initializes all entries.

Parameters
rowsNumber of rows in the logical matrix.
colsNumber of columns in the logical matrix; defaults to 1 (column vector).

Definition at line 53 of file resizeable_matrix.h.

References cols(), resizeAndSetZero(), and rows().

Member Function Documentation

◆ cols()

template<typename T>
Eigen::Index momentum::ResizeableMatrix< T >::cols ( ) const
inlinenodiscard

Definition at line 83 of file resizeable_matrix.h.

Referenced by ResizeableMatrix(), and resizeAndSetZero().

◆ mat() [1/2]

template<typename T>
Eigen::Map< MatrixType, Eigen::Aligned128 > momentum::ResizeableMatrix< T >::mat ( )
inline

Returns an aligned, mutable Eigen::Map view of the logical rows() x cols() matrix.

The map is invalidated by any subsequent call to resizeAndSetZero().

Definition at line 89 of file resizeable_matrix.h.

◆ mat() [2/2]

template<typename T>
Eigen::Map< const MatrixType, Eigen::Aligned128 > momentum::ResizeableMatrix< T >::mat ( ) const
inline

Const overload of mat().

Definition at line 94 of file resizeable_matrix.h.

◆ resizeAndSetZero()

template<typename T>
void momentum::ResizeableMatrix< T >::resizeAndSetZero ( Eigen::Index rows,
Eigen::Index cols = 1 )
inline

Sets the logical size to rows x cols and zeroes every element.

The backing buffer is reused if its current capacity is sufficient; otherwise it grows. Capacity never shrinks. Any Eigen::Map previously returned by mat() / vec() is invalidated by this call (storage may have been reallocated, and existing contents are overwritten with zeros regardless).

Parameters
rowsNumber of rows in the new logical matrix.
colsNumber of columns in the new logical matrix; defaults to 1.
Postcondition
this->rows() == rows && this->cols() == cols and every entry is T(0).

Definition at line 67 of file resizeable_matrix.h.

References cols(), and rows().

Referenced by ResizeableMatrix().

◆ rows()

template<typename T>
Eigen::Index momentum::ResizeableMatrix< T >::rows ( ) const
inlinenodiscard

Definition at line 80 of file resizeable_matrix.h.

Referenced by ResizeableMatrix(), and resizeAndSetZero().

◆ vec() [1/2]

template<typename T>
Eigen::Map< Eigen::Matrix< T, Eigen::Dynamic, 1 >, Eigen::Aligned128 > momentum::ResizeableMatrix< T >::vec ( )
inline

Returns an aligned, mutable Eigen::Map view interpreting the storage as a length-rows() column vector.

The cols() value is ignored; intended for the single-column case. The map is invalidated by any subsequent call to resizeAndSetZero(). TODO: vec() ignores cols_ and only exposes the first column when cols_ > 1. Either document this contract loudly or assert cols_ == 1.

Definition at line 103 of file resizeable_matrix.h.

◆ vec() [2/2]

template<typename T>
Eigen::Map< const Eigen::Matrix< T, Eigen::Dynamic, 1 >, Eigen::Aligned128 > momentum::ResizeableMatrix< T >::vec ( ) const
inline

Const overload of vec().

Definition at line 108 of file resizeable_matrix.h.

Member Data Documentation

◆ kAlignment

template<typename T>
std::size_t momentum::ResizeableMatrix< T >::kAlignment = 128
staticconstexpr

Alignment in bytes for the underlying storage.

Chosen to satisfy Eigen::Aligned128 so the Eigen::Map accessors below can perform aligned SIMD loads/stores.

Definition at line 44 of file resizeable_matrix.h.


The documentation for this class was generated from the following file: