Valkyrie 2026
Loading...
Searching...
No Matches
valor::EigenUnit::UnitVector< UnitTags > Class Template Reference

A vector wrapper that enforces units for each element. More...

#include <valkyrie/util/eigenunit/UnitVector.h>

Public Types

using StorageType = Eigen::Matrix<double, Size, 1>
 The underlying Eigen storage type.
using UnitTuple = std::tuple<UnitTags...>
 A tuple of the unit tags.

Public Member Functions

constexpr UnitVector ()
 Default constructor. Initializes all elements to zero.
constexpr UnitVector (const StorageType &d)
 Construct from a raw Eigen matrix.
constexpr UnitVector (StorageType &&d)
 Construct from a raw Eigen matrix (move).
constexpr UnitVector (const UnitVector &)=default
 Copy constructor.
constexpr UnitVector (UnitVector &&)=default
 Move constructor.
constexpr UnitVectoroperator= (const UnitVector &)=default
 Copy assignment.
constexpr UnitVectoroperator= (UnitVector &&)=default
 Move assignment.
constexpr UnitVector (units::unit_t< UnitTags >... args)
 Construct from individual unit values. Values are automatically converted to the units specified by UnitTags.
constexpr const StorageTypeRaw () const
 Get a constant reference to the underlying raw Eigen data.
constexpr StorageTypeRaw ()
 Get a reference to the underlying raw Eigen data.
template<std::size_t N>
requires ValidIndex<N, UnitTags...>
constexpr auto Get () const
 Get the value of the N-th element with its unit.
template<std::size_t N>
requires ValidIndex<N, UnitTags...>
void Set (units::unit_t< std::tuple_element_t< N, UnitTuple > > val)
 Set the value of the N-th element.
template<typename T = std::tuple_element_t<0, UnitTuple>>
requires AllSameUnits<UnitTags...>
auto Norm () const
 Computes the L2 norm (magnitude). Requires all elements to have the same unit tag.
auto Normalized () const
 Returns a normalized (unit-length) vector. Result is dimensionless. Only valid if all elements have the same unit.
template<typename... OtherTags>
requires (sizeof...(UnitTags) == sizeof...(OtherTags)) && AllSameUnits<MultiplyResult<UnitTags, OtherTags>...>
auto Dot (const UnitVector< OtherTags... > &other) const
 Dot product with another UnitVector. Resulting unit is the product of the first tags of both vectors.
template<std::size_t N>
requires ValidSize<N, UnitTags...>
constexpr auto Head () const
 Extract the first N elements as a new UnitVector.
template<std::size_t N>
requires ValidSize<N, UnitTags...>
constexpr auto Tail () const
 Extract the last N elements as a new UnitVector.
template<std::size_t Start, std::size_t N>
requires ValidSize<Start + N, UnitTags...>
constexpr auto Slice () const
 Extract N elements after the Start as a new UnitVector.
template<typename... OtherTags>
requires UnitEquality<UnitTuple, std::tuple<OtherTags...>>
constexpr UnitVector operator+ (const UnitVector< OtherTags... > &other) const
 Vector addition.
template<typename... OtherTags>
requires UnitEquality<UnitTuple, std::tuple<OtherTags...>>
constexpr UnitVector operator- (const UnitVector< OtherTags... > &other) const
 Vector subtraction.
constexpr UnitVector operator- () const
 Unary negation.
template<typename... OtherTags>
requires UnitEquality<UnitTuple, std::tuple<OtherTags...>>
constexpr UnitVectoroperator+= (const UnitVector< OtherTags... > &other)
 In-place vector addition.
template<typename... OtherTags>
requires UnitEquality<UnitTuple, std::tuple<OtherTags...>>
constexpr UnitVectoroperator-= (const UnitVector< OtherTags... > &other)
 In-place vector subtraction.
template<typename ScalarTag>
constexpr auto operator* (const units::unit_t< ScalarTag > &scalar) const
 Multiplication by a scalar unit.
template<typename ScalarTag>
constexpr auto operator/ (const units::unit_t< ScalarTag > &scalar) const
 Division by a scalar unit.
constexpr UnitVector operator* (double scalar) const
 Raw scaling (multiplication by double).
constexpr UnitVector operator/ (double scalar) const
 Raw scaling (division by double).
