polyroots.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Olga Diamanti <olga.diam@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "polyroots.h"
  9. template <typename S, typename T>
  10. IGL_INLINE void igl::polyRoots(Eigen::Matrix<S, Eigen::Dynamic,1> &polyCoeff, //real or comples coefficients
  11. Eigen::Matrix<std::complex<T>, Eigen::Dynamic,1> &roots // complex roots (double or float)
  12. )
  13. {
  14. // degree
  15. int n = polyCoeff.rows() - 1;
  16. Eigen::Matrix<S, Eigen::Dynamic, 1> d (n,1);
  17. d = polyCoeff.tail(n)/polyCoeff(0);
  18. Eigen::Matrix<S, Eigen::Dynamic, Eigen::Dynamic> I; I.setIdentity(n-1,n-1);
  19. Eigen::Matrix<S, Eigen::Dynamic, 1> z; z.setZero(n-1,1);
  20. Eigen::Matrix<S, Eigen::Dynamic, Eigen::Dynamic> a(n,n);
  21. a<<-d.transpose(),I,z;
  22. roots = a.eigenvalues();
  23. }
  24. #ifndef IGL_HEADER_ONLY
  25. // Explicit template specialization
  26. #endif