| 1 | #ifndef _Eigen_h_ |
| 2 | #define _Eigen_h_ |
| 3 | /* Eigen.h |
| 4 | * |
| 5 | * Copyright (C) 1993-2020, 2026 David Weenink |
| 6 | * |
| 7 | * This code is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or (at |
| 10 | * your option) any later version. |
| 11 | * |
| 12 | * This code is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | Eigen represents the eigenvalues and eigenvectors of a square matrix. |
| 23 | If the matrix is symmetric, the eigenvalues and corresponding eigenvectors |
| 24 | are real and sorted in descending order. |
| 25 | If the square matrix is not symmetric than some of the eigenvalues and |
| 26 | eigenvectors are complex. The complex eigenvalues always occur in pairs |
| 27 | a + i b and a - i b, where the eigenvalue with the positive imaginary part always |
| 28 | occurs before the other one in the list. |
| 29 | */ |
| 30 | |
| 31 | #include "Collection.h" |
| 32 | |
| 33 | #include "Eigen_def.h" |
| 34 | #include "Graphics.h" |
| 35 | |
| 36 | #include "MAT_numerics.h" |
| 37 | |
| 38 | void Eigen_init (Eigen me, integer numberOfEigenvalues, integer dimension); |
| 39 | |
| 40 | void Eigen_initImaginaryParts (Eigen me); |
| 41 | |
| 42 | autoEigen Eigen_create (integer numberOfEigenvalues, integer dimension); |
| 43 | |
| 44 | autoEigen Eigen_createFromSquareRoot (constMATVU const& mat, integer numberOfEigenvalues, bool sortAscending); |
| 45 | |
| 46 | void Eigen_initFromSquareMAT (Eigen me, constMATVU const& mat, kMAT_TYPE matType, integer numberOfEigenvalues, bool sortAscending); |
| 47 | autoEigen Eigen_createFromSquareMAT (constMATVU const& mat, kMAT_TYPE matType, integer numberOfEigenvalues, bool sortAscending); |
| 48 | |
| 49 | |
| 50 | void Eigen_initFromSymmetricMatrix (Eigen me, constMATVU const& a); |
| 51 | |
| 52 | void Eigen_initFromSymmetricTridiagonal (Eigen me, constVEC const& diagonal, constVEC const& offDiagonal, bool sortAscending); |
| 53 | |
| 54 | void Eigen_initFromSquareRoot (Eigen me, constMATVU const& a); |
| 55 | /* |
| 56 | Calculate eigenstructure for symmetric matrix A'A (e.g. covariance matrix), |
| 57 | when only A is given. |
| 58 | Precondition: numberOfRows > 1 |
| 59 | Method: SVD. |
| 60 | */ |
| 61 | |
| 62 | void Eigen_initFromSquareRootPair (Eigen me, constMAT a, constMAT b); |
| 63 | /* |
| 64 | Calculate eigenstructure for A'Ax - lambda B'Bx = 0 |
| 65 | Preconditions: numberOfRows >= numberOfColumns && |
| 66 | numberOfRows_b >= numberOfColumns |
| 67 | Method: Generalized SVD. |
| 68 | */ |
| 69 | |
| 70 | integer Eigen_getNumberOfEigenvectors (Eigen me); |
| 71 | |
| 72 | integer Eigen_getDimensionOfComponents (Eigen me); |
| 73 | |
| 74 | double Eigen_getCumulativeContributionOfComponents (Eigen me, integer from, integer to); |
| 75 | |
| 76 | integer Eigen_getDimensionOfFraction (Eigen me, double fraction); |
| 77 | |
| 78 | double Eigen_getEigenvectorElement (Eigen me, integer ivec, integer element); |
| 79 | |
| 80 | autoVEC Eigen_listEigenvalues (Eigen me); |
| 81 | autoVEC Eigen_listEigenvalues_imag (Eigen me); |
| 82 | |
| 83 | autoVEC Eigen_getEigenvector (Eigen me, integer ivec); |
| 84 | autoVEC Eigen_getEigenvector_imag (Eigen me, integer ivec); |
| 85 | |
| 86 | double Eigen_getSumOfEigenvalues (Eigen me, integer from, integer to); |
| 87 | |
| 88 | inline bool Eigen_areAllEigenvaluesReal (Eigen me) { |
| 89 | return my onlyReals; |
| 90 | } |
| 91 | |
| 92 | void Eigen_sort (Eigen me, bool sortAscending); |
| 93 | /* |
| 94 | Sort eigenvalues and corresponding eigenvectors. |
| 95 | All eigenvalues have to be real! |
| 96 | */ |
| 97 | |
| 98 | void Eigen_sort_special (Eigen me, bool sortAscending); |
| 99 | /* |
| 100 | Precondition: eigenvalues are sorted. |
| 101 | Postcondition: eigenvalues are ascending or descending. |
| 102 | */ |
| 103 | |
| 104 | void Eigen_invertEigenvector (Eigen me, integer ivec); // TODO djmw complex? |
| 105 | |
| 106 | void Eigen_drawEigenvalues (Eigen me, Graphics g, integer first, integer last, double ymin, double ymax, |
| 107 | bool fractionOfTotal, bool cumulative, double size_mm, conststring32 mark, bool garnish); |
| 108 | |
| 109 | void Eigen_drawEigenvector (Eigen me, Graphics g, integer ivec, integer first, integer last, double minimum, double maximum, bool weigh, |
| 110 | double size_mm, conststring32 mark, bool connect, char32 **rowLabels, bool garnish); |
| 111 | /* |
| 112 | Draw eigenvector. When rowLabels != nullptr, draw row text labels on bottom axis. |
| 113 | */ |
| 114 | |
| 115 | /** |
| 116 | Adapt the sign of each eigenvector except the first |
| 117 | in such a way that it correlates positively with the first eigenvector. |
| 118 | */ |
| 119 | void Eigens_alignEigenvectors (OrderedOf<structEigen>* me); |
| 120 | |
| 121 | double Eigens_getAngleBetweenEigenplanes_degrees (Eigen me, Eigen thee); |
| 122 | /* |
| 123 | Get angle between the eigenplanes, spanned by the first two eigenvectors. |
| 124 | */ |
| 125 | |
| 126 | #endif /* _Eigen_h_ */ |
| 127 |