eigen_example.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. ///////////////////////////////////////////////////////////////
  2. // Copyright 2018 John Maddock. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
  5. //[eigen_eg
  6. #include <iostream>
  7. #include <boost/multiprecision/cpp_complex.hpp>
  8. #include <boost/multiprecision/eigen.hpp>
  9. #include <Eigen/Dense>
  10. int main()
  11. {
  12. using namespace Eigen;
  13. typedef boost::multiprecision::cpp_complex_quad complex_type;
  14. //
  15. // We want to solve Ax = b for x,
  16. // define A and b first:
  17. //
  18. Matrix<complex_type, 2, 2> A, b;
  19. A << complex_type(2, 3), complex_type(-1, -2), complex_type(-1, -4), complex_type(3, 6);
  20. b << 1, 2, 3, 1;
  21. std::cout << "Here is the matrix A:\n" << A << std::endl;
  22. std::cout << "Here is the right hand side b:\n" << b << std::endl;
  23. //
  24. // Solve for x:
  25. //
  26. Matrix<complex_type, 2, 2> x = A.fullPivHouseholderQr().solve(b);
  27. std::cout << "The solution is:\n" << x << std::endl;
  28. //
  29. // Compute the error in the solution by using the norms of Ax - b and b:
  30. //
  31. complex_type::value_type relative_error = (A*x - b).norm() / b.norm();
  32. std::cout << "The relative error is: " << relative_error << std::endl;
  33. return 0;
  34. }
  35. //]
  36. /*
  37. //[eigen_out
  38. Here is the matrix A:
  39. (2,3) (-1,-2)
  40. (-1,-4) (3,6)
  41. Here is the right hand side b:
  42. 1 2
  43. 3 1
  44. The solution is:
  45. (0.6,-0.6) (0.7,-0.7)
  46. (0.64,-0.68) (0.58,-0.46)
  47. The relative error is: 2.63132e-34
  48. //]
  49. */