aboutsummaryrefslogtreecommitdiffhomepage
path: root/Eigen/src/Householder/HouseholderSequence.h
blob: 1159f03ed3e7d53cff98ba4103e4ab1c4ee6b7d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2009 Gael Guennebaud <g.gael@free.fr>
//
// Eigen is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// Alternatively, you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License and a copy of the GNU General Public License along with
// Eigen. If not, see <http://www.gnu.org/licenses/>.

#ifndef EIGEN_HOUSEHOLDER_SEQUENCE_H
#define EIGEN_HOUSEHOLDER_SEQUENCE_H

/** \ingroup Householder_Module
  * \householder_module
  * \class HouseholderSequence
  * \brief Represents a sequence of householder reflections with decreasing size
  *
  * This class represents a product sequence of householder reflections \f$ H = \Pi_0^{n-1} H_i \f$
  * where \f$ H_i \f$ is the i-th householder transformation \f$ I - h_i v_i v_i^* \f$,
  * \f$ v_i \f$ is the i-th householder vector \f$ [ 1, m_vectors(i+1,i), m_vectors(i+2,i), ...] \f$
  * and \f$ h_i \f$ is the i-th householder coefficient \c m_coeffs[i].
  *
  * Typical usages are listed below, where H is a HouseholderSequence:
  * \code
  * A.applyOnTheRight(H);             // A = A * H
  * A.applyOnTheLeft(H);              // A = H * A
  * A.applyOnTheRight(H.adjoint());   // A = A * H^*
  * A.applyOnTheLeft(H.adjoint());    // A = H^* * A
  * MatrixXd Q = H;                   // conversion to a dense matrix
  * \endcode
  * In addition to the adjoint, you can also apply the inverse (=adjoint), the transpose, and the conjugate.
  *
  * \sa MatrixBase::applyOnTheLeft(), MatrixBase::applyOnTheRight()
  */

template<typename VectorsType, typename CoeffsType>
struct ei_traits<HouseholderSequence<VectorsType,CoeffsType> >
{
  typedef typename VectorsType::Scalar Scalar;
  enum {
    RowsAtCompileTime = ei_traits<VectorsType>::RowsAtCompileTime,
    ColsAtCompileTime = ei_traits<VectorsType>::RowsAtCompileTime,
    MaxRowsAtCompileTime = ei_traits<VectorsType>::MaxRowsAtCompileTime,
    MaxColsAtCompileTime = ei_traits<VectorsType>::MaxRowsAtCompileTime,
    Flags = 0
  };
};