template<typename... OtherTags>
requires UnitEquality<UnitTuple, std::tuple<OtherTags...>>
bool operator== (const UnitVector< OtherTags... > &other) const
 Equality operator.
template<typename... OtherTags>
constexpr auto CWiseProduct (const UnitVector< OtherTags... > &other) const
 Component-wise product with another UnitVector.
template<typename... OtherTags>
constexpr auto CWiseQuotient (const UnitVector< OtherTags... > &other) const
 Component-wise quotient with another UnitVector.
constexpr UnitVector CWiseAbs () const
 Component-wise absolute value.
constexpr UnitVector CWiseMin (const UnitVector &other) const
 Component-wise minimum.
constexpr UnitVector CWiseMax (const UnitVector &other) const
 Component-wise maximum.
constexpr UnitVector Clamp (const UnitVector &lo, const UnitVector &hi) const
 Clamp each element between lo and hi vectors.
constexpr UnitVector Clamp (units::unit_t< std::tuple_element_t< 0, UnitTuple > > lo, units::unit_t< std::tuple_element_t< 0, UnitTuple > > hi) const
 Clamp each element between lo and hi unit values.
bool IsApprox (const UnitVector &other, units::unit_t< std::tuple_element_t< 0, UnitTuple > > tol=units::unit_t< std::tuple_element_t< 0, UnitTuple > >{1e-9}) const
 Check if two vectors are approximately equal.
template<typename T = std::tuple_element_t<0, UnitTuple>>
requires AllSameUnits<UnitTags...>
auto Sum () const
 Computes the sum of all elements. Only valid if all units are the same.
template<typename T = std::tuple_element_t<0, UnitTuple>>
requires AllSameUnits<UnitTags...>
auto Mean () const
 Computes the mean of all elements. Only valid if all units are the same.
constexpr auto X () const
 Access the first element (x).
void X (units::unit_t< std::tuple_element_t< Size >=1 ? 0 :0, UnitTuple > > val)
 Set the first element (x).
constexpr auto Y () const
 Access the second element (y).
void Y (units::unit_t< std::tuple_element_t< Size >=2 ? 1 :0, UnitTuple > > val)
 Set the second element (y).
constexpr auto Z () const
 Access the third element (z).
void Z (units::unit_t< std::tuple_element_t< Size >=3 ? 2 :0, UnitTuple > > val)
 Set the third element (z).
constexpr auto W () const
 Access the fourth element (w).
void W (units::unit_t< std::tuple_element_t< Size >=4 ? 3 :0, UnitTuple > > val)
 Set the fourth element (w).
auto AsDiagonal () const
 Converts the vector to a diagonal matrix.
template<typename ScalarTag>
requires AllSameUnits<UnitTags...> && UnitEquality<ScalarTag, std::tuple_element_t<0, UnitTuple>>
constexpr UnitVector operator+ (const units::unit_t< ScalarTag > &scalar) const
 Vector + scalar addition (broadcasting).
template<typename ScalarTag>
requires AllSameUnits<UnitTags...> && UnitEquality<ScalarTag, std::tuple_element_t<0, UnitTuple>>
constexpr UnitVector operator- (const units::unit_t< ScalarTag > &scalar) const
 Vector - scalar subtraction (broadcasting).
template<typename ScalarTag>
requires AllSameUnits<UnitTags...> && UnitEquality<ScalarTag, std::tuple_element_t<0, UnitTuple>>
constexpr UnitVectoroperator+= (const units::unit_t< ScalarTag > &scalar)
 In-place vector + scalar addition (broadcasting).
template<typename ScalarTag>
requires AllSameUnits<UnitTags...> && UnitEquality<ScalarTag, std::tuple_element_t<0, UnitTuple>>
constexpr UnitVectoroperator-= (const units::unit_t< ScalarTag > &scalar)
 In-place vector - scalar subtraction (broadcasting).
template<int P, typename T = std::tuple_element_t<0, UnitTuple>>
requires AllSameUnits<UnitTags...>
auto LPNorm () const
 Computes the Lp norm.
template<typename T = std::tuple_element_t<0, UnitTuple>>
requires AllSameUnits<UnitTags...>
auto StableNorm () const
 Computes the stable norm.
