title: How do I convolve() more than two distributions without doubling the result rowcount every time?
body: <p>I am attempting to <code>convolve()</code> 36 beta distributions. The result distribution is one of the two input distributions to the next successive call to <code>convolve()</code>. After every <code>convolve()</code>, the result has row count = <code>(nrow(vector1)+nrow(vector2)-1)</code>. In effect, the row count of the result distribution almost doubles with every call to <code>convolve()</code>. This is very inefficient - it makes runtime impossibly long and consumes large amounts of memory. Is there any way to keep the row count constant?</p>
  <p>Code example below ...</p>
  <pre><code># Function from https://stat.ethz.ch/pipermail/r-help/2008-July/168762.html
  weighted.var &lt;- function(x, w, na.rm = FALSE) {
    if (na.rm) {
      w &lt;- w[i &lt;- !is.na(x)]
      x &lt;- x[i]
    }
    sum.w &lt;- sum(w)
    sum.w2 &lt;- sum(w^2)
    mean.w &lt;- sum(x * w) / sum(w)
    (sum.w / (sum.w^2 - sum.w2)) * sum(w * (x - mean.w)^2, na.rm = na.rm);
  }
  
  # Define beta distribution shape parameters.
  s1a &lt;- 3.52; s1b &lt;- 65.35;
  s2a &lt;- 1.684; s2b &lt;- 189.12;
  s3a &lt;- 5.696; s3b &lt;- 32.34;
  s4a &lt;- 1.81; s4b &lt;- 185.5;
  
  # Define intial set of quantiles.
  mQ1 &lt;- matrix(data=seq(0,1,1/1000),ncol=1);
  
  for (i in 1:3){
    mPDF &lt;- matrix(data=convolve(dbeta(mQ1,s1a,s1b),rev(dbeta(mQ1,s2a,s2b)),type=&quot;open&quot;),ncol=1L);
    print(paste(nrow(mPDF),' rows',sep=''));
  
    if(i &lt; 3){
      # Calculate the merged shape parameters directly from mPDF.
      mQ2 &lt;- matrix(data=seq(0,1L,(1L/(nrow(mPDF)-1L))),ncol=1L);
      wtMean &lt;- weighted.mean(mQ2,mPDF);
      wtStd &lt;- sqrt(weighted.var(mQ2,mPDF));
      s1a &lt;- -1L * ((wtMean*(wtStd^2 + wtMean^2 - wtMean))/wtStd^2);
      s1b &lt;- ((wtStd^2 + wtMean^2 - wtMean)*(wtMean - 1))/wtStd^2;
  
      s2a &lt;- s3a; s2b &lt;- s3b;
      mQ1 &lt;- mQ2;
    }
  
  } #i
  </code></pre>