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) 2006-2008 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 #include "main.h"
11*bf2c3715SXin Li #include <Eigen/QR>
12*bf2c3715SXin Li
13*bf2c3715SXin Li template<typename Derived1, typename Derived2>
14*bf2c3715SXin Li bool areNotApprox(const MatrixBase<Derived1>& m1, const MatrixBase<Derived2>& m2, typename Derived1::RealScalar epsilon = NumTraits<typename Derived1::RealScalar>::dummy_precision())
15*bf2c3715SXin Li {
16*bf2c3715SXin Li return !((m1-m2).cwiseAbs2().maxCoeff() < epsilon * epsilon
17*bf2c3715SXin Li * (std::max)(m1.cwiseAbs2().maxCoeff(), m2.cwiseAbs2().maxCoeff()));
18*bf2c3715SXin Li }
19*bf2c3715SXin Li
product(const MatrixType & m)20*bf2c3715SXin Li template<typename MatrixType> void product(const MatrixType& m)
21*bf2c3715SXin Li {
22*bf2c3715SXin Li /* this test covers the following files:
23*bf2c3715SXin Li Identity.h Product.h
24*bf2c3715SXin Li */
25*bf2c3715SXin Li typedef typename MatrixType::Scalar Scalar;
26*bf2c3715SXin Li typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> RowVectorType;
27*bf2c3715SXin Li typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, 1> ColVectorType;
28*bf2c3715SXin Li typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> RowSquareMatrixType;
29*bf2c3715SXin Li typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, MatrixType::ColsAtCompileTime> ColSquareMatrixType;
30*bf2c3715SXin Li typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime,
31*bf2c3715SXin Li MatrixType::Flags&RowMajorBit?ColMajor:RowMajor> OtherMajorMatrixType;
32*bf2c3715SXin Li
33*bf2c3715SXin Li Index rows = m.rows();
34*bf2c3715SXin Li Index cols = m.cols();
35*bf2c3715SXin Li
36*bf2c3715SXin Li // this test relies a lot on Random.h, and there's not much more that we can do
37*bf2c3715SXin Li // to test it, hence I consider that we will have tested Random.h
38*bf2c3715SXin Li MatrixType m1 = MatrixType::Random(rows, cols),
39*bf2c3715SXin Li m2 = MatrixType::Random(rows, cols),
40*bf2c3715SXin Li m3(rows, cols);
41*bf2c3715SXin Li RowSquareMatrixType
42*bf2c3715SXin Li identity = RowSquareMatrixType::Identity(rows, rows),
43*bf2c3715SXin Li square = RowSquareMatrixType::Random(rows, rows),
44*bf2c3715SXin Li res = RowSquareMatrixType::Random(rows, rows);
45*bf2c3715SXin Li ColSquareMatrixType
46*bf2c3715SXin Li square2 = ColSquareMatrixType::Random(cols, cols),
47*bf2c3715SXin Li res2 = ColSquareMatrixType::Random(cols, cols);
48*bf2c3715SXin Li RowVectorType v1 = RowVectorType::Random(rows);
49*bf2c3715SXin Li ColVectorType vc2 = ColVectorType::Random(cols), vcres(cols);
50*bf2c3715SXin Li OtherMajorMatrixType tm1 = m1;
51*bf2c3715SXin Li
52*bf2c3715SXin Li Scalar s1 = internal::random<Scalar>();
53*bf2c3715SXin Li
54*bf2c3715SXin Li Index r = internal::random<Index>(0, rows-1),
55*bf2c3715SXin Li c = internal::random<Index>(0, cols-1),
56*bf2c3715SXin Li c2 = internal::random<Index>(0, cols-1);
57*bf2c3715SXin Li
58*bf2c3715SXin Li // begin testing Product.h: only associativity for now
59*bf2c3715SXin Li // (we use Transpose.h but this doesn't count as a test for it)
60*bf2c3715SXin Li VERIFY_IS_APPROX((m1*m1.transpose())*m2, m1*(m1.transpose()*m2));
61*bf2c3715SXin Li m3 = m1;
62*bf2c3715SXin Li m3 *= m1.transpose() * m2;
63*bf2c3715SXin Li VERIFY_IS_APPROX(m3, m1 * (m1.transpose()*m2));
64*bf2c3715SXin Li VERIFY_IS_APPROX(m3, m1 * (m1.transpose()*m2));
65*bf2c3715SXin Li
66*bf2c3715SXin Li // continue testing Product.h: distributivity
67*bf2c3715SXin Li VERIFY_IS_APPROX(square*(m1 + m2), square*m1+square*m2);
68*bf2c3715SXin Li VERIFY_IS_APPROX(square*(m1 - m2), square*m1-square*m2);
69*bf2c3715SXin Li
70*bf2c3715SXin Li // continue testing Product.h: compatibility with ScalarMultiple.h
71*bf2c3715SXin Li VERIFY_IS_APPROX(s1*(square*m1), (s1*square)*m1);
72*bf2c3715SXin Li VERIFY_IS_APPROX(s1*(square*m1), square*(m1*s1));
73*bf2c3715SXin Li
74*bf2c3715SXin Li // test Product.h together with Identity.h
75*bf2c3715SXin Li VERIFY_IS_APPROX(v1, identity*v1);
76*bf2c3715SXin Li VERIFY_IS_APPROX(v1.transpose(), v1.transpose() * identity);
77*bf2c3715SXin Li // again, test operator() to check const-qualification
78*bf2c3715SXin Li VERIFY_IS_APPROX(MatrixType::Identity(rows, cols)(r,c), static_cast<Scalar>(r==c));
79*bf2c3715SXin Li
80*bf2c3715SXin Li if (rows!=cols)
81*bf2c3715SXin Li VERIFY_RAISES_ASSERT(m3 = m1*m1);
82*bf2c3715SXin Li
83*bf2c3715SXin Li // test the previous tests were not screwed up because operator* returns 0
84*bf2c3715SXin Li // (we use the more accurate default epsilon)
85*bf2c3715SXin Li if (!NumTraits<Scalar>::IsInteger && (std::min)(rows,cols)>1)
86*bf2c3715SXin Li {
87*bf2c3715SXin Li VERIFY(areNotApprox(m1.transpose()*m2,m2.transpose()*m1));
88*bf2c3715SXin Li }
89*bf2c3715SXin Li
90*bf2c3715SXin Li // test optimized operator+= path
91*bf2c3715SXin Li res = square;
92*bf2c3715SXin Li res.noalias() += m1 * m2.transpose();
93*bf2c3715SXin Li VERIFY_IS_APPROX(res, square + m1 * m2.transpose());
94*bf2c3715SXin Li if (!NumTraits<Scalar>::IsInteger && (std::min)(rows,cols)>1)
95*bf2c3715SXin Li {
96*bf2c3715SXin Li VERIFY(areNotApprox(res,square + m2 * m1.transpose()));
97*bf2c3715SXin Li }
98*bf2c3715SXin Li vcres = vc2;
99*bf2c3715SXin Li vcres.noalias() += m1.transpose() * v1;
100*bf2c3715SXin Li VERIFY_IS_APPROX(vcres, vc2 + m1.transpose() * v1);
101*bf2c3715SXin Li
102*bf2c3715SXin Li // test optimized operator-= path
103*bf2c3715SXin Li res = square;
104*bf2c3715SXin Li res.noalias() -= m1 * m2.transpose();
105*bf2c3715SXin Li VERIFY_IS_APPROX(res, square - (m1 * m2.transpose()));
106*bf2c3715SXin Li if (!NumTraits<Scalar>::IsInteger && (std::min)(rows,cols)>1)
107*bf2c3715SXin Li {
108*bf2c3715SXin Li VERIFY(areNotApprox(res,square - m2 * m1.transpose()));
109*bf2c3715SXin Li }
110*bf2c3715SXin Li vcres = vc2;
111*bf2c3715SXin Li vcres.noalias() -= m1.transpose() * v1;
112*bf2c3715SXin Li VERIFY_IS_APPROX(vcres, vc2 - m1.transpose() * v1);
113*bf2c3715SXin Li
114*bf2c3715SXin Li // test scaled products
115*bf2c3715SXin Li res = square;
116*bf2c3715SXin Li res.noalias() = s1 * m1 * m2.transpose();
117*bf2c3715SXin Li VERIFY_IS_APPROX(res, ((s1*m1).eval() * m2.transpose()));
118*bf2c3715SXin Li res = square;
119*bf2c3715SXin Li res.noalias() += s1 * m1 * m2.transpose();
120*bf2c3715SXin Li VERIFY_IS_APPROX(res, square + ((s1*m1).eval() * m2.transpose()));
121*bf2c3715SXin Li res = square;
122*bf2c3715SXin Li res.noalias() -= s1 * m1 * m2.transpose();
123*bf2c3715SXin Li VERIFY_IS_APPROX(res, square - ((s1*m1).eval() * m2.transpose()));
124*bf2c3715SXin Li
125*bf2c3715SXin Li // test d ?= a+b*c rules
126*bf2c3715SXin Li res.noalias() = square + m1 * m2.transpose();
127*bf2c3715SXin Li VERIFY_IS_APPROX(res, square + m1 * m2.transpose());
128*bf2c3715SXin Li res.noalias() += square + m1 * m2.transpose();
129*bf2c3715SXin Li VERIFY_IS_APPROX(res, 2*(square + m1 * m2.transpose()));
130*bf2c3715SXin Li res.noalias() -= square + m1 * m2.transpose();
131*bf2c3715SXin Li VERIFY_IS_APPROX(res, square + m1 * m2.transpose());
132*bf2c3715SXin Li
133*bf2c3715SXin Li // test d ?= a-b*c rules
134*bf2c3715SXin Li res.noalias() = square - m1 * m2.transpose();
135*bf2c3715SXin Li VERIFY_IS_APPROX(res, square - m1 * m2.transpose());
136*bf2c3715SXin Li res.noalias() += square - m1 * m2.transpose();
137*bf2c3715SXin Li VERIFY_IS_APPROX(res, 2*(square - m1 * m2.transpose()));
138*bf2c3715SXin Li res.noalias() -= square - m1 * m2.transpose();
139*bf2c3715SXin Li VERIFY_IS_APPROX(res, square - m1 * m2.transpose());
140*bf2c3715SXin Li
141*bf2c3715SXin Li
142*bf2c3715SXin Li tm1 = m1;
143*bf2c3715SXin Li VERIFY_IS_APPROX(tm1.transpose() * v1, m1.transpose() * v1);
144*bf2c3715SXin Li VERIFY_IS_APPROX(v1.transpose() * tm1, v1.transpose() * m1);
145*bf2c3715SXin Li
146*bf2c3715SXin Li // test submatrix and matrix/vector product
147*bf2c3715SXin Li for (int i=0; i<rows; ++i)
148*bf2c3715SXin Li res.row(i) = m1.row(i) * m2.transpose();
149*bf2c3715SXin Li VERIFY_IS_APPROX(res, m1 * m2.transpose());
150*bf2c3715SXin Li // the other way round:
151*bf2c3715SXin Li for (int i=0; i<rows; ++i)
152*bf2c3715SXin Li res.col(i) = m1 * m2.transpose().col(i);
153*bf2c3715SXin Li VERIFY_IS_APPROX(res, m1 * m2.transpose());
154*bf2c3715SXin Li
155*bf2c3715SXin Li res2 = square2;
156*bf2c3715SXin Li res2.noalias() += m1.transpose() * m2;
157*bf2c3715SXin Li VERIFY_IS_APPROX(res2, square2 + m1.transpose() * m2);
158*bf2c3715SXin Li if (!NumTraits<Scalar>::IsInteger && (std::min)(rows,cols)>1)
159*bf2c3715SXin Li {
160*bf2c3715SXin Li VERIFY(areNotApprox(res2,square2 + m2.transpose() * m1));
161*bf2c3715SXin Li }
162*bf2c3715SXin Li
163*bf2c3715SXin Li VERIFY_IS_APPROX(res.col(r).noalias() = square.adjoint() * square.col(r), (square.adjoint() * square.col(r)).eval());
164*bf2c3715SXin Li VERIFY_IS_APPROX(res.col(r).noalias() = square * square.col(r), (square * square.col(r)).eval());
165*bf2c3715SXin Li
166*bf2c3715SXin Li // vector at runtime (see bug 1166)
167*bf2c3715SXin Li {
168*bf2c3715SXin Li RowSquareMatrixType ref(square);
169*bf2c3715SXin Li ColSquareMatrixType ref2(square2);
170*bf2c3715SXin Li ref = res = square;
171*bf2c3715SXin Li VERIFY_IS_APPROX(res.block(0,0,1,rows).noalias() = m1.col(0).transpose() * square.transpose(), (ref.row(0) = m1.col(0).transpose() * square.transpose()));
172*bf2c3715SXin Li VERIFY_IS_APPROX(res.block(0,0,1,rows).noalias() = m1.block(0,0,rows,1).transpose() * square.transpose(), (ref.row(0) = m1.col(0).transpose() * square.transpose()));
173*bf2c3715SXin Li VERIFY_IS_APPROX(res.block(0,0,1,rows).noalias() = m1.col(0).transpose() * square, (ref.row(0) = m1.col(0).transpose() * square));
174*bf2c3715SXin Li VERIFY_IS_APPROX(res.block(0,0,1,rows).noalias() = m1.block(0,0,rows,1).transpose() * square, (ref.row(0) = m1.col(0).transpose() * square));
175*bf2c3715SXin Li ref2 = res2 = square2;
176*bf2c3715SXin Li VERIFY_IS_APPROX(res2.block(0,0,1,cols).noalias() = m1.row(0) * square2.transpose(), (ref2.row(0) = m1.row(0) * square2.transpose()));
177*bf2c3715SXin Li VERIFY_IS_APPROX(res2.block(0,0,1,cols).noalias() = m1.block(0,0,1,cols) * square2.transpose(), (ref2.row(0) = m1.row(0) * square2.transpose()));
178*bf2c3715SXin Li VERIFY_IS_APPROX(res2.block(0,0,1,cols).noalias() = m1.row(0) * square2, (ref2.row(0) = m1.row(0) * square2));
179*bf2c3715SXin Li VERIFY_IS_APPROX(res2.block(0,0,1,cols).noalias() = m1.block(0,0,1,cols) * square2, (ref2.row(0) = m1.row(0) * square2));
180*bf2c3715SXin Li }
181*bf2c3715SXin Li
182*bf2c3715SXin Li // vector.block() (see bug 1283)
183*bf2c3715SXin Li {
184*bf2c3715SXin Li RowVectorType w1(rows);
185*bf2c3715SXin Li VERIFY_IS_APPROX(square * v1.block(0,0,rows,1), square * v1);
186*bf2c3715SXin Li VERIFY_IS_APPROX(w1.noalias() = square * v1.block(0,0,rows,1), square * v1);
187*bf2c3715SXin Li VERIFY_IS_APPROX(w1.block(0,0,rows,1).noalias() = square * v1.block(0,0,rows,1), square * v1);
188*bf2c3715SXin Li
189*bf2c3715SXin Li Matrix<Scalar,1,MatrixType::ColsAtCompileTime> w2(cols);
190*bf2c3715SXin Li VERIFY_IS_APPROX(vc2.block(0,0,cols,1).transpose() * square2, vc2.transpose() * square2);
191*bf2c3715SXin Li VERIFY_IS_APPROX(w2.noalias() = vc2.block(0,0,cols,1).transpose() * square2, vc2.transpose() * square2);
192*bf2c3715SXin Li VERIFY_IS_APPROX(w2.block(0,0,1,cols).noalias() = vc2.block(0,0,cols,1).transpose() * square2, vc2.transpose() * square2);
193*bf2c3715SXin Li
194*bf2c3715SXin Li vc2 = square2.block(0,0,1,cols).transpose();
195*bf2c3715SXin Li VERIFY_IS_APPROX(square2.block(0,0,1,cols) * square2, vc2.transpose() * square2);
196*bf2c3715SXin Li VERIFY_IS_APPROX(w2.noalias() = square2.block(0,0,1,cols) * square2, vc2.transpose() * square2);
197*bf2c3715SXin Li VERIFY_IS_APPROX(w2.block(0,0,1,cols).noalias() = square2.block(0,0,1,cols) * square2, vc2.transpose() * square2);
198*bf2c3715SXin Li
199*bf2c3715SXin Li vc2 = square2.block(0,0,cols,1);
200*bf2c3715SXin Li VERIFY_IS_APPROX(square2.block(0,0,cols,1).transpose() * square2, vc2.transpose() * square2);
201*bf2c3715SXin Li VERIFY_IS_APPROX(w2.noalias() = square2.block(0,0,cols,1).transpose() * square2, vc2.transpose() * square2);
202*bf2c3715SXin Li VERIFY_IS_APPROX(w2.block(0,0,1,cols).noalias() = square2.block(0,0,cols,1).transpose() * square2, vc2.transpose() * square2);
203*bf2c3715SXin Li }
204*bf2c3715SXin Li
205*bf2c3715SXin Li // inner product
206*bf2c3715SXin Li {
207*bf2c3715SXin Li Scalar x = square2.row(c) * square2.col(c2);
208*bf2c3715SXin Li VERIFY_IS_APPROX(x, square2.row(c).transpose().cwiseProduct(square2.col(c2)).sum());
209*bf2c3715SXin Li }
210*bf2c3715SXin Li
211*bf2c3715SXin Li // outer product
212*bf2c3715SXin Li {
213*bf2c3715SXin Li VERIFY_IS_APPROX(m1.col(c) * m1.row(r), m1.block(0,c,rows,1) * m1.block(r,0,1,cols));
214*bf2c3715SXin Li VERIFY_IS_APPROX(m1.row(r).transpose() * m1.col(c).transpose(), m1.block(r,0,1,cols).transpose() * m1.block(0,c,rows,1).transpose());
215*bf2c3715SXin Li VERIFY_IS_APPROX(m1.block(0,c,rows,1) * m1.row(r), m1.block(0,c,rows,1) * m1.block(r,0,1,cols));
216*bf2c3715SXin Li VERIFY_IS_APPROX(m1.col(c) * m1.block(r,0,1,cols), m1.block(0,c,rows,1) * m1.block(r,0,1,cols));
217*bf2c3715SXin Li VERIFY_IS_APPROX(m1.leftCols(1) * m1.row(r), m1.block(0,0,rows,1) * m1.block(r,0,1,cols));
218*bf2c3715SXin Li VERIFY_IS_APPROX(m1.col(c) * m1.topRows(1), m1.block(0,c,rows,1) * m1.block(0,0,1,cols));
219*bf2c3715SXin Li }
220*bf2c3715SXin Li
221*bf2c3715SXin Li // Aliasing
222*bf2c3715SXin Li {
223*bf2c3715SXin Li ColVectorType x(cols); x.setRandom();
224*bf2c3715SXin Li ColVectorType z(x);
225*bf2c3715SXin Li ColVectorType y(cols); y.setZero();
226*bf2c3715SXin Li ColSquareMatrixType A(cols,cols); A.setRandom();
227*bf2c3715SXin Li // CwiseBinaryOp
228*bf2c3715SXin Li VERIFY_IS_APPROX(x = y + A*x, A*z);
229*bf2c3715SXin Li x = z;
230*bf2c3715SXin Li VERIFY_IS_APPROX(x = y - A*x, A*(-z));
231*bf2c3715SXin Li x = z;
232*bf2c3715SXin Li // CwiseUnaryOp
233*bf2c3715SXin Li VERIFY_IS_APPROX(x = Scalar(1.)*(A*x), A*z);
234*bf2c3715SXin Li }
235*bf2c3715SXin Li
236*bf2c3715SXin Li // regression for blas_trais
237*bf2c3715SXin Li {
238*bf2c3715SXin Li VERIFY_IS_APPROX(square * (square*square).transpose(), square * square.transpose() * square.transpose());
239*bf2c3715SXin Li VERIFY_IS_APPROX(square * (-(square*square)), -square * square * square);
240*bf2c3715SXin Li VERIFY_IS_APPROX(square * (s1*(square*square)), s1 * square * square * square);
241*bf2c3715SXin Li VERIFY_IS_APPROX(square * (square*square).conjugate(), square * square.conjugate() * square.conjugate());
242*bf2c3715SXin Li }
243*bf2c3715SXin Li
244*bf2c3715SXin Li // destination with a non-default inner-stride
245*bf2c3715SXin Li // see bug 1741
246*bf2c3715SXin Li if(!MatrixType::IsRowMajor)
247*bf2c3715SXin Li {
248*bf2c3715SXin Li typedef Matrix<Scalar,Dynamic,Dynamic> MatrixX;
249*bf2c3715SXin Li MatrixX buffer(2*rows,2*rows);
250*bf2c3715SXin Li Map<RowSquareMatrixType,0,Stride<Dynamic,2> > map1(buffer.data(),rows,rows,Stride<Dynamic,2>(2*rows,2));
251*bf2c3715SXin Li buffer.setZero();
252*bf2c3715SXin Li VERIFY_IS_APPROX(map1 = m1 * m2.transpose(), (m1 * m2.transpose()).eval());
253*bf2c3715SXin Li buffer.setZero();
254*bf2c3715SXin Li VERIFY_IS_APPROX(map1.noalias() = m1 * m2.transpose(), (m1 * m2.transpose()).eval());
255*bf2c3715SXin Li buffer.setZero();
256*bf2c3715SXin Li VERIFY_IS_APPROX(map1.noalias() += m1 * m2.transpose(), (m1 * m2.transpose()).eval());
257*bf2c3715SXin Li }
258*bf2c3715SXin Li
259*bf2c3715SXin Li }
260