#include <iostream> #include <boost/numeric/mtl/mtl.hpp> int main(int argc, char* argv[]) { using namespace mtl; double array[][3]= {{1., 2., 3.}, {4., 5., 6.}, {7., 8., 9.}}; dense2D<double> A(array), A2, A3; // Creating a permutation matrix from a vector (or an array respectively) int indices[]= {1, 2, 0}; matrix::traits::permutation<>::type P= matrix::permutation(indices); std::cout << "\nP =\n" << P; // Permutating rows A2= P * A; std::cout << "\nP * A =\n" << A2; // Permutating columns A3= A2 * trans(P); std::cout << "\nA2 * trans(P) =\n" << A3; dense_vector<double> v(array[2]), w(P * v); std::cout << "\nP * v =\n" << w << "\n"; return 0; }
The function matrix::permutation returns a sparse matrix computed from a permutation vector. The permutation vector is defined as where entries come from, i.e. v[i] == j means that the i-th entry/row/column after the permutation was the j-th entry/row/column before the permutation. If your vector is defined in the inverse manner -- i.e. i.e. v[i] == j signifies that the i-th entry/row/column before the permutation becomes the j-th entry/row/column after the permutation -- your permutation matrix is the transposed of what MTL4 computes: P= trans(matrix::permutation(v)).
Reordering is a generalization of permutation. The entries in the reorder vector/array are defined in the same fashion as in the permutation vector. However, the number of entries is not required to be equal to the set size of projectes indices. Therefore, the projected matrix/vector may have less rows or columns:
#include <iostream> #include <boost/numeric/mtl/mtl.hpp> int main(int argc, char* argv[]) { using namespace mtl; double array[][3]= {{1., 2., 3.}, {4., 5., 6.}, {7., 8., 9.}}; dense2D<double> A(array), B2, B3; // Creating a permutation matrix from a vector (or an array respectively) int indices[]= {2, 1}; matrix::traits::permutation<>::type R= matrix::reorder(indices); std::cout << "\nR =\n" << R; // Permutating rows B2= R * A; std::cout << "\nR * A =\n" << B2; // Permutating columns B3= B2 * trans(R); std::cout << "\nB2 * trans(R) =\n" << B3; dense_vector<double> v(array[2]), w(R * v); std::cout << "\nR * v =\n" << w << "\n"; return 0; }
Indices may appear repeatedly in the reorder vector implying that the respective rows/columns appear multiple times in the resulting matrix/vector:
#include <iostream> #include <boost/numeric/mtl/mtl.hpp> int main(int argc, char* argv[]) { using namespace mtl; double array[][3]= {{1., 2., 3.}, {4., 5., 6.}, {7., 8., 9.}}; dense2D<double> A(array), B2, B3; // Creating a permutation matrix from a vector (or an array respectively) int indices[]= {2, 1, 1, 2}; matrix::traits::permutation<>::type R= matrix::reorder(indices); std::cout << "\nR =\n" << R; // Permutating rows B2= R * A; std::cout << "\nR * A =\n" << B2; // Permutating columns B3= B2 * trans(R); std::cout << "\nB2 * trans(R) =\n" << B3; dense_vector<double> v(array[2]), w(R * v); std::cout << "\nR * v =\n" << w << "\n"; return 0; }
Return to Sub-matrices Table of Content Proceed to Banded Matrix View, Upper and Lower Triangular Views
Permutations and Reordering -- MTL 4 -- Peter Gottschling and Andrew Lumsdaine
-- Generated on 19 May 2009 by Doxygen 1.5.5 -- Copyright 2007 by the Trustees of Indiana University.