MathToolKit  1.1.1
C++ maths framework
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
NVector< T > Class Template Reference

A NVector<T> object represents the coordinates of a finite dimension dense vector \( x \). More...

Inheritance diagram for NVector< T >:
NPMatrix< T >

Public Member Functions

 NVector (const std::vector< T > &data)
 Construct a vector using a std::vector. \( x_k \) represents data[k]. More...
 
 NVector (std::initializer_list< T > list)
 Construct a vector using an initializer list {}. More...
 
 NVector (size_t dim=0)
 Construct a vector of a given size. More...
 
 NVector (const NVector< T > &u)
 Construct a vector by using copy() method. More...
 
virtual ~NVector ()=default
 
virtual std::string str () const
 Creates a string representing the vector. More...
 
size_t dim () const
 Dimension of the vector. More...
 
std::vector< T > array () const
 Creates an array with this vector. More...
 
NVector< T > & operator+= (const NVector< T > &u)
 
NVector< T > & operator-= (const NVector< T > &u)
 
virtual NVector< T > & operator*= (T s)
 
virtual NVector< T > & operator/= (T s)
 
NVector< T > & operator= (const NVector< T > &u)
 Copy source object on this object using copy(). More...
 
Extremums

Methods related to maximum minimum value research. They all use std::max or std::min.

max () const
 Maximum of the coordinates of the vector \( (x_0, x_1, .. x_{(n-1)}) \). More...
 
min () const
 Minimum of the coordinates of the vector \( (x_0, x_1, .. x_{(n-1)}) \). More...
 
size_t maxIndex () const
 Index of maximum of the coordinates of vector \( (x_0, x_1, ..., x_{(n-1)}) \). More...
 
size_t minIndex () const
 Index of minimum of the coordinates of vector \( (x_0, x_1, ..., x_{(n-1)}) \). More...
 
maxAbs () const
 Maximum of the coordinates of the vector \( (|x_0|, |x_1|, ..., |x_{(n-1)}|) \). More...
 
minAbs () const
 Minimum of the coordinates of the vector \( (|x_0|, |x_1|, ..., |x_{(n-1)}|) \). More...
 
size_t maxAbsIndex () const
 Index of maximum of the coordinates of vector \( (|x_0|, |x_1|, ..., |x_{(n-1)}|) \). More...
 
size_t minAbsIndex () const
 Index of minimum of the coordinates of vector \( (|x_0|, |x_1|, ..., |x_{(n-1)}|) \). More...
 
Manipulators

Manipulators such as swap, shift...

All the manipulator return a reference to *this in order to allow cascade calls eg swap().shift().

NVector< T > & swap (size_t k1, size_t k2)
 Permutation of two elements. More...
 
NVector< T > & shift (long iterations)
 Shifts this vector iterations times. More...
 
NVector< T > & fill (T s)
 Fill vector with a scalar. For example fill(3) will set \( (3, 3, 3, ..., 3) \). More...
 
Function Operator

Access to components and sub-matrix

C++ function operator are here used to give intuitive access to the vector. It behaves like numpy [:].

T & operator() (long k)
 Simple access operator. More...
 
operator() (long k) const
 
NVector< T > operator() (size_t k1, size_t k2) const
 Manipulate sub-vector. More...
 
NVector< T > & operator() (size_t k1, size_t k2)
 Manipulate sub-vector. More...
 
Iterators

Similar iterators as std::vector except that they allow use of operator()().

iterator begin ()
 
const_iterator begin () const
 
iterator end ()
 
const_iterator end () const
 

Static Public Member Functions

static NVector< T > zeros (size_t dim)
 
static NVector< T > ones (size_t dim)
 
static NVector< T > scalar (T s, size_t dim)
 
static NVector< T > cano (size_t k, size_t dim)
 
static NVector< T > sum (const std::vector< NVector > &vectors)
 Sum of multiples vectors. More...
 
static NVector< T > sumProd (const std::vector< T > &scalars, const std::vector< NVector > &vectors)
 Linear combination of scalars and vectors. More...
 

Protected Member Functions

 NVector (const std::vector< T > &data, size_t k1, size_t k2)
 Vector with regularly spaced components. More...
 
NVector< T > & add (const NVector< T > &u)
 
NVector< T > & sub (const NVector< T > &u)
 
virtual NVector< T > & opp ()
 
virtual NVector< T > & prod (T s)
 
virtual NVector< T > & div (T s)
 
dotProduct (const NVector< T > &u) const
 
norm () const
 
distance (const NVector< T > &u) const
 
bool isValidIndex (size_t k) const
 
