mu
mu::Vector< N, T > Class Template Reference

A generic vector. More...

#include <vector.h>

Public Types

using value_type = typename std::array< T, N >::value_type
 
using size_type = typename std::array< T, N >::size_type
 
using iterator = typename std::array< T, N >::iterator
 
using const_iterator = typename std::array< T, N >::const_iterator
 

Public Member Functions

constexpr Vector ()=default
 Construct a new Vector object. More...
 
template<typename... TArgs>
 Vector (TArgs &&... args)
 Construct a new Vector object from a number of N values. More...
 
template<std::size_t Nn, class U >
 Vector (const Vector< Nn, U > &v)
 Construct a new Vector from an existing Vector of a different type. More...
 
 Vector (const std::array< T, N > &a)
 Construct a new Vector object from an std::array. More...
 
template<typename U = T, std::enable_if_t< std::is_arithmetic< U >::value, int > = 0>
 Vector (const std::array< U, N > &a)
 Construct a new Vector object from an std::array of a different type. More...
 
template<typename U = T, std::enable_if_t< std::is_arithmetic< U >::value, int > = 0>
 Vector (const U &value)
 Construct a new Vector object from a single value. More...
 
 ~Vector ()=default
 Destroy the Vector object.
 
 Vector (const Vector &other)=default
 Copy construct a new Vector object. More...
 
 Vector (Vector &&other) noexcept=default
 Move construct a new Vector object. More...
 
Vectoroperator= (const Vector &other)=default
 Copy assignment operator. More...
 
Vectoroperator= (Vector &&other) noexcept=default
 Move assignment operator. More...
 
T & operator[] (size_type idx) noexcept
 access an element within the vector More...
 
const T & operator[] (size_type idx) const noexcept
 const access an element within the vector More...
 
T & at (size_type idx)
 access an element within the vector More...
 
const T & at (size_type idx) const
 const access an element within the vector More...
 
constexpr size_type size () const noexcept
 returns the size of the vector More...
 
iterator begin () noexcept
 returns an iterator pointing to the first element More...
 
const_iterator begin () const noexcept
 returns a const iterator pointing to the first element More...
 
iterator end () noexcept
 returns an iterator pointing to the element following the last element More...
 
const_iterator end () const noexcept
 returns a const iterator pointing to the element following the last element More...
 
min () const
 get the min value of the vector More...
 
max () const
 get the max value of the vector More...
 
sum () const
 sum up all the elements of the vector More...
 
template<typename U = T>
mean () const
 mean of all the elements of the vector More...
 
template<typename U = void, std::size_t N2, typename T2 >
std::conditional_t< std::is_same< U, void >::value, T, U > dot (const Vector< N2, T2 > &rhs) const
 dot product of two vectors More...
 
template<typename U = void, std::size_t N2, std::size_t M2, typename T2 >
std::conditional_t< std::is_same< U, void >::value, Vector< M2, T >, Vector< M2, U > > dot (const Matrix< N2, M2, T2 > &rhs) const
 dot product of a vector and a matrix More...
 
template<class U = T>
std () const
 calculates the standard deviation More...
 
template<class U = T>
length () const
 euclidean vector length More...
 
void normalize ()
 normalizes this vector More...
 
Vector< N, T > normalized ()
 returns a normalized vector More...
 
void flip ()
 flips this vector, i.e. reverses its elements More...
 
Vector< N, T > flipped () const
 returns a flipped vector More...
 
void sort ()
 sort vector elements in ascending order More...
 
template<typename Compare >
void sort (const Compare &comp)
 sort vector elements by providing a condition More...
 
Vector< N, T > sorted () const
 returns a sorted vector More...
 
template<typename Compare >
Vector< N, T > sorted (const Compare &comp) const
 
template<typename U = T>
bool operator== (const Vector< N, U > &rhs) const
 equality operator More...
 
template<typename U = T>
bool operator!= (const Vector< N, U > &rhs) const
 unequality operator More...
 
template<typename U = T>
Vector< N, T > & operator+= (const Vector< N, U > &rhs)
 plus equal operator More...
 
template<typename U = T>
Vector< N, T > & operator-= (const Vector< N, U > &rhs)
 minus equal operator More...
 
