5#ifndef LINE_UTIL_SYLVESTER_H
6#define LINE_UTIL_SYLVESTER_H
82 : n_(A.rows()), m_(B.rows()) {
83#ifndef LINE_MP_HAVE_LAPACK
85 "sylvester_schur requires LAPACK: reconfigure with -DLINE_MP_USE_LAPACK=ON and "
86 "liblapack available");
88 if (A.
cols() != n_ || B.
cols() != m_)
89 throw InputError(
"sylvester_schur: A and B must be square");
90 if (n_ == 0 || m_ == 0)
return;
97 for (std::size_t i = 0; i < n_; ++i)
98 for (std::size_t j = 0; j < n_; ++j) ta_[j * n_ + i] = sa.
T(i, j);
99 for (std::size_t i = 0; i < m_; ++i)
100 for (std::size_t j = 0; j < m_; ++j) tb_[j * m_ + i] = sb.
T(i, j);
106#ifndef LINE_MP_HAVE_LAPACK
110 if (C.
rows() != n_ || C.
cols() != m_)
111 throw InputError(
"sylvester_schur: the right-hand side is not conformable");
113 if (n_ == 0 || m_ == 0)
return X;
116 std::vector<double> ta = ta_, tb = tb_, c(n_ * m_);
117 for (std::size_t i = 0; i < n_; ++i)
118 for (std::size_t j = 0; j < m_; ++j) c[j * n_ + i] = Ct(i, j);
120 const int ni =
static_cast<int>(n_), mi =
static_cast<int>(m_), isgn = 1;
123 lapack::dtrsyl(
"N",
"N", &isgn, &ni, &mi, ta.data(), &ni, tb.data(), &mi, c.data(), &ni,
125 if (info < 0)
throw InputError(
"sylvester_schur: LAPACK dtrsyl rejected an argument");
128 "sylvester_schur: A and -B have common or very close eigenvalues, so the "
129 "Sylvester equation is singular or nearly so and its solution is not determined");
132 "sylvester_schur: LAPACK dtrsyl returned a zero scale factor");
135 for (std::size_t i = 0; i < n_; ++i)
136 for (std::size_t j = 0; j < m_; ++j) Y(i, j) = c[j * n_ + i] / scale;
144 std::vector<double> ta_, tb_;
151 throw InputError(
"sylvester_schur: A and B must be square");
153 throw InputError(
"sylvester_schur: the right-hand side is not conformable");
181 if (A.
cols() != n_ || B.
cols() != m_)
182 throw InputError(
"SylvesterFactor: A and B must be square");
183 if (n_ == 0 || m_ == 0)
return;
184#ifdef LINE_MP_HAVE_LAPACK
185 if constexpr (std::is_same<T, double>::value) {
186 schur_ = std::make_shared<const SchurSylvesterFactor>(A, B);
191 for (std::size_t j = 0; j < m_; ++j)
192 for (std::size_t i = 0; i < n_; ++i) {
193 const std::size_t row = j * n_ + i;
194 for (std::size_t k = 0; k < n_; ++k) LU_(row, j * n_ + k) += A(i, k);
195 for (std::size_t k = 0; k < m_; ++k) LU_(row, k * n_ + i) += B(k, j);
204 if (n_ == 0 || m_ == 0)
return X;
205 if (C.
rows() != n_ || C.
cols() != m_)
206 throw InputError(
"SylvesterFactor: the right-hand side is not conformable");
207#ifdef LINE_MP_HAVE_LAPACK
208 if constexpr (std::is_same<T, double>::value)
return schur_->solve(C);
210 std::vector<T> rhs(n_ * m_, zero);
211 for (std::size_t j = 0; j < m_; ++j)
212 for (std::size_t i = 0; i < n_; ++i) rhs[j * n_ + i] = C(i, j);
214 for (std::size_t j = 0; j < m_; ++j)
215 for (std::size_t i = 0; i < n_; ++i) X(i, j) = rhs[j * n_ + i];
222 for (std::size_t i = 0; i < C.
rows(); ++i)
223 for (std::size_t j = 0; j < C.
cols(); ++j) negC(i, j) = -C(i, j);
230 std::vector<std::size_t> piv_;
231 std::shared_ptr<const SchurSylvesterFactor> schur_;
244 throw InputError(
"sylvester_solve: the blocks are not conformable");
252 throw InputError(
"lyap_solve: the blocks are not conformable");
260 for (std::size_t i = 0; i < C.
rows(); ++i)
261 for (std::size_t j = 0; j < C.
cols(); ++j) negC(i, j) = -C(i, j);
ExternalError(const std::string &what)
NumericError(const std::string &what)
Matrix< double > solve(const Matrix< double > &C) const
Solve A X + X B = C.
SchurSylvesterFactor(const Matrix< double > &A, const Matrix< double > &B)
SylvesterFactor(const Matrix< T > &A, const Matrix< T > &B)
Matrix< T > solve_lyap(const Matrix< T > &C) const
Solve A X + X B + C = 0, MATLAB's lyap(A,B,C).
Matrix< T > solve_sylvester(const Matrix< T > &C) const
Solve A X + X B = C.
Eigenvalues and singular values, backed by LAPACK.
The exception types the port throws.
The five LAPACK routines the port calls, behind one indirection.
Dense linear algebra over the templated number type: products, identity, inverse, and powers.
LU factorization with partial pivoting, templated on the number type.
Dense matrix and non-owning view.
Conservation laws of a layered queueing network, enumerated from its structure.
void lu_solve(const Matrix< T > &LU, const std::vector< std::size_t > &piv, std::vector< T > &b)
Solve LUx = Pb in place on b, using the factors from lu_factor.
std::vector< std::size_t > lu_factor(Matrix< T > &A)
In-place LU of A (n x n).
Matrix< T > lyap_solve(const Matrix< T > &A, const Matrix< T > &B, const Matrix< T > &C)
MATLAB lyap(A,B,C) solves A X + X B + C = 0, i.e.
Matrix< T > matmul(const Matrix< T > &A, const Matrix< T > &B)
Matrix product A B.
Matrix< double > sylvester_schur(const Matrix< double > &A, const Matrix< double > &B, const Matrix< double > &C)
A X + X B = C by Bartels-Stewart, at double; see SchurSylvesterFactor.
Matrix< T > sylvester_solve(const Matrix< T > &A, const Matrix< T > &B, const Matrix< T > &C)
Solve A X + X B = C for X.
RealSchur schur_decomposition(const Matrix< double > &A)
Real Schur factorization of a general square matrix (LAPACK dgees, unsorted).
Matrix< double > lyap_schur(const Matrix< double > &A, const Matrix< double > &B, const Matrix< double > &C)
MATLAB lyap(A,B,C) at double via Bartels-Stewart: A X + X B + C = 0.
Number-type abstraction for the templated API port.
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
Real Schur factorization A = Z T Z^T, with Z orthogonal and T upper quasi-triangular: 1 x 1 diagonal ...
Matrix< double > Z
orthogonal Schur vectors
Matrix< double > T
upper quasi-triangular factor