bool isBetweenK12 (size_t k) const
 
bool isNull () const
 
bool isEqual (const NVector< T > &u) const
 
bool hasSameSize (const NVector< T > &u) const
 
virtual bool hasDefaultBrowseIndices () const
 
virtual void setDefaultBrowseIndices () const
 
NVector< T > & forEach (const NVector< T > &u, const std::function< void(T &, const T &)> &binary_op)
 
virtual NVector< T > & forEach (T s, const std::function< void(T &, T)> &binary_op)
 
NVector< T > & copy (const NVector< T > &u)
 
NVector< T > subVector (size_t k1, size_t k2) const
 
void setSubVector (const NVector< T > &u)
 

Protected Attributes

size_t _k1 {}
 
size_t _k2 {}
 

Friends

std::ostream & operator<< (std::ostream &os, const NVector< T > &u)
 Usual stream insertion of vector. More...
 
Algebra

Vector algebraical operations.

Represent euclidean vector space operations such as sum, inner product, distance...

NVector< T > operator+ (NVector< T > u, const NVector< T > &v)
 Add two vectors. More...
 
NVector< T > operator- (NVector< T > u, const NVector< T > &v)
 Substract two vectors. More...
 
NVector< T > operator- (NVector< T > u)
 Opposite of vector. More...
 
NVector< T > operator* (T s, NVector< T > u)
 Multiply vector by scalar. More...
 
NVector< T > operator* (const NVector< T > &u, T s)
 
NVector< T > operator/ (NVector< T > u, T s)
 Divide vector by scalar. More...
 
operator| (const NVector< T > &u, const NVector< T > &v)
 Dot product of two vectors. More...
 
operator! (const NVector< T > &u)
 Norm of the vector. More...
 
operator/ (const NVector< T > &u, const NVector< T > &v)
 Distance between two vectors. More...
 
Comparaison

Norm based comparaison between two vectors.

bool operator== (const NVector< T > &u, const NVector< T > &v)
 Equality of two vectors. More...
 
bool operator== (const NVector< T > &u, T s)
 Equality to zero. More...
 
bool operator== (T s, const NVector< T > &u)
 
bool operator!= (const NVector< T > &u, const NVector< T > &v)
 Non equality of two vector. More...
 
bool operator!= (const NVector< T > &u, T s)
 
bool operator!= (T s, const NVector< T > &u)
 

Detailed Description

template<typename T>
class NVector< T >

A NVector<T> object represents the coordinates of a finite dimension dense vector \( x \).

Date
03/05/2018
Author
samiBendou

Coordinates are stored in the form [ \( x_0, x_1, ..., x_{(n-1)} \) ]. where [...] is a std::vector<T>, \( n \) is the dimension and \( (x_0, x_1, ..., x_{(n-1)}) \) are the coordinates.

    This object inherits from `std::vector<T>`, it is a STL container, iterators and STL library functions can be
    used.

Features

     Featuring algebraical operations \form#72, swaps, shifts and
     classic vectors generator such as ones, zeros...

Sub-range operators

     The `NVector` class provides a function operator which behaves like a classic array subscript operator
     of `std::vector` except that it comes on two version detailed further in the documentation.
     It allows operations on sub-ranges.

Definitions

We will often refer to the following variables in this page :

  • x/u/v : An arbitrary given NVector. By default, \( x \) denotes this vector
  • n/dim : Size of this vector, can be seen as the dimension of the underlying vector space
  • s : a scalar of type T
  • k : index of vector \( u_k \)
  • \( |.| \) : Absolute value, if meaningfull with scalar type T

Constructor & Destructor Documentation

template<typename T>
NVector< T >::NVector ( const std::vector< T > &  data)
inline

Construct a vector using a std::vector. \( x_k \) represents data[k].

Parameters
datastd::vector source.
template<typename T>
NVector< T >::NVector ( std::initializer_list< T >  list)
inline

Construct a vector using an initializer list {}.

Parameters
liststd::initializer_list source.

Convert list and uses array constructor.

template<typename T>
NVector< T >::NVector ( size_t  dim = 0)
inlineexplicit

Construct a vector of a given size.

Parameters
dimvector size
template<typename T>
NVector< T >::NVector ( const NVector< T > &  u)
inline

Construct a vector by using copy() method.

Parameters
uNVector source.
template<typename T>
virtual NVector< T >::~NVector ( )
virtualdefault
template<typename T>
NVector< T >::NVector ( const std::vector< T > &  data,
size_t  k1,
size_t  k2 
)
explicitprotected

Vector with regularly spaced components.

