Friday, January 24, 2014

Permutation matrix - Continuing Linear Algebra for programmers phrase book

Continuing the post from a few days ago about just how different Linear Algebra can look on paper and in C++ I today did a bit of work implementing and testing a permutation matrix.

Permutation matrix

A permutation matrix has a single 1 on each row, the rest is zeros. Like this:


All it does is switch rows around. Try to do a few matrix multiplications with this on the left side and you quickly see how it works. In C++ I implemented it like this for a Permutation Matrix * vector use.

void FastFood::permutate(vector<double>& data) const{
    std::shuffle(data.begin(),data.end(),std::default_random_engine(m_seed));}


Here I have a random permutation, but depending on how I make use of the "m_seed" value I can control when I want a new random order. This is useful because I want my permutation to be random, but I want it to be the same random order every time I use the method from the same object instance.

No comments:

Post a Comment