2009-11-16 10:12:15 +08:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
|
|
|
// for linear algebra.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
|
|
|
|
//
|
2012-07-14 02:42:47 +08:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2009-11-16 10:12:15 +08:00
|
|
|
|
2015-06-19 22:39:24 +08:00
|
|
|
#define TEST_ENABLE_TEMPORARY_TRACKING
|
|
|
|
|
2009-11-16 10:12:15 +08:00
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
template<typename MatrixType> void permutationmatrices(const MatrixType& m)
|
|
|
|
{
|
2010-06-20 23:37:56 +08:00
|
|
|
typedef typename MatrixType::Index Index;
|
2009-11-16 10:12:15 +08:00
|
|
|
typedef typename MatrixType::Scalar Scalar;
|
|
|
|
enum { Rows = MatrixType::RowsAtCompileTime, Cols = MatrixType::ColsAtCompileTime,
|
|
|
|
Options = MatrixType::Options };
|
|
|
|
typedef PermutationMatrix<Rows> LeftPermutationType;
|
2018-04-04 05:06:44 +08:00
|
|
|
typedef Transpositions<Rows> LeftTranspositionsType;
|
2009-11-16 10:12:15 +08:00
|
|
|
typedef Matrix<int, Rows, 1> LeftPermutationVectorType;
|
2011-01-26 23:33:23 +08:00
|
|
|
typedef Map<LeftPermutationType> MapLeftPerm;
|
2009-11-16 10:12:15 +08:00
|
|
|
typedef PermutationMatrix<Cols> RightPermutationType;
|
2018-04-04 05:06:44 +08:00
|
|
|
typedef Transpositions<Cols> RightTranspositionsType;
|
2009-11-16 10:12:15 +08:00
|
|
|
typedef Matrix<int, Cols, 1> RightPermutationVectorType;
|
2011-01-26 23:33:23 +08:00
|
|
|
typedef Map<RightPermutationType> MapRightPerm;
|
2010-02-25 22:31:15 +08:00
|
|
|
|
2010-06-20 23:37:56 +08:00
|
|
|
Index rows = m.rows();
|
|
|
|
Index cols = m.cols();
|
2009-11-16 10:12:15 +08:00
|
|
|
|
|
|
|
MatrixType m_original = MatrixType::Random(rows,cols);
|
|
|
|
LeftPermutationVectorType lv;
|
|
|
|
randomPermutationVector(lv, rows);
|
|
|
|
LeftPermutationType lp(lv);
|
|
|
|
RightPermutationVectorType rv;
|
|
|
|
randomPermutationVector(rv, cols);
|
|
|
|
RightPermutationType rp(rv);
|
2018-04-04 05:06:44 +08:00
|
|
|
LeftTranspositionsType lt(lv);
|
|
|
|
RightTranspositionsType rt(rv);
|
2015-06-19 22:39:24 +08:00
|
|
|
MatrixType m_permuted = MatrixType::Random(rows,cols);
|
|
|
|
|
2017-02-19 17:32:54 +08:00
|
|
|
VERIFY_EVALUATION_COUNT(m_permuted = lp * m_original * rp, 1); // 1 temp for sub expression "lp * m_original"
|
2009-11-16 10:12:15 +08:00
|
|
|
|
2009-11-16 11:20:13 +08:00
|
|
|
for (int i=0; i<rows; i++)
|
|
|
|
for (int j=0; j<cols; j++)
|
2009-11-17 04:07:33 +08:00
|
|
|
VERIFY_IS_APPROX(m_permuted(lv(i),j), m_original(i,rv(j)));
|
2009-11-16 10:12:15 +08:00
|
|
|
|
|
|
|
Matrix<Scalar,Rows,Rows> lm(lp);
|
|
|
|
Matrix<Scalar,Cols,Cols> rm(rp);
|
|
|
|
|
|
|
|
VERIFY_IS_APPROX(m_permuted, lm*m_original*rm);
|
2015-06-19 22:39:24 +08:00
|
|
|
|
|
|
|
m_permuted = m_original;
|
2017-02-19 17:32:54 +08:00
|
|
|
VERIFY_EVALUATION_COUNT(m_permuted = lp * m_permuted * rp, 1);
|
2015-06-19 22:39:24 +08:00
|
|
|
VERIFY_IS_APPROX(m_permuted, lm*m_original*rm);
|
|
|
|
|
2009-11-16 22:39:07 +08:00
|
|
|
VERIFY_IS_APPROX(lp.inverse()*m_permuted*rp.inverse(), m_original);
|
2011-01-26 23:33:23 +08:00
|
|
|
VERIFY_IS_APPROX(lv.asPermutation().inverse()*m_permuted*rv.asPermutation().inverse(), m_original);
|
|
|
|
VERIFY_IS_APPROX(MapLeftPerm(lv.data(),lv.size()).inverse()*m_permuted*MapRightPerm(rv.data(),rv.size()).inverse(), m_original);
|
|
|
|
|
2009-11-16 22:39:07 +08:00
|
|
|
VERIFY((lp*lp.inverse()).toDenseMatrix().isIdentity());
|
2011-01-26 23:33:23 +08:00
|
|
|
VERIFY((lv.asPermutation()*lv.asPermutation().inverse()).toDenseMatrix().isIdentity());
|
|
|
|
VERIFY((MapLeftPerm(lv.data(),lv.size())*MapLeftPerm(lv.data(),lv.size()).inverse()).toDenseMatrix().isIdentity());
|
2009-11-16 22:39:07 +08:00
|
|
|
|
|
|
|
LeftPermutationVectorType lv2;
|
|
|
|
randomPermutationVector(lv2, rows);
|
|
|
|
LeftPermutationType lp2(lv2);
|
|
|
|
Matrix<Scalar,Rows,Rows> lm2(lp2);
|
2009-11-17 04:07:33 +08:00
|
|
|
VERIFY_IS_APPROX((lp*lp2).toDenseMatrix().template cast<Scalar>(), lm*lm2);
|
2011-01-26 23:33:23 +08:00
|
|
|
VERIFY_IS_APPROX((lv.asPermutation()*lv2.asPermutation()).toDenseMatrix().template cast<Scalar>(), lm*lm2);
|
|
|
|
VERIFY_IS_APPROX((MapLeftPerm(lv.data(),lv.size())*MapLeftPerm(lv2.data(),lv2.size())).toDenseMatrix().template cast<Scalar>(), lm*lm2);
|
2009-11-17 10:28:26 +08:00
|
|
|
|
|
|
|
LeftPermutationType identityp;
|
|
|
|
identityp.setIdentity(rows);
|
|
|
|
VERIFY_IS_APPROX(m_original, identityp*m_original);
|
2015-06-19 22:39:24 +08:00
|
|
|
|
2010-02-25 23:30:58 +08:00
|
|
|
// check inplace permutations
|
|
|
|
m_permuted = m_original;
|
2017-02-19 17:32:54 +08:00
|
|
|
VERIFY_EVALUATION_COUNT(m_permuted.noalias()= lp.inverse() * m_permuted, 1); // 1 temp to allocate the mask
|
2010-02-25 23:30:58 +08:00
|
|
|
VERIFY_IS_APPROX(m_permuted, lp.inverse()*m_original);
|
2015-06-19 22:39:24 +08:00
|
|
|
|
2010-02-25 23:30:58 +08:00
|
|
|
m_permuted = m_original;
|
2017-02-19 17:32:54 +08:00
|
|
|
VERIFY_EVALUATION_COUNT(m_permuted.noalias() = m_permuted * rp.inverse(), 1); // 1 temp to allocate the mask
|
2010-02-25 23:30:58 +08:00
|
|
|
VERIFY_IS_APPROX(m_permuted, m_original*rp.inverse());
|
2015-06-19 22:39:24 +08:00
|
|
|
|
2010-02-25 23:30:58 +08:00
|
|
|
m_permuted = m_original;
|
2017-02-19 17:32:54 +08:00
|
|
|
VERIFY_EVALUATION_COUNT(m_permuted.noalias() = lp * m_permuted, 1); // 1 temp to allocate the mask
|
2010-02-25 23:30:58 +08:00
|
|
|
VERIFY_IS_APPROX(m_permuted, lp*m_original);
|
2015-06-19 22:39:24 +08:00
|
|
|
|
2010-02-25 23:30:58 +08:00
|
|
|
m_permuted = m_original;
|
2017-02-19 17:32:54 +08:00
|
|
|
VERIFY_EVALUATION_COUNT(m_permuted.noalias() = m_permuted * rp, 1); // 1 temp to allocate the mask
|
2010-02-25 23:30:58 +08:00
|
|
|
VERIFY_IS_APPROX(m_permuted, m_original*rp);
|
|
|
|
|
2009-11-17 10:28:26 +08:00
|
|
|
if(rows>1 && cols>1)
|
|
|
|
{
|
|
|
|
lp2 = lp;
|
2010-10-25 22:15:22 +08:00
|
|
|
Index i = internal::random<Index>(0, rows-1);
|
2010-06-21 03:44:25 +08:00
|
|
|
Index j;
|
2010-10-25 22:15:22 +08:00
|
|
|
do j = internal::random<Index>(0, rows-1); while(j==i);
|
2009-11-17 10:28:26 +08:00
|
|
|
lp2.applyTranspositionOnTheLeft(i, j);
|
|
|
|
lm = lp;
|
|
|
|
lm.row(i).swap(lm.row(j));
|
|
|
|
VERIFY_IS_APPROX(lm, lp2.toDenseMatrix().template cast<Scalar>());
|
|
|
|
|
|
|
|
RightPermutationType rp2 = rp;
|
2010-10-25 22:15:22 +08:00
|
|
|
i = internal::random<Index>(0, cols-1);
|
|
|
|
do j = internal::random<Index>(0, cols-1); while(j==i);
|
2009-11-17 10:28:26 +08:00
|
|
|
rp2.applyTranspositionOnTheRight(i, j);
|
|
|
|
rm = rp;
|
|
|
|
rm.col(i).swap(rm.col(j));
|
|
|
|
VERIFY_IS_APPROX(rm, rp2.toDenseMatrix().template cast<Scalar>());
|
2016-12-14 06:10:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// simple compilation check
|
|
|
|
Matrix<Scalar, Cols, Cols> A = rp;
|
|
|
|
Matrix<Scalar, Cols, Cols> B = rp.transpose();
|
|
|
|
VERIFY_IS_APPROX(A, B.transpose());
|
|
|
|
}
|
2018-04-04 05:06:44 +08:00
|
|
|
|
|
|
|
m_permuted = m_original;
|
|
|
|
lp = lt;
|
|
|
|
rp = rt;
|
|
|
|
VERIFY_EVALUATION_COUNT(m_permuted = lt * m_permuted * rt, 1);
|
2018-04-23 04:49:08 +08:00
|
|
|
VERIFY_IS_APPROX(m_permuted, lp*m_original*rp.transpose());
|
2018-04-04 05:06:44 +08:00
|
|
|
|
|
|
|
VERIFY_IS_APPROX(lt.inverse()*m_permuted*rt.inverse(), m_original);
|
2009-11-16 10:12:15 +08:00
|
|
|
}
|
|
|
|
|
2015-06-10 15:32:10 +08:00
|
|
|
template<typename T>
|
|
|
|
void bug890()
|
|
|
|
{
|
|
|
|
typedef Matrix<T, Dynamic, Dynamic> MatrixType;
|
|
|
|
typedef Matrix<T, Dynamic, 1> VectorType;
|
|
|
|
typedef Stride<Dynamic,Dynamic> S;
|
|
|
|
typedef Map<MatrixType, Aligned, S> MapType;
|
|
|
|
typedef PermutationMatrix<Dynamic> Perm;
|
|
|
|
|
|
|
|
VectorType v1(2), v2(2), op(4), rhs(2);
|
|
|
|
v1 << 666,667;
|
|
|
|
op << 1,0,0,1;
|
|
|
|
rhs << 42,42;
|
|
|
|
|
|
|
|
Perm P(2);
|
|
|
|
P.indices() << 1, 0;
|
|
|
|
|
|
|
|
MapType(v1.data(),2,1,S(1,1)) = P * MapType(rhs.data(),2,1,S(1,1));
|
|
|
|
VERIFY_IS_APPROX(v1, (P * rhs).eval());
|
|
|
|
|
|
|
|
MapType(v1.data(),2,1,S(1,1)) = P.inverse() * MapType(rhs.data(),2,1,S(1,1));
|
|
|
|
VERIFY_IS_APPROX(v1, (P.inverse() * rhs).eval());
|
|
|
|
}
|
|
|
|
|
2009-11-16 10:12:15 +08:00
|
|
|
void test_permutationmatrices()
|
|
|
|
{
|
|
|
|
for(int i = 0; i < g_repeat; i++) {
|
|
|
|
CALL_SUBTEST_1( permutationmatrices(Matrix<float, 1, 1>()) );
|
|
|
|
CALL_SUBTEST_2( permutationmatrices(Matrix3f()) );
|
|
|
|
CALL_SUBTEST_3( permutationmatrices(Matrix<double,3,3,RowMajor>()) );
|
|
|
|
CALL_SUBTEST_4( permutationmatrices(Matrix4d()) );
|
2010-02-25 23:30:58 +08:00
|
|
|
CALL_SUBTEST_5( permutationmatrices(Matrix<double,40,60>()) );
|
2009-11-16 10:12:15 +08:00
|
|
|
CALL_SUBTEST_6( permutationmatrices(Matrix<double,Dynamic,Dynamic,RowMajor>(20, 30)) );
|
|
|
|
CALL_SUBTEST_7( permutationmatrices(MatrixXcf(15, 10)) );
|
|
|
|
}
|
2015-06-10 15:32:10 +08:00
|
|
|
CALL_SUBTEST_5( bug890<double>() );
|
2009-11-16 10:12:15 +08:00
|
|
|
}
|