Parameters
xstart value of the range
yend value of the range
hstep between elements
Returns
Returns \( ( x, x + h, x + 2h, ..., y) \)

Member Function Documentation

template<typename T >
string NVector< T >::str ( ) const
virtual

Creates a string representing the vector.

The returned string has the following form :

```commandline "(x(0), x(1), ..., x((n-1)))" `` Eachx(k)is formatted using the default<<operator of the typeT`.

Returns
Returns a string representing the vector.

Reimplemented in NPMatrix< T >, and NPMatrix< double_t >.

template<typename T >
size_t NVector< T >::dim ( ) const

Dimension of the vector.

Returns
\( dim \)
template<typename T >
std::vector< T > NVector< T >::array ( ) const

Creates an array with this vector.

Returns
a std::vector representing this.
template<typename T >
T NVector< T >::max ( ) const

Maximum of the coordinates of the vector \( (x_0, x_1, .. x_{(n-1)}) \).

Returns
value of maximum
template<typename T >
T NVector< T >::min ( ) const

Minimum of the coordinates of the vector \( (x_0, x_1, .. x_{(n-1)}) \).

Returns
value of minimum
template<typename T >
size_t NVector< T >::maxIndex ( ) const

Index of maximum of the coordinates of vector \( (x_0, x_1, ..., x_{(n-1)}) \).

Returns
index of maximum
template<typename T >
size_t NVector< T >::minIndex ( ) const

Index of minimum of the coordinates of vector \( (x_0, x_1, ..., x_{(n-1)}) \).

Returns
index of minimum
template<typename T >
T NVector< T >::maxAbs ( ) const

Maximum of the coordinates of the vector \( (|x_0|, |x_1|, ..., |x_{(n-1)}|) \).

Returns
absolute value of absolute maximum
template<typename T >
T NVector< T >::minAbs ( ) const

Minimum of the coordinates of the vector \( (|x_0|, |x_1|, ..., |x_{(n-1)}|) \).

Returns
absolute value of absolute minimum
template<typename T >
size_t NVector< T >::maxAbsIndex ( ) const

Index of maximum of the coordinates of vector \( (|x_0|, |x_1|, ..., |x_{(n-1)}|) \).

Returns
index of absolute maximum
template<typename T >
size_t NVector< T >::minAbsIndex ( ) const

Index of minimum of the coordinates of vector \( (|x_0|, |x_1|, ..., |x_{(n-1)}|) \).

Returns
index of absolute minimum
template<typename T >
NVector< T > & NVector< T >::swap ( size_t  k1,
size_t  k2 
)

Permutation of two elements.

Parameters
k1First index to swap
k2Second index to swap

The vector is set to \( (x_0, ..., x_{(k_1 - 1)}, x_{k_2}, ..., x_{(k_2 - 1)}, x_{k_1}, ..., x_{(n-1)}) \). \( x_{k_1} \) and \( x_{k_2} \) have been swaped used std::swap.

template<typename T >
NVector< T > & NVector< T >::shift ( long  iterations)

Shifts this vector iterations times.

Parameters
iterationsnumber of times to shift.

If iterations is positive, shift is powered to the left, else to the right. For example shift(2) will set \( (x_2, x_3, ..., x_{(n-1)}, x_0, x_1) \).

template<typename T>
NVector< T > & NVector< T >::fill ( s)

Fill vector with a scalar. For example fill(3) will set \( (3, 3, 3, ..., 3) \).

Parameters
svalue to fill the vector with
template<typename T>
NVector<T>& NVector< T >::operator+= ( const NVector< T > &  u)
inline
template<typename T>
NVector<T>& NVector< T >::operator-= ( const NVector< T > &  u)
inline
template<typename T>
virtual NVector<T>& NVector< T >::operator*= ( s)
inlinevirtual

Reimplemented in NPMatrix< T >, and NPMatrix< double_t >.

template<typename T>
virtual NVector<T>& NVector< T >::operator/= ( s)
inlinevirtual

Reimplemented in NPMatrix< T >, and NPMatrix< double_t >.

template<typename T >
T & NVector< T >::operator() ( long  k)

Simple access operator.

Access operator equivalent to [k] on std::vector. Operator can be used to read or write values. const version returns const reference to the element. For example, x(-1) returns the last element, x(0) returns the first element.

Returns
component \( x_{k} \) of the vector.
template<typename T >
T NVector< T >::operator() ( long  k) const
template<typename T>
NVector<T> NVector< T >::operator() ( size_t  k1,
size_t  k2 
) const
inline

Manipulate sub-vector.

Parameters
k1first element to take
k2last element to take \( n \gt k_2 \geq k_1 \geq 0 \)

