CPP Linear Algebra ToolBox  0.1.0
Vector Class Reference

A lightweight class describing a Vector object. Vectors are array with size n (>0) of doubles. More...

#include <vector.h>

Public Member Functions

 Vector ()
 Construct a new Vector object. Default constructor. Return an empty vector of 0 dimension. More...
 
 Vector (uint16_t)
 Construct a new Vector object. Return a Vector with dimension \(n\) filled with zeros. More...
 
 Vector (double *, uint16_t)
 Construct a new Vector object. Construct a vector object from an array of double. More...
 
 Vector (const Vector &)
 Construct a new Vector object. Copy constructor. More...
 
 ~Vector ()
 Destroy the Vector object. More...
 
uint16_t get_dim () const
 Get the dimension of a Vector object. More...
 
double & operator[] (uint16_t) const
 Access the n-th value of a Vector object. More...
 
double & operator() (uint16_t) const
 Access the n-th value of a Vector object. Same as operator []. More...
 
Vectoroperator= (const Vector &)
 Overload the assignement operator. Acts like a copy constructor. More...
 
Vectoroperator+= (const Vector &)
 Overload operator += for vectors objects. More...
 
Vectoroperator-= (const Vector &)
 Overload operator -= for vectors objects. More...
 
Vector operator- () const
 Operator to get the opposite vector. More...
 
double norm (uint8_t)
 A general method to compute the norm of a Vector object. More...
 
double norm1 ()
 Compute the \(L^1\) norm of a Vector object. More...
 
double norm2 ()
 Compute the Euclidean norm of a Vector object. More...
 
double normp (uint8_t)
 Compute the \(L^p\) norm of a Vector object. More...
 
double normInf ()
 Compute the \(L^{\infty}\) norm of a Vector object. More...
 
void sort ()
 Sort the vector with the quickSort algorithm. More...
 
std::vector< double > to_std_vector ()
 Conversion to a standard library vector object. More...
 

Static Public Member Functions

static Vector basis (uint16_t, uint16_t)
 Construct vector of the canonical basis of \(\mathbb{R}^n\). More...
 
static Vector zeros (uint16_t)
 Construct a Vector object of length n filled with zeros. More...
 
static Vector ones (uint16_t)
 Construct a Vector object of length n filled with ones. More...
 
static Vector rand (uint16_t)
 Construct a Vector object of length \(n\) filled with random values sampled from 0. to 1. More...
 
static Vector randn (uint16_t)
 Construct a Vector object of length \(n\) filled with random values sampled from the Gaussian random number distribution: mean is 0. and standart deviation is 1. More...
 
static Vector randn (uint16_t, double, double)
 Construct a Vector object of length \(n\) filled with random values sampled from a Gaussian random number distribution. More...
 
static Vector linspace (double, double, uint16_t)
 Construct a Vector object of length n filled with equally points spaced between t0 and t1 included. More...
 

Friends

bool operator== (const Vector &, const Vector &)
 Overload of the equality operator for Vector objects. Two vectors are said to be equal if and only if they have the same dimension and they have exactly the same values. More...
 
double operator* (const Vector &, const Vector &)
 Dot product between vectors. More...
 
Vector operator* (const double, const Vector &)
 Operator of multiplication of a Vector object by a scalar. More...
 
Vector operator* (const Vector &, const double)
 Operator of multiplication of a Vector object by a scalar. More...
 
Vector operator+ (const Vector &, const Vector &)
 Addition of two vector objects. More...
 
Vector operator+ (const double, const Vector &)
 Operator of addition of a Vector object with a scalar. More...
 
Vector operator+ (const Vector &, const double)
 Operator of addition of a Vector object with a scalar. More...
 
Vector operator- (const Vector &, const Vector &)
 Substract two vector objects. More...
 
Vector operator- (const double, const Vector &)
 Operator of remove a scalar to a Vector. More...
 
Vector operator- (const Vector &, const double)
 Operator of remove a scalar to a Vector. More...
 
