From 955cd7f88455a46c9c31a225a6a63a6a440e3415 Mon Sep 17 00:00:00 2001 From: Benoit Jacob Date: Sun, 15 Nov 2009 21:12:15 -0500 Subject: * add PermutationMatrix * DiagonalMatrix: - add MaxSizeAtCompileTime parameter - DiagonalOnTheLeft ---> OnTheLeft - fix bug in DiagonalMatrix::setIdentity() --- test/permutationmatrices.cpp | 89 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 test/permutationmatrices.cpp (limited to 'test/permutationmatrices.cpp') diff --git a/test/permutationmatrices.cpp b/test/permutationmatrices.cpp new file mode 100644 index 000000000..c0353bc71 --- /dev/null +++ b/test/permutationmatrices.cpp @@ -0,0 +1,89 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. +// +// Copyright (C) 2009 Benoit Jacob +// +// Eigen is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3 of the License, or (at your option) any later version. +// +// Alternatively, you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License and a copy of the GNU General Public License along with +// Eigen. If not, see . + +#include "main.h" + +template +void randomPermutationVector(PermutationVectorType& v, int size) +{ + typedef typename PermutationVectorType::Scalar Scalar; + v.resize(size); + for(int i = 0; i < size; ++i) v(i) = Scalar(i); + if(size == 1) return; + for(int n = 0; n < 3 * size; ++n) + { + int i = ei_random(0, size-1); + int j; + do j = ei_random(0, size-1); while(j==i); + std::swap(v(i), v(j)); + } +} + +using namespace std; +template void permutationmatrices(const MatrixType& m) +{ + typedef typename MatrixType::Scalar Scalar; + typedef typename MatrixType::RealScalar RealScalar; + enum { Rows = MatrixType::RowsAtCompileTime, Cols = MatrixType::ColsAtCompileTime, + Options = MatrixType::Options }; + typedef PermutationMatrix LeftPermutationType; + typedef Matrix LeftPermutationVectorType; + typedef PermutationMatrix RightPermutationType; + typedef Matrix RightPermutationVectorType; + + int rows = m.rows(); + int cols = m.cols(); + + MatrixType m_original = MatrixType::Random(rows,cols); + LeftPermutationVectorType lv; + randomPermutationVector(lv, rows); + LeftPermutationType lp(lv); + RightPermutationVectorType rv; + randomPermutationVector(rv, cols); + RightPermutationType rp(rv); + MatrixType m_permuted = lp * m_original * rp; + + int i = ei_random(0, rows-1); + int j = ei_random(0, cols-1); + + VERIFY_IS_APPROX(m_original(lv(i),j), m_permuted(i,rv(j))); + + Matrix lm(lp); + Matrix rm(rp); + + VERIFY_IS_APPROX(m_permuted, lm*m_original*rm); +} + +void test_permutationmatrices() +{ + for(int i = 0; i < g_repeat; i++) { + CALL_SUBTEST_1( permutationmatrices(Matrix()) ); + CALL_SUBTEST_2( permutationmatrices(Matrix3f()) ); + CALL_SUBTEST_3( permutationmatrices(Matrix()) ); + CALL_SUBTEST_4( permutationmatrices(Matrix4d()) ); + CALL_SUBTEST_5( permutationmatrices(Matrix()) ); + CALL_SUBTEST_6( permutationmatrices(Matrix(20, 30)) ); + CALL_SUBTEST_7( permutationmatrices(MatrixXcf(15, 10)) ); + } +} -- cgit v1.2.3