Search code examples
c++eigeneigen3

Whether does Eigen comma initializer accept 0 vector?


Eigen::VectorXi a, b, aAndb;
a.resize(10);
b.resize(0);
aAndb.resize(10);    

aAndb << a, b;

Please read the above code. Basically, I have a vector 'a' which length is 10 and a vector 'b' which length is 0. When I use them to create aAndb, it gives me an assertion failure in CommaInitializer class destructor. However, if 'b''s length is larger than 0, there is no error. I'm using Eigen 3.2.9. Is this a correct response from Eigen or because my usage is wrong?


Solution

  • The comma initializer creates side by side column.

    // From Eigen 3.2.9
    /* inserts a matrix expression in the target matrix */
    template<typename OtherDerived>
    CommaInitializer& operator,(const DenseBase<OtherDerived>& other)
    {
      if(other.rows()==0)
      {
        m_col += other.cols();
        return *this;
      }
      ...
    

    From the patch linked in Peter's answer

    template<typename OtherDerived>
    CommaInitializer& operator,(const DenseBase<OtherDerived>& other)
    {
    +    if(other.cols()==0 || other.rows()==0)
    +      return *this;
         if (m_col==m_xpr.cols())
    

    Changed in the dev branch (as well as in the 3.1 & 3.2 branches, but not in 3.2.9):

    /* inserts a matrix expression in the target matrix */
    template<typename OtherDerived>
    EIGEN_DEVICE_FUNC
    CommaInitializer& operator,(const DenseBase<OtherDerived>& other)
    {
        if (m_col==m_xpr.cols() && (other.cols()!=0 || other.rows()!=m_currentBlockRows))
        {
           m_row+=m_currentBlockRows;
           m_col = 0;
           m_currentBlockRows = other.rows();
           eigen_assert(m_row+m_currentBlockRows<=m_xpr.rows()
             && "Too many rows passed to comma initializer (operator<<)");
        }
    

    This was addressed here (Christoph's comment).