Eigen-Contrib  5.0.1-dev+master
 
Loading...
Searching...
No Matches
LookAheadLevinson.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#ifndef EIGEN_STRUCTURED_LOOK_AHEAD_LEVINSON_H
11#define EIGEN_STRUCTURED_LOOK_AHEAD_LEVINSON_H
12
13// IWYU pragma: private
14#include "./InternalHeaderCheck.h"
15
16namespace Eigen {
17
18template <typename Scalar_>
20
21namespace internal {
22
23template <typename Scalar_>
24struct traits<LookAheadLevinson<Scalar_>> : traits<Matrix<Scalar_, Dynamic, Dynamic>> {
25 using XprKind = MatrixXpr;
26 using StorageKind = SolverStorage;
27 using StorageIndex = int;
28 using BaseTraits = traits<Matrix<Scalar_, Dynamic, Dynamic>>;
29 static constexpr int Flags = BaseTraits::Flags & RowMajorBit;
30 static constexpr int CoeffReadCost = Dynamic;
31};
32
33// Persymmetry E_n T_n E_n = T_n^T requires sum_i a_i b_i, not the conjugating dot().
34template <typename D1, typename D2>
35typename D1::Scalar structured_tdot(const MatrixBase<D1>& a, const MatrixBase<D2>& b) {
36 return a.cwiseProduct(b).sum();
37}
38
39template <typename Scalar>
40Matrix<Scalar, Dynamic, 1> structured_upshift(const Matrix<Scalar, Dynamic, 1>& v) {
41 const Index k = v.size();
42 Matrix<Scalar, Dynamic, 1> w = Matrix<Scalar, Dynamic, 1>::Zero(k);
43 if (k > 1) w.head(k - 1) = v.tail(k - 1);
44 return w;
45}
46
47} // namespace internal
48
86template <typename Scalar_>
87class LookAheadLevinson : public SolverBase<LookAheadLevinson<Scalar_>> {
88 public:
90 friend class SolverBase<LookAheadLevinson>;
91 EIGEN_GENERIC_PUBLIC_INTERFACE(LookAheadLevinson)
92 using DenseVector = Matrix<Scalar, Dynamic, 1>;
93 using DenseMatrix = Matrix<Scalar, Dynamic, Dynamic>;
94
96 LookAheadLevinson() : m_maxBlockSize(4), m_n(0), m_isInitialized(false), m_info(InvalidInput) {}
97
100 template <int Rows_, int Cols_>
102 : m_maxBlockSize(4), m_n(0), m_isInitialized(false), m_info(InvalidInput) {
103 compute(T);
104 }
105
110 eigen_assert(p >= 1);
111 m_maxBlockSize = p;
112 return *this;
113 }
114
116 Index maxBlockSize() const { return m_maxBlockSize; }
117
118 Index rows() const noexcept { return m_n; }
119 Index cols() const noexcept { return m_n; }
120
122 template <int Rows_, int Cols_>
124
125#ifdef EIGEN_PARSED_BY_DOXYGEN
131 template <typename Rhs>
133#endif
134
135#ifndef EIGEN_PARSED_BY_DOXYGEN
143 template <typename RhsType, typename DstType>
144 void _solve_impl(const RhsType& rhs, DstType& dst) const {
145 dst.noalias() = solveForward(rhs);
146 }
147
152 template <bool Conjugate, typename RhsType, typename DstType>
153 void _solve_impl_transposed(const RhsType& rhs, DstType& dst) const {
154 const DenseMatrix reversed = rhs.template conjugateIf<Conjugate>().colwise().reverse();
155 dst = solveForward(reversed).colwise().reverse().template conjugateIf<Conjugate>();
156 }
157#endif
158
169 eigen_assert(m_isInitialized && "LookAheadLevinson is not initialized.");
170 return m_info;
171 }
172
175 RealScalar conditionEstimate() const {
176 eigen_assert(m_isInitialized && "LookAheadLevinson is not initialized.");
177 return m_sMin > RealScalar(0) ? m_normEst / m_sMin : NumTraits<RealScalar>::infinity();
178 }
179
180 private:
183 template <typename Rhs>
184 DenseMatrix solveForward(const Rhs& b) const {
185 const Index nrhs = b.cols();
186 if (!m_complete) return DenseMatrix::Constant(m_n, nrhs, Scalar(NumTraits<RealScalar>::quiet_NaN()));
187 DenseMatrix x = m_luInit.solve(b.topRows(m_k0));
188 DenseMatrix rhs, a, xn;
189 for (const Step& s : m_steps) {
190 rhs.noalias() = b.middleRows(s.k, s.p) - s.WspEk * x; // (27): b_p - S_p^T E_k x_k
191 a = s.luGamma.solve(rhs);
192 xn.resize(s.k + s.p, nrhs);
193 xn.topRows(s.k).noalias() = x + s.EkYp * a; // [E_k Y_p; I_p] a_p
194 xn.bottomRows(s.p) = a;
195 x = xn;
196 }
197 return x;
198 }
199
200 // Per accepted block step, the b-independent data needed to advance x in solve().
201 struct Step {
202 Index k; // order before the step
203 Index p; // block size
204 DenseMatrix EkYp; // E_k Y_p (k x p)
205 DenseMatrix WspEk; // S_p^T E_k (p x k)
206 FullPivLU<DenseMatrix> luGamma;
207 };
208
209 // Leading p x p principal submatrix built directly from the generators.
210 static DenseMatrix leadingBlock(const DenseVector& c, const DenseVector& r, Index p) {
211 DenseMatrix B(p, p);
212 for (Index j = 0; j < p; ++j) {
213 B.col(j).head(j) = r.segment(1, j).reverse();
214 B.col(j).tail(p - j) = c.head(p - j);
215 }
216 return B;
217 }
218
219 // Builds a (len x cols) block whose column j is the length-len generator segment
220 // v.segment(first + j, len); used to assemble the S_p and R_p shift blocks.
221 static DenseMatrix shiftBlock(const DenseVector& v, Index first, Index len, Index cols) {
222 DenseMatrix B(len, cols);
223 for (Index j = 0; j < cols; ++j) B.col(j) = v.segment(first + j, len);
224 return B;
225 }
226
227 static RealScalar smallestSingularValue(const DenseMatrix& M) {
228 if (M.rows() == 1) return numext::abs(M(0, 0));
229 JacobiSVD<DenseMatrix> svd(M);
230 return svd.singularValues()(svd.singularValues().size() - 1);
231 }
232
233 Index m_maxBlockSize;
234 Index m_n;
235 Index m_k0;
236 FullPivLU<DenseMatrix> m_luInit; // LU of the leading T_{k0}
237 std::vector<Step> m_steps;
238 bool m_complete = false; // the steps reach order n; else every solve is NaN
239 bool m_isInitialized;
240 ComputationInfo m_info;
241 RealScalar m_sMin;
242 RealScalar m_normEst;
243};
244
245template <typename Scalar_>
246template <int Rows_, int Cols_>
248 using internal::structured_tdot;
249 using internal::structured_upshift;
250
251 EIGEN_INTERNAL_CHECK_SHAPE(T.rows() == T.cols() && "LookAheadLevinson requires a square Toeplitz matrix",
252 YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES,
253 internal::square_dimensions<Toeplitz<Scalar, Rows_, Cols_>>);
254 const Index n = T.rows();
255 const DenseVector& c = T.column(); // (rho_0, sigma_1, ..., sigma_{n-1})
256 const DenseVector& r = T.row(); // (rho_0, rho_1, ..., rho_{n-1})
257 const Scalar rho0 = c[0];
258
259 m_n = n;
260 m_steps.clear();
261 m_complete = true;
262 m_info = Success;
263 m_normEst = c.cwiseAbs().sum() + r.cwiseAbs().sum(); // cheap upper bound on ||T||_2
264
265 if (!(c.allFinite() && r.allFinite())) {
266 // A non-finite entry of T is a breakdown whatever the block size: a block with a NaN entry has the computed
267 // singular value 0 (maxi drops the NaN), is accepted, and its LU solves to zeros or to partly finite vectors.
268 m_complete = false;
269 m_sMin = RealScalar(0);
270 m_info = NumericalIssue;
271 m_isInitialized = true;
272 return *this;
273 }
274
275 Index k;
276 DenseVector y, z;
277 Scalar gamma(0);
278 {
279 Index kbest = 1;
280 RealScalar best = RealScalar(-1);
281 const Index kmax = numext::mini<Index>(m_maxBlockSize, n);
282 for (Index i = 1; i <= kmax; ++i) {
283 RealScalar sv = smallestSingularValue(leadingBlock(c, r, i));
284 if (sv > best) {
285 best = sv;
286 kbest = i;
287 }
288 }
289 k = m_k0 = kbest;
290 m_luInit.compute(leadingBlock(c, r, k));
291 m_sMin = best;
292 }
293
294 bool lastWasBlock = false;
295 DenseVector yPrev, zPrev;
296 Scalar gammaPrev(0);
297 Index kp = 0, pp = 0;
298 DenseMatrix Ypp, Zpp;
299 FullPivLU<DenseMatrix> luGammaPP;
300 DenseVector y_kp_1, z_kp_1;
301
302 if (k < n) {
303 y = m_luInit.transpose().solve((-r.segment(1, k)).eval()); // T_k^T y = -r_k
304 z = m_luInit.solve((-c.segment(1, k)).eval()); // T_k z = -s_k
305 gamma = rho0 + structured_tdot(c.segment(1, k), y);
306 if (k == 1) {
307 lastWasBlock = false;
308 yPrev = DenseVector::Zero(0);
309 zPrev = DenseVector::Zero(0);
310 gammaPrev = rho0; // gamma^{(0)}
311 } else {
312 lastWasBlock = true; // treat init as a block step of size k from order 0
313 kp = 0;
314 pp = k;
315 Ypp = DenseMatrix::Zero(0, k);
316 Zpp = DenseMatrix::Zero(0, k);
317 luGammaPP = m_luInit;
318 y_kp_1 = DenseVector::Zero(0);
319 z_kp_1 = DenseVector::Zero(0);
320 }
321 }
322
323 while (k < n) {
324 const Index pcap = numext::mini<Index>(m_maxBlockSize, n - k);
325 const DenseVector Ek_y = y.reverse(), Ek_z = z.reverse();
326
327 DenseVector g_k, h_k;
328 bool gh_ready = false;
329 auto ensure_gh = [&]() {
330 if (gh_ready) return;
331 if (!lastWasBlock) {
332 // Theorem 1, eq (33)
333 g_k.resize(k);
334 h_k.resize(k);
335 g_k.head(k - 1) = zPrev.reverse();
336 g_k[k - 1] = Scalar(1);
337 g_k /= gammaPrev;
338 h_k.head(k - 1) = yPrev.reverse();
339 h_k[k - 1] = Scalar(1);
340 h_k /= gammaPrev;
341 } else {
342 // Theorem 2: y_{k,1}, z_{k,1} (eqs 47-49), then g_k, h_k (eq 50).
343 DenseVector rhs_y = -r.segment(kp + 2, pp);
344 DenseVector rhs_z = -c.segment(kp + 2, pp);
345 if (kp > 0) {
346 const DenseMatrix Rpp = shiftBlock(r, 1, kp, pp);
347 const DenseMatrix Spp = shiftBlock(c, 1, kp, pp);
348 rhs_y.noalias() -= Rpp.transpose() * y_kp_1.reverse();
349 rhs_z.noalias() -= Spp.transpose() * z_kp_1.reverse();
350 }
351 const DenseVector a_y = luGammaPP.transpose().solve(rhs_y);
352 const DenseVector a_z = luGammaPP.solve(rhs_z);
353 DenseVector yk1 = DenseVector::Zero(k), zk1 = DenseVector::Zero(k);
354 if (kp > 0) {
355 yk1.head(kp) = y_kp_1 + (Zpp * a_y).reverse();
356 zk1.head(kp) = z_kp_1 + (Ypp * a_z).reverse();
357 }
358 yk1.tail(pp) = a_y;
359 zk1.tail(pp) = a_z;
360 const Scalar c1 = -r[k + 1] - structured_tdot(r.segment(1, k), Ek_y);
361 const Scalar d1 = -c[k + 1] - structured_tdot(c.segment(1, k), Ek_z);
362 g_k = (yk1 - structured_upshift<Scalar>(y) + y[0] * y) / c1;
363 h_k = (zk1 - structured_upshift<Scalar>(z) + z[0] * z) / d1;
364 }
365 gh_ready = true;
366 };
367
368 // Build Y_p, Z_p incrementally and pick the block size p (eqs 31-32, 46, 39).
369 DenseMatrix Yp(k, pcap), Zp(k, pcap);
370 Yp.col(0) = y;
371 Zp.col(0) = z;
372 Index chosen = -1;
373 Index bestP = 1;
374 RealScalar bestPsi = RealScalar(-1);
375 DenseMatrix bestGamma;
376
377 for (Index p = 1; p <= pcap; ++p) {
378 if (p >= 2) {
379 ensure_gh();
380 const Index i = p - 1;
381 const Scalar ci = -r[k + i] - structured_tdot(r.segment(i, k), Ek_y);
382 const Scalar di = -c[k + i] - structured_tdot(c.segment(i, k), Ek_z);
383 Yp.col(i) = structured_upshift<Scalar>(Yp.col(i - 1)) - Yp.col(i - 1)[0] * y + ci * g_k;
384 Zp.col(i) = structured_upshift<Scalar>(Zp.col(i - 1)) - Zp.col(i - 1)[0] * z + di * h_k;
385 }
386 const DenseMatrix Sp = shiftBlock(c, 1, k, p);
387 DenseMatrix Gamma = leadingBlock(c, r, p) + Sp.transpose() * Yp.leftCols(p); // (20)
388 const RealScalar muY = Yp.leftCols(p).cwiseAbs().maxCoeff();
389 const RealScalar muZ = Zp.leftCols(p).cwiseAbs().maxCoeff();
390 const RealScalar denom = numext::maxi(numext::maxi(RealScalar(1), muY), numext::maxi(muZ, muY * muZ));
391 const RealScalar psi = smallestSingularValue(Gamma) / denom; // (46)
392 if (psi > bestPsi) {
393 bestPsi = psi;
394 bestP = p;
395 bestGamma = Gamma;
396 }
397 if (psi > RealScalar(0.1) * m_sMin) { // (39)
398 chosen = p;
399 bestGamma = Gamma;
400 break;
401 }
402 }
403
404 if (bestGamma.size() == 0) {
405 // Every candidate block has a NaN psi (an overflowing recursion on finite generators): no step can be taken.
406 m_complete = false;
407 m_steps.clear();
408 m_sMin = RealScalar(0);
409 break;
410 }
411
412 DenseMatrix Gamma = bestGamma;
413 Index p;
414 if (chosen > 0) {
415 p = chosen;
416 } else {
417 p = bestP;
418 m_sMin = bestPsi; // pmax too small to skip cleanly; record the degraded conditioning
419 }
420
421 const Index K = k + p;
422 const bool finalStep = (K == n);
423
424 const DenseMatrix Sp = shiftBlock(c, 1, k, p);
425 const DenseMatrix Rp = shiftBlock(r, 1, k, p);
426 FullPivLU<DenseMatrix> luG(Gamma);
427
428 // Record the b-independent data for solve(): rhs = b_p - (S_p^T E_k) x_k ; x update via E_k Y_p.
429 Step step;
430 step.k = k;
431 step.p = p;
432 step.EkYp = Yp.leftCols(p).colwise().reverse(); // E_k Y_p
433 step.WspEk = Sp.transpose().rowwise().reverse(); // S_p^T E_k
434 step.luGamma = luG;
435 m_steps.push_back(step);
436
437 if (!finalStep) {
438 const DenseVector rhs_e = -r.segment(k + 1, p) - Rp.transpose() * Ek_y; // (28)
439 const DenseVector rhs_f = -c.segment(k + 1, p) - Sp.transpose() * Ek_z; // (29)
440 const DenseVector e_p = luG.transpose().solve(rhs_e);
441 const DenseVector f_p = luG.solve(rhs_f);
442
443 DenseVector y_new(K), z_new(K);
444 y_new.head(k) = y + (Zp.leftCols(p) * e_p).reverse(); // (25)
445 y_new.tail(p) = e_p;
446 z_new.head(k) = z + (Yp.leftCols(p) * f_p).reverse(); // (26)
447 z_new.tail(p) = f_p;
448
449 const DenseVector y_save = y, z_save = z;
450 const Scalar gamma_save = gamma;
451 if (p == 1)
452 gamma = (Scalar(1) - e_p[0] * f_p[0]) * gamma; // (15)
453 else
454 gamma = rho0 + structured_tdot(c.segment(1, K), y_new); // (5)
455 y = y_new;
456 z = z_new;
457
458 if (p == 1) {
459 lastWasBlock = false;
460 yPrev = y_save;
461 zPrev = z_save;
462 gammaPrev = gamma_save;
463 } else {
464 lastWasBlock = true;
465 kp = k;
466 pp = p;
467 Ypp = Yp.leftCols(p);
468 Zpp = Zp.leftCols(p);
469 luGammaPP = luG;
470 y_kp_1 = Yp.col(1);
471 z_kp_1 = Zp.col(1);
472 }
473 }
474 k = K;
475 }
476
477 const RealScalar kappa = m_sMin > RealScalar(0) ? m_normEst / m_sMin : NumTraits<RealScalar>::infinity();
478 if (!(kappa * NumTraits<RealScalar>::epsilon() < RealScalar(1))) m_info = NumericalIssue;
479 m_isInitialized = true;
480 return *this;
481}
482
483} // namespace Eigen
484
485#endif // EIGEN_STRUCTURED_LOOK_AHEAD_LEVINSON_H
Look-ahead Levinson direct solver for general Toeplitz systems.
Definition LookAheadLevinson.h:87
ComputationInfo info() const
Definition LookAheadLevinson.h:168
LookAheadLevinson(const Toeplitz< Scalar, Rows_, Cols_ > &T)
Definition LookAheadLevinson.h:101
LookAheadLevinson()
Definition LookAheadLevinson.h:96
LookAheadLevinson & setMaxBlockSize(Index p)
Definition LookAheadLevinson.h:109
Index maxBlockSize() const
Definition LookAheadLevinson.h:116
RealScalar conditionEstimate() const
Definition LookAheadLevinson.h:175
LookAheadLevinson & compute(const Toeplitz< Scalar, Rows_, Cols_ > &T)
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
ComputationInfo
NumericalIssue
constexpr unsigned int RowMajorBit
Namespace containing all symbols from the Eigen library.
constexpr Index size() const noexcept