template<typename U = T>
Vector< N, T > & operator*= (const Vector< N, U > &rhs)
 multiplication equal operator More...
 
template<typename U = T>
Vector< N, T > & operator/= (const Vector< N, U > &rhs)
 divison equal operator More...
 
template<class TScalar >
std::enable_if_t< std::is_arithmetic< TScalar >::value, Vector< N, T > & > operator+= (const TScalar &scalar)
 add a scalar to this vector More...
 
template<class TScalar >
std::enable_if_t< std::is_arithmetic< TScalar >::value, Vector< N, T > & > operator-= (const TScalar &scalar)
 subtract a scalar from every element of this vector More...
 
template<class TScalar >
std::enable_if_t< std::is_arithmetic< TScalar >::value, Vector< N, T > & > operator*= (const TScalar &scalar)
 multiply a scalar with this vector More...
 
template<class TScalar >
std::enable_if_t< std::is_arithmetic< TScalar >::value, Vector< N, T > & > operator/= (const TScalar &scalar)
 divide every element of this vector by a scalar More...
 

Protected Attributes

std::array< T, N > data_
 

Friends

template<std::size_t Nn, class U >
std::ostream & operator<< (std::ostream &os, const Vector< Nn, U > &v)
 print vector values More...
 

Detailed Description

template<std::size_t N, typename T>
class mu::Vector< N, T >

A generic vector.

Can be instantiated with an arithmetic type as defined by the standard library.

  • implementation-defined extended integer types, including any signed, unsigned, and cv-qualified variants. (bool, char, int, long ...)
  • implementation-defined extended floating-point types including any cv-qualified variants. (float, double, long double)
Template Parameters
Nsize
Tthe type of the values inside the vector

Constructor & Destructor Documentation

◆ Vector() [1/8]

template<std::size_t N, typename T>
constexpr mu::Vector< N, T >::Vector ( )
default

Construct a new Vector object.

Example
Here is the caller graph for this function:

◆ Vector() [2/8]

template<std::size_t N, typename T>
template<typename... TArgs>
mu::Vector< N, T >::Vector ( TArgs &&...  args)
inline

Construct a new Vector object from a number of N values.

the number of values must match the static size of this Vector
the type must be arithmetic or a reference of an arithmetic

Example
// direct initialization
// list initialization
mu::Vector<2, int> b = {2, 3};
Template Parameters
TArgs
Parameters
args

◆ Vector() [3/8]

template<std::size_t N, typename T>
template<std::size_t Nn, class U >
mu::Vector< N, T >::Vector ( const Vector< Nn, U > &  v)
inline

Construct a new Vector from an existing Vector of a different type.

implicit narrowing may be applied
it is checked that the Vector sizes are the same

Example
mu::Vector<2, float> a = {1.5F, 2.5F};
Template Parameters
Nn
U
Parameters
v

◆ Vector() [4/8]

template<std::size_t N, typename T>
mu::Vector< N, T >::Vector ( const std::array< T, N > &  a)
inline

Construct a new Vector object from an std::array.

Example
std::array<int, 3> arr = {3, 4, 5};
// direct initialization
// copy initialization
// list initialization
mu::Vector<3, int> c = {arr};
Parameters
a

◆ Vector() [5/8]

template<std::size_t N, typename T>
template<typename U = T, std::enable_if_t< std::is_arithmetic< U >::value, int > = 0>
mu::Vector< N, T >::Vector ( const std::array< U, N > &  a)
inline

Construct a new Vector object from an std::array of a different type.

implicit narrowing may be applied

Example
std::array<float, 3> arr = {3.5F, 4.5F, 5.5F};
// direct initialization
// copy initialization
// list initialization
mu::Vector<3, int> c = {arr};
Template Parameters
U
Parameters
a

◆ Vector() [6/8]

template<std::size_t N, typename T>
template<typename U = T, std::enable_if_t< std::is_arithmetic< U >::value, int > = 0>
mu::Vector< N, T >::Vector ( const U &  value)
inline

Construct a new Vector object from a single value.

all the values inside the vector will be set to this value

Example
// direct initialization
// copy initialization
// list initialization
Template Parameters
U
Parameters
value

