1*bf2c3715SXin Li // This file is part of Eigen, a lightweight C++ template library
2*bf2c3715SXin Li // for linear algebra.
3*bf2c3715SXin Li //
4*bf2c3715SXin Li // Copyright (C) 2009 Benoit Jacob <[email protected]>
5*bf2c3715SXin Li //
6*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
7*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
8*bf2c3715SXin Li // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9*bf2c3715SXin Li
10*bf2c3715SXin Li #define TEST_ENABLE_TEMPORARY_TRACKING
11*bf2c3715SXin Li
12*bf2c3715SXin Li #include "main.h"
13*bf2c3715SXin Li
14*bf2c3715SXin Li using namespace std;
permutationmatrices(const MatrixType & m)15*bf2c3715SXin Li template<typename MatrixType> void permutationmatrices(const MatrixType& m)
16*bf2c3715SXin Li {
17*bf2c3715SXin Li typedef typename MatrixType::Scalar Scalar;
18*bf2c3715SXin Li enum { Rows = MatrixType::RowsAtCompileTime, Cols = MatrixType::ColsAtCompileTime,
19*bf2c3715SXin Li Options = MatrixType::Options };
20*bf2c3715SXin Li typedef PermutationMatrix<Rows> LeftPermutationType;
21*bf2c3715SXin Li typedef Transpositions<Rows> LeftTranspositionsType;
22*bf2c3715SXin Li typedef Matrix<int, Rows, 1> LeftPermutationVectorType;
23*bf2c3715SXin Li typedef Map<LeftPermutationType> MapLeftPerm;
24*bf2c3715SXin Li typedef PermutationMatrix<Cols> RightPermutationType;
25*bf2c3715SXin Li typedef Transpositions<Cols> RightTranspositionsType;
26*bf2c3715SXin Li typedef Matrix<int, Cols, 1> RightPermutationVectorType;
27*bf2c3715SXin Li typedef Map<RightPermutationType> MapRightPerm;
28*bf2c3715SXin Li
29*bf2c3715SXin Li Index rows = m.rows();
30*bf2c3715SXin Li Index cols = m.cols();
31*bf2c3715SXin Li
32*bf2c3715SXin Li MatrixType m_original = MatrixType::Random(rows,cols);
33*bf2c3715SXin Li LeftPermutationVectorType lv;
34*bf2c3715SXin Li randomPermutationVector(lv, rows);
35*bf2c3715SXin Li LeftPermutationType lp(lv);
36*bf2c3715SXin Li RightPermutationVectorType rv;
37*bf2c3715SXin Li randomPermutationVector(rv, cols);
38*bf2c3715SXin Li RightPermutationType rp(rv);
39*bf2c3715SXin Li LeftTranspositionsType lt(lv);
40*bf2c3715SXin Li RightTranspositionsType rt(rv);
41*bf2c3715SXin Li MatrixType m_permuted = MatrixType::Random(rows,cols);
42*bf2c3715SXin Li
43*bf2c3715SXin Li VERIFY_EVALUATION_COUNT(m_permuted = lp * m_original * rp, 1); // 1 temp for sub expression "lp * m_original"
44*bf2c3715SXin Li
45*bf2c3715SXin Li for (int i=0; i<rows; i++)
46*bf2c3715SXin Li for (int j=0; j<cols; j++)
47*bf2c3715SXin Li VERIFY_IS_APPROX(m_permuted(lv(i),j), m_original(i,rv(j)));
48*bf2c3715SXin Li
49*bf2c3715SXin Li Matrix<Scalar,Rows,Rows> lm(lp);
50*bf2c3715SXin Li Matrix<Scalar,Cols,Cols> rm(rp);
51*bf2c3715SXin Li
52*bf2c3715SXin Li VERIFY_IS_APPROX(m_permuted, lm*m_original*rm);
53*bf2c3715SXin Li
54*bf2c3715SXin Li m_permuted = m_original;
55*bf2c3715SXin Li VERIFY_EVALUATION_COUNT(m_permuted = lp * m_permuted * rp, 1);
56*bf2c3715SXin Li VERIFY_IS_APPROX(m_permuted, lm*m_original*rm);
57*bf2c3715SXin Li
58*bf2c3715SXin Li LeftPermutationType lpi;
59*bf2c3715SXin Li lpi = lp.inverse();
60*bf2c3715SXin Li VERIFY_IS_APPROX(lpi*m_permuted,lp.inverse()*m_permuted);
61*bf2c3715SXin Li
62*bf2c3715SXin Li VERIFY_IS_APPROX(lp.inverse()*m_permuted*rp.inverse(), m_original);
63*bf2c3715SXin Li VERIFY_IS_APPROX(lv.asPermutation().inverse()*m_permuted*rv.asPermutation().inverse(), m_original);
64*bf2c3715SXin Li VERIFY_IS_APPROX(MapLeftPerm(lv.data(),lv.size()).inverse()*m_permuted*MapRightPerm(rv.data(),rv.size()).inverse(), m_original);
65*bf2c3715SXin Li
66*bf2c3715SXin Li VERIFY((lp*lp.inverse()).toDenseMatrix().isIdentity());
67*bf2c3715SXin Li VERIFY((lv.asPermutation()*lv.asPermutation().inverse()).toDenseMatrix().isIdentity());
68*bf2c3715SXin Li VERIFY((MapLeftPerm(lv.data(),lv.size())*MapLeftPerm(lv.data(),lv.size()).inverse()).toDenseMatrix().isIdentity());
69*bf2c3715SXin Li
70*bf2c3715SXin Li LeftPermutationVectorType lv2;
71*bf2c3715SXin Li randomPermutationVector(lv2, rows);
72*bf2c3715SXin Li LeftPermutationType lp2(lv2);
73*bf2c3715SXin Li Matrix<Scalar,Rows,Rows> lm2(lp2);
74*bf2c3715SXin Li VERIFY_IS_APPROX((lp*lp2).toDenseMatrix().template cast<Scalar>(), lm*lm2);
75*bf2c3715SXin Li VERIFY_IS_APPROX((lv.asPermutation()*lv2.asPermutation()).toDenseMatrix().template cast<Scalar>(), lm*lm2);
76*bf2c3715SXin Li VERIFY_IS_APPROX((MapLeftPerm(lv.data(),lv.size())*MapLeftPerm(lv2.data(),lv2.size())).toDenseMatrix().template cast<Scalar>(), lm*lm2);
77*bf2c3715SXin Li
78*bf2c3715SXin Li LeftPermutationType identityp;
79*bf2c3715SXin Li identityp.setIdentity(rows);
80*bf2c3715SXin Li VERIFY_IS_APPROX(m_original, identityp*m_original);
81*bf2c3715SXin Li
82*bf2c3715SXin Li // check inplace permutations
83*bf2c3715SXin Li m_permuted = m_original;
84*bf2c3715SXin Li VERIFY_EVALUATION_COUNT(m_permuted.noalias()= lp.inverse() * m_permuted, 1); // 1 temp to allocate the mask
85*bf2c3715SXin Li VERIFY_IS_APPROX(m_permuted, lp.inverse()*m_original);
86*bf2c3715SXin Li
87*bf2c3715SXin Li m_permuted = m_original;
88*bf2c3715SXin Li VERIFY_EVALUATION_COUNT(m_permuted.noalias() = m_permuted * rp.inverse(), 1); // 1 temp to allocate the mask
89*bf2c3715SXin Li VERIFY_IS_APPROX(m_permuted, m_original*rp.inverse());
90*bf2c3715SXin Li
91*bf2c3715SXin Li m_permuted = m_original;
92*bf2c3715SXin Li VERIFY_EVALUATION_COUNT(m_permuted.noalias() = lp * m_permuted, 1); // 1 temp to allocate the mask
93*bf2c3715SXin Li VERIFY_IS_APPROX(m_permuted, lp*m_original);
94*bf2c3715SXin Li
95*bf2c3715SXin Li m_permuted = m_original;
96*bf2c3715SXin Li VERIFY_EVALUATION_COUNT(m_permuted.noalias() = m_permuted * rp, 1); // 1 temp to allocate the mask
97*bf2c3715SXin Li VERIFY_IS_APPROX(m_permuted, m_original*rp);
98*bf2c3715SXin Li
99*bf2c3715SXin Li if(rows>1 && cols>1)
100*bf2c3715SXin Li {
101*bf2c3715SXin Li lp2 = lp;
102*bf2c3715SXin Li Index i = internal::random<Index>(0, rows-1);
103*bf2c3715SXin Li Index j;
104*bf2c3715SXin Li do j = internal::random<Index>(0, rows-1); while(j==i);
105*bf2c3715SXin Li lp2.applyTranspositionOnTheLeft(i, j);
106*bf2c3715SXin Li lm = lp;
107*bf2c3715SXin Li lm.row(i).swap(lm.row(j));
108*bf2c3715SXin Li VERIFY_IS_APPROX(lm, lp2.toDenseMatrix().template cast<Scalar>());
109*bf2c3715SXin Li
110*bf2c3715SXin Li RightPermutationType rp2 = rp;
111*bf2c3715SXin Li i = internal::random<Index>(0, cols-1);
112*bf2c3715SXin Li do j = internal::random<Index>(0, cols-1); while(j==i);
113*bf2c3715SXin Li rp2.applyTranspositionOnTheRight(i, j);
114*bf2c3715SXin Li rm = rp;
115*bf2c3715SXin Li rm.col(i).swap(rm.col(j));
116*bf2c3715SXin Li VERIFY_IS_APPROX(rm, rp2.toDenseMatrix().template cast<Scalar>());
117*bf2c3715SXin Li }
118*bf2c3715SXin Li
119*bf2c3715SXin Li {
120*bf2c3715SXin Li // simple compilation check
121*bf2c3715SXin Li Matrix<Scalar, Cols, Cols> A = rp;
122*bf2c3715SXin Li Matrix<Scalar, Cols, Cols> B = rp.transpose();
123*bf2c3715SXin Li VERIFY_IS_APPROX(A, B.transpose());
124*bf2c3715SXin Li }
125*bf2c3715SXin Li
126*bf2c3715SXin Li m_permuted = m_original;
127*bf2c3715SXin Li lp = lt;
128*bf2c3715SXin Li rp = rt;
129*bf2c3715SXin Li VERIFY_EVALUATION_COUNT(m_permuted = lt * m_permuted * rt, 1);
130*bf2c3715SXin Li VERIFY_IS_APPROX(m_permuted, lp*m_original*rp.transpose());
131*bf2c3715SXin Li
132*bf2c3715SXin Li VERIFY_IS_APPROX(lt.inverse()*m_permuted*rt.inverse(), m_original);
133*bf2c3715SXin Li
134*bf2c3715SXin Li // Check inplace transpositions
135*bf2c3715SXin Li m_permuted = m_original;
136*bf2c3715SXin Li VERIFY_IS_APPROX(m_permuted = lt * m_permuted, lp * m_original);
137*bf2c3715SXin Li m_permuted = m_original;
138*bf2c3715SXin Li VERIFY_IS_APPROX(m_permuted = lt.inverse() * m_permuted, lp.inverse() * m_original);
139*bf2c3715SXin Li m_permuted = m_original;
140*bf2c3715SXin Li VERIFY_IS_APPROX(m_permuted = m_permuted * rt, m_original * rt);
141*bf2c3715SXin Li m_permuted = m_original;
142*bf2c3715SXin Li VERIFY_IS_APPROX(m_permuted = m_permuted * rt.inverse(), m_original * rt.inverse());
143*bf2c3715SXin Li }
144*bf2c3715SXin Li
145*bf2c3715SXin Li template<typename T>
bug890()146*bf2c3715SXin Li void bug890()
147*bf2c3715SXin Li {
148*bf2c3715SXin Li typedef Matrix<T, Dynamic, Dynamic> MatrixType;
149*bf2c3715SXin Li typedef Matrix<T, Dynamic, 1> VectorType;
150*bf2c3715SXin Li typedef Stride<Dynamic,Dynamic> S;
151*bf2c3715SXin Li typedef Map<MatrixType, Aligned, S> MapType;
152*bf2c3715SXin Li typedef PermutationMatrix<Dynamic> Perm;
153*bf2c3715SXin Li
154*bf2c3715SXin Li VectorType v1(2), v2(2), op(4), rhs(2);
155*bf2c3715SXin Li v1 << 666,667;
156*bf2c3715SXin Li op << 1,0,0,1;
157*bf2c3715SXin Li rhs << 42,42;
158*bf2c3715SXin Li
159*bf2c3715SXin Li Perm P(2);
160*bf2c3715SXin Li P.indices() << 1, 0;
161*bf2c3715SXin Li
162*bf2c3715SXin Li MapType(v1.data(),2,1,S(1,1)) = P * MapType(rhs.data(),2,1,S(1,1));
163*bf2c3715SXin Li VERIFY_IS_APPROX(v1, (P * rhs).eval());
164*bf2c3715SXin Li
165*bf2c3715SXin Li MapType(v1.data(),2,1,S(1,1)) = P.inverse() * MapType(rhs.data(),2,1,S(1,1));
166*bf2c3715SXin Li VERIFY_IS_APPROX(v1, (P.inverse() * rhs).eval());
167*bf2c3715SXin Li }
168*bf2c3715SXin Li
EIGEN_DECLARE_TEST(permutationmatrices)169*bf2c3715SXin Li EIGEN_DECLARE_TEST(permutationmatrices)
170*bf2c3715SXin Li {
171*bf2c3715SXin Li for(int i = 0; i < g_repeat; i++) {
172*bf2c3715SXin Li CALL_SUBTEST_1( permutationmatrices(Matrix<float, 1, 1>()) );
173*bf2c3715SXin Li CALL_SUBTEST_2( permutationmatrices(Matrix3f()) );
174*bf2c3715SXin Li CALL_SUBTEST_3( permutationmatrices(Matrix<double,3,3,RowMajor>()) );
175*bf2c3715SXin Li CALL_SUBTEST_4( permutationmatrices(Matrix4d()) );
176*bf2c3715SXin Li CALL_SUBTEST_5( permutationmatrices(Matrix<double,40,60>()) );
177*bf2c3715SXin Li CALL_SUBTEST_6( permutationmatrices(Matrix<double,Dynamic,Dynamic,RowMajor>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
178*bf2c3715SXin Li CALL_SUBTEST_7( permutationmatrices(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
179*bf2c3715SXin Li }
180*bf2c3715SXin Li CALL_SUBTEST_5( bug890<double>() );
181*bf2c3715SXin Li }
182