Allows operations on a restricted range of the vector \( (x_{k_1}, x_{(k_1+1)}, ...,x_{k_2}) \) . The two arguments function operator has the following behavior :

  • It can be used to read/write values.
  • It Allows chaining operations : u(0, 1) + v(1, 2), x(1, 4).max(), ...

Most of methods or operators can be chained. It will be explicitly specified if it's not the case. For example x(1, 3) will return the value of \( (x_1, x_2, x_3) \).

Returns
value of the sub-vector.
template<typename T >
NVector< T > & NVector< T >::operator() ( size_t  k1,
size_t  k2 
)

Manipulate sub-vector.

This operator is similar to previous operator()(size_t, size_t) const "operator" except that it sets browse indices _k1 and _k2 in order to modify efficiently non const reference.

Returns
reference to *this.
template<typename T>
NVector<T>& NVector< T >::operator= ( const NVector< T > &  u)
inline

Copy source object on this object using copy().

Parameters
usource NVector<T> object
Returns
reference to this.
template<typename T>
iterator NVector< T >::begin ( )
inline
template<typename T>
const_iterator NVector< T >::begin ( ) const
inline
template<typename T>
iterator NVector< T >::end ( )
inline
template<typename T>
const_iterator NVector< T >::end ( ) const
inline
template<typename T>
static NVector<T> NVector< T >::zeros ( size_t  dim)
inlinestatic
Parameters
dimdimension of the vector
Returns
a 0 vector \( (0, 0, ..., 0) \).
template<typename T>
static NVector<T> NVector< T >::ones ( size_t  dim)
inlinestatic
Parameters
dimdimension of the vector
Returns
Returns vector filled with 1 \( (1, 1, ..., 1) \).
template<typename T>
NVector< T > NVector< T >::scalar ( s,
size_t  dim 
)
static
Parameters
svalue of the scalar vector
dimdimension of the scalar vector
Returns
a vector filled with s \( (s, s, ..., s) \).
template<typename T >
NVector< T > NVector< T >::cano ( size_t  k,
size_t  dim 
)
static
Parameters
kindex of vector in base
dimdimension of vector space represented by the base.
Returns
return the kth vector of canonical base. ie \( (e_{0}, e_{1}, ..., e_{(n-1)}) \) where :

\[ \begin{align*} & e_{0} &=& (1, 0, 0, ..., 0) \\ & e_{1} &=& (0, 1, 0, ..., 0) \\ & &\vdots & \\ &e_{(n - 1)} &=& (0, 0, 0, ..., 1) \\ \end{align*} \]

template<typename T >
NVector< T > NVector< T >::sum ( const std::vector< NVector< T > > &  vectors)
static

Sum of multiples vectors.

Parameters
vectorsan array of vectors \( (u, v, ..., x) \).

Sum of the vectors contained in vectors array.

Returns
\( u + v + ... + x \).
template<typename T>
NVector< T > NVector< T >::sumProd ( const std::vector< T > &  scalars,
const std::vector< NVector< T > > &  vectors 
)
static

Linear combination of scalars and vectors.

Parameters
scalarscoefficients of linear combination [ \( \alpha, \beta, ..., \lambda \)].
vectorsvectors of linear combination \( (u, v, ..., x) \)
Returns
\( \alpha u + \beta v + ... + \lambda x \).
template<typename T>
NVector<T>& NVector< T >::add ( const NVector< T > &  u)
inlineprotected
template<typename T>
NVector<T>& NVector< T >::sub ( const NVector< T > &  u)
inlineprotected
template<typename T>
virtual NVector<T>& NVector< T >::opp ( )
inlineprotectedvirtual

Reimplemented in NPMatrix< T >, and NPMatrix< double_t >.

template<typename T>
virtual NVector<T>& NVector< T >::prod ( s)
inlineprotectedvirtual

Reimplemented in NPMatrix< T >, and NPMatrix< double_t >.

template<typename T>
virtual NVector<T>& NVector< T >::div ( s)
inlineprotectedvirtual

Reimplemented in NPMatrix< T >, and NPMatrix< double_t >.

template<typename T>
T NVector< T >::dotProduct ( const NVector< T > &  u) const
protected
template<typename T>
T NVector< T >::norm ( ) const
inlineprotected
template<typename T>
T NVector< T >::distance ( const NVector< T > &  u) const
inlineprotected
template<typename T>
bool NVector< T >::isValidIndex ( size_t  k) const
inlineprotected
template<typename T>
bool NVector< T >::isBetweenK12 ( size_t  k) const
inlineprotected
template<typename T>
bool NVector< T >::isNull ( ) const
inlineprotected
template<typename T>
bool NVector< T >::isEqual ( const NVector< T > &  u) const
protected
template<typename T>
bool NVector< T >::hasSameSize ( const NVector< T > &  u) const
inlineprotected
template<typename T>
virtual bool NVector< T >::hasDefaultBrowseIndices ( ) const
inlineprotectedvirtual

