-
Notifications
You must be signed in to change notification settings - Fork 0
shifted QR algorithm implementation for computing roots given coefficients #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pasquale90
wants to merge
16
commits into
master
Choose a base branch
from
coef2roots
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ebc5334
add prime implementation for calculating roots from coefficients base…
pasquale90 1241d52
improve extract roots method, symplifying it and making it more reada…
pasquale90 6f78561
fix typo in CoefficientsToRoots::extractRoots method
pasquale90 e1fcfe8
implement updating of the filter state on coefficient edits
pasquale90 fba0a71
fix 4 bugs all at once.
pasquale90 f0dc2f2
minor improvements and fixes:
pasquale90 2e9d98b
supress gcc warnings
pasquale90 6cd4ac6
implement rayleigh quotient shift to speed up QR convergence
pasquale90 bfb04e3
fix 3 bugs:
pasquale90 307dd97
2 fixes:
pasquale90 5332c6d
add documentation on CoefficientsToRoots class and change method name…
pasquale90 b2d6f3a
clean print statements
pasquale90 50f60fe
add tests for Coefficients2Roots::QR function (order computation only…
pasquale90 e75f2ae
2 significant performance improvements applied (optimization and micr…
pasquale90 f4bb673
apply micro-opt by replacing divisions with multiplications a) on the…
pasquale90 445fdd6
skip updating of the filterstate in case that computed poles are viol…
pasquale90 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,243 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // #pragma once | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // #include <juce_audio_processors/juce_audio_processors.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // #include <juce_dsp/juce_dsp.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include "CoefficientsToRoots.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <algorithm> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <cmath> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::vector<std::pair<c128, int>> CoefficientsToRoots::QR(std::vector<double> coefs) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // filter out leading zeros, increasing counter until the leading 1.0 is found. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| size_t degree = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while( degree < coefs.size() && coefs[degree] != 1.0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| degree++; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| degree = coefs.size() - degree -1; // subtract 1 for the leading 1.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // filter out trailing zeros increasing the order of root at Zero (usually for default poles) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| size_t orderAtZero = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for( int i = static_cast<int>(coefs.size()-1); i >= 0 && std::abs(coefs[(size_t)i]) == 0.0 ; --i) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ++orderAtZero; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| --degree; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // append root at zero of "orderAtZero" order | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::vector<std::pair<c128, int>> roots; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (orderAtZero>0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| roots.emplace_back(std::make_pair(0.0, orderAtZero)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (degree == 0 ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Returing root at (0,0) if any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return roots; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (degree==1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Returing 1 non-zero root + root at (0,0) if any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| roots.emplace_back( std::make_pair(static_cast<c128>(-coefs[coefs.size() - 1]), 1) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return roots; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // build companion Matrix | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::vector<double> A(degree * degree, 0.0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Build companion matrix (Hessenberg form) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (size_t i=0; i< degree; i++) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| size_t j = i+1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| size_t coef_idx = coefs.size()-1 - orderAtZero -i; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| A[i * degree + (degree-1)] = -coefs[coef_idx]; // fill last column with negative vals of coefs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (i!=degree-1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| A[j * degree + i] = 1.0; // fill subdiagonal entries | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // QR algorithm | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::vector<double> Q(degree * degree); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::vector<double> R(degree * degree); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| size_t shift_idx {degree}, iter {0}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::vector<double> v(shift_idx); // vector for storing current column of A. Note: only the first {0 to (curr val of shift_idx - 1) } indexes are used per iteration. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while(shift_idx > 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (++iter > degree * MaxIterations) break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Reset R,Q | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (size_t r = 0; r < shift_idx; ++r) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (size_t c = 0; c < shift_idx; ++c) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| R[r * degree + c] = 0.0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Q[r * degree + c] = 0.0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // subtract rayleigh quotient shift | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const double shift = A[(shift_idx - 1) * degree + (shift_idx - 1)]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (size_t i = 0; i < shift_idx; ++i) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| A[i * degree + i] -= shift; // Decompose (Ak - sI) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // step 1 - calculate Q : Q = A[col] - projections onto the previous Q[col] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (size_t col = 0 ; col < shift_idx; col++) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // set v with column A[col] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (size_t row = 0 ; row< shift_idx; row++) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| v[row] = A[row * degree + col]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // subtract projection : v[col] = A[col] - proj onto the previous (<col) orthonormal colums of Q. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (size_t curr_col = 0; curr_col < col; curr_col++) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // compute dot product ... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| double dot_product {0.0}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (size_t curr_row = 0; curr_row < shift_idx; ++curr_row) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dot_product += Q[curr_row * degree + curr_col] * A[curr_row * degree + col]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // .. and subtract it from the current column vector | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (size_t curr_row=0; curr_row < shift_idx; ++curr_row) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| v[curr_row] -= dot_product * Q[curr_row * degree + curr_col]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // normalize column of q | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| double norm = 0.0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (size_t k = 0; k < shift_idx; ++k) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| norm += v[k] * v[k]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| double normReciprocal = 1.0 / std::sqrt(norm); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (size_t k = 0; k < shift_idx; ++k) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Q[k * degree + col] = v[k] * normReciprocal; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // step 2 - calculate R : R = Q A | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (size_t row = 0; row < shift_idx; row++) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (size_t col = 0; col < shift_idx; col++) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| double sum {0.0}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (size_t inner = 0; inner < shift_idx; inner++) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sum += (Q[inner * degree + row] * A[inner * degree + col]); // Q transposed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| R[row* degree + col] = sum; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // step 3 - A = R Q | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (size_t row = 0; row < shift_idx; row++) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (size_t col = 0; col < shift_idx; col++) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| double sum {0.0}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (size_t inner = 0; inner< shift_idx; ++inner) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sum += R[row * degree + inner] * Q[inner * degree + col]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| A[row* degree + col] = sum; // resetting A[row][col] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // step 4 add shift back in A and check sub-diagonal entries | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (size_t i = 0; i < shift_idx; ++i) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| A[i * degree + i] += shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // check only the last subdiagonal entry (real eigenvalue) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (std::abs(A[(shift_idx-1)* degree + (shift_idx-2)]) < Epsilon) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| shift_idx--; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // check the entry above the 2x2 block in search of complex conjugate pairs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else if (shift_idx > 2 && std::abs(A[(shift_idx-2) * degree + (shift_idx-3)]) < Epsilon) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| shift_idx -= 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Extract roots — read diagonal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| extractRoots(roots, A, degree); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return roots; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void CoefficientsToRoots::extractRoots(std::vector<std::pair<c128, int>> & roots, const std::vector<double>& M, size_t degree) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| auto addRoot = [&](c128 newVal) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (auto& [val, order] : roots) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| double diff_re = std::abs(val.real() - newVal.real()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| double diff_im = std::abs(val.imag() - newVal.imag()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| double scale = std::abs(val) + std::abs(newVal) + 1e-7; // + 1e-7 to avoid division with zero | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (diff_re / scale < tolerance && diff_im / scale < tolerance) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| order++; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| roots.emplace_back(newVal, 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| size_t i = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while (i < degree) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( i == degree - 1 || std::abs(M[(i+1) * degree + i]) < Epsilon ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Real eigenvalue on diagonal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| c128 newRoot (M[i * degree + i], 0.0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| addRoot(newRoot); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ++i; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // check if conjugate by solving the equation which instantiates by the 2x2 cell: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // https://www.physicsforums.com/threads/how-do-i-estimate-complex-eigenvalues.170108/post-1330547 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // https://www.mosismath.com/Eigenvalues/EigenvalsQR.html | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // det(A - λI) = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // => det| a-λ b | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // | c d-λ | = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // => (a-λ)(d-λ) - bc = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // => λ^2 - (a+d)λ + (ad-bc) = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // => λ^2 - (a+d)λ + detA = 0 --> solve with discriminant | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const double a = M[i * degree + i]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const double b = M[i * degree + i+1]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const double c = M[(i+1) * degree + i]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const double d = M[(i+1) * degree + i+1]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const double det = a*d - b*c; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // solve with discriminant | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const double discriminant = (a+d)*(a+d) - 4.0*det; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const double halfSum = 0.5 * (a + d); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const double halfSqrt = 0.5 * std::sqrt(std::abs(discriminant)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( discriminant >= 0.0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // tow real roots | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| addRoot(c128(halfSum + halfSqrt, 0.0)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| addRoot(c128(halfSum - halfSqrt, 0.0)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Complex conjugate pair | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const double re = halfSum; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const double im = halfSqrt; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| addRoot(c128(re,im)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // addRoot(c128(re,-im)); // Note: this is added automatically later using FilterState::add method. Commenting this, removes the bug of overlapping roots. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+226
to
+239
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed as mentioned here #3 (comment) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| i += 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.