GteUnsymmetricEigenvalues.h
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #pragma once
9 
10 #include <LowLevel/GteWrapper.h>
11 #include <algorithm>
12 #include <array>
13 #include <cmath>
14 #include <cstdint>
15 #include <functional>
16 #include <vector>
17 
18 // An implementation of the QR algorithm described in "Matrix Computations,
19 // 2nd edition" by G. H. Golub and C. F. Van Loan, The Johns Hopkins
20 // University Press, Baltimore MD, Fourth Printing 1993. In particular,
21 // the implementation is based on Chapter 7 (The Unsymmetric Eigenvalue
22 // Problem), Section 7.5 (The Practical QR Algorithm).
23 
24 namespace gte
25 {
26 
27 template <typename Real>
29 {
30 public:
31  // The solver processes NxN matrices (not necessarily symmetric), where
32  // N >= 3 ('size' is N) and the matrix is stored in row-major order. The
33  // maximum number of iterations ('maxIterations') must be specified for
34  // reducing an upper Hessenberg matrix to an upper quasi-triangular
35  // matrix (upper triangular matrix of blocks where the diagonal blocks
36  // are 1x1 or 2x2). The goal is to compute the real-valued eigenvalues.
37  UnsymmetricEigenvalues(int32_t size, uint32_t maxIterations);
38 
39  // A copy of the NxN input is made internally. The order of the
40  // eigenvalues is specified by sortType: -1 (decreasing), 0 (no
41  // sorting), or +1 (increasing). When sorted, the eigenvectors are
42  // ordered accordingly. The return value is the number of iterations
43  // consumed when convergence occurred, 0xFFFFFFFF when convergence did
44  // not occur, or 0 when N <= 1 was passed to the constructor.
45  uint32_t Solve(Real const* input, int32_t sortType);
46 
47  // Get the real-valued eigenvalues of the matrix passed to Solve(...).
48  // The input 'eigenvalues' must have at least N elements.
49  void GetEigenvalues(uint32_t& numEigenvalues, Real* eigenvalues) const;
50 
51 private:
52  // 2D accessors to elements of mMatrix[].
53  inline Real const& A(int r, int c) const;
54  inline Real& A(int r, int c);
55 
56  // Compute the Householder vector for (X[rmin],...,x[rmax]). The
57  // input vector is stored in mX in the index range [rmin,rmax]. The
58  // output vector V is stored in mV in the index range [rmin,rmax]. The
59  // scaled vector is S = (-2/Dot(V,V))*V and is stored in mScaledV in
60  // the index range [rmin,rmax].
61  void House(int rmin, int rmax);
62 
63  // Support for replacing matrix A by P^T*A*P, where P is a Householder
64  // reflection computed using House(...).
65  void RowHouse(int rmin, int rmax, int cmin, int cmax);
66  void ColHouse(int rmin, int rmax, int cmin, int cmax);
67 
69  void FrancisQRStep(int rmin, int rmax);
70  bool GetBlock(std::array<int, 2>& block);
71 
72  // The number N of rows and columns of the matrices to be processed.
73  int32_t mSize, mSizeM1;
74 
75  // The maximum number of iterations for reducing the tridiagonal mtarix
76  // to a diagonal matrix.
77  uint32_t mMaxIterations;
78 
79  // The internal copy of a matrix passed to the solver.
80  std::vector<Real> mMatrix; // NxN elements
81 
82  // Temporary storage to compute Householder reflections.
83  std::vector<Real> mX, mV, mScaledV, mW; // N elements
84 
85  // Flags about the zeroness of the subdiagonal entries. This is used
86  // to detect uncoupled submatrices and apply the QR algorithm to the
87  // corresponding subproblems. The storage is padded on both ends with
88  // zeros to avoid additional code logic when packing the eigenvalues
89  // for access by the caller.
90  std::vector<int> mFlagStorage;
92 
94  std::vector<Real> mEigenvalues;
95 };
96 
97 
98 template <typename Real>
100  :
101  mSize(0),
102  mSizeM1(0),
103  mMaxIterations(0),
104  mNumEigenvalues(0)
105 {
106  if (size >= 3 && maxIterations > 0)
107  {
108  mSize = size;
109  mSizeM1 = size - 1;
110  mMaxIterations = maxIterations;
111  mMatrix.resize(size * size);
112  mX.resize(size);
113  mV.resize(size);
114  mScaledV.resize(size);
115  mW.resize(size);
116  mFlagStorage.resize(size + 1);
117  std::fill(mFlagStorage.begin(), mFlagStorage.end(), 0);
119  mEigenvalues.resize(mSize);
120  }
121 }
122 
123 template <typename Real>
124 uint32_t UnsymmetricEigenvalues<Real>::Solve(Real const* input, int32_t sortType)
125 {
126  if (mSize > 0)
127  {
128  std::copy(input, input + mSize * mSize, mMatrix.begin());
130 
131  std::array<int, 2> block;
132  bool found = GetBlock(block);
133  uint32_t numIterations;
134  for (numIterations = 0; numIterations < mMaxIterations; ++numIterations)
135  {
136  if (found)
137  {
138  // Solve the current subproblem.
139  FrancisQRStep(block[0], block[1] + 1);
140 
141  // Find another subproblem (if any).
142  found = GetBlock(block);
143  }
144  else
145  {
146  break;
147  }
148  }
149 
150  // The matrix is fully uncoupled, upper Hessenberg with 1x1 or
151  // 2x2 diagonal blocks. Golub and Van Loan call this "upper
152  // quasi-triangular".
153  mNumEigenvalues = 0;
154  std::fill(mEigenvalues.begin(), mEigenvalues.end(), (Real)0);
155  for (int i = 0; i < mSizeM1; ++i)
156  {
157  if (mSubdiagonalFlag[i] == 0)
158  {
159  if (mSubdiagonalFlag[i - 1] == 0)
160  {
161  // We have a 1x1 block with a real eigenvalue.
162  mEigenvalues[mNumEigenvalues++] = A(i, i);
163  }
164  }
165  else
166  {
167  if (mSubdiagonalFlag[i - 1] == 0 && mSubdiagonalFlag[i + 1] == 0)
168  {
169  // We have a 2x2 block that might have real eigenvalues.
170  Real a00 = A(i, i);
171  Real a01 = A(i, i + 1);
172  Real a10 = A(i + 1, i);
173  Real a11 = A(i + 1, i + 1);
174  Real tr = a00 + a11;
175  Real det = a00 * a11 - a01 * a10;
176  Real halfTr = tr * (Real)0.5;
177  Real discr = halfTr * halfTr - det;
178  if (discr >= (Real)0)
179  {
180  Real rootDiscr = sqrt(discr);
181  mEigenvalues[mNumEigenvalues++] = halfTr - rootDiscr;
182  mEigenvalues[mNumEigenvalues++] = halfTr + rootDiscr;
183  }
184  }
185  // else:
186  // The QR iteration failed to converge at this block. It
187  // must also be the case that numIterations == mMaxIterations.
188  // TODO: The caller will be aware of this when testing the
189  // returned numIterations. Is there a remedy for such a
190  // case? (This happened with root finding using the
191  // companion matrix of a polynomial.)
192  }
193  }
194 
195  if (sortType != 0 && mNumEigenvalues > 1)
196  {
197  if (sortType > 0)
198  {
199  std::sort(mEigenvalues.begin(), mEigenvalues.begin() + mNumEigenvalues,
200  std::less<Real>());
201  }
202  else
203  {
204  std::sort(mEigenvalues.begin(), mEigenvalues.begin() + mNumEigenvalues,
205  std::greater<Real>());
206  }
207  }
208 
209  return numIterations;
210  }
211  return 0;
212 }
213 
214 template <typename Real>
215 void UnsymmetricEigenvalues<Real>::GetEigenvalues(uint32_t& numEigenvalues, Real* eigenvalues) const
216 {
217  if (mSize > 0)
218  {
219  numEigenvalues = mNumEigenvalues;
220  Memcpy(eigenvalues, mEigenvalues.data(), numEigenvalues * sizeof(Real));
221  }
222  else
223  {
224  numEigenvalues = 0;
225  }
226 }
227 
228 template <typename Real>
229 inline Real const& UnsymmetricEigenvalues<Real>::A(int r, int c) const
230 {
231  return mMatrix[c + r * mSize];
232 }
233 
234 template <typename Real>
235 inline Real& UnsymmetricEigenvalues<Real>::A(int r, int c)
236 {
237  return mMatrix[c + r * mSize];
238 }
239 
240 template <typename Real>
241 void UnsymmetricEigenvalues<Real>::House(int rmin, int rmax)
242 {
243  Real length = (Real)0;
244  for (int r = rmin; r <= rmax; ++r)
245  {
246  length += mX[r] * mX[r];
247  }
248  length = sqrt(length);
249  if (length != (Real)0)
250  {
251  Real sign = (mX[rmin] >= (Real)0 ? (Real)1 : (Real)-1);
252  Real invDenom = ((Real)1) / (mX[rmin] + sign * length);
253  for (int r = rmin + 1; r <= rmax; ++r)
254  {
255  mV[r] = mX[r] * invDenom;
256  }
257  }
258  mV[rmin] = (Real)1;
259 
260  Real dot = (Real)1;
261  for (int r = rmin + 1; r <= rmax; ++r)
262  {
263  dot += mV[r] * mV[r];
264  }
265  Real scale = ((Real)-2) / dot;
266  for (int r = rmin; r <= rmax; ++r)
267  {
268  mScaledV[r] = scale * mV[r];
269  }
270 }
271 
272 template <typename Real>
273 void UnsymmetricEigenvalues<Real>::RowHouse(int rmin, int rmax, int cmin, int cmax)
274 {
275  for (int c = cmin; c <= cmax; ++c)
276  {
277  mW[c] = (Real)0;
278  for (int r = rmin; r <= rmax; ++r)
279  {
280  mW[c] += mScaledV[r] * A(r, c);
281  }
282  }
283 
284  for (int r = rmin; r <= rmax; ++r)
285  {
286  for (int c = cmin; c <= cmax; ++c)
287  {
288  A(r, c) += mV[r] * mW[c];
289  }
290  }
291 }
292 
293 template <typename Real>
294 void UnsymmetricEigenvalues<Real>::ColHouse(int rmin, int rmax, int cmin, int cmax)
295 {
296  for (int r = rmin; r <= rmax; ++r)
297  {
298  mW[r] = (Real)0;
299  for (int c = cmin; c <= cmax; ++c)
300  {
301  mW[r] += mScaledV[c] * A(r, c);
302  }
303  }
304 
305  for (int r = rmin; r <= rmax; ++r)
306  {
307  for (int c = cmin; c <= cmax; ++c)
308  {
309  A(r, c) += mW[r] * mV[c];
310  }
311  }
312 }
313 
314 template <typename Real>
316 {
317  for (int c = 0, cp1 = 1; c <= mSize - 3; ++c, ++cp1)
318  {
319  for (int r = cp1; r <= mSizeM1; ++r)
320  {
321  mX[r] = A(r, c);
322  }
323 
324  House(cp1, mSizeM1);
325  RowHouse(cp1, mSizeM1, c, mSizeM1);
326  ColHouse(0, mSizeM1, cp1, mSizeM1);
327  }
328 }
329 
330 template <typename Real>
332 {
333  // Apply the double implicit shift step.
334  int const i0 = rmax - 1, i1 = rmax;
335  Real a00 = A(i0, i0);
336  Real a01 = A(i0, i1);
337  Real a10 = A(i1, i0);
338  Real a11 = A(i1, i1);
339  Real tr = a00 + a11;
340  Real det = a00 * a11 - a01 * a10;
341 
342  int const j0 = rmin, j1 = j0 + 1, j2 = j1 + 1;
343  Real b00 = A(j0, j0);
344  Real b01 = A(j0, j1);
345  Real b10 = A(j1, j0);
346  Real b11 = A(j1, j1);
347  Real b21 = A(j2, j1);
348  mX[rmin] = b00 * (b00 - tr) + b01 * b10 + det;
349  mX[rmin + 1] = b10 * (b00 + b11 - tr);
350  mX[rmin + 2] = b10 * b21;
351 
352  House(rmin, rmin + 2);
353  RowHouse(rmin, rmin + 2, rmin, rmax);
354  ColHouse(rmin, std::min(rmax, rmin + 3), rmin, rmin + 2);
355 
356  // Apply Householder reflections to restore the matrix to upper
357  // Hessenberg form.
358  for (int c = 0, cp1 = 1; c <= mSize - 3; ++c, ++cp1)
359  {
360  int kmax = std::min(cp1 + 2, mSizeM1);
361  for (int r = cp1; r <= kmax; ++r)
362  {
363  mX[r] = A(r, c);
364  }
365 
366  House(cp1, kmax);
367  RowHouse(cp1, kmax, c, mSizeM1);
368  ColHouse(0, mSizeM1, cp1, kmax);
369  }
370 }
371 
372 template <typename Real>
373 bool UnsymmetricEigenvalues<Real>::GetBlock(std::array<int, 2>& block)
374 {
375  for (int i = 0; i < mSizeM1; ++i)
376  {
377  Real a00 = A(i, i);
378  Real a11 = A(i + 1, i + 1);
379  Real a21 = A(i + 1, i);
380  Real sum0 = a00 + a11;
381  Real sum1 = sum0 + a21;
382  mSubdiagonalFlag[i] = (sum1 != sum0 ? 1 : 0);
383  }
384 
385  for (int i = 0; i < mSizeM1; ++i)
386  {
387  if (mSubdiagonalFlag[i] == 1)
388  {
389  block = { i, -1 };
390  while (i < mSizeM1 && mSubdiagonalFlag[i] == 1)
391  {
392  block[1] = i++;
393  }
394  if (block[1] != block[0])
395  {
396  return true;
397  }
398  }
399  }
400  return false;
401 }
402 
403 }
GLenum GLenum GLenum GLenum GLenum scale
Definition: glext.h:9914
Real const & A(int r, int c) const
UnsymmetricEigenvalues(int32_t size, uint32_t maxIterations)
bool GetBlock(std::array< int, 2 > &block)
GLsizeiptr size
Definition: glcorearb.h:659
void FrancisQRStep(int rmin, int rmax)
const GLubyte * c
Definition: glext.h:11671
void GetEigenvalues(uint32_t &numEigenvalues, Real *eigenvalues) const
void RowHouse(int rmin, int rmax, int cmin, int cmax)
void ColHouse(int rmin, int rmax, int cmin, int cmax)
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:790
GLboolean r
Definition: glcorearb.h:1217
GLenum GLenum GLenum input
Definition: glext.h:9913
uint32_t Solve(Real const *input, int32_t sortType)
void Memcpy(void *target, void const *source, size_t count)
Definition: GteWrapper.cpp:16


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:01