0

I am trying to write a function that will read data from a binary file into an Eigen matrix. It is not possible to know the type of data to be read, at compile time. As a consequence, I need a way to change the data type of an Eigen matrix at compile time. As far as i know, there is no way to convert an existing eigen matrix into a different type.

How can I do this?

What can I give into my function, so that upon it's return, there will be an eigen matrix of the binary data?

Is it maybe possible to declare a generic matrix, like Eigen::PlainObjectBase Data; that will remain without instantiation and be instantiated inside the function?

yizzlez
  • 8,757
  • 4
  • 29
  • 44
  • Just instanciate it with a `double`; this gives you 53 bits of precision, so unless you're dealing with integers larger than 53 bits you're not loosing any information by loading an integer into `double` floats. Your file reader then should simply convert whatever there is in the file into doubles. – datenwolf Jul 06 '15 at 17:12

1 Answers1

0

How can I do this?

If possible, record the data type with the object. Perhaps overkill, but take a look at SQLite and try storing your data as a TEXT entry for the type and a BLOB for the binary data. This will allow you to determine the type by simply querying the database for it.

I have not used Eigen before, but if you have no other choice (perhaps you have a lot of existing binary data) try creating several matrices of different types and attempt to fit the data to them. If that works, you could create an object with pointers to these data types as members, a member for what type the matrix is, and just ask the object for the reference when needed.

What can I give into my function, so that upon its return, there will be an Eigen matrix of the binary data?

You should give the function at least

  • location of the binary data
  • type of the binary data (if possible)
  • a pointer to reference the Eigen matrix (or object containing it!)

Is it maybe possible to declare a generic matrix, like Eigen::PlainObjectBase Data; that will remain without instantiation and be instantiated inside the function?

Yes, ideally you'll be able to create a pointer and just reference it elsewhere.

You can use a class to contain your matrix and just reference your class elsewhere. You won't be able to use templating such as Eigen::PlainObjectBase during runtime without unpleasant hacks. If you're a masochist, you could even pre-allocate "enough space" and determine what you've stuffed into it.

Community
  • 1
  • 1
ti7
  • 16,375
  • 6
  • 40
  • 68