# Vector and matrices mathematical operations¶

and other utilities

// global functions

template <size_t NROWS, size_t NCOLS>
mrpt::serialization::CArchive& mrpt::math::operator >> (mrpt::serialization::CArchive& in, CMatrixFixed<float, NROWS, NCOLS>& M);

template <size_t NROWS, size_t NCOLS>
mrpt::serialization::CArchive& mrpt::math::operator >> (mrpt::serialization::CArchive& in, CMatrixFixed<double, NROWS, NCOLS>& M);

template <size_t NROWS, size_t NCOLS>
mrpt::serialization::CArchive& mrpt::math::operator << (
mrpt::serialization::CArchive& out,
const CMatrixFixed<float, NROWS, NCOLS>& M
);

template <size_t NROWS, size_t NCOLS>
mrpt::serialization::CArchive& mrpt::math::operator << (
mrpt::serialization::CArchive& out,
const CMatrixFixed<double, NROWS, NCOLS>& M
);

template <typename MAT>
void mrpt::math::deserializeSymmetricMatrixFrom(MAT& m, mrpt::serialization::CArchive& in);

template <typename MAT>
void mrpt::math::serializeSymmetricMatrixTo(MAT& m, mrpt::serialization::CArchive& out);

template <typename T1, typename T2>
std::vector<T1>& mrpt::math::operator *= (
std::vector<T1>& a,
const std::vector<T2>& b
);

template <typename T1>
std::vector<T1>& mrpt::math::operator *= (std::vector<T1>& a, const T1 b);

template <typename T1, typename T2>
std::vector<T1> mrpt::math::operator * (
const std::vector<T1>& a,
const std::vector<T2>& b
);

template <typename T1, typename T2>
std::vector<T1>& mrpt::math::operator += (
std::vector<T1>& a,
const std::vector<T2>& b
);

template <typename T1>
std::vector<T1>& mrpt::math::operator += (std::vector<T1>& a, const T1 b);

template <typename T1, typename T2>
std::vector<T1> mrpt::math::operator + (
const std::vector<T1>& a,
const std::vector<T2>& b
);

template <typename T1, typename T2>
std::vector<T1> mrpt::math::operator - (
const std::vector<T1>& v1,
const std::vector<T2>& v2
);

template <class T>
std::ostream& mrpt::math::operator << (std::ostream& out, const std::vector<T>& d);

template <class T>
std::ostream& mrpt::math::operator << (std::ostream& out, std::vector<T>* d);

template <typename T, size_t N>
mrpt::serialization::CArchive& mrpt::math::operator << (mrpt::serialization::CArchive& ostrm, const CVectorFixed<T, N>& a);

template <typename T, size_t N>
mrpt::serialization::CArchive& mrpt::math::operator >> (mrpt::serialization::CArchive& istrm, CVectorFixed<T, N>& a);

void mrpt::math::medianFilter(
const std::vector<double>& inV,
std::vector<double>& outV,
int winSize,
int numberOfSigmas = 2
);

template <typename T, typename VECTOR>
void mrpt::math::linspace(
T first,
T last,
size_t count,
VECTOR& out_vector
);

template <class T, T STEP>
std::vector<T> mrpt::math::sequenceStdVec(T first, size_t length);

template <class VEC1, class VEC2>
void mrpt::math::normalize(const VEC1& v, VEC2& out_v);

template <class VECTOR_OF_VECTORS, class VECTORLIKE>
void mrpt::math::extractColumnFromVectorOfVectors(
const size_t colIndex,
const VECTOR_OF_VECTORS& data,
VECTORLIKE& out_column
);

uint64_t mrpt::math::factorial64(unsigned int n);
double mrpt::math::factorial(unsigned int n);

template <typename VECTOR_T, typename At, size_t N>
VECTOR_T& v,
At(&) theArray [N]
);

template <typename T, typename At, size_t N>
std::vector<T>& v,
At(&) theArray [N]
);

