10#ifndef EIGEN_STRUCTURED_LOOK_AHEAD_LEVINSON_H
11#define EIGEN_STRUCTURED_LOOK_AHEAD_LEVINSON_H
14#include "./InternalHeaderCheck.h"
18template <
typename Scalar_>
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;
34template <
typename D1,
typename D2>
35typename D1::Scalar structured_tdot(
const MatrixBase<D1>& a,
const MatrixBase<D2>& b) {
36 return a.cwiseProduct(b).sum();
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);
86template <
typename Scalar_>
100 template <
int Rows_,
int Cols_>
102 : m_maxBlockSize(4), m_n(0), m_isInitialized(false), m_info(
InvalidInput) {
110 eigen_assert(p >= 1);
118 Index rows() const noexcept {
return m_n; }
119 Index cols() const noexcept {
return m_n; }
122 template <
int Rows_,
int Cols_>
125#ifdef EIGEN_PARSED_BY_DOXYGEN
131 template <
typename Rhs>
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);
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>();
169 eigen_assert(m_isInitialized &&
"LookAheadLevinson is not initialized.");
176 eigen_assert(m_isInitialized &&
"LookAheadLevinson is not initialized.");
183 template <
typename Rhs>
184 DenseMatrix solveForward(
const Rhs& b)
const {
185 const Index nrhs = b.cols();
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;
191 a = s.luGamma.solve(rhs);
192 xn.resize(s.k + s.p, nrhs);
193 xn.topRows(s.k).noalias() = x + s.EkYp * a;
194 xn.bottomRows(s.p) = a;
206 FullPivLU<DenseMatrix> luGamma;
210 static DenseMatrix leadingBlock(
const DenseVector& c,
const DenseVector& r,
Index 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);
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);
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);
233 Index m_maxBlockSize;
236 FullPivLU<DenseMatrix> m_luInit;
237 std::vector<Step> m_steps;
238 bool m_complete =
false;
239 bool m_isInitialized;
242 RealScalar m_normEst;
245template <
typename Scalar_>
246template <
int Rows_,
int Cols_>
248 using internal::structured_tdot;
249 using internal::structured_upshift;
251 EIGEN_INTERNAL_CHECK_SHAPE(T.rows() == T.cols() &&
"LookAheadLevinson requires a square Toeplitz matrix",
252 YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES,
254 const Index n = T.rows();
255 const DenseVector& c = T.column();
256 const DenseVector& r = T.row();
257 const Scalar rho0 = c[0];
263 m_normEst = c.cwiseAbs().sum() + r.cwiseAbs().sum();
265 if (!(c.allFinite() && r.allFinite())) {
269 m_sMin = RealScalar(0);
271 m_isInitialized =
true;
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));
290 m_luInit.compute(leadingBlock(c, r, k));
294 bool lastWasBlock =
false;
295 DenseVector yPrev, zPrev;
297 Index kp = 0, pp = 0;
298 DenseMatrix Ypp, Zpp;
300 DenseVector y_kp_1, z_kp_1;
303 y = m_luInit.transpose().solve((-r.segment(1, k)).eval());
304 z = m_luInit.solve((-c.segment(1, k)).eval());
305 gamma = rho0 + structured_tdot(c.segment(1, k), y);
307 lastWasBlock =
false;
308 yPrev = DenseVector::Zero(0);
309 zPrev = DenseVector::Zero(0);
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);
324 const Index pcap = numext::mini<Index>(m_maxBlockSize, n - k);
325 const DenseVector Ek_y = y.reverse(), Ek_z = z.reverse();
327 DenseVector g_k, h_k;
328 bool gh_ready =
false;
329 auto ensure_gh = [&]() {
330 if (gh_ready)
return;
335 g_k.head(k - 1) = zPrev.reverse();
336 g_k[k - 1] = Scalar(1);
338 h_k.head(k - 1) = yPrev.reverse();
339 h_k[k - 1] = Scalar(1);
343 DenseVector rhs_y = -r.segment(kp + 2, pp);
344 DenseVector rhs_z = -c.segment(kp + 2, pp);
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();
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);
355 yk1.head(kp) = y_kp_1 + (Zpp * a_y).reverse();
356 zk1.head(kp) = z_kp_1 + (Ypp * a_z).reverse();
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;
369 DenseMatrix Yp(k, pcap), Zp(k, pcap);
374 RealScalar bestPsi = RealScalar(-1);
375 DenseMatrix bestGamma;
377 for (Index p = 1; p <= pcap; ++p) {
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;
386 const DenseMatrix Sp = shiftBlock(c, 1, k, p);
387 DenseMatrix Gamma = leadingBlock(c, r, p) + Sp.transpose() * Yp.leftCols(p);
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;
397 if (psi > RealScalar(0.1) * m_sMin) {
404 if (bestGamma.size() == 0) {
408 m_sMin = RealScalar(0);
412 DenseMatrix Gamma = bestGamma;
421 const Index K = k + p;
422 const bool finalStep = (K == n);
424 const DenseMatrix Sp = shiftBlock(c, 1, k, p);
425 const DenseMatrix Rp = shiftBlock(r, 1, k, p);
432 step.EkYp = Yp.leftCols(p).colwise().reverse();
433 step.WspEk = Sp.transpose().rowwise().reverse();
435 m_steps.push_back(step);
438 const DenseVector rhs_e = -r.segment(k + 1, p) - Rp.transpose() * Ek_y;
439 const DenseVector rhs_f = -c.segment(k + 1, p) - Sp.transpose() * Ek_z;
440 const DenseVector e_p = luG.transpose().solve(rhs_e);
441 const DenseVector f_p = luG.solve(rhs_f);
443 DenseVector y_new(K), z_new(K);
444 y_new.head(k) = y + (Zp.leftCols(p) * e_p).reverse();
446 z_new.head(k) = z + (Yp.leftCols(p) * f_p).reverse();
449 const DenseVector y_save = y, z_save = z;
450 const Scalar gamma_save = gamma;
452 gamma = (Scalar(1) - e_p[0] * f_p[0]) * gamma;
454 gamma = rho0 + structured_tdot(c.segment(1, K), y_new);
459 lastWasBlock =
false;
462 gammaPrev = gamma_save;
467 Ypp = Yp.leftCols(p);
468 Zpp = Zp.leftCols(p);
479 m_isInitialized =
true;
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
constexpr unsigned int RowMajorBit
Namespace containing all symbols from the Eigen library.
constexpr Index size() const noexcept