1*bfa7851aSJames Wright // Copyright (c) 2017-2023, Lawrence Livermore National Security, LLC and other CEED contributors. 2*bfa7851aSJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3*bfa7851aSJames Wright // 4*bfa7851aSJames Wright // SPDX-License-Identifier: BSD-2-Clause 5*bfa7851aSJames Wright // 6*bfa7851aSJames Wright // This file is part of CEED: http://github.com/ceed 7*bfa7851aSJames Wright 8*bfa7851aSJames Wright /// @file 9*bfa7851aSJames Wright /// Eigen system solver for symmetric NxN matrices. Modified from the CC0 code provided at https://github.com/jewettaij/jacobi_pd 10*bfa7851aSJames Wright 11*bfa7851aSJames Wright #ifndef utils_eigensolver_jacobi_h 12*bfa7851aSJames Wright #define utils_eigensolver_jacobi_h 13*bfa7851aSJames Wright 14*bfa7851aSJames Wright #include <ceed.h> 15*bfa7851aSJames Wright #include <math.h> 16*bfa7851aSJames Wright 17*bfa7851aSJames Wright #include "utils.h" 18*bfa7851aSJames Wright 19*bfa7851aSJames Wright // @typedef choose the criteria for sorting eigenvalues and eigenvectors 20*bfa7851aSJames Wright typedef enum eSortCriteria { 21*bfa7851aSJames Wright SORT_NONE, 22*bfa7851aSJames Wright SORT_DECREASING_EVALS, 23*bfa7851aSJames Wright SORT_INCREASING_EVALS, 24*bfa7851aSJames Wright SORT_DECREASING_ABS_EVALS, 25*bfa7851aSJames Wright SORT_INCREASING_ABS_EVALS 26*bfa7851aSJames Wright } SortCriteria; 27*bfa7851aSJames Wright 28*bfa7851aSJames Wright ///@brief Find the off-diagonal index in row i whose absolute value is largest 29*bfa7851aSJames Wright /// 30*bfa7851aSJames Wright /// @param[in] *A matrix 31*bfa7851aSJames Wright /// @param[in] i row index 32*bfa7851aSJames Wright /// @returns Index of absolute largest off-diagonal element in row i 33*bfa7851aSJames Wright CEED_QFUNCTION_HELPER CeedInt MaxEntryRow(const CeedScalar *A, CeedInt N, CeedInt i) { 34*bfa7851aSJames Wright CeedInt j_max = i + 1; 35*bfa7851aSJames Wright for (CeedInt j = i + 2; j < N; j++) 36*bfa7851aSJames Wright if (fabs(A[i * N + j]) > fabs(A[i * N + j_max])) j_max = j; 37*bfa7851aSJames Wright return j_max; 38*bfa7851aSJames Wright } 39*bfa7851aSJames Wright 40*bfa7851aSJames Wright /// @brief Find the indices (i_max, j_max) marking the location of the 41*bfa7851aSJames Wright /// entry in the matrix with the largest absolute value. This 42*bfa7851aSJames Wright /// uses the max_idx_row[] array to find the answer in O(n) time. 43*bfa7851aSJames Wright /// 44*bfa7851aSJames Wright /// @param[in] *A matrix 45*bfa7851aSJames Wright /// @param[inout] i_max row index 46*bfa7851aSJames Wright /// @param[inout] j_max column index 47*bfa7851aSJames Wright CEED_QFUNCTION_HELPER void MaxEntry(const CeedScalar *A, CeedInt N, CeedInt *max_idx_row, CeedInt *i_max, CeedInt *j_max) { 48*bfa7851aSJames Wright *i_max = 0; 49*bfa7851aSJames Wright *j_max = max_idx_row[*i_max]; 50*bfa7851aSJames Wright CeedScalar max_entry = fabs(A[*i_max * N + *j_max]); 51*bfa7851aSJames Wright for (CeedInt i = 1; i < N - 1; i++) { 52*bfa7851aSJames Wright CeedInt j = max_idx_row[i]; 53*bfa7851aSJames Wright if (fabs(A[i * N + j]) > max_entry) { 54*bfa7851aSJames Wright max_entry = fabs(A[i * N + j]); 55*bfa7851aSJames Wright *i_max = i; 56*bfa7851aSJames Wright *j_max = j; 57*bfa7851aSJames Wright } 58*bfa7851aSJames Wright } 59*bfa7851aSJames Wright } 60*bfa7851aSJames Wright 61*bfa7851aSJames Wright /// @brief Calculate the components of a rotation matrix which performs a 62*bfa7851aSJames Wright /// rotation in the i,j plane by an angle (θ) that (when multiplied on 63*bfa7851aSJames Wright /// both sides) will zero the ij'th element of A, so that afterwards 64*bfa7851aSJames Wright /// A[i][j] = 0. The results will be stored in c, s, and t 65*bfa7851aSJames Wright /// (which store cos(θ), sin(θ), and tan(θ), respectively). 66*bfa7851aSJames Wright /// 67*bfa7851aSJames Wright /// @param[in] *A matrix 68*bfa7851aSJames Wright /// @param[in] i row index 69*bfa7851aSJames Wright /// @param[in] j column index 70*bfa7851aSJames Wright CEED_QFUNCTION_HELPER void CalcRot(const CeedScalar *A, CeedInt N, CeedInt i, CeedInt j, CeedScalar *rotmat_cst) { 71*bfa7851aSJames Wright rotmat_cst[2] = 1.0; // = tan(θ) 72*bfa7851aSJames Wright CeedScalar A_jj_ii = (A[j * N + j] - A[i * N + i]); 73*bfa7851aSJames Wright if (A_jj_ii != 0.0) { 74*bfa7851aSJames Wright // kappa = (A[j][j] - A[i][i]) / (2*A[i][j]) 75*bfa7851aSJames Wright CeedScalar kappa = A_jj_ii; 76*bfa7851aSJames Wright rotmat_cst[2] = 0.0; 77*bfa7851aSJames Wright CeedScalar A_ij = A[i * N + j]; 78*bfa7851aSJames Wright if (A_ij != 0.0) { 79*bfa7851aSJames Wright kappa /= (2.0 * A_ij); 80*bfa7851aSJames Wright // t satisfies: t^2 + 2*t*kappa - 1 = 0 81*bfa7851aSJames Wright // (choose the root which has the smaller absolute value) 82*bfa7851aSJames Wright rotmat_cst[2] = 1.0 / (sqrt(1 + kappa * kappa) + fabs(kappa)); 83*bfa7851aSJames Wright if (kappa < 0.0) rotmat_cst[2] = -rotmat_cst[2]; 84*bfa7851aSJames Wright } 85*bfa7851aSJames Wright } 86*bfa7851aSJames Wright rotmat_cst[0] = 1.0 / sqrt(1 + rotmat_cst[2] * rotmat_cst[2]); 87*bfa7851aSJames Wright rotmat_cst[1] = rotmat_cst[0] * rotmat_cst[2]; 88*bfa7851aSJames Wright } 89*bfa7851aSJames Wright 90*bfa7851aSJames Wright /// @brief Perform a similarity transformation by multiplying matrix A on both 91*bfa7851aSJames Wright /// sides by a rotation matrix (and its transpose) to eliminate A[i][j]. 92*bfa7851aSJames Wright /// @details This rotation matrix performs a rotation in the i,j plane by 93*bfa7851aSJames Wright /// angle θ. This function assumes that c=cos(θ). s=sin(θ), t=tan(θ) 94*bfa7851aSJames Wright /// have been calculated in advance (using the CalcRot() function). 95*bfa7851aSJames Wright /// It also assumes that i<j. The max_idx_row[] array is also updated. 96*bfa7851aSJames Wright /// To save time, since the matrix is symmetric, the elements 97*bfa7851aSJames Wright /// below the diagonal (ie. A[u][v] where u>v) are not computed. 98*bfa7851aSJames Wright /// @verbatim 99*bfa7851aSJames Wright /// A' = R^T * A * R 100*bfa7851aSJames Wright /// where R the rotation in the i,j plane and ^T denotes the transpose. 101*bfa7851aSJames Wright /// i j 102*bfa7851aSJames Wright /// _ _ 103*bfa7851aSJames Wright /// | 1 | 104*bfa7851aSJames Wright /// | . | 105*bfa7851aSJames Wright /// | . | 106*bfa7851aSJames Wright /// | 1 | 107*bfa7851aSJames Wright /// | c ... s | 108*bfa7851aSJames Wright /// | . . . | 109*bfa7851aSJames Wright /// R = | . 1 . | 110*bfa7851aSJames Wright /// | . . . | 111*bfa7851aSJames Wright /// | -s ... c | 112*bfa7851aSJames Wright /// | 1 | 113*bfa7851aSJames Wright /// | . | 114*bfa7851aSJames Wright /// | . | 115*bfa7851aSJames Wright /// |_ 1 _| 116*bfa7851aSJames Wright /// @endverbatim 117*bfa7851aSJames Wright /// 118*bfa7851aSJames Wright /// Let A' denote the matrix A after multiplication by R^T and R. 119*bfa7851aSJames Wright /// The components of A' are: 120*bfa7851aSJames Wright /// 121*bfa7851aSJames Wright /// @verbatim 122*bfa7851aSJames Wright /// A'_uv = Σ_w Σ_z R_wu * A_wz * R_zv 123*bfa7851aSJames Wright /// @endverbatim 124*bfa7851aSJames Wright /// 125*bfa7851aSJames Wright /// Note that a the rotation at location i,j will modify all of the matrix 126*bfa7851aSJames Wright /// elements containing at least one index which is either i or j 127*bfa7851aSJames Wright /// such as: A[w][i], A[i][w], A[w][j], A[j][w]. 128*bfa7851aSJames Wright /// Check and see whether these modified matrix elements exceed the 129*bfa7851aSJames Wright /// corresponding values in max_idx_row[] array for that row. 130*bfa7851aSJames Wright /// If so, then update max_idx_row for that row. 131*bfa7851aSJames Wright /// This is somewhat complicated by the fact that we must only consider 132*bfa7851aSJames Wright /// matrix elements in the upper-right triangle strictly above the diagonal. 133*bfa7851aSJames Wright /// (ie. matrix elements whose second index is > the first index). 134*bfa7851aSJames Wright /// The modified elements we must consider are marked with an "X" below: 135*bfa7851aSJames Wright /// 136*bfa7851aSJames Wright /// @verbatim 137*bfa7851aSJames Wright /// i j 138*bfa7851aSJames Wright /// _ _ 139*bfa7851aSJames Wright /// | . X X | 140*bfa7851aSJames Wright /// | . X X | 141*bfa7851aSJames Wright /// | . X X | 142*bfa7851aSJames Wright /// | . X X | 143*bfa7851aSJames Wright /// | X X X X X 0 X X X X | i 144*bfa7851aSJames Wright /// | . X | 145*bfa7851aSJames Wright /// | . X | 146*bfa7851aSJames Wright /// A = | . X | 147*bfa7851aSJames Wright /// | . X | 148*bfa7851aSJames Wright /// | X X X X X | j 149*bfa7851aSJames Wright /// | . | 150*bfa7851aSJames Wright /// | . | 151*bfa7851aSJames Wright /// | . | 152*bfa7851aSJames Wright /// |_ . _| 153*bfa7851aSJames Wright /// @endverbatim 154*bfa7851aSJames Wright /// 155*bfa7851aSJames Wright /// @param[in] *A matrix 156*bfa7851aSJames Wright /// @param[in] i row index 157*bfa7851aSJames Wright /// @param[in] j column index 158*bfa7851aSJames Wright CEED_QFUNCTION_HELPER void ApplyRot(CeedScalar *A, CeedInt N, CeedInt i, CeedInt j, CeedInt *max_idx_row, CeedScalar *rotmat_cst) { 159*bfa7851aSJames Wright // Compute the diagonal elements of A which have changed: 160*bfa7851aSJames Wright A[i * N + i] -= rotmat_cst[2] * A[i * N + j]; 161*bfa7851aSJames Wright A[j * N + j] += rotmat_cst[2] * A[i * N + j]; 162*bfa7851aSJames Wright // Note: This is algebraically equivalent to: 163*bfa7851aSJames Wright // A[i][i] = c*c*A[i][i] + s*s*A[j][j] - 2*s*c*A[i][j] 164*bfa7851aSJames Wright // A[j][j] = s*s*A[i][i] + c*c*A[j][j] + 2*s*c*A[i][j] 165*bfa7851aSJames Wright 166*bfa7851aSJames Wright // Update the off-diagonal elements of A which will change (above the diagonal) 167*bfa7851aSJames Wright 168*bfa7851aSJames Wright A[i * N + j] = 0.0; 169*bfa7851aSJames Wright 170*bfa7851aSJames Wright // compute A[w][i] and A[i][w] for all w!=i,considering above-diagonal elements 171*bfa7851aSJames Wright for (CeedInt w = 0; w < i; w++) { // 0 <= w < i < j < N 172*bfa7851aSJames Wright A[i * N + w] = A[w * N + i]; // backup the previous value. store below diagonal (i>w) 173*bfa7851aSJames Wright A[w * N + i] = rotmat_cst[0] * A[w * N + i] - rotmat_cst[1] * A[w * N + j]; // A[w][i], A[w][j] from previous iteration 174*bfa7851aSJames Wright if (i == max_idx_row[w]) max_idx_row[w] = MaxEntryRow(A, N, w); 175*bfa7851aSJames Wright else if (fabs(A[w * N + i]) > fabs(A[w * N + max_idx_row[w]])) max_idx_row[w] = i; 176*bfa7851aSJames Wright } 177*bfa7851aSJames Wright for (CeedInt w = i + 1; w < j; w++) { // 0 <= i < w < j < N 178*bfa7851aSJames Wright A[w * N + i] = A[i * N + w]; // backup the previous value. store below diagonal (w>i) 179*bfa7851aSJames Wright A[i * N + w] = rotmat_cst[0] * A[i * N + w] - rotmat_cst[1] * A[w * N + j]; // A[i][w], A[w][j] from previous iteration 180*bfa7851aSJames Wright } 181*bfa7851aSJames Wright for (CeedInt w = j + 1; w < N; w++) { // 0 <= i < j+1 <= w < N 182*bfa7851aSJames Wright A[w * N + i] = A[i * N + w]; // backup the previous value. store below diagonal (w>i) 183*bfa7851aSJames Wright A[i * N + w] = rotmat_cst[0] * A[i * N + w] - rotmat_cst[1] * A[j * N + w]; // A[i][w], A[j][w] from previous iteration 184*bfa7851aSJames Wright } 185*bfa7851aSJames Wright 186*bfa7851aSJames Wright // now that we're done modifying row i, we can update max_idx_row[i] 187*bfa7851aSJames Wright max_idx_row[i] = MaxEntryRow(A, N, i); 188*bfa7851aSJames Wright 189*bfa7851aSJames Wright // compute A[w][j] and A[j][w] for all w!=j,considering above-diagonal elements 190*bfa7851aSJames Wright for (CeedInt w = 0; w < i; w++) { // 0 <= w < i < j < N 191*bfa7851aSJames Wright A[w * N + j] = rotmat_cst[1] * A[i * N + w] + rotmat_cst[0] * A[w * N + j]; // A[i][w], A[w][j] from previous iteration 192*bfa7851aSJames Wright if (j == max_idx_row[w]) max_idx_row[w] = MaxEntryRow(A, N, w); 193*bfa7851aSJames Wright else if (fabs(A[w * N + j]) > fabs(A[w * N + max_idx_row[w]])) max_idx_row[w] = j; 194*bfa7851aSJames Wright } 195*bfa7851aSJames Wright for (CeedInt w = i + 1; w < j; w++) { // 0 <= i+1 <= w < j < N 196*bfa7851aSJames Wright A[w * N + j] = rotmat_cst[1] * A[w * N + i] + rotmat_cst[0] * A[w * N + j]; // A[w][i], A[w][j] from previous iteration 197*bfa7851aSJames Wright if (j == max_idx_row[w]) max_idx_row[w] = MaxEntryRow(A, N, w); 198*bfa7851aSJames Wright else if (fabs(A[w * N + j]) > fabs(A[w * N + max_idx_row[w]])) max_idx_row[w] = j; 199*bfa7851aSJames Wright } 200*bfa7851aSJames Wright for (CeedInt w = j + 1; w < N; w++) { // 0 <= i < j < w < N 201*bfa7851aSJames Wright A[j * N + w] = rotmat_cst[1] * A[w * N + i] + rotmat_cst[0] * A[j * N + w]; // A[w][i], A[j][w] from previous iteration 202*bfa7851aSJames Wright } 203*bfa7851aSJames Wright // now that we're done modifying row j, we can update max_idx_row[j] 204*bfa7851aSJames Wright max_idx_row[j] = MaxEntryRow(A, N, j); 205*bfa7851aSJames Wright } 206*bfa7851aSJames Wright 207*bfa7851aSJames Wright ///@brief Multiply matrix A on the LEFT side by a transposed rotation matrix R^T 208*bfa7851aSJames Wright /// This matrix performs a rotation in the i,j plane by angle θ (where 209*bfa7851aSJames Wright /// the arguments "s" and "c" refer to cos(θ) and sin(θ), respectively). 210*bfa7851aSJames Wright /// @verbatim 211*bfa7851aSJames Wright /// A'_uv = Σ_w R_wu * A_wv 212*bfa7851aSJames Wright /// @endverbatim 213*bfa7851aSJames Wright /// 214*bfa7851aSJames Wright /// @param[in] *A matrix 215*bfa7851aSJames Wright /// @param[in] i row index 216*bfa7851aSJames Wright /// @param[in] j column index 217*bfa7851aSJames Wright CEED_QFUNCTION_HELPER void ApplyRotLeft(CeedScalar *A, CeedInt N, CeedInt i, CeedInt j, CeedScalar *rotmat_cst) { 218*bfa7851aSJames Wright // Recall that c = cos(θ) and s = sin(θ) 219*bfa7851aSJames Wright for (CeedInt v = 0; v < N; v++) { 220*bfa7851aSJames Wright CeedScalar Aiv = A[i * N + v]; 221*bfa7851aSJames Wright A[i * N + v] = rotmat_cst[0] * A[i * N + v] - rotmat_cst[1] * A[j * N + v]; 222*bfa7851aSJames Wright A[j * N + v] = rotmat_cst[1] * Aiv + rotmat_cst[0] * A[j * N + v]; 223*bfa7851aSJames Wright } 224*bfa7851aSJames Wright } 225*bfa7851aSJames Wright 226*bfa7851aSJames Wright /// @brief Sort the rows in evec according to the numbers in v (also sorted) 227*bfa7851aSJames Wright /// 228*bfa7851aSJames Wright /// @param[inout] *eval vector containing the keys used for sorting 229*bfa7851aSJames Wright /// @param[inout] *evec matrix whose rows will be sorted according to v 230*bfa7851aSJames Wright /// @param[in] n size of the vector and matrix 231*bfa7851aSJames Wright /// @param[in] s sort decreasing order? 232*bfa7851aSJames Wright CEED_QFUNCTION_HELPER void SortRows(CeedScalar *eval, CeedScalar *evec, CeedInt N, SortCriteria sort_criteria) { 233*bfa7851aSJames Wright if (sort_criteria == SORT_NONE) return; 234*bfa7851aSJames Wright 235*bfa7851aSJames Wright for (CeedInt i = 0; i < N - 1; i++) { 236*bfa7851aSJames Wright CeedInt i_max = i; 237*bfa7851aSJames Wright for (CeedInt j = i + 1; j < N; j++) { 238*bfa7851aSJames Wright // find the "maximum" element in the array starting at position i+1 239*bfa7851aSJames Wright switch (sort_criteria) { 240*bfa7851aSJames Wright case SORT_DECREASING_EVALS: 241*bfa7851aSJames Wright if (eval[j] > eval[i_max]) i_max = j; 242*bfa7851aSJames Wright break; 243*bfa7851aSJames Wright case SORT_INCREASING_EVALS: 244*bfa7851aSJames Wright if (eval[j] < eval[i_max]) i_max = j; 245*bfa7851aSJames Wright break; 246*bfa7851aSJames Wright case SORT_DECREASING_ABS_EVALS: 247*bfa7851aSJames Wright if (fabs(eval[j]) > fabs(eval[i_max])) i_max = j; 248*bfa7851aSJames Wright break; 249*bfa7851aSJames Wright case SORT_INCREASING_ABS_EVALS: 250*bfa7851aSJames Wright if (fabs(eval[j]) < fabs(eval[i_max])) i_max = j; 251*bfa7851aSJames Wright break; 252*bfa7851aSJames Wright default: 253*bfa7851aSJames Wright break; 254*bfa7851aSJames Wright } 255*bfa7851aSJames Wright } 256*bfa7851aSJames Wright SwapScalar(&eval[i], &eval[i_max]); 257*bfa7851aSJames Wright for (CeedInt k = 0; k < N; k++) SwapScalar(&evec[i * N + k], &evec[i_max * N + k]); 258*bfa7851aSJames Wright } 259*bfa7851aSJames Wright } 260*bfa7851aSJames Wright 261*bfa7851aSJames Wright /// @brief Calculate all the eigenvalues and eigevectors of a symmetric matrix 262*bfa7851aSJames Wright /// using the Jacobi eigenvalue algorithm: 263*bfa7851aSJames Wright /// https://en.wikipedia.org/wiki/Jacobi_eigenvalue_algorithm 264*bfa7851aSJames Wright /// @returns The number of Jacobi iterations attempted, which should be > 0. 265*bfa7851aSJames Wright /// If the return value is not strictly > 0 then convergence failed. 266*bfa7851aSJames Wright /// @note To reduce the computation time further, set calc_evecs=false. 267*bfa7851aSJames Wright /// Additionally, note that the output evecs should be normalized. It 268*bfa7851aSJames Wright /// simply takes the Identity matrix and performs (isometric) rotations 269*bfa7851aSJames Wright /// on it, so divergence from normalized is due to finite-precision 270*bfa7851aSJames Wright /// arithmetic of the rotations. 271*bfa7851aSJames Wright // 272*bfa7851aSJames Wright // @param[in] A the matrix you wish to diagonalize (size NxN) 273*bfa7851aSJames Wright // @param[in] N size of the matrix 274*bfa7851aSJames Wright // @param[out] eval store the eigenvalues here (size N) 275*bfa7851aSJames Wright // @param[out] evec store the eigenvectors here (in rows, size NxN) 276*bfa7851aSJames Wright // @param[out] max_idx_row work vector of size N-1 277*bfa7851aSJames Wright // @param[in] sort_criteria sort results? 278*bfa7851aSJames Wright // @param[in] calc_evecs calculate the eigenvectors? 279*bfa7851aSJames Wright // @param[in] max_num_sweeps maximum number of iterations = max_num_sweeps * number of off-diagonals (N*(N-1)/2) 280*bfa7851aSJames Wright CEED_QFUNCTION_HELPER CeedInt Diagonalize(CeedScalar *A, CeedInt N, CeedScalar *eval, CeedScalar *evec, CeedInt *max_idx_row, 281*bfa7851aSJames Wright SortCriteria sort_criteria, bool calc_evec, const CeedInt max_num_sweeps) { 282*bfa7851aSJames Wright CeedScalar rotmat_cst[3] = {0.}; // cos(θ), sin(θ), and tan(θ), 283*bfa7851aSJames Wright 284*bfa7851aSJames Wright if (calc_evec) 285*bfa7851aSJames Wright for (CeedInt i = 0; i < N; i++) 286*bfa7851aSJames Wright for (CeedInt j = 0; j < N; j++) evec[i * N + j] = (i == j) ? 1.0 : 0.0; // Set evec equal to the identity matrix 287*bfa7851aSJames Wright 288*bfa7851aSJames Wright for (CeedInt i = 0; i < N - 1; i++) max_idx_row[i] = MaxEntryRow(A, N, i); 289*bfa7851aSJames Wright 290*bfa7851aSJames Wright // -- Iteration -- 291*bfa7851aSJames Wright CeedInt n_iters; 292*bfa7851aSJames Wright CeedInt max_num_iters = max_num_sweeps * N * (N - 1) / 2; 293*bfa7851aSJames Wright for (n_iters = 1; n_iters <= max_num_iters; n_iters++) { 294*bfa7851aSJames Wright CeedInt i, j; 295*bfa7851aSJames Wright MaxEntry(A, N, max_idx_row, &i, &j); 296*bfa7851aSJames Wright 297*bfa7851aSJames Wright // If A[i][j] is small compared to A[i][i] and A[j][j], set it to 0. 298*bfa7851aSJames Wright if ((A[i * N + i] + A[i * N + j] == A[i * N + i]) && (A[j * N + j] + A[i * N + j] == A[j * N + j])) { 299*bfa7851aSJames Wright A[i * N + j] = 0.0; 300*bfa7851aSJames Wright max_idx_row[i] = MaxEntryRow(A, N, i); 301*bfa7851aSJames Wright } 302*bfa7851aSJames Wright 303*bfa7851aSJames Wright if (A[i * N + j] == 0.0) break; 304*bfa7851aSJames Wright 305*bfa7851aSJames Wright CalcRot(A, N, i, j, rotmat_cst); // Calculate the parameters of the rotation matrix. 306*bfa7851aSJames Wright ApplyRot(A, N, i, j, max_idx_row, rotmat_cst); // Apply this rotation to the A matrix. 307*bfa7851aSJames Wright if (calc_evec) ApplyRotLeft(evec, N, i, j, rotmat_cst); 308*bfa7851aSJames Wright } 309*bfa7851aSJames Wright 310*bfa7851aSJames Wright for (CeedInt i = 0; i < N; i++) eval[i] = A[i * N + i]; 311*bfa7851aSJames Wright 312*bfa7851aSJames Wright // Optional: Sort results by eigenvalue. 313*bfa7851aSJames Wright SortRows(eval, evec, N, sort_criteria); 314*bfa7851aSJames Wright 315*bfa7851aSJames Wright if ((n_iters > max_num_iters) && (N > 1)) // If we exceeded max_num_iters, 316*bfa7851aSJames Wright return 0; // indicate an error occured. 317*bfa7851aSJames Wright 318*bfa7851aSJames Wright return n_iters; 319*bfa7851aSJames Wright } 320*bfa7851aSJames Wright 321*bfa7851aSJames Wright // @brief Interface to Diagonalize for 3x3 systems 322*bfa7851aSJames Wright CEED_QFUNCTION_HELPER CeedInt Diagonalize3(CeedScalar A[3][3], CeedScalar eval[3], CeedScalar evec[3][3], CeedInt max_idx_row[3], 323*bfa7851aSJames Wright SortCriteria sort_criteria, bool calc_evec, const CeedInt max_num_sweeps) { 324*bfa7851aSJames Wright return Diagonalize((CeedScalar *)A, 3, (CeedScalar *)eval, (CeedScalar *)evec, (CeedInt *)max_idx_row, sort_criteria, calc_evec, max_num_sweeps); 325*bfa7851aSJames Wright } 326*bfa7851aSJames Wright 327*bfa7851aSJames Wright #endif // utils_eigensolver_jacobi_h 328