constexpr auto Begin ()
constexpr auto End ()
constexpr auto Begin () const
constexpr auto End () const

Static Public Member Functions

static constexpr UnitVector Zero ()
 Create a zero-initialized vector.
static UnitVector FromArray (const std::array< double, Size > &arr)
 Construct a UnitVector from a std::array of doubles. Values are assumed to be in the target units.
static UnitVector FromSpan (const std::span< double, Size > &s)
 Construct a UnitVector from a std::span of doubles. Values are assumed to be in the target units.
static UnitVector Constant (units::unit_t< typename std::tuple_element_t< 0, UnitTuple > > val)
 Create a vector where all elements are set to a constant value. Only valid if all units are the same.
static auto Skew (const UnitVector< UnitTags... > &vec)
 Converts a 3D vector to a skew-symmetric matrix.

Static Public Attributes

static constexpr std::size_t Size = sizeof...(UnitTags)
 The number of elements in the vector.

Friends

std::ostream & operator<< (std::ostream &os, const UnitVector &vec)
 Debug printing support.

Detailed Description

template<typename... UnitTags>
class valor::EigenUnit::UnitVector< UnitTags >

A vector wrapper that enforces units for each element.

Each element in the vector can have a different unit tag. Operations between UnitVectors are only permitted if their tag packs match exactly.

Template Parameters
UnitTagsThe unit types for each element in the vector.

Constructor & Destructor Documentation

◆ UnitVector() [1/3]

template<typename... UnitTags>
valor::EigenUnit::UnitVector< UnitTags >::UnitVector ( const StorageType & d)
inlineexplicitconstexpr

Construct from a raw Eigen matrix.

Parameters
dThe raw Eigen matrix containing values in the specified units.

◆ UnitVector() [2/3]

template<typename... UnitTags>
valor::EigenUnit::UnitVector< UnitTags >::UnitVector ( StorageType && d)
inlineexplicitconstexpr

Construct from a raw Eigen matrix (move).

Parameters
dThe raw Eigen matrix containing values in the specified units.

◆ UnitVector() [3/3]

template<typename... UnitTags>
valor::EigenUnit::UnitVector< UnitTags >::UnitVector ( units::unit_t< UnitTags >... args)
inlineconstexpr

Construct from individual unit values. Values are automatically converted to the units specified by UnitTags.

Parameters
argsThe unit values for each element.

Member Function Documentation

◆ AsDiagonal()

template<typename... UnitTags>
auto valor::EigenUnit::UnitVector< UnitTags >::AsDiagonal ( ) const

Converts the vector to a diagonal matrix.

Returns
A UnitMatrix with the vector elements on the diagonal.

◆ Clamp() [1/2]

template<typename... UnitTags>
UnitVector valor::EigenUnit::UnitVector< UnitTags >::Clamp ( const UnitVector< UnitTags > & lo,
const UnitVector< UnitTags > & hi ) const
inlinenodiscardconstexpr

Clamp each element between lo and hi vectors.

Parameters
loLower bound vector.
hiUpper bound vector.

◆ Clamp() [2/2]

template<typename... UnitTags>
UnitVector valor::EigenUnit::UnitVector< UnitTags >::Clamp ( units::unit_t< std::tuple_element_t< 0, UnitTuple > > lo,
units::unit_t< std::tuple_element_t< 0, UnitTuple > > hi ) const
inlinenodiscardconstexpr

Clamp each element between lo and hi unit values.

Parameters
loLower bound value.
hiUpper bound value.

◆ Constant()

template<typename... UnitTags>
UnitVector valor::EigenUnit::UnitVector< UnitTags >::Constant ( units::unit_t< typename std::tuple_element_t< 0, UnitTuple > > val)
inlinestatic

Create a vector where all elements are set to a constant value. Only valid if all units are the same.

Parameters
valThe constant value to set.
Returns
A UnitVector with all elements set to val.

◆ CWiseProduct()

template<typename... UnitTags>
template<typename... OtherTags>
auto valor::EigenUnit::UnitVector< UnitTags >::CWiseProduct ( const UnitVector< OtherTags... > & other) const
inlinenodiscardconstexpr

Component-wise product with another UnitVector.