Vector operator/ (const Vector &, const double)
 Operator to divide a Vector by a scalar. More...
 
std::ostream & operator<< (std::ostream &, const Vector &)
 Overload output stream operator for Vector object. More...
 
Vector abs (const Vector &)
 Elementwise absolute value of a vector. More...
 
Vector pow2 (const Vector &)
 Elementwise square of a vector. More...
 
double prod (const Vector &)
 Compute the product of the element of a vector. More...
 
Vector prod (const Vector &, const Vector &)
 Compute the elementwise product of two vectors. More...
 
Vector sqrt (const Vector &)
 Elementwise square root of a vector. More...
 
double sum (const Vector &)
 Compute the sum of the element of a vector. More...
 

Detailed Description

A lightweight class describing a Vector object. Vectors are array with size n (>0) of doubles.

Warning
Maximum dimension of a Vector is 65 535
Indices are 0-based. Math formulae must be adapted consequently.
Todo:

Template this class to use different data types (double, float, complex)

Parallelize the class

Interface with eigen or other lib if exists

Constructor & Destructor Documentation

◆ Vector() [1/4]

Vector::Vector ( )

Construct a new Vector object. Default constructor. Return an empty vector of 0 dimension.

◆ Vector() [2/4]

Vector::Vector ( uint16_t  n)

Construct a new Vector object. Return a Vector with dimension \(n\) filled with zeros.

Parameters
nDimension of the desired vector (must be greater or equal than \(1\))

◆ Vector() [3/4]

Vector::Vector ( double *  dat,
uint16_t  n 
)

Construct a new Vector object. Construct a vector object from an array of double.

Parameters
datArray of double of size n
nSize of the input array

◆ Vector() [4/4]

Vector::Vector ( const Vector v)

Construct a new Vector object. Copy constructor.

Parameters
vVector oject to be copied.

◆ ~Vector()

Vector::~Vector ( )

Destroy the Vector object.

Member Function Documentation

◆ basis()

Vector Vector::basis ( uint16_t  n,
uint16_t  k 
)
static

Construct vector of the canonical basis of \(\mathbb{R}^n\).

Parameters
nDimension of the vector space (must be greater or equal than 1)
kNumber of the basis vector (must be between 0 and n-1).
Returns
Vector k-th vector of the canonical basis of \(\mathbb{R}^n\).
Warning
Indices are 0-based.

◆ get_dim()

uint16_t Vector::get_dim ( ) const

Get the dimension of a Vector object.

Returns
uint16_t Dimension of the Vector.

◆ linspace()

Vector Vector::linspace ( double  t0,
double  t1,
uint16_t  n 
)
static

Construct a Vector object of length n filled with equally points spaced between t0 and t1 included.

Parameters
t0Starting value
t1Final value (must be greater than t0)
nNumber of timestep including t0 and t1 (must be greater or equal than 2)
Returns
Vector of size n of equally spaced timestep between t0 and t1

◆ norm()

double Vector::norm ( uint8_t  type)

A general method to compute the norm of a Vector object.

Parameters
typeUnisgned integer to decide the norm to choose: type = 0 compute the \(L^{\infty}\) norm type >= 1 Compute the \(L^p\) norm
Returns
double \(L^p\) norm of the Vector.

◆ norm1()

double Vector::norm1 ( )

Compute the \(L^1\) norm of a Vector object.

Returns
double \(L^1\) norm of the Vector.

◆ norm2()

double Vector::norm2 ( )

Compute the Euclidean norm of a Vector object.

Returns
double Euclidean of the Vector.

◆ normInf()

double Vector::normInf ( )

Compute the \(L^{\infty}\) norm of a Vector object.

Returns
double \(L^{\infty}\) norm of the Vector.

◆ normp()

double Vector::normp ( uint8_t  p)

Compute the \(L^p\) norm of a Vector object.

Parameters
pOrder of the norm
Returns
double \(L^p\) norm of the Vector.

◆ ones()