template <class T>
void mrpt::math::wrapTo2PiInPlace(T& a);

template <class T>
T mrpt::math::wrapTo2Pi(T a);

template <class T>
T mrpt::math::wrapToPi(T a);

template <class T>
void mrpt::math::wrapToPiInPlace(T& a);

template <class VECTOR>
void mrpt::math::unwrap2PiSequence(VECTOR& x);

template <class T>
T mrpt::math::angDistance(T from, T to);

## Global Functions¶

template <size_t NROWS, size_t NCOLS>
mrpt::serialization::CArchive& mrpt::math::operator >> (
mrpt::serialization::CArchive& in,
CMatrixFixed<float, NROWS, NCOLS>& M
)

The format is compatible with that of CMatrixF & CMatrixD

template <size_t NROWS, size_t NCOLS>
mrpt::serialization::CArchive& mrpt::math::operator >> (
mrpt::serialization::CArchive& in,
CMatrixFixed<double, NROWS, NCOLS>& M
)

The format is compatible with that of CMatrixF & CMatrixD

template <size_t NROWS, size_t NCOLS>
mrpt::serialization::CArchive& mrpt::math::operator << (
mrpt::serialization::CArchive& out,
const CMatrixFixed<float, NROWS, NCOLS>& M
)

Write operator for writing into a CStream.

The format is compatible with that of CMatrixF & CMatrixD

template <size_t NROWS, size_t NCOLS>
mrpt::serialization::CArchive& mrpt::math::operator << (
mrpt::serialization::CArchive& out,
const CMatrixFixed<double, NROWS, NCOLS>& M
)

Write operator for writing into a CStream.

The format is compatible with that of CMatrixF & CMatrixD

template <typename MAT>
void mrpt::math::deserializeSymmetricMatrixFrom(
MAT& m,
mrpt::serialization::CArchive& in
)

Binary serialization of symmetric matrices, saving the space of duplicated values.

serializeSymmetricMatrixTo()

template <typename MAT>
void mrpt::math::serializeSymmetricMatrixTo(
MAT& m,
mrpt::serialization::CArchive& out
)

Binary serialization of symmetric matrices, saving the space of duplicated values.

deserializeSymmetricMatrixFrom()

template <typename T1, typename T2>
std::vector<T1>& mrpt::math::operator *= (
std::vector<T1>& a,
const std::vector<T2>& b
)

a*=b (element-wise multiplication)

template <typename T1>
std::vector<T1>& mrpt::math::operator *= (
std::vector<T1>& a,
const T1 b
)

a*=k (multiplication by a constant)

template <typename T1, typename T2>
std::vector<T1> mrpt::math::operator * (
const std::vector<T1>& a,
const std::vector<T2>& b
)

a*b (element-wise multiplication)

template <typename T1, typename T2>
std::vector<T1>& mrpt::math::operator += (
std::vector<T1>& a,
const std::vector<T2>& b
)

a+=b (element-wise sum)

template <typename T1>
std::vector<T1>& mrpt::math::operator += (
std::vector<T1>& a,
const T1 b
)

a+=b (sum a constant)

template <typename T1, typename T2>
std::vector<T1> mrpt::math::operator + (
const std::vector<T1>& a,
const std::vector<T2>& b
)

a+b (element-wise sum)

template <class T>
std::ostream& mrpt::math::operator << (
std::ostream& out,
const std::vector<T>& d
)

A template function for printing out the contents of a std::vector variable.

template <class T>
std::ostream& mrpt::math::operator << (
std::ostream& out,
std::vector<T>* d
)

A template function for printing out the contents of a std::vector variable.

template <typename T, size_t N>
mrpt::serialization::CArchive& mrpt::math::operator << (
mrpt::serialization::CArchive& ostrm,
const CVectorFixed<T, N>& a
)

Binary dump of a CVectorFixed<T,N> to a stream.

