title: Generate an exponentially-spaced list of numbers
body: <p>I want to generate an exponentially-spaced list of numbers in C++, where the number of points and the bounds are known (just like Matlab's linspace or logspace, or Python's numpy.logspace). I have found several implementations for log-spaced numbers (see below), but couldn't think of a way to invert these to exponentially-spaced numbers, besides, bounds can be negative.</p>
  
  <p>Here's is what I have found so far :</p>
  
  <p><a href="https://stackoverflow.com/questions/21429294/is-there-something-like-numpy-logspace-in-c">Is there something like numpy.logspace in C++?</a></p>
  
  <p><a href="http://econpapers.repec.org/software/bocbocode/s426701.htm" rel="nofollow noreferrer">EXPLIST: Stata module to generate an exponentially-spaced list of numbers</a> (No idea what this language is actually)</p>
  
  <p><a href="http://www.codeproject.com/Questions/188926/Generating-a-logarithmically-spaced-numbers" rel="nofollow noreferrer">Generating a logarithmically spaced numbers</a></p>
  
  <p><strong>EDIT :</strong></p>
  
  <p>I should have given the problem a little more thinking before rushing to stackoverflow, here's what I actually did (inspired by <a href="https://stackoverflow.com/questions/11443737/how-to-generate-exponentially-increasing-range-in-python">this question</a>) :</p>
  
  <p>Given two bounds <code>first</code> and <code>last</code>, I wanted to generate a <code>n</code>-size array that starts with <code>first</code> and ends with <code>last</code> where each array's element is the exponential of some <code>x</code>. </p>
  
  <p>This mathematical problem is a simple series <code>U(i)</code> that starts with <code>U(0) = first</code> and ends with <code>U(n) = last</code> with <code>U(i) = first * q^i</code> (for i in {0, 1, ..., n}) and <code>q = pow(last / first, 1 / (n - 1))</code>. </p>
  
  <p>Here's a raw code :</p>
  
  <pre><code>#include &lt;Eigen\Dense&gt;
  
  using namespace Eigen;
  
  VectorXd expList(double first, double last, DenseIndex n)
  {
      VectorXd vector(n); // native C++ array or vector can be used of course
      double m = (double) 1 / (n - 1);
      double quotient = pow(last / first, m);
  
      vector(0) = first;
  
      for (DenseIndex i = 1; i &lt; n; i++) // DenseIndex is just a typedef ptrdiff_t from the Eigen library
          vector(i) = vector(i - 1) * quotient;
  
      return vector;
  }
  </code></pre>
  
  <p>This works for any same sign doubles <code>first</code> and <code>last</code> where <code>first &lt; last</code> of course, but It can work for a negative <code>first</code> and positive <code>last</code> too with a little tweaking.</p>
  
  <p><strong>Example :</strong> </p>
  
  <p>for first = 50 and last = 300 000 and a 100 elements array</p>
  
  <p><img src="https://i.stack.imgur.com/v4K2W.png" alt="Example"></p>