template<typename VectorsType, typename CoeffsType> class HouseholderSequence
  : public AnyMatrixBase<HouseholderSequence<VectorsType,CoeffsType> >
{
    typedef typename VectorsType::Scalar Scalar;
  public:

    typedef HouseholderSequence<VectorsType,
      typename ei_meta_if<NumTraits<Scalar>::IsComplex,
        NestByValue<typename ei_cleantype<typename CoeffsType::ConjugateReturnType>::type >,
        CoeffsType>::ret> ConjugateReturnType;

    HouseholderSequence(const VectorsType& v, const CoeffsType& h, bool trans = false)
      : m_vectors(v), m_coeffs(h), m_trans(trans), m_actualVectors(v.diagonalSize())
    {}

    HouseholderSequence(const VectorsType& v, const CoeffsType& h, bool trans, int actualVectors)
      : m_vectors(v), m_coeffs(h), m_trans(trans), m_actualVectors(actualVectors)
    {}

    int rows() const { return m_vectors.rows(); }
    int cols() const { return m_vectors.rows(); }

    HouseholderSequence transpose() const
    { return HouseholderSequence(m_vectors, m_coeffs, !m_trans, m_actualVectors); }

    ConjugateReturnType conjugate() const
    { return ConjugateReturnType(m_vectors, m_coeffs.conjugate(), m_trans, m_actualVectors); }

    ConjugateReturnType adjoint() const
    { return ConjugateReturnType(m_vectors, m_coeffs.conjugate(), !m_trans, m_actualVectors); }

    ConjugateReturnType inverse() const { return adjoint(); }

    /** \internal */
    template<typename DestType> void evalTo(DestType& dst) const
    {
      int vecs = m_actualVectors;
      int length = m_vectors.rows();
      dst.setIdentity();
      Matrix<Scalar,1,DestType::RowsAtCompileTime> temp(dst.rows());
      for(int k = vecs-1; k >= 0; --k)
      {
        if(m_trans)
          dst.corner(BottomRight, length-k, length-k)
          .applyHouseholderOnTheRight(m_vectors.col(k).end(length-k-1), m_coeffs.coeff(k), &temp.coeffRef(0));
        else
          dst.corner(BottomRight, length-k, length-k)
            .applyHouseholderOnTheLeft(m_vectors.col(k).end(length-k-1), m_coeffs.coeff(k), &temp.coeffRef(k));
      }
    }

    /** \internal */
    template<typename Dest> inline void applyThisOnTheRight(Dest& dst) const
    {
      int vecs = m_actualVectors; // number of householder vectors
      int length = m_vectors.rows(); // size of the largest householder vector
      Matrix<Scalar,1,Dest::RowsAtCompileTime> temp(dst.rows());
      for(int k = 0; k < vecs; ++k)
      {
        int actual_k = m_trans ? vecs-k-1 : k;
        dst.corner(BottomRight, dst.rows(), length-actual_k)
           .applyHouseholderOnTheRight(m_vectors.col(actual_k).end(length-actual_k-1), m_coeffs.coeff(actual_k), &temp.coeffRef(0));
      }
    }

    /** \internal */
    template<typename Dest> inline void applyThisOnTheLeft(Dest& dst) const
    {
      int vecs = m_actualVectors; // number of householder vectors
      int length = m_vectors.rows(); // size of the largest householder vector
      Matrix<Scalar,1,Dest::ColsAtCompileTime> temp(dst.cols());
      for(int k = 0; k < vecs; ++k)
      {
        int actual_k = m_trans ? k : vecs-k-1;
        dst.corner(BottomRight, length-actual_k, dst.cols())
           .applyHouseholderOnTheLeft(m_vectors.col(actual_k).end(length-actual_k-1), m_coeffs.coeff(actual_k), &temp.coeffRef(0));
      }
    }

    template<typename OtherDerived>
    typename OtherDerived::PlainMatrixType operator*(const MatrixBase<OtherDerived>& other) const
    {
      typename OtherDerived::PlainMatrixType res(other);
      applyThisOnTheLeft(res);
      return res;
    }

    template<typename OtherDerived> friend
    typename OtherDerived::PlainMatrixType operator*(const MatrixBase<OtherDerived>& other, const HouseholderSequence& h)
    {
      typename OtherDerived::PlainMatrixType res(other);
      h.applyThisOnTheRight(res);
      return res;
    }

  protected:
    typename VectorsType::Nested m_vectors;
    typename CoeffsType::Nested m_coeffs;
    bool m_trans;
    int m_actualVectors;

private:
  HouseholderSequence& operator=(const HouseholderSequence&);
};

template<typename VectorsType, typename CoeffsType>
HouseholderSequence<VectorsType,CoeffsType> householderSequence(const VectorsType& v, const CoeffsType& h, bool trans=false)
{
  return HouseholderSequence<VectorsType,CoeffsType>(v, h, trans);
}

template<typename VectorsType, typename CoeffsType>
HouseholderSequence<VectorsType,CoeffsType> householderSequence(const VectorsType& v, const CoeffsType& h, bool trans, int actualVectors)
{
  return HouseholderSequence<VectorsType,CoeffsType>(v, h, trans, actualVectors);
}

#endif // EIGEN_HOUSEHOLDER_SEQUENCE_H