1

What is the simplest way to replace all Eigen::MatrixXds and Eigen::VectorXds with Vectors and Matrices that have long double elements?

Every basic floating point variable in my code is of type long double. Also, everytime I use a matrix or vector, I use the following typedefs.

typedef Eigen::VectorXd Vec;
typedef Eigen::MatrixXd Mat;

What's the best thing to switch these typedefs to? What happens if I leave them as they are?

Taylor
  • 1,797
  • 4
  • 26
  • 51
  • The more important question is however, do you really need `long double`? It might significantly slow down all your calculations because most processors are built to do `double` arithmetic fast. Also it might be that your compiler just ignores `long double` and uses `double` instead. Read the [Wikipedia article](https://en.wikipedia.org/wiki/Long_double) and think about it again. – Henri Menke Jul 02 '17 at 22:54

1 Answers1

5

Simply define your own typedefs based on Eigen's own global matrix typedefs.

If you use Eigen::MatrixXd and fill it with elements of type long double, those values will be narrowed to fit into the double elements of the matrix, which results in a loss of precision or, in the worst case, overflow errors. However, on many architectures double-precision floating point arithmetic is done in 80-bit extended precision, so the results may be the same. You surely shouldn't rely on this! For more see, e.g., long double vs double.

#include <Eigen/Core>

typedef Eigen::Matrix< long double, Eigen::Dynamic, 1              > Vec;
typedef Eigen::Matrix< long double, Eigen::Dynamic, Eigen::Dynamic > Mat;

int main()
{
  long double ld = 2;

  Mat m(1,1);
  m(0,0) = ld;
}
Henri Menke
  • 10,705
  • 1
  • 24
  • 42
  • thanks. I saw that link and was wondering about the difference between `long double` as the first template parameter and `std::complex.' And I'll look into that other thing thanks – Taylor Jul 02 '17 at 23:38