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 #define EIGEN_NO_STATIC_ASSERT
11*bf2c3715SXin Li
12*bf2c3715SXin Li #include "main.h"
13*bf2c3715SXin Li
14*bf2c3715SXin Li template<bool IsInteger> struct adjoint_specific;
15*bf2c3715SXin Li
16*bf2c3715SXin Li template<> struct adjoint_specific<true> {
17*bf2c3715SXin Li template<typename Vec, typename Mat, typename Scalar>
runadjoint_specific18*bf2c3715SXin Li static void run(const Vec& v1, const Vec& v2, Vec& v3, const Mat& square, Scalar s1, Scalar s2) {
19*bf2c3715SXin Li VERIFY(test_isApproxWithRef((s1 * v1 + s2 * v2).dot(v3), numext::conj(s1) * v1.dot(v3) + numext::conj(s2) * v2.dot(v3), 0));
20*bf2c3715SXin Li VERIFY(test_isApproxWithRef(v3.dot(s1 * v1 + s2 * v2), s1*v3.dot(v1)+s2*v3.dot(v2), 0));
21*bf2c3715SXin Li
22*bf2c3715SXin Li // check compatibility of dot and adjoint
23*bf2c3715SXin Li VERIFY(test_isApproxWithRef(v1.dot(square * v2), (square.adjoint() * v1).dot(v2), 0));
24*bf2c3715SXin Li }
25*bf2c3715SXin Li };
26*bf2c3715SXin Li
27*bf2c3715SXin Li template<> struct adjoint_specific<false> {
28*bf2c3715SXin Li template<typename Vec, typename Mat, typename Scalar>
runadjoint_specific29*bf2c3715SXin Li static void run(const Vec& v1, const Vec& v2, Vec& v3, const Mat& square, Scalar s1, Scalar s2) {
30*bf2c3715SXin Li typedef typename NumTraits<Scalar>::Real RealScalar;
31*bf2c3715SXin Li using std::abs;
32*bf2c3715SXin Li
33*bf2c3715SXin Li RealScalar ref = NumTraits<Scalar>::IsInteger ? RealScalar(0) : (std::max)((s1 * v1 + s2 * v2).norm(),v3.norm());
34*bf2c3715SXin Li VERIFY(test_isApproxWithRef((s1 * v1 + s2 * v2).dot(v3), numext::conj(s1) * v1.dot(v3) + numext::conj(s2) * v2.dot(v3), ref));
35*bf2c3715SXin Li VERIFY(test_isApproxWithRef(v3.dot(s1 * v1 + s2 * v2), s1*v3.dot(v1)+s2*v3.dot(v2), ref));
36*bf2c3715SXin Li
37*bf2c3715SXin Li VERIFY_IS_APPROX(v1.squaredNorm(), v1.norm() * v1.norm());
38*bf2c3715SXin Li // check normalized() and normalize()
39*bf2c3715SXin Li VERIFY_IS_APPROX(v1, v1.norm() * v1.normalized());
40*bf2c3715SXin Li v3 = v1;
41*bf2c3715SXin Li v3.normalize();
42*bf2c3715SXin Li VERIFY_IS_APPROX(v1, v1.norm() * v3);
43*bf2c3715SXin Li VERIFY_IS_APPROX(v3, v1.normalized());
44*bf2c3715SXin Li VERIFY_IS_APPROX(v3.norm(), RealScalar(1));
45*bf2c3715SXin Li
46*bf2c3715SXin Li // check null inputs
47*bf2c3715SXin Li VERIFY_IS_APPROX((v1*0).normalized(), (v1*0));
48*bf2c3715SXin Li #if (!EIGEN_ARCH_i386) || defined(EIGEN_VECTORIZE)
49*bf2c3715SXin Li RealScalar very_small = (std::numeric_limits<RealScalar>::min)();
50*bf2c3715SXin Li VERIFY( (v1*very_small).norm() == 0 );
51*bf2c3715SXin Li VERIFY_IS_APPROX((v1*very_small).normalized(), (v1*very_small));
52*bf2c3715SXin Li v3 = v1*very_small;
53*bf2c3715SXin Li v3.normalize();
54*bf2c3715SXin Li VERIFY_IS_APPROX(v3, (v1*very_small));
55*bf2c3715SXin Li #endif
56*bf2c3715SXin Li
57*bf2c3715SXin Li // check compatibility of dot and adjoint
58*bf2c3715SXin Li ref = NumTraits<Scalar>::IsInteger ? 0 : (std::max)((std::max)(v1.norm(),v2.norm()),(std::max)((square * v2).norm(),(square.adjoint() * v1).norm()));
59*bf2c3715SXin Li VERIFY(internal::isMuchSmallerThan(abs(v1.dot(square * v2) - (square.adjoint() * v1).dot(v2)), ref, test_precision<Scalar>()));
60*bf2c3715SXin Li
61*bf2c3715SXin Li // check that Random().normalized() works: tricky as the random xpr must be evaluated by
62*bf2c3715SXin Li // normalized() in order to produce a consistent result.
63*bf2c3715SXin Li VERIFY_IS_APPROX(Vec::Random(v1.size()).normalized().norm(), RealScalar(1));
64*bf2c3715SXin Li }
65*bf2c3715SXin Li };
66*bf2c3715SXin Li
adjoint(const MatrixType & m)67*bf2c3715SXin Li template<typename MatrixType> void adjoint(const MatrixType& m)
68*bf2c3715SXin Li {
69*bf2c3715SXin Li /* this test covers the following files:
70*bf2c3715SXin Li Transpose.h Conjugate.h Dot.h
71*bf2c3715SXin Li */
72*bf2c3715SXin Li using std::abs;
73*bf2c3715SXin Li typedef typename MatrixType::Scalar Scalar;
74*bf2c3715SXin Li typedef typename NumTraits<Scalar>::Real RealScalar;
75*bf2c3715SXin Li typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
76*bf2c3715SXin Li typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> SquareMatrixType;
77*bf2c3715SXin Li const Index PacketSize = internal::packet_traits<Scalar>::size;
78*bf2c3715SXin Li
79*bf2c3715SXin Li Index rows = m.rows();
80*bf2c3715SXin Li Index cols = m.cols();
81*bf2c3715SXin Li
82*bf2c3715SXin Li MatrixType m1 = MatrixType::Random(rows, cols),
83*bf2c3715SXin Li m2 = MatrixType::Random(rows, cols),
84*bf2c3715SXin Li m3(rows, cols),
85*bf2c3715SXin Li square = SquareMatrixType::Random(rows, rows);
86*bf2c3715SXin Li VectorType v1 = VectorType::Random(rows),
87*bf2c3715SXin Li v2 = VectorType::Random(rows),
88*bf2c3715SXin Li v3 = VectorType::Random(rows),
89*bf2c3715SXin Li vzero = VectorType::Zero(rows);
90*bf2c3715SXin Li
91*bf2c3715SXin Li Scalar s1 = internal::random<Scalar>(),
92*bf2c3715SXin Li s2 = internal::random<Scalar>();
93*bf2c3715SXin Li
94*bf2c3715SXin Li // check basic compatibility of adjoint, transpose, conjugate
95*bf2c3715SXin Li VERIFY_IS_APPROX(m1.transpose().conjugate().adjoint(), m1);
96*bf2c3715SXin Li VERIFY_IS_APPROX(m1.adjoint().conjugate().transpose(), m1);
97*bf2c3715SXin Li
98*bf2c3715SXin Li // check multiplicative behavior
99*bf2c3715SXin Li VERIFY_IS_APPROX((m1.adjoint() * m2).adjoint(), m2.adjoint() * m1);
100*bf2c3715SXin Li VERIFY_IS_APPROX((s1 * m1).adjoint(), numext::conj(s1) * m1.adjoint());
101*bf2c3715SXin Li
102*bf2c3715SXin Li // check basic properties of dot, squaredNorm
103*bf2c3715SXin Li VERIFY_IS_APPROX(numext::conj(v1.dot(v2)), v2.dot(v1));
104*bf2c3715SXin Li VERIFY_IS_APPROX(numext::real(v1.dot(v1)), v1.squaredNorm());
105*bf2c3715SXin Li
106*bf2c3715SXin Li adjoint_specific<NumTraits<Scalar>::IsInteger>::run(v1, v2, v3, square, s1, s2);
107*bf2c3715SXin Li
108*bf2c3715SXin Li VERIFY_IS_MUCH_SMALLER_THAN(abs(vzero.dot(v1)), static_cast<RealScalar>(1));
109*bf2c3715SXin Li
110*bf2c3715SXin Li // like in testBasicStuff, test operator() to check const-qualification
111*bf2c3715SXin Li Index r = internal::random<Index>(0, rows-1),
112*bf2c3715SXin Li c = internal::random<Index>(0, cols-1);
113*bf2c3715SXin Li VERIFY_IS_APPROX(m1.conjugate()(r,c), numext::conj(m1(r,c)));
114*bf2c3715SXin Li VERIFY_IS_APPROX(m1.adjoint()(c,r), numext::conj(m1(r,c)));
115*bf2c3715SXin Li
116*bf2c3715SXin Li // check inplace transpose
117*bf2c3715SXin Li m3 = m1;
118*bf2c3715SXin Li m3.transposeInPlace();
119*bf2c3715SXin Li VERIFY_IS_APPROX(m3,m1.transpose());
120*bf2c3715SXin Li m3.transposeInPlace();
121*bf2c3715SXin Li VERIFY_IS_APPROX(m3,m1);
122*bf2c3715SXin Li
123*bf2c3715SXin Li if(PacketSize<m3.rows() && PacketSize<m3.cols())
124*bf2c3715SXin Li {
125*bf2c3715SXin Li m3 = m1;
126*bf2c3715SXin Li Index i = internal::random<Index>(0,m3.rows()-PacketSize);
127*bf2c3715SXin Li Index j = internal::random<Index>(0,m3.cols()-PacketSize);
128*bf2c3715SXin Li m3.template block<PacketSize,PacketSize>(i,j).transposeInPlace();
129*bf2c3715SXin Li VERIFY_IS_APPROX( (m3.template block<PacketSize,PacketSize>(i,j)), (m1.template block<PacketSize,PacketSize>(i,j).transpose()) );
130*bf2c3715SXin Li m3.template block<PacketSize,PacketSize>(i,j).transposeInPlace();
131*bf2c3715SXin Li VERIFY_IS_APPROX(m3,m1);
132*bf2c3715SXin Li }
133*bf2c3715SXin Li
134*bf2c3715SXin Li // check inplace adjoint
135*bf2c3715SXin Li m3 = m1;
136*bf2c3715SXin Li m3.adjointInPlace();
137*bf2c3715SXin Li VERIFY_IS_APPROX(m3,m1.adjoint());
138*bf2c3715SXin Li m3.transposeInPlace();
139*bf2c3715SXin Li VERIFY_IS_APPROX(m3,m1.conjugate());
140*bf2c3715SXin Li
141*bf2c3715SXin Li // check mixed dot product
142*bf2c3715SXin Li typedef Matrix<RealScalar, MatrixType::RowsAtCompileTime, 1> RealVectorType;
143*bf2c3715SXin Li RealVectorType rv1 = RealVectorType::Random(rows);
144*bf2c3715SXin Li VERIFY_IS_APPROX(v1.dot(rv1.template cast<Scalar>()), v1.dot(rv1));
145*bf2c3715SXin Li VERIFY_IS_APPROX(rv1.template cast<Scalar>().dot(v1), rv1.dot(v1));
146*bf2c3715SXin Li
147*bf2c3715SXin Li VERIFY( is_same_type(m1,m1.template conjugateIf<false>()) );
148*bf2c3715SXin Li VERIFY( is_same_type(m1.conjugate(),m1.template conjugateIf<true>()) );
149*bf2c3715SXin Li }
150*bf2c3715SXin Li
151*bf2c3715SXin Li template<int>
adjoint_extra()152*bf2c3715SXin Li void adjoint_extra()
153*bf2c3715SXin Li {
154*bf2c3715SXin Li MatrixXcf a(10,10), b(10,10);
155*bf2c3715SXin Li VERIFY_RAISES_ASSERT(a = a.transpose());
156*bf2c3715SXin Li VERIFY_RAISES_ASSERT(a = a.transpose() + b);
157*bf2c3715SXin Li VERIFY_RAISES_ASSERT(a = b + a.transpose());
158*bf2c3715SXin Li VERIFY_RAISES_ASSERT(a = a.conjugate().transpose());
159*bf2c3715SXin Li VERIFY_RAISES_ASSERT(a = a.adjoint());
160*bf2c3715SXin Li VERIFY_RAISES_ASSERT(a = a.adjoint() + b);
161*bf2c3715SXin Li VERIFY_RAISES_ASSERT(a = b + a.adjoint());
162*bf2c3715SXin Li
163*bf2c3715SXin Li // no assertion should be triggered for these cases:
164*bf2c3715SXin Li a.transpose() = a.transpose();
165*bf2c3715SXin Li a.transpose() += a.transpose();
166*bf2c3715SXin Li a.transpose() += a.transpose() + b;
167*bf2c3715SXin Li a.transpose() = a.adjoint();
168*bf2c3715SXin Li a.transpose() += a.adjoint();
169*bf2c3715SXin Li a.transpose() += a.adjoint() + b;
170*bf2c3715SXin Li
171*bf2c3715SXin Li // regression tests for check_for_aliasing
172*bf2c3715SXin Li MatrixXd c(10,10);
173*bf2c3715SXin Li c = 1.0 * MatrixXd::Ones(10,10) + c;
174*bf2c3715SXin Li c = MatrixXd::Ones(10,10) * 1.0 + c;
175*bf2c3715SXin Li c = c + MatrixXd::Ones(10,10) .cwiseProduct( MatrixXd::Zero(10,10) );
176*bf2c3715SXin Li c = MatrixXd::Ones(10,10) * MatrixXd::Zero(10,10);
177*bf2c3715SXin Li
178*bf2c3715SXin Li // regression for bug 1646
179*bf2c3715SXin Li for (int j = 0; j < 10; ++j) {
180*bf2c3715SXin Li c.col(j).head(j) = c.row(j).head(j);
181*bf2c3715SXin Li }
182*bf2c3715SXin Li
183*bf2c3715SXin Li for (int j = 0; j < 10; ++j) {
184*bf2c3715SXin Li c.col(j) = c.row(j);
185*bf2c3715SXin Li }
186*bf2c3715SXin Li
187*bf2c3715SXin Li a.conservativeResize(1,1);
188*bf2c3715SXin Li a = a.transpose();
189*bf2c3715SXin Li
190*bf2c3715SXin Li a.conservativeResize(0,0);
191*bf2c3715SXin Li a = a.transpose();
192*bf2c3715SXin Li }
193*bf2c3715SXin Li
EIGEN_DECLARE_TEST(adjoint)194*bf2c3715SXin Li EIGEN_DECLARE_TEST(adjoint)
195*bf2c3715SXin Li {
196*bf2c3715SXin Li for(int i = 0; i < g_repeat; i++) {
197*bf2c3715SXin Li CALL_SUBTEST_1( adjoint(Matrix<float, 1, 1>()) );
198*bf2c3715SXin Li CALL_SUBTEST_2( adjoint(Matrix3d()) );
199*bf2c3715SXin Li CALL_SUBTEST_3( adjoint(Matrix4f()) );
200*bf2c3715SXin Li
201*bf2c3715SXin Li CALL_SUBTEST_4( adjoint(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
202*bf2c3715SXin Li CALL_SUBTEST_5( adjoint(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
203*bf2c3715SXin Li CALL_SUBTEST_6( adjoint(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
204*bf2c3715SXin Li
205*bf2c3715SXin Li // Complement for 128 bits vectorization:
206*bf2c3715SXin Li CALL_SUBTEST_8( adjoint(Matrix2d()) );
207*bf2c3715SXin Li CALL_SUBTEST_9( adjoint(Matrix<int,4,4>()) );
208*bf2c3715SXin Li
209*bf2c3715SXin Li // 256 bits vectorization:
210*bf2c3715SXin Li CALL_SUBTEST_10( adjoint(Matrix<float,8,8>()) );
211*bf2c3715SXin Li CALL_SUBTEST_11( adjoint(Matrix<double,4,4>()) );
212*bf2c3715SXin Li CALL_SUBTEST_12( adjoint(Matrix<int,8,8>()) );
213*bf2c3715SXin Li }
214*bf2c3715SXin Li // test a large static matrix only once
215*bf2c3715SXin Li CALL_SUBTEST_7( adjoint(Matrix<float, 100, 100>()) );
216*bf2c3715SXin Li
217*bf2c3715SXin Li CALL_SUBTEST_13( adjoint_extra<0>() );
218*bf2c3715SXin Li }
219*bf2c3715SXin Li
220