Vector Vector::ones ( uint16_t  n)
static

Construct a Vector object of length n filled with ones.

Parameters
nDimension of the vector (must be greater or equal than 1).
Returns
Vector A Vector object of size n filled with ones

◆ operator()()

double & Vector::operator() ( uint16_t  n) const

Access the n-th value of a Vector object. Same as operator [].

Parameters
nindex of the required value (must be between 0 and dim-1)
Returns
double & address of the n-th element of the Vector object.
Warning
indices are 0-based.

◆ operator+=()

Vector & Vector::operator+= ( const Vector v)

Overload operator += for vectors objects.

Parameters
vRHS
Returns
Vector& Addition of current object with rhs
Warning
Both vector must be of the same dimension

◆ operator-()

Vector Vector::operator- ( ) const

Operator to get the opposite vector.

Parameters
va Vector object
Returns
Vector the opposite of v, i.e. -v

◆ operator-=()

Vector & Vector::operator-= ( const Vector v)

Overload operator -= for vectors objects.

Parameters
vRHS
Returns
Vector& Difference of current object with rhs
Warning
Both vector must be of the same dimension

◆ operator=()

Vector & Vector::operator= ( const Vector v)

Overload the assignement operator. Acts like a copy constructor.

Parameters
vThe vector to be copied.
Returns
Vector& A copy of vector v.

◆ operator[]()

double & Vector::operator[] ( uint16_t  n) const

Access the n-th value of a Vector object.

Parameters
nindex of the required value (must be between 0 and dim-1)
Returns
double & address of the n-th element of the Vector object.
Warning
indices are 0-based.

◆ rand()

Vector Vector::rand ( uint16_t  n)
static

Construct a Vector object of length \(n\) filled with random values sampled from 0. to 1.

Parameters
nDimension of the vector (must be greater or equal than 1).
Returns
Vector A Vector object of size n filled with random values

◆ randn() [1/2]

Vector Vector::randn ( uint16_t  n)
static

Construct a Vector object of length \(n\) filled with random values sampled from the Gaussian random number distribution: mean is 0. and standart deviation is 1.

Parameters
nDimension of the vector (must be greater or equal than 1).
Returns
Vector A Vector object of size \(n\) filled with random values picked following a \(\mathcal{N}(0,1)\) law.

◆ randn() [2/2]

Vector Vector::randn ( uint16_t  n,
double  m,
double  s 
)
static

Construct a Vector object of length \(n\) filled with random values sampled from a Gaussian random number distribution.

Parameters
nDimension of the vector (must be greater or equal than 1).
mMean of the Gaussian distribution.
sStandart deviation of the Gaussian distribution
Returns
Vector A Vector object of size \(n\) filled with random values picked following a \(\mathcal{N}(m,s)\) law.

◆ sort()

void Vector::sort ( )

Sort the vector with the quickSort algorithm.

◆ to_std_vector()

std::vector< double > Vector::to_std_vector ( )

Conversion to a standard library vector object.

Returns
std::vector<double> a standard vector object with the same data as the current object

◆ zeros()

Vector Vector::zeros ( uint16_t  n)
static

Construct a Vector object of length n filled with zeros.

Parameters
nDimension of the Mector (must be greater or equal than 1).
Returns
Vector A Vector object of size n filled with zeros

Friends And Related Function Documentation

◆ abs

Vector abs ( const Vector v)
friend

Elementwise absolute value of a vector.

Parameters
va vector
Returns
Vector a vector whose components are the absolute value of the components of v

◆ operator* [1/3]

Vector operator* ( const double  d,
const Vector v 
)
friend

Operator of multiplication of a Vector object by a scalar.

Parameters
dDouble value to multiply v with
vVector to be multiplied
Returns
Vector whose components are v[i]*d

◆ operator* [2/3]

Vector operator* ( const Vector v,
const double  d 
)
friend

Operator of multiplication of a Vector object by a scalar.

Parameters
vVector to be multiplied
dDouble value to multiply v with
Returns
Vector whose components are v[i]*d

