carl  24.04
Computer ARithmetic Library
EigenWrapper.cpp
Go to the documentation of this file.
1 #include "EigenWrapper.h"
2 
3 #include <eigen3/Eigen/Dense>
4 #include <eigen3/Eigen/Eigenvalues>
5 
6 #include <cmath>
7 #include <vector>
8 
9 namespace carl {
10 namespace roots {
11 namespace eigen {
12 
13 std::vector<double> root_approximation(const std::vector<double>& coeffs) {
14 
15  using Index = Eigen::MatrixXd::Index;
16  // Create companion matrix
17  std::size_t degree = coeffs.size() - 1;
18  Eigen::MatrixXd m = Eigen::MatrixXd::Zero(Index(degree + 1), Index(degree + 1));
19  m(0, Index(degree)) = -coeffs[0] / coeffs[degree];
20  for (std::size_t i = 1; i <= degree; ++i) {
21  m(Index(i), Index(i)-1) = 1;
22  m(Index(i), Index(degree)) = -coeffs[i] / coeffs[degree];
23  }
24 
25  // Obtain eigenvalues
26  Eigen::VectorXcd eigenvalues = m.eigenvalues();
27 
28  // Save real parts to tmp
29  std::vector<double> tmp;
30  for (uint i = 0; i < std::size_t(eigenvalues.size()); ++i) {
31  if (!std::isfinite(eigenvalues[Index(i)].real()) || !std::isfinite(eigenvalues[Index(i)].imag())) {
32  continue;
33  }
34  if (eigenvalues[Index(i)].imag() < eigenvalues[Index(i)].real() / 4) {
35  tmp.emplace_back(eigenvalues[Index(i)].real());
36  }
37  }
38  return tmp;
39 }
40 
41 }
42 }
43 }
carl is the main namespace for the library.
std::uint64_t uint
Definition: numbers.h:16
std::vector< double > root_approximation(const std::vector< double > &coeffs)
Compute approximations of the real roots of the univariate polynomials with the given coefficients.