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) 2008-2009 Gael Guennebaud <[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
symm(int size=Size,int othersize=OtherSize)12*bf2c3715SXin Li template<typename Scalar, int Size, int OtherSize> void symm(int size = Size, int othersize = OtherSize)
13*bf2c3715SXin Li {
14*bf2c3715SXin Li typedef Matrix<Scalar, Size, Size> MatrixType;
15*bf2c3715SXin Li typedef Matrix<Scalar, Size, OtherSize> Rhs1;
16*bf2c3715SXin Li typedef Matrix<Scalar, OtherSize, Size> Rhs2;
17*bf2c3715SXin Li enum { order = OtherSize==1 ? 0 : RowMajor };
18*bf2c3715SXin Li typedef Matrix<Scalar, Size, OtherSize,order> Rhs3;
19*bf2c3715SXin Li
20*bf2c3715SXin Li Index rows = size;
21*bf2c3715SXin Li Index cols = size;
22*bf2c3715SXin Li
23*bf2c3715SXin Li MatrixType m1 = MatrixType::Random(rows, cols),
24*bf2c3715SXin Li m2 = MatrixType::Random(rows, cols), m3;
25*bf2c3715SXin Li
26*bf2c3715SXin Li m1 = (m1+m1.adjoint()).eval();
27*bf2c3715SXin Li
28*bf2c3715SXin Li Rhs1 rhs1 = Rhs1::Random(cols, othersize), rhs12(cols, othersize), rhs13(cols, othersize);
29*bf2c3715SXin Li Rhs2 rhs2 = Rhs2::Random(othersize, rows), rhs22(othersize, rows), rhs23(othersize, rows);
30*bf2c3715SXin Li Rhs3 rhs3 = Rhs3::Random(cols, othersize), rhs32(cols, othersize), rhs33(cols, othersize);
31*bf2c3715SXin Li
32*bf2c3715SXin Li Scalar s1 = internal::random<Scalar>(),
33*bf2c3715SXin Li s2 = internal::random<Scalar>();
34*bf2c3715SXin Li
35*bf2c3715SXin Li m2 = m1.template triangularView<Lower>();
36*bf2c3715SXin Li m3 = m2.template selfadjointView<Lower>();
37*bf2c3715SXin Li VERIFY_IS_EQUAL(m1, m3);
38*bf2c3715SXin Li VERIFY_IS_APPROX(rhs12 = (s1*m2).template selfadjointView<Lower>() * (s2*rhs1),
39*bf2c3715SXin Li rhs13 = (s1*m1) * (s2*rhs1));
40*bf2c3715SXin Li
41*bf2c3715SXin Li VERIFY_IS_APPROX(rhs12 = (s1*m2).transpose().template selfadjointView<Upper>() * (s2*rhs1),
42*bf2c3715SXin Li rhs13 = (s1*m1.transpose()) * (s2*rhs1));
43*bf2c3715SXin Li
44*bf2c3715SXin Li VERIFY_IS_APPROX(rhs12 = (s1*m2).template selfadjointView<Lower>().transpose() * (s2*rhs1),
45*bf2c3715SXin Li rhs13 = (s1*m1.transpose()) * (s2*rhs1));
46*bf2c3715SXin Li
47*bf2c3715SXin Li VERIFY_IS_APPROX(rhs12 = (s1*m2).conjugate().template selfadjointView<Lower>() * (s2*rhs1),
48*bf2c3715SXin Li rhs13 = (s1*m1).conjugate() * (s2*rhs1));
49*bf2c3715SXin Li
50*bf2c3715SXin Li VERIFY_IS_APPROX(rhs12 = (s1*m2).template selfadjointView<Lower>().conjugate() * (s2*rhs1),
51*bf2c3715SXin Li rhs13 = (s1*m1).conjugate() * (s2*rhs1));
52*bf2c3715SXin Li
53*bf2c3715SXin Li VERIFY_IS_APPROX(rhs12 = (s1*m2).adjoint().template selfadjointView<Upper>() * (s2*rhs1),
54*bf2c3715SXin Li rhs13 = (s1*m1).adjoint() * (s2*rhs1));
55*bf2c3715SXin Li
56*bf2c3715SXin Li VERIFY_IS_APPROX(rhs12 = (s1*m2).template selfadjointView<Lower>().adjoint() * (s2*rhs1),
57*bf2c3715SXin Li rhs13 = (s1*m1).adjoint() * (s2*rhs1));
58*bf2c3715SXin Li
59*bf2c3715SXin Li m2 = m1.template triangularView<Upper>(); rhs12.setRandom(); rhs13 = rhs12;
60*bf2c3715SXin Li m3 = m2.template selfadjointView<Upper>();
61*bf2c3715SXin Li VERIFY_IS_EQUAL(m1, m3);
62*bf2c3715SXin Li VERIFY_IS_APPROX(rhs12 += (s1*m2).template selfadjointView<Upper>() * (s2*rhs1),
63*bf2c3715SXin Li rhs13 += (s1*m1) * (s2*rhs1));
64*bf2c3715SXin Li
65*bf2c3715SXin Li m2 = m1.template triangularView<Lower>();
66*bf2c3715SXin Li VERIFY_IS_APPROX(rhs12 = (s1*m2).template selfadjointView<Lower>() * (s2*rhs2.adjoint()),
67*bf2c3715SXin Li rhs13 = (s1*m1) * (s2*rhs2.adjoint()));
68*bf2c3715SXin Li
69*bf2c3715SXin Li m2 = m1.template triangularView<Upper>();
70*bf2c3715SXin Li VERIFY_IS_APPROX(rhs12 = (s1*m2).template selfadjointView<Upper>() * (s2*rhs2.adjoint()),
71*bf2c3715SXin Li rhs13 = (s1*m1) * (s2*rhs2.adjoint()));
72*bf2c3715SXin Li
73*bf2c3715SXin Li m2 = m1.template triangularView<Upper>();
74*bf2c3715SXin Li VERIFY_IS_APPROX(rhs12 = (s1*m2.adjoint()).template selfadjointView<Lower>() * (s2*rhs2.adjoint()),
75*bf2c3715SXin Li rhs13 = (s1*m1.adjoint()) * (s2*rhs2.adjoint()));
76*bf2c3715SXin Li
77*bf2c3715SXin Li // test row major = <...>
78*bf2c3715SXin Li m2 = m1.template triangularView<Lower>(); rhs32.setRandom(); rhs13 = rhs32;
79*bf2c3715SXin Li VERIFY_IS_APPROX(rhs32.noalias() -= (s1*m2).template selfadjointView<Lower>() * (s2*rhs3),
80*bf2c3715SXin Li rhs13 -= (s1*m1) * (s2 * rhs3));
81*bf2c3715SXin Li
82*bf2c3715SXin Li m2 = m1.template triangularView<Upper>();
83*bf2c3715SXin Li VERIFY_IS_APPROX(rhs32.noalias() = (s1*m2.adjoint()).template selfadjointView<Lower>() * (s2*rhs3).conjugate(),
84*bf2c3715SXin Li rhs13 = (s1*m1.adjoint()) * (s2*rhs3).conjugate());
85*bf2c3715SXin Li
86*bf2c3715SXin Li
87*bf2c3715SXin Li m2 = m1.template triangularView<Upper>(); rhs13 = rhs12;
88*bf2c3715SXin Li VERIFY_IS_APPROX(rhs12.noalias() += s1 * ((m2.adjoint()).template selfadjointView<Lower>() * (s2*rhs3).conjugate()),
89*bf2c3715SXin Li rhs13 += (s1*m1.adjoint()) * (s2*rhs3).conjugate());
90*bf2c3715SXin Li
91*bf2c3715SXin Li m2 = m1.template triangularView<Lower>();
92*bf2c3715SXin Li VERIFY_IS_APPROX(rhs22 = (rhs2) * (m2).template selfadjointView<Lower>(), rhs23 = (rhs2) * (m1));
93*bf2c3715SXin Li VERIFY_IS_APPROX(rhs22 = (s2*rhs2) * (s1*m2).template selfadjointView<Lower>(), rhs23 = (s2*rhs2) * (s1*m1));
94*bf2c3715SXin Li
95*bf2c3715SXin Li // destination with a non-default inner-stride
96*bf2c3715SXin Li // see bug 1741
97*bf2c3715SXin Li {
98*bf2c3715SXin Li typedef Matrix<Scalar,Dynamic,Dynamic> MatrixX;
99*bf2c3715SXin Li MatrixX buffer(2*cols,2*othersize);
100*bf2c3715SXin Li Map<Rhs1,0,Stride<Dynamic,2> > map1(buffer.data(),cols,othersize,Stride<Dynamic,2>(2*rows,2));
101*bf2c3715SXin Li buffer.setZero();
102*bf2c3715SXin Li VERIFY_IS_APPROX( map1.noalias() = (s1*m2).template selfadjointView<Lower>() * (s2*rhs1),
103*bf2c3715SXin Li rhs13 = (s1*m1) * (s2*rhs1));
104*bf2c3715SXin Li
105*bf2c3715SXin Li Map<Rhs2,0,Stride<Dynamic,2> > map2(buffer.data(),rhs22.rows(),rhs22.cols(),Stride<Dynamic,2>(2*rhs22.outerStride(),2));
106*bf2c3715SXin Li buffer.setZero();
107*bf2c3715SXin Li VERIFY_IS_APPROX(map2 = (rhs2) * (m2).template selfadjointView<Lower>(), rhs23 = (rhs2) * (m1));
108*bf2c3715SXin Li }
109*bf2c3715SXin Li }
110*bf2c3715SXin Li
EIGEN_DECLARE_TEST(product_symm)111*bf2c3715SXin Li EIGEN_DECLARE_TEST(product_symm)
112*bf2c3715SXin Li {
113*bf2c3715SXin Li for(int i = 0; i < g_repeat ; i++)
114*bf2c3715SXin Li {
115*bf2c3715SXin Li CALL_SUBTEST_1(( symm<float,Dynamic,Dynamic>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE)) ));
116*bf2c3715SXin Li CALL_SUBTEST_2(( symm<double,Dynamic,Dynamic>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE)) ));
117*bf2c3715SXin Li CALL_SUBTEST_3(( symm<std::complex<float>,Dynamic,Dynamic>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2),internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2)) ));
118*bf2c3715SXin Li CALL_SUBTEST_4(( symm<std::complex<double>,Dynamic,Dynamic>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2),internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2)) ));
119*bf2c3715SXin Li
120*bf2c3715SXin Li CALL_SUBTEST_5(( symm<float,Dynamic,1>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE)) ));
121*bf2c3715SXin Li CALL_SUBTEST_6(( symm<double,Dynamic,1>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE)) ));
122*bf2c3715SXin Li CALL_SUBTEST_7(( symm<std::complex<float>,Dynamic,1>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE)) ));
123*bf2c3715SXin Li CALL_SUBTEST_8(( symm<std::complex<double>,Dynamic,1>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE)) ));
124*bf2c3715SXin Li }
125*bf2c3715SXin Li }
126