11#ifndef EIGEN_HOUSEHOLDER_SEQUENCE_H
12#define EIGEN_HOUSEHOLDER_SEQUENCE_H
15#include "./InternalHeaderCheck.h"
62template <
typename VectorsType,
typename CoeffsType,
int S
ide>
63struct traits<HouseholderSequence<VectorsType, CoeffsType, Side> > {
64 typedef typename VectorsType::Scalar Scalar;
65 typedef typename VectorsType::StorageIndex StorageIndex;
66 typedef typename VectorsType::StorageKind StorageKind;
69 Side ==
OnTheLeft ? traits<VectorsType>::RowsAtCompileTime : traits<VectorsType>::ColsAtCompileTime,
70 ColsAtCompileTime = RowsAtCompileTime,
71 MaxRowsAtCompileTime =
72 Side ==
OnTheLeft ? traits<VectorsType>::MaxRowsAtCompileTime : traits<VectorsType>::MaxColsAtCompileTime,
73 MaxColsAtCompileTime = MaxRowsAtCompileTime,
78struct HouseholderSequenceShape {};
80template <
typename VectorsType,
typename CoeffsType,
int S
ide>
81struct evaluator_traits<HouseholderSequence<VectorsType, CoeffsType, Side> >
82 :
public evaluator_traits_base<HouseholderSequence<VectorsType, CoeffsType, Side> > {
83 typedef HouseholderSequenceShape Shape;
86template <
typename VectorsType,
typename CoeffsType,
int S
ide>
87struct hseq_side_dependent_impl {
88 typedef Block<const VectorsType, Dynamic, 1> EssentialVectorType;
89 typedef HouseholderSequence<VectorsType, CoeffsType, OnTheLeft> HouseholderSequenceType;
90 static EIGEN_DEVICE_FUNC
inline const EssentialVectorType essentialVector(
const HouseholderSequenceType& h, Index k) {
91 Index start = k + 1 + h.m_shift;
92 return Block<const VectorsType, Dynamic, 1>(h.m_vectors, start, k, h.rows() - start, 1);
96template <
typename VectorsType,
typename CoeffsType>
97struct hseq_side_dependent_impl<VectorsType, CoeffsType,
OnTheRight> {
98 typedef Transpose<Block<const VectorsType, 1, Dynamic> > EssentialVectorType;
99 typedef HouseholderSequence<VectorsType, CoeffsType, OnTheRight> HouseholderSequenceType;
100 static inline const EssentialVectorType essentialVector(
const HouseholderSequenceType& h, Index k) {
101 Index start = k + 1 + h.m_shift;
102 return Block<const VectorsType, 1, Dynamic>(h.m_vectors, k, start, 1, h.rows() - start).transpose();
106template <
typename OtherScalarType,
typename MatrixType>
107struct matrix_type_times_scalar_type {
108 typedef typename ScalarBinaryOpTraits<OtherScalarType, typename MatrixType::Scalar>::ReturnType ResultScalar;
109 typedef Matrix<ResultScalar, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime, 0,
110 MatrixType::MaxRowsAtCompileTime, MatrixType::MaxColsAtCompileTime>
116template <
typename VectorsType,
typename CoeffsType,
int S
ide>
123 RowsAtCompileTime = internal::traits<HouseholderSequence>::RowsAtCompileTime,
124 ColsAtCompileTime = internal::traits<HouseholderSequence>::ColsAtCompileTime,
125 MaxRowsAtCompileTime = internal::traits<HouseholderSequence>::MaxRowsAtCompileTime,
126 MaxColsAtCompileTime = internal::traits<HouseholderSequence>::MaxColsAtCompileTime
128 typedef typename internal::traits<HouseholderSequence>::Scalar Scalar;
131 std::conditional_t<NumTraits<Scalar>::IsComplex,
132 internal::remove_all_t<typename VectorsType::ConjugateReturnType>, VectorsType>,
133 std::conditional_t<NumTraits<Scalar>::IsComplex, internal::remove_all_t<typename CoeffsType::ConjugateReturnType>,
140 std::conditional_t<NumTraits<Scalar>::IsComplex, internal::remove_all_t<typename CoeffsType::ConjugateReturnType>,
146 std::conditional_t<NumTraits<Scalar>::IsComplex,
147 internal::remove_all_t<typename VectorsType::ConjugateReturnType>, VectorsType>,
172 : m_vectors(v), m_coeffs(h), m_reverse(false), m_length(v.diagonalSize()), m_shift(0) {}
176 : m_vectors(other.m_vectors),
177 m_coeffs(other.m_coeffs),
178 m_reverse(other.m_reverse),
179 m_length(other.m_length),
180 m_shift(other.m_shift) {}
186 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
Index rows() const EIGEN_NOEXCEPT {
187 return Side ==
OnTheLeft ? m_vectors.rows() : m_vectors.cols();
194 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
Index cols() const EIGEN_NOEXCEPT {
return rows(); }
211 eigen_assert(k >= 0 && k < m_length);
212 return internal::hseq_side_dependent_impl<VectorsType, CoeffsType, Side>::essentialVector(*
this, k);
218 .setReverseFlag(!m_reverse)
226 .setReverseFlag(m_reverse)
235 EIGEN_DEVICE_FUNC
inline std::conditional_t<Cond, ConjugateReturnType, ConstHouseholderSequence>
conjugateIf()
const {
236 typedef std::conditional_t<Cond, ConjugateReturnType, ConstHouseholderSequence> ReturnType;
243 .setReverseFlag(!m_reverse)
252 template <
typename DestType>
253 inline EIGEN_DEVICE_FUNC
void evalTo(DestType& dst)
const {
256 evalTo(dst, workspace);
260 template <
typename Dest,
typename Workspace>
261 EIGEN_DEVICE_FUNC
void evalTo(Dest& dst, Workspace& workspace)
const {
262 workspace.resize(
rows());
263 Index vecs = m_length;
264 if (internal::is_same_dense(dst, m_vectors)) {
266 dst.diagonal().setOnes();
267 dst.template triangularView<StrictlyUpper>().setZero();
268 for (Index k = vecs - 1; k >= 0; --k) {
269 Index cornerSize =
rows() - k - m_shift;
271 dst.bottomRightCorner(cornerSize, cornerSize)
272 .applyHouseholderOnTheRight(
essentialVector(k), m_coeffs.coeff(k), workspace.data());
274 dst.bottomRightCorner(cornerSize, cornerSize)
275 .applyHouseholderOnTheLeft(
essentialVector(k), m_coeffs.coeff(k), workspace.data());
278 dst.col(k).tail(
rows() - k - 1).setZero();
281 for (Index k = 0; k <
cols() - vecs; ++k) dst.col(k).tail(
rows() - k - 1).setZero();
282 }
else if (m_length > BlockSize) {
285 applyThisOnTheLeft(dst, workspace,
true);
287 applyThisOnTheLeft(dst, workspace,
true);
290 for (Index k = vecs - 1; k >= 0; --k) {
291 Index cornerSize =
rows() - k - m_shift;
293 dst.bottomRightCorner(cornerSize, cornerSize)
294 .applyHouseholderOnTheRight(
essentialVector(k), m_coeffs.coeff(k), workspace.data());
296 dst.bottomRightCorner(cornerSize, cornerSize)
297 .applyHouseholderOnTheLeft(
essentialVector(k), m_coeffs.coeff(k), workspace.data());
303 template <
typename Dest>
304 inline void applyThisOnTheRight(Dest& dst)
const {
305 Matrix<Scalar, 1, Dest::RowsAtCompileTime, RowMajor, 1, Dest::MaxRowsAtCompileTime> workspace(dst.rows());
306 applyThisOnTheRight(dst, workspace);
310 template <
typename Dest,
typename Workspace>
311 inline void applyThisOnTheRight(Dest& dst, Workspace& workspace)
const {
312 workspace.resize(dst.rows());
313 for (Index k = 0; k < m_length; ++k) {
314 Index actual_k = m_reverse ? m_length - k - 1 : k;
315 dst.rightCols(
rows() - m_shift - actual_k)
316 .applyHouseholderOnTheRight(
essentialVector(actual_k), m_coeffs.coeff(actual_k), workspace.data());
321 template <
typename Dest>
322 inline void applyThisOnTheLeft(Dest& dst,
bool inputIsIdentity =
false)
const {
323 Matrix<Scalar, 1, Dest::ColsAtCompileTime, RowMajor, 1, Dest::MaxColsAtCompileTime> workspace;
324 applyThisOnTheLeft(dst, workspace, inputIsIdentity);
328 template <
typename Dest,
typename Workspace>
329 inline void applyThisOnTheLeft(Dest& dst, Workspace& workspace,
bool inputIsIdentity =
false)
const {
330 if (inputIsIdentity && m_reverse) inputIsIdentity =
false;
332 if (m_length >= BlockSize && dst.cols() > 1) {
334 Index blockSize = m_length <
Index(2 * BlockSize) ? (m_length + 1) / 2 : Index(BlockSize);
335 for (Index i = 0; i < m_length; i += blockSize) {
336 Index end = m_reverse ? (std::min)(m_length, i + blockSize) : m_length - i;
337 Index k = m_reverse ? i : (std::max)(
Index(0), end - blockSize);
339 Index start = k + m_shift;
341 typedef Block<internal::remove_all_t<VectorsType>,
Dynamic,
Dynamic> SubVectorsType;
342 SubVectorsType sub_vecs1(m_vectors.const_cast_derived(), Side ==
OnTheRight ? k : start,
345 std::conditional_t<Side == OnTheRight, Transpose<SubVectorsType>, SubVectorsType&> sub_vecs(sub_vecs1);
347 Index dstRows =
rows() - m_shift - k;
349 if (inputIsIdentity) {
350 Block<Dest, Dynamic, Dynamic> sub_dst = dst.bottomRightCorner(dstRows, dstRows);
351 apply_block_householder_on_the_left(sub_dst, sub_vecs, m_coeffs.segment(k, bs), !m_reverse);
353 auto sub_dst = dst.bottomRows(dstRows);
354 apply_block_householder_on_the_left(sub_dst, sub_vecs, m_coeffs.segment(k, bs), !m_reverse);
358 workspace.resize(dst.cols());
359 for (Index k = 0; k < m_length; ++k) {
360 Index actual_k = m_reverse ? k : m_length - k - 1;
361 Index dstRows =
rows() - m_shift - actual_k;
363 if (inputIsIdentity) {
364 Block<Dest, Dynamic, Dynamic> sub_dst = dst.bottomRightCorner(dstRows, dstRows);
365 sub_dst.applyHouseholderOnTheLeft(
essentialVector(actual_k), m_coeffs.coeff(actual_k), workspace.data());
367 auto sub_dst = dst.bottomRows(dstRows);
368 sub_dst.applyHouseholderOnTheLeft(
essentialVector(actual_k), m_coeffs.coeff(actual_k), workspace.data());
381 template <
typename OtherDerived>
385 other.template cast<
typename internal::matrix_type_times_scalar_type<Scalar, OtherDerived>::ResultScalar>());
386 applyThisOnTheLeft(res, internal::is_identity<OtherDerived>::value && res.rows() == res.cols());
390 template <
typename VectorsType_,
typename CoeffsType_,
int S
ide_>
391 friend struct internal::hseq_side_dependent_impl;
432 template <
typename VectorsType2,
typename CoeffsType2,
int S
ide2>
451 bool reverseFlag()
const {
return m_reverse; }
453 typename VectorsType::Nested m_vectors;
454 typename CoeffsType::Nested m_coeffs;
458 enum { BlockSize = 48 };
469template <
typename OtherDerived,
typename VectorsType,
typename CoeffsType,
int S
ide>
473 other.template cast<
typename internal::matrix_type_times_scalar_type<
typename VectorsType::Scalar,
474 OtherDerived>::ResultScalar>());
475 h.applyThisOnTheRight(res);
483template <
typename VectorsType,
typename CoeffsType>
494template <
typename VectorsType,
typename CoeffsType>
496 const CoeffsType& h) {
Expression of a fixed-size or dynamic-size block.
Definition Block.h:110
Sequence of Householder reflections acting on subspaces with decreasing size.
Definition HouseholderSequence.h:117
AdjointReturnType inverse() const
Inverse of the Householder sequence (equals the adjoint).
Definition HouseholderSequence.h:249
HouseholderSequence & setLength(Index length)
Sets the length of the Householder sequence.
Definition HouseholderSequence.h:402
std::conditional_t< Cond, ConjugateReturnType, ConstHouseholderSequence > conjugateIf() const
Definition HouseholderSequence.h:235
Index shift() const
Returns the shift of the Householder sequence.
Definition HouseholderSequence.h:427
EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Number of columns of transformation viewed as a matrix.
Definition HouseholderSequence.h:194
HouseholderSequence & setShift(Index shift)
Sets the shift of the Householder sequence.
Definition HouseholderSequence.h:418
HouseholderSequence(const HouseholderSequence &other)
Copy constructor.
Definition HouseholderSequence.h:175
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:382
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Number of rows of transformation viewed as a matrix.
Definition HouseholderSequence.h:186
Index length() const
Returns the length of the Householder sequence.
Definition HouseholderSequence.h:423
ConjugateReturnType conjugate() const
Complex conjugate of the Householder sequence.
Definition HouseholderSequence.h:224
const EssentialVectorType essentialVector(Index k) const
Essential part of a Householder vector.
Definition HouseholderSequence.h:210
TransposeReturnType transpose() const
Transpose of the Householder sequence.
Definition HouseholderSequence.h:216
AdjointReturnType adjoint() const
Adjoint (conjugate transpose) of the Householder sequence.
Definition HouseholderSequence.h:241
HouseholderSequence(const VectorsType &v, const CoeffsType &h)
Constructor.
Definition HouseholderSequence.h:171
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:52
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:186
HouseholderSequence< VectorsType, CoeffsType > householderSequence(const VectorsType &v, const CoeffsType &h)
Convenience function for constructing a Householder sequence.
Definition HouseholderSequence.h:484
HouseholderSequence< VectorsType, CoeffsType, OnTheRight > rightHouseholderSequence(const VectorsType &v, const CoeffsType &h)
Convenience function for constructing a Householder sequence.
Definition HouseholderSequence.h:495
@ OnTheLeft
Definition Constants.h:331
@ OnTheRight
Definition Constants.h:333
Namespace containing all symbols from the Eigen library.
Definition Core:137
const int Dynamic
Definition Constants.h:25
Definition EigenBase.h:33
Eigen::Index Index
Definition EigenBase.h:43