1c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This file is part of Eigen, a lightweight C++ template library
2c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// for linear algebra.
3c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//
4c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
5c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//
6c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This Source Code Form is subject to the terms of the Mozilla
7c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Public License v. 2.0. If a copy of the MPL was not distributed
8c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
10c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include "main.h"
11c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
12c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename MatrixType> void product_selfadjoint(const MatrixType& m)
13c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
14c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename MatrixType::Index Index;
15c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename MatrixType::Scalar Scalar;
16c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
17c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef Matrix<Scalar, 1, MatrixType::RowsAtCompileTime> RowVectorType;
18c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
19c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, Dynamic, RowMajor> RhsMatrixType;
20c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
21c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  Index rows = m.rows();
22c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  Index cols = m.cols();
23c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
24c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  MatrixType m1 = MatrixType::Random(rows, cols),
25c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath             m2 = MatrixType::Random(rows, cols),
26c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath             m3;
27c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  VectorType v1 = VectorType::Random(rows),
28c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath             v2 = VectorType::Random(rows),
29c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath             v3(rows);
30c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  RowVectorType r1 = RowVectorType::Random(rows),
31c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath                r2 = RowVectorType::Random(rows);
32c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  RhsMatrixType m4 = RhsMatrixType::Random(rows,10);
33c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
34c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  Scalar s1 = internal::random<Scalar>(),
35c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath         s2 = internal::random<Scalar>(),
36c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath         s3 = internal::random<Scalar>();
37c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
38c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  m1 = (m1.adjoint() + m1).eval();
39c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
40c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  // rank2 update
41c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  m2 = m1.template triangularView<Lower>();
42c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  m2.template selfadjointView<Lower>().rankUpdate(v1,v2);
43c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  VERIFY_IS_APPROX(m2, (m1 + v1 * v2.adjoint()+ v2 * v1.adjoint()).template triangularView<Lower>().toDenseMatrix());
44c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
45c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  m2 = m1.template triangularView<Upper>();
46c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  m2.template selfadjointView<Upper>().rankUpdate(-v1,s2*v2,s3);
477faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_IS_APPROX(m2, (m1 + (s3*(-v1)*(s2*v2).adjoint()+numext::conj(s3)*(s2*v2)*(-v1).adjoint())).template triangularView<Upper>().toDenseMatrix());
48c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
49c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  m2 = m1.template triangularView<Upper>();
50c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  m2.template selfadjointView<Upper>().rankUpdate(-s2*r1.adjoint(),r2.adjoint()*s3,s1);
517faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_IS_APPROX(m2, (m1 + s1*(-s2*r1.adjoint())*(r2.adjoint()*s3).adjoint() + numext::conj(s1)*(r2.adjoint()*s3) * (-s2*r1.adjoint()).adjoint()).template triangularView<Upper>().toDenseMatrix());
52c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
53c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  if (rows>1)
54c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
55c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    m2 = m1.template triangularView<Lower>();
56c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    m2.block(1,1,rows-1,cols-1).template selfadjointView<Lower>().rankUpdate(v1.tail(rows-1),v2.head(cols-1));
57c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    m3 = m1;
58c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    m3.block(1,1,rows-1,cols-1) += v1.tail(rows-1) * v2.head(cols-1).adjoint()+ v2.head(cols-1) * v1.tail(rows-1).adjoint();
59c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    VERIFY_IS_APPROX(m2, m3.template triangularView<Lower>().toDenseMatrix());
60c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
61c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
62c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
63c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathvoid test_product_selfadjoint()
64c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
657faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  int s = 0;
66c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  for(int i = 0; i < g_repeat ; i++) {
67c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    CALL_SUBTEST_1( product_selfadjoint(Matrix<float, 1, 1>()) );
68c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    CALL_SUBTEST_2( product_selfadjoint(Matrix<float, 2, 2>()) );
69c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    CALL_SUBTEST_3( product_selfadjoint(Matrix3d()) );
70c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2);
71c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    CALL_SUBTEST_4( product_selfadjoint(MatrixXcf(s, s)) );
72c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2);
73c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    CALL_SUBTEST_5( product_selfadjoint(MatrixXcd(s,s)) );
74c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE);
75c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    CALL_SUBTEST_6( product_selfadjoint(MatrixXd(s,s)) );
76c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE);
77c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    CALL_SUBTEST_7( product_selfadjoint(Matrix<float,Dynamic,Dynamic,RowMajor>(s,s)) );
78c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
797faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  TEST_SET_BUT_UNUSED_VARIABLE(s)
80c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
81