Eigen  3.2.10
 
Loading...
Searching...
No Matches
HouseholderSequence.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
5// Copyright (C) 2010 Benoit Jacob <jacob.benoit.1@gmail.com>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10
11#ifndef EIGEN_HOUSEHOLDER_SEQUENCE_H
12#define EIGEN_HOUSEHOLDER_SEQUENCE_H
13
14namespace Eigen {
15
56
57namespace internal {
58
59template<typename VectorsType, typename CoeffsType, int Side>
60struct traits<HouseholderSequence<VectorsType,CoeffsType,Side> >
61{
62 typedef typename VectorsType::Scalar Scalar;
63 typedef typename VectorsType::Index Index;
64 typedef typename VectorsType::StorageKind StorageKind;
65 enum {
66 RowsAtCompileTime = Side==OnTheLeft ? traits<VectorsType>::RowsAtCompileTime
67 : traits<VectorsType>::ColsAtCompileTime,
68 ColsAtCompileTime = RowsAtCompileTime,
69 MaxRowsAtCompileTime = Side==OnTheLeft ? traits<VectorsType>::MaxRowsAtCompileTime
70 : traits<VectorsType>::MaxColsAtCompileTime,
71 MaxColsAtCompileTime = MaxRowsAtCompileTime,
72 Flags = 0
73 };
74};
75
76template<typename VectorsType, typename CoeffsType, int Side>
77struct hseq_side_dependent_impl
78{
79 typedef Block<const VectorsType, Dynamic, 1> EssentialVectorType;
80 typedef HouseholderSequence<VectorsType, CoeffsType, OnTheLeft> HouseholderSequenceType;
81 typedef typename VectorsType::Index Index;
82 static inline const EssentialVectorType essentialVector(const HouseholderSequenceType& h, Index k)
83 {
84 Index start = k+1+h.m_shift;
85 return Block<const VectorsType,Dynamic,1>(h.m_vectors, start, k, h.rows()-start, 1);
86 }
87};
88
89template<typename VectorsType, typename CoeffsType>
90struct hseq_side_dependent_impl<VectorsType, CoeffsType, OnTheRight>
91{
92 typedef Transpose<Block<const VectorsType, 1, Dynamic> > EssentialVectorType;
93 typedef HouseholderSequence<VectorsType, CoeffsType, OnTheRight> HouseholderSequenceType;
94 typedef typename VectorsType::Index Index;
95 static inline const EssentialVectorType essentialVector(const HouseholderSequenceType& h, Index k)
96 {
97 Index start = k+1+h.m_shift;
98 return Block<const VectorsType,1,Dynamic>(h.m_vectors, k, start, 1, h.rows()-start).transpose();
99 }
100};
101
102template<typename OtherScalarType, typename MatrixType> struct matrix_type_times_scalar_type
103{
104 typedef typename scalar_product_traits<OtherScalarType, typename MatrixType::Scalar>::ReturnType
105 ResultScalar;
106 typedef Matrix<ResultScalar, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime,
107 0, MatrixType::MaxRowsAtCompileTime, MatrixType::MaxColsAtCompileTime> Type;
108};
109
110} // end namespace internal
111
112template<typename VectorsType, typename CoeffsType, int Side> class HouseholderSequence
113 : public EigenBase<HouseholderSequence<VectorsType,CoeffsType,Side> >
114{
115 typedef typename internal::hseq_side_dependent_impl<VectorsType,CoeffsType,Side>::EssentialVectorType EssentialVectorType;
116
117 public:
118 enum {
119 RowsAtCompileTime = internal::traits<HouseholderSequence>::RowsAtCompileTime,
120 ColsAtCompileTime = internal::traits<HouseholderSequence>::ColsAtCompileTime,
121 MaxRowsAtCompileTime = internal::traits<HouseholderSequence>::MaxRowsAtCompileTime,
122 MaxColsAtCompileTime = internal::traits<HouseholderSequence>::MaxColsAtCompileTime
123 };
124 typedef typename internal::traits<HouseholderSequence>::Scalar Scalar;
125 typedef typename VectorsType::Index Index;
126
127 typedef HouseholderSequence<
128 typename internal::conditional<NumTraits<Scalar>::IsComplex,
129 typename internal::remove_all<typename VectorsType::ConjugateReturnType>::type,
130 VectorsType>::type,
131 typename internal::conditional<NumTraits<Scalar>::IsComplex,
132 typename internal::remove_all<typename CoeffsType::ConjugateReturnType>::type,
133 CoeffsType>::type,
134 Side
135 > ConjugateReturnType;
136
154 HouseholderSequence(const VectorsType& v, const CoeffsType& h)
155 : m_vectors(v), m_coeffs(h), m_trans(false), m_length(v.diagonalSize()),
156 m_shift(0)
157 {
158 }
159
161 HouseholderSequence(const HouseholderSequence& other)
162 : m_vectors(other.m_vectors),
163 m_coeffs(other.m_coeffs),
164 m_trans(other.m_trans),
165 m_length(other.m_length),
166 m_shift(other.m_shift)
167 {
168 }
169
174 Index rows() const { return Side==OnTheLeft ? m_vectors.rows() : m_vectors.cols(); }
175
180 Index cols() const { return rows(); }
181
196 const EssentialVectorType essentialVector(Index k) const
197 {
198 eigen_assert(k >= 0 && k < m_length);
199 return internal::hseq_side_dependent_impl<VectorsType,CoeffsType,Side>::essentialVector(*this, k);
200 }
201
203 HouseholderSequence transpose() const
204 {
205 return HouseholderSequence(*this).setTrans(!m_trans);
206 }
207
209 ConjugateReturnType conjugate() const
210 {
211 return ConjugateReturnType(m_vectors.conjugate(), m_coeffs.conjugate())
212 .setTrans(m_trans)
213 .setLength(m_length)
214 .setShift(m_shift);
215 }
216
218 ConjugateReturnType adjoint() const
219 {
220 return conjugate().setTrans(!m_trans);
221 }
222
224 ConjugateReturnType inverse() const { return adjoint(); }
225
227 template<typename DestType> inline void evalTo(DestType& dst) const
228 {
229 Matrix<Scalar, DestType::RowsAtCompileTime, 1,
230 AutoAlign|ColMajor, DestType::MaxRowsAtCompileTime, 1> workspace(rows());
231 evalTo(dst, workspace);
232 }
233
235 template<typename Dest, typename Workspace>
236 void evalTo(Dest& dst, Workspace& workspace) const
237 {
238 workspace.resize(rows());
239 Index vecs = m_length;
240 const typename Dest::Scalar *dst_data = internal::extract_data(dst);
241 if( internal::is_same<typename internal::remove_all<VectorsType>::type,Dest>::value
242 && dst_data!=0 && dst_data == internal::extract_data(m_vectors))
243 {
244 // in-place
245 dst.diagonal().setOnes();
246 dst.template triangularView<StrictlyUpper>().setZero();
247 for(Index k = vecs-1; k >= 0; --k)
248 {
249 Index cornerSize = rows() - k - m_shift;
250 if(m_trans)
251 dst.bottomRightCorner(cornerSize, cornerSize)
252 .applyHouseholderOnTheRight(essentialVector(k), m_coeffs.coeff(k), workspace.data());
253 else
254 dst.bottomRightCorner(cornerSize, cornerSize)
255 .applyHouseholderOnTheLeft(essentialVector(k), m_coeffs.coeff(k), workspace.data());
256
257 // clear the off diagonal vector
258 dst.col(k).tail(rows()-k-1).setZero();
259 }
260 // clear the remaining columns if needed
261 for(Index k = 0; k<cols()-vecs ; ++k)
262 dst.col(k).tail(rows()-k-1).setZero();
263 }
264 else
265 {
266 dst.setIdentity(rows(), rows());
267 for(Index k = vecs-1; k >= 0; --k)
268 {
269 Index cornerSize = rows() - k - m_shift;
270 if(m_trans)
271 dst.bottomRightCorner(cornerSize, cornerSize)
272 .applyHouseholderOnTheRight(essentialVector(k), m_coeffs.coeff(k), &workspace.coeffRef(0));
273 else
274 dst.bottomRightCorner(cornerSize, cornerSize)
275 .applyHouseholderOnTheLeft(essentialVector(k), m_coeffs.coeff(k), &workspace.coeffRef(0));
276 }
277 }
278 }
279
281 template<typename Dest> inline void applyThisOnTheRight(Dest& dst) const
282 {
283 Matrix<Scalar,1,Dest::RowsAtCompileTime,RowMajor,1,Dest::MaxRowsAtCompileTime> workspace(dst.rows());
284 applyThisOnTheRight(dst, workspace);
285 }
286
288 template<typename Dest, typename Workspace>
289 inline void applyThisOnTheRight(Dest& dst, Workspace& workspace) const
290 {
291 workspace.resize(dst.rows());
292 for(Index k = 0; k < m_length; ++k)
293 {
294 Index actual_k = m_trans ? m_length-k-1 : k;
295 dst.rightCols(rows()-m_shift-actual_k)
296 .applyHouseholderOnTheRight(essentialVector(actual_k), m_coeffs.coeff(actual_k), workspace.data());
297 }
298 }
299
301 template<typename Dest> inline void applyThisOnTheLeft(Dest& dst) const
302 {
303 Matrix<Scalar,1,Dest::ColsAtCompileTime,RowMajor,1,Dest::MaxColsAtCompileTime> workspace(dst.cols());
304 applyThisOnTheLeft(dst, workspace);
305 }
306
308 template<typename Dest, typename Workspace>
309 inline void applyThisOnTheLeft(Dest& dst, Workspace& workspace) const
310 {
311 workspace.resize(dst.cols());
312 for(Index k = 0; k < m_length; ++k)
313 {
314 Index actual_k = m_trans ? k : m_length-k-1;
315 dst.bottomRows(rows()-m_shift-actual_k)
316 .applyHouseholderOnTheLeft(essentialVector(actual_k), m_coeffs.coeff(actual_k), workspace.data());
317 }
318 }
319
327 template<typename OtherDerived>
328 typename internal::matrix_type_times_scalar_type<Scalar, OtherDerived>::Type operator*(const MatrixBase<OtherDerived>& other) const
329 {
330 typename internal::matrix_type_times_scalar_type<Scalar, OtherDerived>::Type
331 res(other.template cast<typename internal::matrix_type_times_scalar_type<Scalar,OtherDerived>::ResultScalar>());
332 applyThisOnTheLeft(res);
333 return res;
334 }
335
336 template<typename _VectorsType, typename _CoeffsType, int _Side> friend struct internal::hseq_side_dependent_impl;
337
347 HouseholderSequence& setLength(Index length)
348 {
349 m_length = length;
350 return *this;
351 }
352
364 HouseholderSequence& setShift(Index shift)
365 {
366 m_shift = shift;
367 return *this;
368 }
369
370 Index length() const { return m_length; }
371 Index shift() const { return m_shift; }
372
373 /* Necessary for .adjoint() and .conjugate() */
374 template <typename VectorsType2, typename CoeffsType2, int Side2> friend class HouseholderSequence;
375
376 protected:
377
386 HouseholderSequence& setTrans(bool trans)
387 {
388 m_trans = trans;
389 return *this;
390 }
391
392 bool trans() const { return m_trans; }
393
394 typename VectorsType::Nested m_vectors;
395 typename CoeffsType::Nested m_coeffs;
396 bool m_trans;
397 Index m_length;
398 Index m_shift;
399};
400
409template<typename OtherDerived, typename VectorsType, typename CoeffsType, int Side>
410typename internal::matrix_type_times_scalar_type<typename VectorsType::Scalar,OtherDerived>::Type operator*(const MatrixBase<OtherDerived>& other, const HouseholderSequence<VectorsType,CoeffsType,Side>& h)
411{
412 typename internal::matrix_type_times_scalar_type<typename VectorsType::Scalar,OtherDerived>::Type
413 res(other.template cast<typename internal::matrix_type_times_scalar_type<typename VectorsType::Scalar,OtherDerived>::ResultScalar>());
414 h.applyThisOnTheRight(res);
415 return res;
416}
417
422template<typename VectorsType, typename CoeffsType>
427
434template<typename VectorsType, typename CoeffsType>
439
440} // end namespace Eigen
441
442#endif // EIGEN_HOUSEHOLDER_SEQUENCE_H
Sequence of Householder reflections acting on subspaces with decreasing size.
Definition HouseholderSequence.h:114
Index cols() const
Number of columns of transformation viewed as a matrix.
Definition HouseholderSequence.h:180
const EssentialVectorType essentialVector(Index k) const
Essential part of a Householder vector.
Definition HouseholderSequence.h:196
HouseholderSequence(const HouseholderSequence &other)
Copy constructor.
Definition HouseholderSequence.h:161
HouseholderSequence transpose() const
Transpose of the Householder sequence.
Definition HouseholderSequence.h:203
HouseholderSequence & setShift(Index shift)
Sets the shift of the Householder sequence.
Definition HouseholderSequence.h:364
HouseholderSequence(const VectorsType &v, const CoeffsType &h)
Constructor.
Definition HouseholderSequence.h:154
HouseholderSequence & setLength(Index length)
Sets the length of the Householder sequence.
Definition HouseholderSequence.h:347
Index length() const
Definition HouseholderSequence.h:370
ConjugateReturnType inverse() const
Inverse of the Householder sequence (equals the adjoint).
Definition HouseholderSequence.h:224
ConjugateReturnType adjoint() const
Adjoint (conjugate transpose) of the Householder sequence.
Definition HouseholderSequence.h:218
internal::matrix_type_times_scalar_type< Scalar, OtherDerived >::Type operator*(const MatrixBase< OtherDerived > &other) const
Computes the product of a Householder sequence with a matrix.
Definition HouseholderSequence.h:328
ConjugateReturnType conjugate() const
Complex conjugate of the Householder sequence.
Definition HouseholderSequence.h:209
Index rows() const
Number of rows of transformation viewed as a matrix.
Definition HouseholderSequence.h:174
HouseholderSequence & setTrans(bool trans)
Sets the transpose flag.
Definition HouseholderSequence.h:386
bool trans() const
Definition HouseholderSequence.h:392
Index shift() const
Definition HouseholderSequence.h:371
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:50
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:129
HouseholderSequence< VectorsType, CoeffsType, OnTheRight > rightHouseholderSequence(const VectorsType &v, const CoeffsType &h)
Convenience function for constructing a Householder sequence.
Definition HouseholderSequence.h:435
HouseholderSequence< VectorsType, CoeffsType > householderSequence(const VectorsType &v, const CoeffsType &h)
Convenience function for constructing a Householder sequence.
Definition HouseholderSequence.h:423
@ OnTheLeft
Definition Constants.h:277
@ OnTheRight
Definition Constants.h:279
@ AutoAlign
Definition Constants.h:268
@ ColMajor
Definition Constants.h:264
Definition LDLT.h:18
Definition EigenBase.h:27