title: Building Compile-Time Matrix Expressions
body: <p>I've just started using Eigen and have read from their documentation that the best performance comes from their lazy evaluation of matrix expressions. Hence, expressions like this are very efficient once evaluated:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>Eigen::Matrix&lt;float, 3, 1&gt; a;
  a &lt;&lt; 0, 1, 2;
  
  Eigen::Matrix&lt;float, 3, 1&gt; b;
  b &lt;&lt; 3, 4, 5;
  
  Eigen::Matrix&lt;float, 3, 1&gt; c;
  c &lt;&lt; (a + b).sum(),
       (a - b).sum(),
       a.sum();
  
  std::cout &lt;&lt; c &lt;&lt; std::endl;
  </code></pre>
  
  <p>I ran into a problem when constructing matrices whose column count depends on a template parameter. For example:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>template &lt;std::size_t w&gt;
  auto buildGradient() {
    Eigen::Matrix&lt;float, 3, w&gt; matrix;
    matrix &lt;&lt; /* ? */;
    return matrix;
  }
  </code></pre>
  
  <p>My first inclination was to use a recursive c++ template do do this.</p>
  
  <pre class="lang-cpp prettyprint-override"><code>template &lt;std::size_t w, typename Functor&gt;
  auto buildGradientExpr(Functor functor) {
    if constexpr (w == 0) {
      return;
    } else if constexpr (w == 1) {
      return functor();
    } else {
      return functor(), buildGradientExpr&lt;w - 1, Functor&gt;(functor);
    }
  }
  </code></pre>
  
  <p>But using this leads to runtime errors issued by Eigen, since the expression only has one initializer.</p>
  
  <pre class="lang-cpp prettyprint-override"><code>template &lt;std::size_t w&gt;
  auto buildGradient() {
    Eigen::Matrix&lt;float, 3, w&gt; gradient;
    /* Emits an error about too few coefficients being passed to the initializer. */
    gradient &lt;&lt; buildGradientExpr&lt;w&gt;([]() { /* Return 3x1 matrix */ });
    return gradient;
  }
  </code></pre>
  
  <p>Here's a complete runnable example.</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;Eigen/Dense&gt;
  
  #include &lt;iostream&gt;
  
  #include &lt;cstddef&gt;
  
  namespace {
  
  template &lt;std::size_t w, typename Functor&gt;
  auto buildGradientExpr(Functor functor) {
    if constexpr (w == 0) {
      return;
    } else if constexpr (w == 1) {
      return functor(w);
    } else {
      return functor(w), buildGradientExpr&lt;w - 1, Functor&gt;(functor);
    }
  }
  
  template &lt;std::size_t w, typename Functor&gt;
  auto buildGradient(Functor functor) {
    Eigen::Matrix&lt;float, 3, w&gt; gradient;
    gradient &lt;&lt; buildGradientExpr&lt;w&gt;(functor);
    return gradient;
  }
  
  } // namespace
  
  int main() {
  
    constexpr std::size_t gradient_width = 10;
  
    auto gradient_functor = [](std::size_t w) {
      return Eigen::Matrix&lt;float, 3, 1&gt;::Constant(float(w) / gradient_width);
    };
  
    auto gradient = buildGradient&lt;gradient_width&gt;(gradient_functor);
  
    std::cout &lt;&lt; gradient &lt;&lt; std::endl;
  
    return 0;
  }
  </code></pre>
  
  <p>Is there a way of constructing matrices with sizes that depend on template parameters, without resorting to a for loop? Nothing against for loops, that's what I'm using in the mean time. I'd just like to know if there's a way to initialize a matrix expression using template loops.</p>
  
  <p><strong>Edit:*</strong> I updated the example, because the gradient functor was suppose to be returning a vector and not a scalar. The sample problem occurs, though.</p>