File indexing completed on 2026-09-28 09:20:50
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef _MathLin_Jacobi_HeaderFile
0015 #define _MathLin_Jacobi_HeaderFile
0016
0017 #include <MathUtils_Types.hxx>
0018 #include <MathUtils_Config.hxx>
0019 #include <math_Recipes.hxx>
0020 #include <MathUtils_Core.hxx>
0021
0022 #include <cmath>
0023 #include <algorithm>
0024
0025 namespace MathLin
0026 {
0027 using namespace MathUtils;
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051 inline EigenResult Jacobi(const math_Matrix& theA, bool theSortDescending = true)
0052 {
0053 EigenResult aResult;
0054
0055 const int aRowLower = theA.LowerRow();
0056 const int aRowUpper = theA.UpperRow();
0057 const int aColLower = theA.LowerCol();
0058 const int aColUpper = theA.UpperCol();
0059 const int aN = aRowUpper - aRowLower + 1;
0060
0061
0062 if (aColUpper - aColLower + 1 != aN)
0063 {
0064 aResult.Status = Status::InvalidInput;
0065 return aResult;
0066 }
0067
0068
0069 math_Matrix aWorkA(1, aN, 1, aN);
0070 math_Vector aEigenVals(1, aN);
0071 math_Matrix aEigenVecs(1, aN, 1, aN);
0072
0073
0074 for (int i = aRowLower; i <= aRowUpper; ++i)
0075 {
0076 for (int j = aColLower; j <= aColUpper; ++j)
0077 {
0078 aWorkA(i - aRowLower + 1, j - aColLower + 1) = theA(i, j);
0079 }
0080 }
0081
0082
0083 int aNbRotations = 0;
0084 if (::Jacobi(aWorkA, aEigenVals, aEigenVecs, aNbRotations) != 0)
0085 {
0086 aResult.Status = Status::NotConverged;
0087 return aResult;
0088 }
0089
0090 aResult.NbIterations = static_cast<size_t>(aNbRotations);
0091
0092
0093 if (theSortDescending && aN > 1)
0094 {
0095
0096 for (int i = 1; i < aN; ++i)
0097 {
0098 int aMaxIdx = i;
0099 double aMaxVal = aEigenVals(i);
0100
0101 for (int j = i + 1; j <= aN; ++j)
0102 {
0103 if (aEigenVals(j) > aMaxVal)
0104 {
0105 aMaxVal = aEigenVals(j);
0106 aMaxIdx = j;
0107 }
0108 }
0109
0110 if (aMaxIdx != i)
0111 {
0112
0113 std::swap(aEigenVals(i), aEigenVals(aMaxIdx));
0114
0115
0116 for (int k = 1; k <= aN; ++k)
0117 {
0118 std::swap(aEigenVecs(k, i), aEigenVecs(k, aMaxIdx));
0119 }
0120 }
0121 }
0122 }
0123
0124
0125 aResult.EigenValues = math_Vector(aRowLower, aRowUpper);
0126 for (int i = 1; i <= aN; ++i)
0127 {
0128 (*aResult.EigenValues)(aRowLower + i - 1) = aEigenVals(i);
0129 }
0130
0131 aResult.EigenVectors = math_Matrix(aRowLower, aRowUpper, aColLower, aColUpper);
0132 for (int i = 1; i <= aN; ++i)
0133 {
0134 for (int j = 1; j <= aN; ++j)
0135 {
0136 (*aResult.EigenVectors)(aRowLower + i - 1, aColLower + j - 1) = aEigenVecs(i, j);
0137 }
0138 }
0139
0140 aResult.Status = Status::OK;
0141 return aResult;
0142 }
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152 inline EigenResult EigenValues(const math_Matrix& theA, bool theSortDescending = true)
0153 {
0154
0155
0156 return Jacobi(theA, theSortDescending);
0157 }
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169 inline EigenResult SpectralDecomposition(const math_Matrix& theA)
0170 {
0171 return Jacobi(theA, false);
0172 }
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182 inline std::optional<math_Matrix> MatrixPower(const math_Matrix& theA, double thePower)
0183 {
0184 EigenResult aEigen = Jacobi(theA, false);
0185 if (!aEigen.IsDone())
0186 {
0187 return std::nullopt;
0188 }
0189
0190 const math_Vector& aD = *aEigen.EigenValues;
0191 const math_Matrix& aV = *aEigen.EigenVectors;
0192
0193 const int aLower = aD.Lower();
0194 const int aUpper = aD.Upper();
0195
0196
0197 math_Vector aDp(aLower, aUpper);
0198 for (int i = aLower; i <= aUpper; ++i)
0199 {
0200 if (aD(i) < 0.0 && thePower != std::floor(thePower))
0201 {
0202
0203 return std::nullopt;
0204 }
0205 aDp(i) = (aD(i) >= 0.0) ? std::pow(aD(i), thePower) : std::pow(-aD(i), thePower);
0206 if (aD(i) < 0.0 && static_cast<int>(thePower) % 2 != 0)
0207 {
0208 aDp(i) = -aDp(i);
0209 }
0210 }
0211
0212
0213 math_Matrix aResult(aLower, aUpper, aLower, aUpper, 0.0);
0214 for (int i = aLower; i <= aUpper; ++i)
0215 {
0216 for (int j = aLower; j <= aUpper; ++j)
0217 {
0218 double aSum = 0.0;
0219 for (int k = aLower; k <= aUpper; ++k)
0220 {
0221 aSum += aV(i, k) * aDp(k) * aV(j, k);
0222 }
0223 aResult(i, j) = aSum;
0224 }
0225 }
0226
0227 return aResult;
0228 }
0229
0230
0231
0232
0233
0234 inline std::optional<math_Matrix> MatrixSqrt(const math_Matrix& theA)
0235 {
0236 return MatrixPower(theA, 0.5);
0237 }
0238
0239
0240
0241
0242
0243 inline std::optional<math_Matrix> MatrixInvSqrt(const math_Matrix& theA)
0244 {
0245 return MatrixPower(theA, -0.5);
0246 }
0247
0248 }
0249
0250 #endif