Re: [eigen] Default input operator? [Bug 622]

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


On 16.05.2014 12:16, Helmut Jarausch wrote:
we need a decision about the properties of a default input operator.

Thanks for pushing this. We also have not found a solution how to work off our increasing bug-list ...


   Eigen::MatrixXd A(3,3);
   // ...
   std::istringstream Inp(Out.str());
   Inp >> A;                          // we need this one

This is actually already a debatable point:
Shall this work, if A is not sized correctly before streaming?
I agree that if we ignore this option and only allow whitespace as separators, an implementation is really straight-forward.

Generally, I would consider non-fixed input useful. That would require
 * a temporary storage while loading the coefficients --
   or a two-pass strategy, first determining the size than reading it
   (which, OTOH, would require a buffered input stream)
 * some specification of valid element and row separators and
   end-of-matrix (EOM) markers
 * alternatively, specify the matrix size by matching brackets,
   parentheses or braces.
 * For matrices like Matrix<double, Fixed, Dynamic> and vice versa
   (important special case are row and column vectors) it would be
   sufficient to just specify an EOL or EOM marker

The most generic solution I can think of is to have an IFormat struct similar to (or to be joined with) our IOFormat struct which has
 * a flag if the matrix size is to be adapted according to the input
 * a set of valid coefficient-separators (e.g. ',')
 * a set of valid row-separators (e.g. '\n' or ';') this might require
   ignoring some whitespace during coefficient separation
 * a set of valid end-of-matrix markers (e.g. EOF, '\0', '$')
 * alternatively, a set of valid matching bracket pairs, e.g. "()", "[]"
   this could be used for row separation as well as matrix separation.

As for the syntax, I see two alternatives (we could allow both):

  IOFormat iof(...);
  MatrixXd A;

  cin >> A.format(iof); // sharing our current output syntax
  // or
  cin >> iof >> A; // cin >> iof must return a proxy stream class,
  cin >> iof; cin >> A; // This would not work!

Also, what shall happen if the input does not form a valid matrix? Default C++ IO behavior would be to simply mark the stream a bad() and silently return.

The simplest solution would be to ignore all this and require the user to correctly resize the matrix before streaming (If I understood Helmut correctly, this would be sufficient for him). As a compromise I would not mind to allow only one single character as separator or just one bracket pair, i.e. do not allow to input both "{[1 2][3 4]}" and "[1,2;3,4]" with the same IOFormat object. The advantage would be that we somehow could use the same IOFormat object for both input and output.


Christoph


--
----------------------------------------------
Dipl.-Inf., Dipl.-Math. Christoph Hertzberg
Cartesium 0.049
Universität Bremen
Enrique-Schmidt-Straße 5
28359 Bremen

Tel: +49 (421) 218-64252
----------------------------------------------



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/