Template Parameters
OtherTagsUnit tags of the other vector.
Parameters
otherThe other UnitVector.
Returns
A new UnitVector with multiplied units.

◆ CWiseQuotient()

template<typename... UnitTags>
template<typename... OtherTags>
auto valor::EigenUnit::UnitVector< UnitTags >::CWiseQuotient ( const UnitVector< OtherTags... > & other) const
inlinenodiscardconstexpr

Component-wise quotient with another UnitVector.

Template Parameters
OtherTagsUnit tags of the other vector.
Parameters
otherThe other UnitVector.
Returns
A new UnitVector with divided units.

◆ Dot()

template<typename... UnitTags>
template<typename... OtherTags>
requires (sizeof...(UnitTags) == sizeof...(OtherTags)) && AllSameUnits<MultiplyResult<UnitTags, OtherTags>...>
auto valor::EigenUnit::UnitVector< UnitTags >::Dot ( const UnitVector< OtherTags... > & other) const
inlinenodiscard

Dot product with another UnitVector. Resulting unit is the product of the first tags of both vectors.

Template Parameters
OtherTagsUnit tags of the other vector.
Parameters
otherThe other UnitVector to dot with.
Returns
The dot product as a unit_t.

◆ FromArray()

template<typename... UnitTags>
UnitVector valor::EigenUnit::UnitVector< UnitTags >::FromArray ( const std::array< double, Size > & arr)
inlinestatic

Construct a UnitVector from a std::array of doubles. Values are assumed to be in the target units.

Parameters
arrThe array of values.
Returns
A UnitVector initialized with the given values.

◆ FromSpan()

template<typename... UnitTags>
UnitVector valor::EigenUnit::UnitVector< UnitTags >::FromSpan ( const std::span< double, Size > & s)
inlinestatic

Construct a UnitVector from a std::span of doubles. Values are assumed to be in the target units.

Parameters
sThe span of values.
Returns
A UnitVector initialized with the given values.

◆ Get()

template<typename... UnitTags>
template<std::size_t N>
requires ValidIndex<N, UnitTags...>
auto valor::EigenUnit::UnitVector< UnitTags >::Get ( ) const
inlineconstexpr

Get the value of the N-th element with its unit.

Template Parameters
NThe index of the element to retrieve.
Returns
The value of the N-th element as a units::unit_t.

◆ Head()

template<typename... UnitTags>
template<std::size_t N>
requires ValidSize<N, UnitTags...>
auto valor::EigenUnit::UnitVector< UnitTags >::Head ( ) const
inlineconstexpr

Extract the first N elements as a new UnitVector.

Template Parameters
NThe number of elements to extract.
Returns
A new UnitVector containing the first N elements.

◆ IsApprox()

template<typename... UnitTags>
bool valor::EigenUnit::UnitVector< UnitTags >::IsApprox ( const UnitVector< UnitTags > & other,
units::unit_t< std::tuple_element_t< 0, UnitTuple > > tol = units::unit_t<std::tuple_element_t<0, UnitTuple>>{1e-9} ) const
inline

Check if two vectors are approximately equal.

Parameters
otherThe other vector.
tolThe tolerance.
Returns
True if approximately equal.

◆ LPNorm()

template<typename... UnitTags>
template<int P, typename T = std::tuple_element_t<0, UnitTuple>>
requires AllSameUnits<UnitTags...>
auto valor::EigenUnit::UnitVector< UnitTags >::LPNorm ( ) const
inlinenodiscard

Computes the Lp norm.

Template Parameters
PThe order of the norm.

◆ Norm()

template<typename... UnitTags>
template<typename T = std::tuple_element_t<0, UnitTuple>>
requires AllSameUnits<UnitTags...>
auto valor::EigenUnit::UnitVector< UnitTags >::Norm ( ) const
inlinenodiscard

Computes the L2 norm (magnitude). Requires all elements to have the same unit tag.