◆ Vector() [7/8]

template<std::size_t N, typename T>
mu::Vector< N, T >::Vector ( const Vector< N, T > &  other)
default

Copy construct a new Vector object.

Example
Parameters
other

◆ Vector() [8/8]

template<std::size_t N, typename T>
mu::Vector< N, T >::Vector ( Vector< N, T > &&  other)
defaultnoexcept

Move construct a new Vector object.

Example
mu::Vector<2, int> b{std::move(a)};
Parameters
other

Member Function Documentation

◆ at() [1/2]

template<std::size_t N, typename T>
T& mu::Vector< N, T >::at ( size_type  idx)
inline

access an element within the vector

Example
int first = a.at(0);
int second = a.at(1);
Exceptions
outof range
Parameters
idx
Returns
T&

◆ at() [2/2]

template<std::size_t N, typename T>
const T& mu::Vector< N, T >::at ( size_type  idx) const
inline

const access an element within the vector

Example
const mu::Vector<2, int> a{2, 3};
int first = a.at(0);
int second = a.at(1);
Exceptions
outof range
Parameters
idx
Returns
const T&

◆ begin() [1/2]

template<std::size_t N, typename T>
iterator mu::Vector< N, T >::begin ( )
inlinenoexcept

returns an iterator pointing to the first element

Example
int first = *a.begin(); // 2
Returns
iterator
Here is the caller graph for this function:

◆ begin() [2/2]

template<std::size_t N, typename T>
const_iterator mu::Vector< N, T >::begin ( ) const
inlinenoexcept

returns a const iterator pointing to the first element

Example
const mu::Vector<2, int> a{2, 3};
int first = *a.begin(); // 2
Returns
const_iterator

◆ dot() [1/2]

template<std::size_t N, typename T>
template<typename U = void, std::size_t N2, typename T2 >
std::conditional_t<std::is_same<U, void>::value, T, U> mu::Vector< N, T >::dot ( const Vector< N2, T2 > &  rhs) const
inline

dot product of two vectors

For a Vector \( a \) of size \( N \) and a Vector \( b \) of size \( N \) the result is a scalar,

\( a \cdot b = \sum_{i=1}^{N} a_i b_i \)

Vector sizes must be equal

For two Vectors of the same type, specifying the return type is optional. It will be of the type of the two Vectors by default.

For two Vectors of different types, specifying the return type is required.

return value is a scalar of the type of the two Vectors or else of the explicitly stated type

Example
// same types
int res1 = a.dot(b); // 11
// different types (must specify internal precision)
mu::Vector<2, float> d{3.5F, 4.5F};
float res2 = c.dot<int>(d); // 12
float res3 = c.dot<float>(d); // 12.5
Template Parameters
U
N2
T2
Parameters
rhs
Returns
std::conditional_t<std::is_same<U, void>::value, T, U>
Here is the caller graph for this function:

◆ dot() [2/2]

template<std::size_t N, typename T>
template<typename U = void, std::size_t N2, std::size_t M2, typename T2 >
std::conditional_t<std::is_same<U, void>::value, Vector<M2, T>, Vector<M2, U> > mu::Vector< N, T >::dot ( const Matrix< N2, M2, T2 > &  rhs) const
inline

dot product of a vector and a matrix

For a Vector \( a \) of size \( N \) and a Matrix \( B \) of size \( N \times M \) the result is a Vector \( c \) of size \( M \),

\( c = a \cdot B \) with

\( c_{j} = a_1 b_{1j} + a_2 b_{2j} + ... + a_N b_{Nj} = \sum_{i=1}^{N} a_i B_{ij} \)

Vector size must be equal to the size of the first Matrix dimension (N)

For two objects of the same type, specifying the return type is optional. It will be of the type of the two objects by default.

For two objects of different types, specifying the return type is required.

return value is a Vector of the size of the second Matrix dimension (M) containing the type of the two objects or else of the explicitly stated type