template <typename T, size_t N>
mrpt::serialization::CArchive& mrpt::math::operator >> (mrpt::serialization::CArchive& istrm, CVectorFixed<T, N>& a)

Binary read of a CVectorFixed<T,N> from a stream.

bool mrpt::math::loadVector(std::istream& f, std::vector<int>& d)

Loads one row of a text file as a numerical std::vector.

Returns:

false on EOF or invalid format. The body of the function is implemented in MATH.cpp

bool mrpt::math::loadVector(std::istream& f, std::vector<double>& d)

Loads one row of a text file as a numerical std::vector.

Returns:

false on EOF or invalid format. The body of the function is implemented in MATH.cpp

template <typename T, typename VECTOR>
void mrpt::math::linspace(
T first,
T last,
size_t count,
VECTOR& out_vector
)

Generates an equidistant sequence of numbers given the first one, the last one and the desired number of points.

sequence

template <class T, T STEP>
std::vector<T> mrpt::math::sequenceStdVec(
T first,
size_t length
)

Generates a sequence of values [first,first+STEP,first+2*STEP,…].

linspace, sequence

template <class VEC1, class VEC2>
void mrpt::math::normalize(
const VEC1& v,
VEC2& out_v
)

Normalize a vector, such as its norm is the unity.

If the vector has a null norm, the output is a null vector.

template <class VECTOR_OF_VECTORS, class VECTORLIKE>
void mrpt::math::extractColumnFromVectorOfVectors(
const size_t colIndex,
const VECTOR_OF_VECTORS& data,
VECTORLIKE& out_column
)

Extract a column from a vector of vectors, and store it in another vector.

• Input data can be: std::vector<mrpt::math::CVectorDouble>, std::deque<std::list<double> >, std::list<CVectorFixedDouble<5> >, etc. etc.

• Output is the sequence: data[0][idx],data[1][idx],data[2][idx], etc..

For the sake of generality, this function does NOT check the limits in the number of column, unless it’s implemented in the [] operator of each of the “rows”.

uint64_t mrpt::math::factorial64(unsigned int n)

Computes the factorial of an integer number and returns it as a 64-bit integer number.

double mrpt::math::factorial(unsigned int n)

Computes the factorial of an integer number and returns it as a double value (internally it uses logarithms for avoiding overflow).

template <typename VECTOR_T, typename At, size_t N>
VECTOR_T& v,
At(&) theArray [N]
)

Assignment operator for initializing a std::vector from a C array (The vector will be automatically set to the correct size).

   CVectorDouble  v;
const double numbers[] = { 1,2,3,5,6,7,8,9,10 };
loadVector( v, numbers );

This operator performs the appropiate type castings, if required.

template <typename T, typename At, size_t N>
std::vector<T>& v,
At(&) theArray [N]
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

template <class T>
void mrpt::math::wrapTo2PiInPlace(T& a)

Modifies the given angle to translate it into the [0,2pi[ range.

Take care of not instancing this template for integer numbers, since it only works for float, double and long double.

template <class T>
T mrpt::math::wrapTo2Pi(T a)

Modifies the given angle to translate it into the [0,2pi[ range.

Take care of not instancing this template for integer numbers, since it only works for float, double and long double.

template <class T>
T mrpt::math::wrapToPi(T a)

Modifies the given angle to translate it into the ]-pi,pi] range.

Take care of not instancing this template for integer numbers, since it only works for float, double and long double.

template <class T>
void mrpt::math::wrapToPiInPlace(T& a)

Modifies the given angle to translate it into the ]-pi,pi] range.

Take care of not instancing this template for integer numbers, since it only works for float, double and long double.

template <class VECTOR>
void mrpt::math::unwrap2PiSequence(VECTOR& x)

Modify a sequence of angle values such as no consecutive values have a jump larger than PI in absolute value.

template <class T>
T mrpt::math::angDistance(T from, T to)