title: Repeated Squaring - Matrix Multiplication using NEWMAT
body: <p>I'm trying to use the repeated squaring algorithm (using recursion) to perform matrix exponentiation. I've included header files from the NEWMAT library instead of using arrays. The original matrix has elements in the range (-5,5), all numbers being of type float.</p>
  
  <pre><code># include "C:\User\newmat10\newmat.h"
  # include "C:\User\newmat10\newmatio.h"
  # include "C:\User\newmat10\newmatap.h"
  
  # include &lt;iostream&gt;
  # include &lt;time.h&gt;
  # include &lt;ctime&gt;
  # include &lt;cstdlib&gt;
  # include &lt;iomanip&gt;
  
  using namespace std;
  
  Matrix repeated_squaring(Matrix A, int exponent, int n) //Recursive function
   {
      A(n,n);
      IdentityMatrix I(n);
  
      if (exponent == 0) //Matrix raised to zero returns an Identity Matrix
      return I;
  
      else 
      {
  
          if ( exponent%2 == 1 ) // if exponent is odd
          return (A * repeated_squaring (A*A, (exponent-1)/2, n));
  
          else //if exponent is even
          return (A * repeated_squaring( A*A, exponent/2, n));
      }
    }
  
   Matrix direct_squaring(Matrix B, int k, int no) //Brute Force Multiplication
    {
  B(no,no);
  Matrix C = B;
  for (int i = 1; i &lt;= k; i++)
      C = B*C;
  return C;
     }
  
      //----Creating a matrix with elements b/w (-5,5)----
  
  
  float unifRandom()
  {
  int a = -5;
  int b = 5;
  float temp = (float)((b-a)*( rand()/RAND_MAX) + a);
  return temp;
  }
  
  Matrix initialize_mat(Matrix H, int ord)
  {
  H(ord,ord);
  for (int y = 1; y &lt;= ord; y++)
      for(int z = 1; z&lt;= ord; z++)
          H(y,z) = unifRandom();
  
  return(H);
  }
  //---------------------------------------------------
  
  void main()
  {
  int exponent, dimension;
  
  cout&lt;&lt;"Insert exponent:"&lt;&lt;endl;
  cin&gt;&gt;exponent;
  cout&lt;&lt; "Insert dimension:"&lt;&lt;endl;   
  cin&gt;&gt;dimension;
  
  
  cout&lt;&lt;"The number of rows/columns in the square matrix is: "&lt;&lt;dimension&lt;&lt;endl;
  cout&lt;&lt;"The exponent is: "&lt;&lt;exponent&lt;&lt;endl;
  
  Matrix      A(dimension,dimension),B(dimension,dimension);
      Matrix C(dimension,dimension),D(dimension,dimension);
  
  B= initialize_mat(A,dimension);
  
  cout&lt;&lt;"Initial Matrix: "&lt;&lt;endl;
  
  cout&lt;&lt;setw(5)&lt;&lt;setprecision(2)&lt;&lt;B&lt;&lt;endl;
  //-----------------------------------------------------------------------------
  
  cout&lt;&lt;"Repeated Squaring Result: "&lt;&lt;endl;
  
  clock_t time_before1 = clock();
  
  C = repeated_squaring (B, exponent , dimension);
  cout&lt;&lt; setw(5) &lt;&lt;setprecision(2) &lt;&lt;C;
  
  clock_t time_after1 = clock();
  float diff1 = ((float) time_after1 - (float) time_before1);
  cout &lt;&lt; "It took " &lt;&lt; diff1/CLOCKS_PER_SEC &lt;&lt; " seconds to complete" &lt;&lt; endl&lt;&lt;endl;
  
  //---------------------------------------------------------------------------------
  
  cout&lt;&lt;"Direct Squaring Result:"&lt;&lt;endl;
  
  clock_t time_before2 = clock();
  
  D = direct_squaring (B, exponent , dimension);
  cout&lt;&lt;setw(5)&lt;&lt;setprecision(2)&lt;&lt;D;
  
  clock_t time_after2 = clock();
  float diff2 = ((float) time_after2 - (float) time_before2);
  cout &lt;&lt; "It took " &lt;&lt; diff2/CLOCKS_PER_SEC &lt;&lt; " seconds to complete" &lt;&lt; endl&lt;&lt;endl;
  
  }
  </code></pre>
  
  <p>I face the following problems:</p>
  
  <ol>
  <li>The random number generator returns only "-5" as each element in the output.</li>
  <li>The Matrix multiplication yield different results with brute force multiplication and using the repeated squaring algorithm.</li>
  </ol>
  
  <p>I'm timing the execution time of my code to compare the times taken by brute force multiplication and by repeated squaring.</p>
  
  <p>Could someone please find out what's wrong with the recursion and with the matrix initialization?</p>
  
  <p><strong>NOTE:</strong> While compiling this program, make sure you've imported the NEWMAT library.</p>
  
  <p>Thanks in advance!</p>