Example
// same types
mu::Matrix<2, 3, int> b{{3, 4, 5}, {6, 7, 8}};
mu::Vector<3, int> res1 = a.dot(b); // [ 15, 18, 21 ]
// different types (must specify internal precision)
mu::Vector<2, float> c{1.5F, 2.5F};
mu::Matrix<2, 3, int> d{{3, 4, 5}, {6, 7, 8}};
mu::Vector<3, float> res2 = c.dot<int>(d); // [ 19, 23, 27 ]
mu::Vector<3, float> res3 = c.dot<float>(d); // [ 19.5, 23.5, 27.5 ]
Template Parameters
U
N2
M2
T2
Parameters
rhs
Returns
std::conditional_t<std::is_same<U, void>::value, Vector<M2, T>, Vector<M2, U>>

◆ end() [1/2]

template<std::size_t N, typename T>
iterator mu::Vector< N, T >::end ( )
inlinenoexcept

returns an iterator pointing to the element following the last element

Example
int last = *(a.end() - 1); // 3
Returns
iterator
Here is the caller graph for this function:

◆ end() [2/2]

template<std::size_t N, typename T>
const_iterator mu::Vector< N, T >::end ( ) const
inlinenoexcept

returns a const iterator pointing to the element following the last element

Example
const mu::Vector<2, int> a{2, 3};
int last = *(a.end() - 1); // 3
Returns
const_iterator

◆ flip()

template<std::size_t N, typename T>
void mu::Vector< N, T >::flip ( )
inline

flips this vector, i.e. reverses its elements

Example
mu::Vector<3, int> a{1, 2, 3};
a.flip(); // a is now [ 3, 2, 1 ]
Here is the caller graph for this function:

◆ flipped()

template<std::size_t N, typename T>
Vector<N, T> mu::Vector< N, T >::flipped ( ) const
inline

returns a flipped vector

Example
mu::Vector<3, int> a{1, 2, 3};
mu::Vector<3, int> b = a.flipped(); // b is now [ 3, 2, 1 ]
See also
flip()
Returns
Vector<N, T>
Here is the caller graph for this function:

◆ length()

template<std::size_t N, typename T>
template<class U = T>
U mu::Vector< N, T >::length ( ) const
inline

euclidean vector length

returns the length as

  • the type of this vector (default)
  • the explicitly stated type
Example
float len1 = a.length(); // 2 (because a holds type int)
float len2 = a.length<float>(); // 2.23606801
Template Parameters
U
Returns
U
Here is the caller graph for this function:

◆ max()

template<std::size_t N, typename T>
T mu::Vector< N, T >::max ( ) const
inline

get the max value of the vector

Example
int max = a.max(); // 3
Returns
T
Here is the caller graph for this function:

◆ mean()

template<std::size_t N, typename T>
template<typename U = T>
U mu::Vector< N, T >::mean ( ) const
inline

mean of all the elements of the vector

\( \mu = \frac{\sum(x_i)}{N} \)

returns the mean as

  • the type of this vector (default)
  • the explicitly stated type
Example
float mean1 = a.mean(); // 2 (because a holds type int)
float mean2 = a.mean<float>(); // 2.5F
Template Parameters
U
Returns
U
Here is the caller graph for this function:

◆ min()

template<std::size_t N, typename T>
T mu::Vector< N, T >::min ( ) const
inline

get the min value of the vector

Example
int min = a.min(); // 2
Returns
T
Here is the caller graph for this function:

◆ normalize()

template<std::size_t N, typename T>
void mu::Vector< N, T >::normalize ( )
inline

normalizes this vector

\( v_{norm} = \frac{v}{\sqrt \sum_{i=1}^{N} v_i^2} \)

divides every element by the euclidean vector length
the resulting euclidean vector length will be 1

Example
mu::Vector<2, float> a{3.0F, 4.0F};
a.normalize(); // a is now [ 0.6, 0.8 ]
Here is the caller graph for this function:

◆ normalized()

template<std::size_t N, typename T>
Vector<N, T> mu::Vector< N, T >::normalized ( )
inline

returns a normalized vector

Example
mu::Vector<2, float> a{3.0F, 4.0F};
mu::Vector<2, float> a_norm = a.normalized(); // a_norm is [ 0.6, 0.8 ]
See also
normalize()
Returns
Vector<N, T>

◆ operator!=()

