title: Assertion failure multiplying Eigen matrices
body: <p>I am writing a c++ program for least square leaner regression problem in interpolation.  I use Eigen for matrix operations.  The problem I am getting is when I run the program it shows an error displaying an assertion error.  Here is my code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;Eigen/Dense&gt;
  using Eigen::MatrixXd;
  using namespace std;
  int main()
  {
      int i;
      int nmbrOfPoints;
      cout&lt;&lt;" Enter the number of data points : ";
      cin&gt;&gt;nmbrOfPoints;
  
      MatrixXd matY(nmbrOfPoints,1);       //initialize matrix Y
      MatrixXd matX(nmbrOfPoints,2);       //initialize matrix X
      MatrixXd matXdup(nmbrOfPoints,2);      //initialize matrix X duplicate
      MatrixXd matAns(2,1);
  
  
      for(i=0;i&lt;nmbrOfPoints;i++)
      {
          matX(i,0)=1;                    // storing the 1 st column of the matrix x, all 1s.
          matXdup(i,0)=1;
      }
  
      cout&lt;&lt;"Enter all sample points (x and y values ): "&lt;&lt;endl;
  
      for(i=0;i&lt;nmbrOfPoints;i++)
      {
          cin&gt;&gt;matX(i,1)&gt;&gt;matY(i,0); // read both (x,f(x)) ,, store x values to matrix x and y values to matrix y
      }
  
      for(i=0;i&lt;nmbrOfPoints;i++)
      {
          matXdup(i,1)=matX(i,1);    //copying matrix x to its duplicate
      }
  
      cout&lt;&lt;"\n \n";
      cout &lt;&lt; matX &lt;&lt; endl;
      cout&lt;&lt;"\n \n";
      cout &lt;&lt; matY &lt;&lt; endl;
      cout&lt;&lt;"\n \n";
      cout &lt;&lt; matXdup &lt;&lt; endl;
  
      // find the transpose of matrix x
  
      cout &lt;&lt; "\nHere is the transposed matrix x duplicate:\n" &lt;&lt; endl;
      matXdup.transposeInPlace();
  
  
      cout &lt;&lt; matXdup &lt;&lt; endl;
      cout&lt;&lt;"\n \n";
      cout &lt;&lt; matX &lt;&lt; endl;
  
      //find the multiplication of x and transpose of x
  
      matX = matX* matXdup;   // now the matrix x holds the multiplication of transpose of x and x
  
      cout &lt;&lt; "\nmultiplication of x and xdup:\n" &lt;&lt; endl;
      cout &lt;&lt; matX &lt;&lt; endl;
      cout&lt;&lt;"\n \n";
  
      //find the inverse of x
  
      double q,a,b,c,d;
  
      a=matX(0,0);
      b=matX(0,1);
      c=matX(1,0);
      d=matX(1,1);
  
      q=1/((a*d)-(b*c));
  
      matX(0,0) = d*q;
      matX(0,1) = b*-1*q;             //now matrix x holds the inverse of x
      matX(1,0) = c*-1*q;
      matX(1,1) = a*q;
  
      cout&lt;&lt;"\n \n";
      cout &lt;&lt; "\n inverse of x:\n" &lt;&lt; endl;
      cout &lt;&lt; matX &lt;&lt; endl;
  
      //find the multiplication of transpose of x(x duplicate matrix) and y
  
       matY = matXdup* matY;   // now the matrix x duplicate holds the multiplication of y and x transpose
  
      //find the multiplication of x(inverse of xt*x) and matXdup (xt*y)
  
      // matAns = matY* matX;
  
       cout &lt;&lt; "\nfinal answers :\n" &lt;&lt; endl;
       cout &lt;&lt; "\n *********************:\n" &lt;&lt; endl;
  
       cout &lt;&lt; matY &lt;&lt; endl;
       cout&lt;&lt;"\n \n";
       cout &lt;&lt; matX &lt;&lt; endl;
  
       cout &lt;&lt; "\nfinal answer FINAL :\n" &lt;&lt; endl;
       cout &lt;&lt; "\n *********************:\n" &lt;&lt; endl;
       matAns = matY* matX;
       cout &lt;&lt; matAns &lt;&lt; endl;
  
       /*cout&lt;&lt;"\n matx dup = \n";
       cout &lt;&lt; matXdup &lt;&lt; endl;
       cout&lt;&lt;"\n maty =  \n";
       cout &lt;&lt; matY &lt;&lt; endl;
       cout&lt;&lt;"\n \n";*/
  
       return 0;
  
  
  }
  </code></pre>
  
  <p>I am getting the error from the final multiplication part which is <code>matAns = matY* matX</code>:</p>
  
  <pre>Assertion failed: a_lhs.cols() == a_rhs.rows() && "invalid matrix product" && "if you wanted a coeff-wise or a dot product use the respective explicit functions"</pre>
  
  <p>When I remove that statement code works. Up to that point the code works fine. Can someone explain me what is assertion problem and how to fix it here?</p>