Substitutions for Eigen::MatrixXd typedefs

。_饼干妹妹 提交于 2019-12-31 04:38:09

问题


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?


回答1:


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;
}


来源:https://stackoverflow.com/questions/44875345/substitutions-for-eigenmatrixxd-typedefs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!