template<std::size_t N, typename T>
template<typename U = T>
bool mu::Vector< N, T >::operator!= ( const Vector< N, U > &  rhs) const
inline

unequality operator

Parameters
rhs
Returns
bool true if unequal, false if equal

◆ operator*=() [1/2]

template<std::size_t N, typename T>
template<typename U = T>
Vector<N, T>& mu::Vector< N, T >::operator*= ( const Vector< N, U > &  rhs)
inline

multiplication equal operator

subject to implicit conversions

Template Parameters
U
Parameters
rhs
Returns
Vector<N, T>&

◆ operator*=() [2/2]

template<std::size_t N, typename T>
template<class TScalar >
std::enable_if_t<std::is_arithmetic<TScalar>::value, Vector<N, T> &> mu::Vector< N, T >::operator*= ( const TScalar &  scalar)
inline

multiply a scalar with this vector

Template Parameters
TScalar
Parameters
scalar
Returns
std::enable_if_t<std::is_arithmetic<TScalar>::value,Vector<N, T> &>

◆ operator+=() [1/2]

template<std::size_t N, typename T>
template<typename U = T>
Vector<N, T>& mu::Vector< N, T >::operator+= ( const Vector< N, U > &  rhs)
inline

plus equal operator

subject to implicit conversions

Template Parameters
U
Parameters
rhs
Returns
Vector<N, T>&

◆ operator+=() [2/2]

template<std::size_t N, typename T>
template<class TScalar >
std::enable_if_t<std::is_arithmetic<TScalar>::value, Vector<N, T> &> mu::Vector< N, T >::operator+= ( const TScalar &  scalar)
inline

add a scalar to this vector

Template Parameters
TScalar
Parameters
scalar
Returns
std::enable_if_t<std::is_arithmetic<TScalar>::value,Vector<N, T> &>

◆ operator-=() [1/2]

template<std::size_t N, typename T>
template<typename U = T>
Vector<N, T>& mu::Vector< N, T >::operator-= ( const Vector< N, U > &  rhs)
inline

minus equal operator

subject to implicit conversions

Template Parameters
U
Parameters
rhs
Returns
Vector<N, T>&

◆ operator-=() [2/2]

template<std::size_t N, typename T>
template<class TScalar >
std::enable_if_t<std::is_arithmetic<TScalar>::value, Vector<N, T> &> mu::Vector< N, T >::operator-= ( const TScalar &  scalar)
inline

subtract a scalar from every element of this vector

Template Parameters
TScalar
Parameters
scalar
Returns
std::enable_if_t<std::is_arithmetic<TScalar>::value, Vector<N, T> &>

◆ operator/=() [1/2]

template<std::size_t N, typename T>
template<typename U = T>
Vector<N, T>& mu::Vector< N, T >::operator/= ( const Vector< N, U > &  rhs)
inline

divison equal operator

subject to implicit conversions division by zero on integral type vector elements results in undefined behavior

Template Parameters
U
Parameters
rhs
Returns
Vector<N, T>&

◆ operator/=() [2/2]

template<std::size_t N, typename T>
template<class TScalar >
std::enable_if_t<std::is_arithmetic<TScalar>::value, Vector<N, T> &> mu::Vector< N, T >::operator/= ( const TScalar &  scalar)
inline

divide every element of this vector by a scalar

division by zero on integral types triggers an assert

Template Parameters
TScalar
Parameters
scalar
Returns
std::enable_if_t<std::is_arithmetic<TScalar>::value, Vector<N, T> &>

◆ operator=() [1/2]

template<std::size_t N, typename T>
Vector& mu::Vector< N, T >::operator= ( const Vector< N, T > &  other)
default

Copy assignment operator.

Example
Parameters
other
Returns
Vector&
Here is the caller graph for this function:

◆ operator=() [2/2]

template<std::size_t N, typename T>
Vector& mu::Vector< N, T >::operator= ( Vector< N, T > &&  other)
defaultnoexcept

Move assignment operator.

Example
mu::Vector<2, int> b = std::move(a);
Parameters
other
Returns
Vector&

◆ operator==()

template<std::size_t N, typename T>
template<typename U = T>
bool mu::Vector< N, T >::operator== ( const Vector< N, U > &  rhs) const
inline