◆ operator* [3/3]

double operator* ( const Vector lhs,
const Vector rhs 
)
friend

Dot product between vectors.

Parameters
lhsLeft Hand Side
rhsRight Hand Side
Returns
double Dot product between vetcor object and v
Warning
Both vectors must be of the same dimension.

◆ operator+ [1/3]

Vector operator+ ( const double  d,
const Vector v 
)
friend

Operator of addition of a Vector object with a scalar.

Parameters
dDouble value added to v
vVector to be additioned
Returns
Vector whose components are v[i] + d

◆ operator+ [2/3]

Vector operator+ ( const Vector v,
const double  d 
)
friend

Operator of addition of a Vector object with a scalar.

Parameters
vVector to be additioned
dDouble value added to v
Returns
Vector whose components are v[i] + d

◆ operator+ [3/3]

Vector operator+ ( const Vector lhs,
const Vector rhs 
)
friend

Addition of two vector objects.

Parameters
lhsA vector of dimension \(n\),
rhsA vector of dimension \(n\)
Warning
lhs and rhs must be of the same dimension.
Returns
Vector Sum of lhs and rhs.

◆ operator- [1/3]

Vector operator- ( const double  d,
const Vector v 
)
friend

Operator of remove a scalar to a Vector.

Parameters
dDouble value to remove fro v
vRight Hand Side
Returns
Vector whose components are d - v[i]

◆ operator- [2/3]

Vector operator- ( const Vector v,
const double  d 
)
friend

Operator of remove a scalar to a Vector.

Parameters
vLeft Hand Side
dDouble value to remove from v
Returns
Vector whose components are v[i] - d

◆ operator- [3/3]

Vector operator- ( const Vector lhs,
const Vector rhs 
)
friend

Substract two vector objects.

Parameters
lhsA vector of dimension \(n\),
rhsA vector of dimension \(n\)
Warning
lhs and rhs must be of the same dimension.
Returns
Vector Difference of lhs and rhs.

◆ operator/

Vector operator/ ( const Vector v,
const double  d 
)
friend

Operator to divide a Vector by a scalar.

Parameters
vVector v
dDouble value to divide v (must be non-zero)
Returns
Vector whose components are v[i] / d
Warning
d must me non-zero

◆ operator<<

std::ostream& operator<< ( std::ostream &  os,
const Vector v 
)
friend

Overload output stream operator for Vector object.

Parameters
osOutput stream.
vVector object.
Returns
std::ostream & updated output stream

◆ operator==

bool operator== ( const Vector lhs,
const Vector rhs 
)
friend

Overload of the equality operator for Vector objects. Two vectors are said to be equal if and only if they have the same dimension and they have exactly the same values.

Parameters
lhsLeft hand side
rhsRight hand side
Returns
true If all values of both vectors coincide.
false If at least of the value of the two vectors differ.

◆ pow2

Vector pow2 ( const Vector v)
friend

Elementwise square of a vector.

Parameters
va vector
Returns
Vector a vector whose components are the square of the components of v

◆ prod [1/2]

double prod ( const Vector v)
friend

Compute the product of the element of a vector.

Parameters
va vector object
Returns
double product of the components of \(v\)
Warning
If v has zero length then we return 1

◆ prod [2/2]

Vector prod ( const Vector v1,
const Vector v2 
)
friend

Compute the elementwise product of two vectors.

Parameters
v1a vector
v2a vector
Returns
Vector a vector whose elements are the product of the corresponding elements of v1 and v2
Warning
v1 and v2 must have the same dimension

◆ sqrt

Vector sqrt ( const Vector v)
friend

Elementwise square root of a vector.

Parameters
va vector
Returns
Vector a vector whose components are the square root of the components of v
Warning
the components must be non negative

◆ sum

double sum ( const Vector v)
friend

Compute the sum of the element of a vector.

Parameters
va vector object
Returns
double sum of the components of \(v\)
Warning
If v has zero length then we return 0

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