title: c++ Eigen making copy?
body: <p>Let A be a symmetric matrix and let v be a vector. I extract from A a block of n columns starting from j and multiply it by v using </p>
  
  <pre><code>VectorXd a;
  a = A.middleCols(j,n).selfadjointView&lt;Lower&gt;() * v // does not compile
  </code></pre>
  
  <p>As this does not compile, whereas this</p>
  
  <pre><code>a = MatrixXd(A.middleCols(j,n).selfadjointView&lt;Lower&gt;()) * v
  </code></pre>
  
  <p>does, I am wondering whether the second version makes a copy of </p>
  
  <pre><code>A.middleCols(j,n).selfadjointView&lt;Lower&gt;()  
  </code></pre>
  
  <p>or performs the computation directly?</p>
  
  <p>Thanks for any hint.</p>
  
  <p><strong>EDIT:</strong> I suspect the problem has something to do with argument types, as I get the error : </p>
  
  <pre><code>invalid argument type 'typename ConstSelfAdjointViewReturnType.... to unary expression' 
  </code></pre>
  
  <p>Indeed, A is the argument of a function passed by const reference using either of </p>
  
  <pre><code>const MatrixXd&amp; A 
  const Ref&lt;const MatrixXd&gt;&amp; A
  </code></pre>
  
  <p>Here is an example:</p>
  
  <pre><code>// this version doesn't compile
  MatrixXd returnSymmetricMatrix(const MatrixXd&amp; A, const VectorXd&amp; v, const MatrixXd&amp; B){
  // B is a symmetric matrix
  
  VectorXd a;
  a = A.middleCols(3, 4).selfadjointView&lt;Lower&gt;() * v;
  MatrixXd M(code_fill(){...}); 
  // code_fill is the function filling the lower triangular part of a symmetric matrix
  M.block(1, 2, 3, 4).triangularView&lt;Lower&gt;() += B.selfadjointView&lt;Lower&gt;();
  
  return M;
  }
  
  // this version compiles
  MatrixXd returnSymmetricMatrix(const MatrixXd&amp; A, const VectorXd&amp; v, const MatrixXd&amp; B){
  // B is a symmetric matrix
  
  VectorXd a;
  a = MatrixXd(A.middleCols(3, 4).selfadjointView&lt;Lower&gt;()) * v;
  MatrixXd M(code_fill(){...}); 
  // code_fill is the function filling the lower triangular part of a symmetric matrix
  Matrix(M.block(1, 2, 3, 4).triangularView&lt;Lower&gt;()) += B.selfadjointView&lt;Lower&gt;();
  
  return M;
  }
  </code></pre>
  
  <p><strong>EDIT2</strong> Regarding my initial question and the example I've added at the Edit section, I am a bit confused regarding the copying. As I understand the difference between the working and the non-working versions, the line </p>
  
  <pre><code>Matrix(M.block(1, 2, 3, 4).triangularView&lt;Lower&gt;()) += B.selfadjointView&lt;Lower&gt;();  
  </code></pre>
  
  <p>works because its lhs tells to Eigen that M.block(1, 2, 3, 4).triangularView() is actually a matrix rather than a reference to a matrix. Otherwise, the operator += would through an error that this operator is not overloaded for .block(). So my original question is whether Matrix(...) only tells that it's a Matrix to enable the computation, or rather copy the ... into a Matrix? Thanks!</p>