Reimplemented in NPMatrix< T >, and NPMatrix< double_t >.

template<typename T>
virtual void NVector< T >::setDefaultBrowseIndices ( ) const
inlineprotectedvirtual

Reimplemented in NPMatrix< T >, and NPMatrix< double_t >.

template<typename T>
NVector< T > & NVector< T >::forEach ( const NVector< T > &  u,
const std::function< void(T &, const T &)> &  binary_op 
)
protected
template<typename T>
NVector< T > & NVector< T >::forEach ( s,
const std::function< void(T &, T)> &  binary_op 
)
protectedvirtual
template<typename T>
NVector< T > & NVector< T >::copy ( const NVector< T > &  u)
protected
template<typename T >
NVector< T > NVector< T >::subVector ( size_t  k1,
size_t  k2 
) const
protected
template<typename T>
void NVector< T >::setSubVector ( const NVector< T > &  u)
protected

Friends And Related Function Documentation

template<typename T>
NVector<T> operator+ ( NVector< T >  u,
const NVector< T > &  v 
)
friend

Add two vectors.

Using usual addition \( (u_0 + v_0, u_1 + v_1, ...) \).

Returns
value of \( u + v \)
template<typename T>
NVector<T> operator- ( NVector< T >  u,
const NVector< T > &  v 
)
friend

Substract two vectors.

Using usual difference \( (u_0 - v_0, u_1 - v_1, ...) \).

Returns
value of \( u - v \)
template<typename T>
NVector<T> operator- ( NVector< T >  u)
friend

Opposite of vector.

Returns
value of \( (-u_0, -u_1, ...). \)
template<typename T>
NVector<T> operator* ( s,
NVector< T >  u 
)
friend

Multiply vector by scalar.

Using usual scalar multiplication difference \( (s \cdot u_0, s \cdot u_1, ...) \).

Returns
value of \( s \cdot u \)
template<typename T>
NVector<T> operator* ( const NVector< T > &  u,
s 
)
friend
template<typename T>
NVector<T> operator/ ( NVector< T >  u,
s 
)
friend

Divide vector by scalar.

Usual scalar division based on multiplication.

Returns
value of \( s^{-1} \cdot u \)
template<typename T>
T operator| ( const NVector< T > &  u,
const NVector< T > &  v 
)
friend

Dot product of two vectors.

Usual inner product \( u_0 \cdot v_0 + u_1 \cdot v_1 + ... + u_{(n-1)} \cdot v_{(n-1)} \).

Returns
value of \( u \cdot v \)
template<typename T>
T operator! ( const NVector< T > &  u)
friend

Norm of the vector.

The norm of vector \( ||u|| = \sqrt{u \cdot u} \) derived from dot product.

Returns
value of \( ||u|| \).
template<typename T>
T operator/ ( const NVector< T > &  u,
const NVector< T > &  v 
)
friend

Distance between two vectors.

Returns
value of \( ||u - v|| \).
template<typename T>
std::ostream& operator<< ( std::ostream &  os,
const NVector< T > &  u 
)
friend

Usual stream insertion of vector.

Parameters
osoutput stream
uvector source

Inserts u.str() into os stream.

Returns
reference to os.
template<typename T>
bool operator== ( const NVector< T > &  u,
const NVector< T > &  v 
)
friend

Equality of two vectors.

Returns
return true if \( ||u - v|| < \epsilon \).
template<typename T>
bool operator== ( const NVector< T > &  u,
s 
)
friend

Equality to zero.

Returns
true if s is 0 and \( u \lt \epsilon \).
template<typename T>
bool operator== ( s,
const NVector< T > &  u 
)
friend
template<typename T>
bool operator!= ( const NVector< T > &  u,
const NVector< T > &  v 
)
friend

Non equality of two vector.

Returns
return true if \( ||u - v|| \geq \epsilon \).
template<typename T>
bool operator!= ( const NVector< T > &  u,
s 
)
friend
template<typename T>
bool operator!= ( s,
const NVector< T > &  u 
)
friend

Member Data Documentation

template<typename T>
size_t NVector< T >::_k1 {}
mutableprotected
template<typename T>
size_t NVector< T >::_k2 {}
mutableprotected