Eigen-Contrib  5.0.1-dev+master
 
Loading...
Searching...
No Matches
Hankel.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// This Source Code Form is subject to the terms of the Mozilla
5// Public License v. 2.0. If a copy of the MPL was not distributed
6// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
7// SPDX-FileCopyrightText: The Eigen Authors
8// SPDX-License-Identifier: MPL-2.0
9
10// References:
11// [1] G. H. Golub and C. F. Van Loan, "Matrix Computations", 4th ed., Johns
12// Hopkins University Press, 2013, chapter 4.7 (Toeplitz, Hankel and
13// related systems) and chapter 4.8 (fast products via circulant embedding
14// and the FFT).
15// [2] R. H. Chan and X.-Q. Jin, "An Introduction to Iterative Toeplitz
16// Solvers", SIAM, 2007 (a Hankel matrix is a column-reversed Toeplitz
17// matrix, so its products and solves reduce to the Toeplitz machinery).
18
19#ifndef EIGEN_STRUCTURED_HANKEL_H
20#define EIGEN_STRUCTURED_HANKEL_H
21
22// IWYU pragma: private
23#include "./InternalHeaderCheck.h"
24
25namespace Eigen {
26
27template <typename Scalar_, int Rows_ = Dynamic, int Cols_ = Dynamic>
28class Hankel;
29
30namespace internal {
31
32template <typename Scalar_, int Rows_, int Cols_>
33struct traits<Hankel<Scalar_, Rows_, Cols_>> {
34 using Scalar = Scalar_;
35 using StorageKind = Dense;
36 using XprKind = MatrixXpr;
37 using StorageIndex = int;
38 static constexpr int RowsAtCompileTime = Rows_;
39 static constexpr int ColsAtCompileTime = Cols_;
40 static constexpr int MaxRowsAtCompileTime = Rows_;
41 static constexpr int MaxColsAtCompileTime = Cols_;
42 // Deliberately no NestByRefBit: transpose(), conjugate() and adjoint() return
43 // owning temporaries, so Product must nest the operator by value for a
44 // delayed-evaluated product expression to keep its left factor alive. The copy
45 // is O(n), negligible against the O(n log n) product evaluation.
46 static constexpr unsigned int Flags = 0;
47};
48
49template <typename Scalar_, int Rows_, int Cols_>
50struct evaluator_traits<Hankel<Scalar_, Rows_, Cols_>> {
51 using Kind = IndexBased;
52 using Shape = StructuredShape;
53};
54
55} // namespace internal
56
101template <typename Scalar_, int Rows_, int Cols_>
102class Hankel : public EigenBase<Hankel<Scalar_, Rows_, Cols_>> {
103 public:
104 using Scalar = Scalar_;
105 using RealScalar = typename NumTraits<Scalar>::Real;
106 using StorageIndex = int;
107 using Complex = std::complex<RealScalar>;
108 using ColGeneratorType = Matrix<Scalar, Rows_, 1>;
109 using RowGeneratorType = Matrix<Scalar, Cols_, 1>;
110 using ComplexVector = Matrix<Complex, Dynamic, 1>;
111
112 static constexpr int RowsAtCompileTime = Rows_;
113 static constexpr int ColsAtCompileTime = Cols_;
114 static constexpr int MaxRowsAtCompileTime = Rows_;
115 static constexpr int MaxColsAtCompileTime = Cols_;
116 static constexpr int SizeAtCompileTime = internal::size_at_compile_time(Rows_, Cols_);
117 static constexpr int MaxSizeAtCompileTime = SizeAtCompileTime;
118 static constexpr bool IsRowMajor = false;
119 // Deliberately no IsVectorAtCompileTime: Ref<const Hankel>'s default StrideType
120 // argument reads it, so its absence makes internal::is_ref_compatible SFINAE to
121 // false and keeps the iterative solvers on their matrix-free path.
122
124 static constexpr int GenSizeAtCompileTime = (Rows_ == Dynamic || Cols_ == Dynamic) ? Dynamic : (Rows_ + Cols_ - 1);
125 using GeneratorType = Matrix<Scalar, GenSizeAtCompileTime, 1>;
126
135 template <typename ColDerived, typename RowDerived>
137 : m_rows(col.size()), m_cols(row.size()) {
138 EIGEN_STATIC_ASSERT_VECTOR_ONLY(ColDerived)
139 EIGEN_STATIC_ASSERT_VECTOR_ONLY(RowDerived)
140 const Index m = col.size(), n = row.size();
141 EIGEN_INTERNAL_ASSERT_NONEMPTY_SIZE_MSG(col, m, "Hankel generators must be non-empty");
142 EIGEN_INTERNAL_ASSERT_NONEMPTY_SIZE_MSG(row, n, "Hankel generators must be non-empty");
143 m_h.resize(m + n - 1);
144 m_h.head(m) = col;
145 if (n > 1) m_h.tail(n - 1) = row.tail(n - 1);
146 if (m > 1 && n > 1 && (m > internal::structured_direct_threshold() || n > internal::structured_direct_threshold()))
147 m_symbol = computeSymbol();
148 }
149
150 EIGEN_DEVICE_FUNC Index rows() const { return m_rows.value(); }
151 EIGEN_DEVICE_FUNC Index cols() const { return m_cols.value(); }
152
155 const GeneratorType& generator() const { return m_h; }
156
158 ColGeneratorType column() const { return m_h.head(rows()); }
159
161 RowGeneratorType lastRow() const { return m_h.segment(rows() - 1, cols()); }
162
168 ComplexVector symbol() const { return m_symbol.size() > 0 ? m_symbol : computeSymbol(); }
169
171 Scalar coeff(Index row, Index col) const { return m_h.coeff(row + col); }
172
178 Hankel<Scalar, Cols_, Rows_> transpose() const {
179 return Hankel<Scalar, Cols_, Rows_>(m_h, cols(), rows(), transposedSymbol());
180 }
181
185 Hankel conjugate() const {
186 return Hankel(m_h.conjugate(), rows(), cols(), internal::structured_reverse_symbol(m_symbol).conjugate());
187 }
188
192 Hankel<Scalar, Cols_, Rows_> adjoint() const { return conjugate().transpose(); }
193
199 ColGeneratorType c = m_h.segment(cols() - 1, rows());
200 RowGeneratorType r = m_h.head(cols()).reverse();
202 }
203
207 static constexpr int SolveRowsAtCompileTime = Cols_ != Dynamic ? Cols_ : Rows_;
208
217 template <typename Rhs>
219 EIGEN_INTERNAL_ASSERT_SQUARE_MSG(*this, "Hankel::solve requires a square matrix");
220 EIGEN_INTERNAL_ASSERT_SOLVER_RHS_MSG(*this, b, false, "right-hand side has the wrong number of rows");
222 return levinson.solve(b).colwise().reverse();
223 }
224
228 template <typename Dest>
229 void evalTo(Dest& dst) const {
230 EIGEN_IF_CONSTEXPR (Dest::IsRowMajor) {
231 for (Index i = 0; i < rows(); ++i) dst.row(i) = m_h.segment(i, cols()).transpose();
232 return;
233 }
234 for (Index j = 0; j < cols(); ++j) dst.col(j) = m_h.segment(j, rows());
235 }
236
238 template <typename Dest>
239 void addTo(Dest& dst) const {
240 EIGEN_IF_CONSTEXPR (Dest::IsRowMajor) {
241 for (Index i = 0; i < rows(); ++i) dst.row(i) += m_h.segment(i, cols()).transpose();
242 return;
243 }
244 for (Index j = 0; j < cols(); ++j) dst.col(j) += m_h.segment(j, rows());
245 }
246
248 template <typename Dest>
249 void subTo(Dest& dst) const {
250 EIGEN_IF_CONSTEXPR (Dest::IsRowMajor) {
251 for (Index i = 0; i < rows(); ++i) dst.row(i) -= m_h.segment(i, cols()).transpose();
252 return;
253 }
254 for (Index j = 0; j < cols(); ++j) dst.col(j) -= m_h.segment(j, rows());
255 }
256
261 template <typename Rhs>
263 return Product<Hankel, Rhs>(*this, x.derived());
264 }
265
274 template <typename Dest, typename Rhs, typename ProductScalar>
275 void addProduct(Dest& dst, const Rhs& rhs, const ProductScalar& alpha) const {
276 const Index m = rows(), n = cols();
277 EIGEN_INTERNAL_ASSERT_PRODUCT_SHAPE_MSG(*this, rhs, "invalid product: dimensions do not match");
278 const bool small = m == 1 || n == 1 ||
279 (m <= internal::structured_direct_threshold() && n <= internal::structured_direct_threshold());
280 if (small) {
281 directProduct(dst, rhs, alpha);
282 return;
283 }
284 internal::structured_fft_apply(dst, m_symbol, m, rhs.colwise().reverse(), alpha);
285 }
286
287 private:
288 template <typename OtherScalar, int OtherRows, int OtherCols>
289 friend class Hankel;
290
295 Hankel(const GeneratorType& h, Index rows, Index cols, const ComplexVector& symbol)
296 : m_rows(rows), m_cols(cols), m_h(h), m_symbol(symbol) {}
297
302 template <typename Dest, typename Rhs, typename ProductScalar>
303 void directProductColumn(Dest& dst, const Rhs& rhs, Index k, const ProductScalar& alpha) const {
304 const Index m = rows(), n = cols();
305 // A unit alpha must not multiply: even the identity complex scalar (1,0)
306 // pollutes an (Inf,0) value with NaN through the 0*Inf cross term.
307 const bool unitAlpha = alpha == ProductScalar(1);
308
309 if (n == 1) {
310 // The runtime-sized head keeps other fixed-size shapes well-formed.
311 const ProductScalar xj = unitAlpha ? ProductScalar(rhs.coeff(0, k)) : ProductScalar(alpha * rhs.coeff(0, k));
312 dst.col(k) += xj * m_h.head(m);
313 return;
314 }
315
316 if (m == 1) {
317 const ProductScalar acc = m_h.head(n).cwiseProduct(rhs.col(k)).sum();
318 dst.coeffRef(0, k) += unitAlpha ? acc : ProductScalar(alpha * acc);
319 return;
320 }
321
322 if (m <= internal::structured_scalar_threshold() && n <= internal::structured_scalar_threshold()) {
323 // Tiny sizes: a plain scalar loop beats the segment-based path below, whose
324 // per-segment setup dominates when segments hold only a few entries.
325 for (Index i = 0; i < m; ++i) {
326 ProductScalar acc(0);
327 for (Index j = 0; j < n; ++j) acc += coeff(i, j) * rhs.coeff(j, k);
328 dst.coeffRef(i, k) += unitAlpha ? acc : ProductScalar(alpha * acc);
329 }
330 return;
331 }
332
333 // Column j is the contiguous, vectorizable slice h[j..j+m-1].
334 auto dstCol = dst.col(k);
335 for (Index j = 0; j < n; ++j) {
336 const ProductScalar xj = unitAlpha ? ProductScalar(rhs.coeff(j, k)) : ProductScalar(alpha * rhs.coeff(j, k));
337 dstCol += xj * m_h.segment(j, m);
338 }
339 }
340
343 template <typename Dest, typename Rhs, typename ProductScalar>
344 void directProduct(Dest& dst, const Rhs& rhs, const ProductScalar& alpha) const {
345 for (Index k = 0; k < rhs.cols(); ++k) directProductColumn(dst, rhs, k, alpha);
346 }
347
351 ComplexVector computeSymbol() const {
352 const Index m = rows(), n = cols();
353 const Index p = internal::fft_next_good_size(m + n - 1);
354 ComplexVector embedding = ComplexVector::Zero(p);
355 embedding.head(m) = m_h.tail(m).template cast<Complex>(); // h[n-1 .. m+n-2]
356 embedding.tail(n - 1) = m_h.head(n - 1).template cast<Complex>();
357 if (p == 1) return embedding; // the DFT of a single sample is the identity
358 ComplexVector symbol(p);
359 auto&& fft = internal::structured_fft_engine<RealScalar>();
360 fft.fwd(symbol, embedding, p);
361 return symbol;
362 }
363
369 ComplexVector transposedSymbol() const {
370 ComplexVector sym = m_symbol;
371 const Index p = sym.size();
372 if (p > 0 && rows() != cols()) {
373 Index s = (rows() - cols()) % p;
374 if (s < 0) s += p;
375 // Contiguous real buffers let sin/cos use packets; complex component views
376 // alone have stride two and would force scalar trigonometric evaluation.
377 Array<RealScalar, 64, 1> angles, values;
378 Matrix<Complex, 64, 1> phases;
379 Index fs = 0; // f * s mod p
380 for (Index f = 0; f < p;) {
381 const Index count = numext::mini<Index>(64, p - f);
382 for (Index i = 0; i < count; ++i) {
383 angles[i] = RealScalar(2 * EIGEN_PI) * RealScalar(fs) / RealScalar(p);
384 fs += s;
385 if (fs >= p) fs -= p;
386 }
387 values.head(count) = angles.head(count).cos();
388 phases.head(count).real() = values.head(count);
389 values.head(count) = angles.head(count).sin();
390 phases.head(count).imag() = values.head(count);
391 sym.segment(f, count).array() *= phases.head(count).array();
392 f += count;
393 }
394 }
395 return sym;
396 }
397
398 // Empty (compile-time constant) when the corresponding dimension is fixed; the
399 // dimensions cannot be recovered from m_h alone, whose length is rows()+cols()-1.
400 internal::variable_if_dynamic<Index, RowsAtCompileTime> m_rows;
401 internal::variable_if_dynamic<Index, ColsAtCompileTime> m_cols;
402 GeneratorType m_h;
403 ComplexVector m_symbol;
404};
405
409template <typename ColDerived, typename RowDerived>
414
415namespace internal {
416
417// Single product specialization covering every product tag; see the note in
418// Circulant.h.
419template <typename Scalar_, int Rows_, int Cols_, typename Rhs, int ProductTag>
420struct generic_product_impl<Hankel<Scalar_, Rows_, Cols_>, Rhs, StructuredShape, DenseShape, ProductTag>
421 : structured_product_impl<Hankel<Scalar_, Rows_, Cols_>, Rhs> {};
422
423} // namespace internal
424
425#if EIGEN_COMP_CXXVER < 17
426template <typename Scalar_, int Rows_, int Cols_>
427constexpr int Hankel<Scalar_, Rows_, Cols_>::RowsAtCompileTime;
428template <typename Scalar_, int Rows_, int Cols_>
429constexpr int Hankel<Scalar_, Rows_, Cols_>::ColsAtCompileTime;
430template <typename Scalar_, int Rows_, int Cols_>
431constexpr int Hankel<Scalar_, Rows_, Cols_>::MaxRowsAtCompileTime;
432template <typename Scalar_, int Rows_, int Cols_>
433constexpr int Hankel<Scalar_, Rows_, Cols_>::MaxColsAtCompileTime;
434template <typename Scalar_, int Rows_, int Cols_>
435constexpr int Hankel<Scalar_, Rows_, Cols_>::SizeAtCompileTime;
436template <typename Scalar_, int Rows_, int Cols_>
437constexpr int Hankel<Scalar_, Rows_, Cols_>::MaxSizeAtCompileTime;
438template <typename Scalar_, int Rows_, int Cols_>
439constexpr bool Hankel<Scalar_, Rows_, Cols_>::IsRowMajor;
440template <typename Scalar_, int Rows_, int Cols_>
441constexpr int Hankel<Scalar_, Rows_, Cols_>::GenSizeAtCompileTime;
442template <typename Scalar_, int Rows_, int Cols_>
443constexpr int Hankel<Scalar_, Rows_, Cols_>::SolveRowsAtCompileTime;
444#endif
445
446} // namespace Eigen
447
448#endif // EIGEN_STRUCTURED_HANKEL_H
constexpr FixedSegmentReturnType< N >::Type tail(Index n=N)
An m x n Hankel matrix represented by its first column and last row.
Definition Hankel.h:102
Scalar coeff(Index row, Index col) const
Definition Hankel.h:171
Hankel(const MatrixBase< ColDerived > &col, const MatrixBase< RowDerived > &row)
Definition Hankel.h:136
ColGeneratorType column() const
Definition Hankel.h:158
Matrix< Scalar, SolveRowsAtCompileTime, Rhs::ColsAtCompileTime > solve(const MatrixBase< Rhs > &b) const
Definition Hankel.h:218
Toeplitz< Scalar, Rows_, Cols_ > toToeplitz() const
Definition Hankel.h:198
ComplexVector symbol() const
Definition Hankel.h:168
Hankel< Scalar, Cols_, Rows_ > adjoint() const
Definition Hankel.h:192
RowGeneratorType lastRow() const
Definition Hankel.h:161
Hankel< Scalar, Cols_, Rows_ > transpose() const
Definition Hankel.h:178
Hankel conjugate() const
Definition Hankel.h:185
Product< Hankel, Rhs > operator*(const MatrixBase< Rhs > &x) const
Definition Hankel.h:262
const GeneratorType & generator() const
Definition Hankel.h:155
Look-ahead Levinson direct solver for general Toeplitz systems.
Definition LookAheadLevinson.h:87
const Solve< LookAheadLevinson, Rhs > solve(const MatrixBase< Rhs > &b) const
An m x n Toeplitz matrix represented by its first column and row.
Definition Toeplitz.h:85
Hankel< typename ColDerived::Scalar, ColDerived::SizeAtCompileTime, RowDerived::SizeAtCompileTime > makeHankel(const MatrixBase< ColDerived > &col, const MatrixBase< RowDerived > &row)
Definition Hankel.h:410
Namespace containing all symbols from the Eigen library.