Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print Eigen variables in columns?

I have a bunch of data in Eigen::Vector2f variables. I want to print it in columns, but I end up with uneven results like so:

Vec1                  |Vec2                   |Vec3
   1.94609 -0.0863508 |  1.71155 -0.137481    |3.00915
   1.94609 -0.0863508 |1.57448 1.8755 |387.864
   1.94609 -0.0863508 |-0.415677 1.66801      |583.542
2.01589 1.94324       |  1.71155 -0.137481    |433.156
2.01589 1.94324       |1.57448 1.8755 |10.1969
2.01589 1.94324       |-0.415677 1.66801      |303.132
0.00212092 1.966      |  1.71155 -0.137481    |584.061
0.00212092 1.966      |1.57448 1.8755 |124.429
0.00212092 1.966      |-0.415677 1.66801      |17.5172

The print statement I'm using is this:

Eigen::IOFormat fmt(4, 0, ", ", "\n", "", "");
std::cerr << vec1.transpose().format(fmt) << "\t|" << vec2.transpose().format(fmt) << "\t|" << vec3.transpose().format(fmt) << std::endl;

The format statement's precision seems to mean the number of non-zero digits, not decimal places. For instance, if I set the precision to 2, I get numbers like 2, -0.044, and 2.8. And regardless of this, the columns are not aligned.

Any ideas on how to get my columns aligned? Where are all these extra spaces coming from?

like image 717
amo Avatar asked Jul 21 '14 23:07

amo


1 Answers

The scond parameter of the Eigen::IOFormat controls the Column Align settings. But since you are printing a column vector with transpose, it has no effect.

I get the following output with the code shown below:

vec1            |vec2           |vec3
-1      0.13    |-0.61  0.62    |0.17   -0.04
-0.3    0.79    |0.65   0.49    |-0.65  0.72
0.42    0.027   |-0.39  -0.97   |-0.82  -0.27
-0.71   -0.67   |0.98   -0.11   |-0.76  -0.99
-0.98   -0.24   |0.063  0.14    |0.2    0.21
-0.67   0.33    |-0.098 -0.3    |-0.89  0.22
0.57    0.61    |0.04   -0.4    |0.75   0.45
0.91    0.85    |0.079  -0.72   |-0.076 -0.53
0.72    -0.58   |0.56   0.69    |0.99   1
0.22    -0.22   |-0.47  -0.41   |0.68   -0.95

Corresponding code:

#include <iostream>
#include <Eigen/Dense>

using namespace Eigen;

int main() {
  std::cout << "vec1\t" << "\t|" << "vec2\t" << "\t|" << "vec3" << std::endl;
  for (int i = 0; i < 10; ++i) {
    Vector2f vec1, vec2, vec3;
    vec1.setRandom();
    vec2.setRandom();
    vec3.setRandom();

    const IOFormat fmt(2, DontAlignCols, "\t", " ", "", "", "", "");
    std::cout << vec1.transpose().format(fmt) << "\t|" << vec2.transpose().format(fmt) << "\t|" << vec3.transpose().format(fmt) << std::endl;
  }
}
like image 175
iNFINITEi Avatar answered Oct 23 '22 19:10

iNFINITEi