equality operator

comparisons between different arithmetic types are possible. they are subject to the c++ usual arithmetic conversions!

checked both ways for equality. First, casting both values to the type of the first argument, then casting both values to the type of the second argument.

Parameters
rhs
Returns
bool true if equal, false if unequal
Here is the caller graph for this function:

◆ operator[]() [1/2]

template<std::size_t N, typename T>
T& mu::Vector< N, T >::operator[] ( size_type  idx)
inlinenoexcept

access an element within the vector

does not throw an exception if idx is out of range

Example
int first = a[0];
int second = a[1];
Parameters
idx
Returns
T&

◆ operator[]() [2/2]

template<std::size_t N, typename T>
const T& mu::Vector< N, T >::operator[] ( size_type  idx) const
inlinenoexcept

const access an element within the vector

does not throw an exception if idx is out of range

Example
const mu::Vector<2, int> a{2, 3};
int first = a[0];
int second = a[1];
Parameters
idx
Returns
const T&

◆ size()

template<std::size_t N, typename T>
constexpr size_type mu::Vector< N, T >::size ( ) const
inlinenoexcept

returns the size of the vector

Example
mu::Vector<3, int> a{2, 3, 4};
std::size_t size = a.size(); // 3
Returns
size_type

◆ sort() [1/2]

template<std::size_t N, typename T>
void mu::Vector< N, T >::sort ( )
inline

sort vector elements in ascending order

Example
mu::Vector<5, int> a{6, 4, 9, 3, 1};
a.sort(); // a is now [ 1, 3, 4, 6, 9 ]
Here is the caller graph for this function:

◆ sort() [2/2]

template<std::size_t N, typename T>
template<typename Compare >
void mu::Vector< N, T >::sort ( const Compare &  comp)
inline

sort vector elements by providing a condition

the condition should be a lambda function. it must return a bool

Example
mu::Vector<5, int> a{6, 4, 9, 3, 1};
a.sort([](int a, int b) { return a > b; }); // a is now [ 9, 6, 4, 3, 1 ]
Template Parameters
Compare
Parameters
comp

◆ sorted() [1/2]

template<std::size_t N, typename T>
Vector<N, T> mu::Vector< N, T >::sorted ( ) const
inline

returns a sorted vector

Example
mu::Vector<5, int> a{6, 4, 9, 3, 1};
mu::Vector<5, int> b = a.sorted(); // b is now [ 1, 3, 4, 6, 9 ]
Returns
Vector<N, T>
Here is the caller graph for this function:

◆ sorted() [2/2]

template<std::size_t N, typename T>
template<typename Compare >
Vector<N, T> mu::Vector< N, T >::sorted ( const Compare &  comp) const
inline
Example
mu::Vector<5, int> a{6, 4, 9, 3, 1};
[](int a, int b) { return a > b; }); // b is now [ 9, 6, 4, 3, 1 ]
Template Parameters
Compare
Parameters
comp
Returns
Vector<N, T>

◆ std()

template<std::size_t N, typename T>
template<class U = T>
U mu::Vector< N, T >::std ( ) const
inline

calculates the standard deviation

$$ = {{(x_i - )^2}{N}} $$

Example
mu::Vector<4, int> a{2, 3, 5, 7};
float std1 = a.std(); // 1 (because a holds type int)
float std2 = a.std<float>(); // 1.92028642
Template Parameters
U
Returns
U

◆ sum()

template<std::size_t N, typename T>
T mu::Vector< N, T >::sum ( ) const
inline

sum up all the elements of the vector

Example
int sum = a.sum(); // 5
Returns
T
Here is the caller graph for this function:

Friends And Related Function Documentation

◆ operator<<

template<std::size_t N, typename T>
template<std::size_t Nn, class U >
std::ostream& operator<< ( std::ostream &  os,
const Vector< Nn, U > &  v 
)
friend

print vector values

Example
mu::Vector<4, int> a{2, 3, 4, 5};
// std::cout << a << std::endl;
// [ 2, 3, 4, 5 ]
Template Parameters
Nn
U
Parameters
os
v
Returns
std::ostream&

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