title: A^k for matrix multiplication in R?
body: <p>Suppose <strong>A</strong> is some square matrix. How can I easily exponentiate this matrix in R? </p>
  
  <p>I tried two ways already: Trial 1 with a for-loop hack and Trial 2 a bit more elegantly but it is still a far cry from <strong>A</strong><sup>k</sup> simplicity.</p>
  
  <p><strong>Trial 1</strong></p>
  
  <pre><code>set.seed(10)
  t(matrix(rnorm(16),ncol=4,nrow=4)) -&gt; a 
  for(i in 1:2){a &lt;- a %*% a}
  </code></pre>
  
  <p><strong>Trial 2</strong></p>
  
  <pre><code>a &lt;- t(matrix(c(0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0),nrow=4))
  i &lt;- diag(4) 
  (function(n) {if (n&lt;=1) a else (i+a) %*% Recall(n-1)})(10)
  </code></pre>