Template Parameters
TThe unit type of the elements (defaults to the first element's unit).
Returns
The L2 norm of the vector with unit T.

◆ Normalized()

template<typename... UnitTags>
auto valor::EigenUnit::UnitVector< UnitTags >::Normalized ( ) const
inline

Returns a normalized (unit-length) vector. Result is dimensionless. Only valid if all elements have the same unit.

Returns
A dimensionless Eigen vector representing the normalized UnitVector.

◆ operator*() [1/2]

template<typename... UnitTags>
template<typename ScalarTag>
auto valor::EigenUnit::UnitVector< UnitTags >::operator* ( const units::unit_t< ScalarTag > & scalar) const
inlinenodiscardconstexpr

Multiplication by a scalar unit.

Template Parameters
ScalarTagThe unit type of the scalar.
Parameters
scalarThe scalar value.
Returns
A new UnitVector with each element multiplied by the scalar.

◆ operator*() [2/2]

template<typename... UnitTags>
UnitVector valor::EigenUnit::UnitVector< UnitTags >::operator* ( double scalar) const
inlinenodiscardconstexpr

Raw scaling (multiplication by double).

Parameters
scalarThe scaling factor.
Returns
A new UnitVector scaled by the factor.

◆ operator/() [1/2]

template<typename... UnitTags>
template<typename ScalarTag>
auto valor::EigenUnit::UnitVector< UnitTags >::operator/ ( const units::unit_t< ScalarTag > & scalar) const
inlinenodiscardconstexpr

Division by a scalar unit.

Template Parameters
ScalarTagThe unit type of the scalar.
Parameters
scalarThe scalar value.
Returns
A new UnitVector with each element divided by the scalar.

◆ operator/() [2/2]

template<typename... UnitTags>
UnitVector valor::EigenUnit::UnitVector< UnitTags >::operator/ ( double scalar) const
inlinenodiscardconstexpr

Raw scaling (division by double).

Parameters
scalarThe scaling factor.
Returns
A new UnitVector divided by the factor.

◆ Raw() [1/2]

template<typename... UnitTags>
StorageType & valor::EigenUnit::UnitVector< UnitTags >::Raw ( )
inlineconstexpr

Get a reference to the underlying raw Eigen data.

Returns
Reference to the raw Eigen matrix.

◆ Raw() [2/2]

template<typename... UnitTags>
const StorageType & valor::EigenUnit::UnitVector< UnitTags >::Raw ( ) const
inlineconstexpr

Get a constant reference to the underlying raw Eigen data.

Returns
Constant reference to the raw Eigen matrix.

◆ Set()

template<typename... UnitTags>
template<std::size_t N>
requires ValidIndex<N, UnitTags...>
void valor::EigenUnit::UnitVector< UnitTags >::Set ( units::unit_t< std::tuple_element_t< N, UnitTuple > > val)
inline

Set the value of the N-th element.

Template Parameters
NThe index of the element to set.
Parameters
valThe unit value to set.

◆ Skew()

template<typename... UnitTags>
auto valor::EigenUnit::UnitVector< UnitTags >::Skew ( const UnitVector< UnitTags... > & vec)
inlinestatic

Converts a 3D vector to a skew-symmetric matrix.

Returns
A 3x3 skew-symmetric UnitMatrix.

◆ Slice()

template<typename... UnitTags>
template<std::size_t Start, std::size_t N>
requires ValidSize<Start + N, UnitTags...>
auto valor::EigenUnit::UnitVector< UnitTags >::Slice ( ) const
inlineconstexpr

Extract N elements after the Start as a new UnitVector.

Template Parameters
StartThe offset to begin extracting at
NThe number of elements to extract after the offset
Returns
A new UnitVector containing the number of elements after the start

◆ Tail()

template<typename... UnitTags>
template<std::size_t N>
requires ValidSize<N, UnitTags...>
auto valor::EigenUnit::UnitVector< UnitTags >::Tail ( ) const
inlineconstexpr

Extract the last N elements as a new UnitVector.

Template Parameters
NThe number of elements to extract.
Returns
A new UnitVector containing the last N elements.

◆ Zero()

template<typename... UnitTags>
constexpr UnitVector valor::EigenUnit::UnitVector< UnitTags >::Zero ( )
inlinestaticconstexpr

Create a zero-initialized vector.

Returns
A UnitVector with all elements set to zero.

◆ operator<<

template<typename... UnitTags>
std::ostream & operator<< ( std::ostream & os,
const UnitVector< UnitTags > & vec )
friend

Debug printing support.

Parameters
osThe output stream.
vecThe UnitVector to print.
Returns
The output stream.

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