47#ifndef LINE_MP_HAVE_LAPACK
50 "eig_values requires LAPACK: reconfigure with -DLINE_MP_USE_LAPACK=ON and liblapack "
53 const std::size_t n = A.
rows();
54 if (A.
cols() != n)
throw InputError(
"eig_values: matrix is not square");
55 if (n == 0)
return {};
58 std::vector<double> a(n * n);
59 for (std::size_t i = 0; i < n; ++i)
60 for (std::size_t j = 0; j < n; ++j) a[j * n + i] = A(i, j);
62 const int ni =
static_cast<int>(n);
63 std::vector<double> wr(n), wi(n);
65 int info = 0, lwork = -1;
68 lapack::dgeev(
"N",
"N", &ni, a.data(), &ni, wr.data(), wi.data(), &vdummy, &one, &vdummy, &one, &wopt,
71 lwork =
static_cast<int>(wopt);
72 std::vector<double> work(
static_cast<std::size_t
>(lwork));
73 lapack::dgeev(
"N",
"N", &ni, a.data(), &ni, wr.data(), wi.data(), &vdummy, &one, &vdummy, &one,
74 work.data(), &lwork, &info);
75 if (info != 0)
throw NumericError(
ErrorKind{
"NonConvergence"},
"eig_values: LAPACK dgeev failed to converge");
77 std::vector<std::complex<double>> out(n);
78 for (std::size_t i = 0; i < n; ++i) out[i] = std::complex<double>(wr[i], wi[i]);
86 for (
const std::complex<double>& z :
eig_values(A)) {
87 const double m = std::abs(z);
99 std::vector<std::complex<double>> e =
eig_values(A);
100 if (e.size() < 2)
return 0.0;
101 double first = 0.0, second = 0.0;
102 for (
const std::complex<double>& z : e) {
103 const double m = std::abs(z);
107 }
else if (m > second) {
116#ifndef LINE_MP_HAVE_LAPACK
119 "svd_values requires LAPACK: reconfigure with -DLINE_MP_USE_LAPACK=ON and liblapack "
122 const std::size_t m = A.
rows(), n = A.
cols();
123 if (m == 0 || n == 0)
return {};
124 std::vector<double> a(m * n);
125 for (std::size_t i = 0; i < m; ++i)
126 for (std::size_t j = 0; j < n; ++j) a[j * m + i] = A(i, j);
128 const int mi =
static_cast<int>(m), nj =
static_cast<int>(n);
129 const std::size_t k = m < n ? m : n;
130 std::vector<double> s(k);
131 double udummy = 0.0, vtdummy = 0.0;
132 int info = 0, lwork = -1;
135 lapack::dgesvd(
"N",
"N", &mi, &nj, a.data(), &mi, s.data(), &udummy, &one, &vtdummy, &one, &wopt,
138 lwork =
static_cast<int>(wopt);
139 std::vector<double> work(
static_cast<std::size_t
>(lwork));
140 lapack::dgesvd(
"N",
"N", &mi, &nj, a.data(), &mi, s.data(), &udummy, &one, &vtdummy, &one, work.data(),
142 if (info != 0)
throw NumericError(
ErrorKind{
"NonConvergence"},
"svd_values: LAPACK dgesvd failed to converge");
170#ifndef LINE_MP_HAVE_LAPACK
173 "schur_decomposition requires LAPACK: reconfigure with -DLINE_MP_USE_LAPACK=ON and "
174 "liblapack available");
176 const std::size_t n = A.
rows();
177 if (A.
cols() != n)
throw InputError(
"schur_decomposition: matrix is not square");
181 if (n == 0)
return out;
183 std::vector<double> a(n * n);
184 for (std::size_t i = 0; i < n; ++i)
185 for (std::size_t j = 0; j < n; ++j) a[j * n + i] = A(i, j);
187 const int ni =
static_cast<int>(n);
188 std::vector<double> wr(n), wi(n), vs(n * n);
189 int sdim = 0, info = 0, lwork = -1;
191 lapack::dgees(
"V",
"N",
nullptr, &ni, a.data(), &ni, &sdim, wr.data(), wi.data(), vs.data(), &ni,
192 &wopt, &lwork,
nullptr, &info);
193 if (info != 0)
throw NumericError(
ErrorKind{
"Lapack"},
"schur_decomposition: LAPACK workspace query failed");
194 lwork =
static_cast<int>(wopt);
195 std::vector<double> work(
static_cast<std::size_t
>(lwork));
196 lapack::dgees(
"V",
"N",
nullptr, &ni, a.data(), &ni, &sdim, wr.data(), wi.data(), vs.data(), &ni,
197 work.data(), &lwork,
nullptr, &info);
198 if (info != 0)
throw NumericError(
ErrorKind{
"NonConvergence"},
"schur_decomposition: LAPACK dgees failed to converge");
200 for (std::size_t i = 0; i < n; ++i)
201 for (std::size_t j = 0; j < n; ++j) {
202 out.
T(i, j) = a[j * n + i];
203 out.
Z(i, j) = vs[j * n + i];
236#ifndef LINE_MP_HAVE_LAPACK
240 "schur_reorder requires LAPACK: reconfigure with -DLINE_MP_USE_LAPACK=ON and liblapack "
243 const std::size_t n = s.
T.
rows();
245 throw InputError(
"schur_reorder: the factors are not square or not conformable");
246 if (key.size() != n)
throw InputError(
"schur_reorder: one key per diagonal entry is required");
250 if (n <= 1)
return out;
253 std::vector<double> t(n * n), q(n * n);
254 for (std::size_t i = 0; i < n; ++i)
255 for (std::size_t j = 0; j < n; ++j) {
256 t[j * n + i] = s.
T(i, j);
257 q[j * n + i] = s.
Z(i, j);
260 std::vector<double> k = key;
262 const int ni =
static_cast<int>(n);
263 std::vector<double> work(n);
264 const double tiny = 0.0;
272 std::size_t best = pos;
273 double bestkey = k[pos];
276 const std::size_t sz = (i + 1 < n && t[i * n + (i + 1)] != tiny) ? 2u : 1u;
277 if (k[i] > bestkey) {
283 const std::size_t bsz =
284 (best + 1 < n && t[best * n + (best + 1)] != tiny) ? 2u : 1u;
286 int ifst =
static_cast<int>(best) + 1;
287 int ilst =
static_cast<int>(pos) + 1;
289 lapack::dtrexc(
"V", &ni, t.data(), &ni, q.data(), &ni, &ifst, &ilst, work.data(), &info);
292 "schur_reorder: dtrexc could not separate two eigenvalues that are too close; "
293 "the requested ordering splits a 2 x 2 block");
297 const std::vector<double> moved(k.begin() +
static_cast<long>(best),
298 k.begin() +
static_cast<long>(best + bsz));
299 k.erase(k.begin() +
static_cast<long>(best),
300 k.begin() +
static_cast<long>(best + bsz));
301 k.insert(k.begin() +
static_cast<long>(pos), moved.begin(), moved.end());
306 for (std::size_t i = 0; i < n; ++i)
307 for (std::size_t j = 0; j < n; ++j) {
308 out.
T(i, j) = t[j * n + i];
309 out.
Z(i, j) = q[j * n + i];
318 if (s.empty())
return 0;
320 const double tol =
static_cast<double>(dim) * 2.220446049250313e-16 * s[0];
ExternalError(const std::string &what)
NumericError(const std::string &what)
The exception types the port throws.
The five LAPACK routines the port calls, behind one indirection.
Dense matrix and non-owning view.
Conservation laws of a layered queueing network, enumerated from its structure.
double subdominant_modulus(const Matrix< double > &A)
Second largest modulus over the spectrum.
double spectral_radius(const Matrix< double > &A)
Largest modulus over the spectrum, i.e.
RealSchur schur_reorder(const RealSchur &s, const std::vector< double > &key)
Reorder the diagonal blocks of a real Schur form into DESCENDING key order, stably,...
std::vector< std::complex< double > > eig_values(const Matrix< double > &A)
Eigenvalues of a general real square matrix, in LAPACK's order.
std::size_t matrix_rank(const Matrix< double > &A)
Numerical rank at the standard max(m,n) eps sigma_1 threshold.
std::vector< double > svd_values(const Matrix< double > &A)
Singular values in descending order.
RealSchur schur_decomposition(const Matrix< double > &A)
Real Schur factorization of a general square matrix (LAPACK dgees, unsorted).
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