Repeated Squaring - Matrix Multiplication using NEWMAT

Posted by Dinakar Kulkarni on Stack Overflow See other posts from Stack Overflow or by Dinakar Kulkarni
Published on 2012-10-18T23:11:37Z Indexed on 2012/10/22 5:01 UTC
Read the original article Hit count: 324

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.

# include "C:\User\newmat10\newmat.h"
# include "C:\User\newmat10\newmatio.h"
# include "C:\User\newmat10\newmatap.h"

# include <iostream>
# include <time.h>
# include <ctime>
# include <cstdlib>
# include <iomanip>

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 <= 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 <= ord; y++)
    for(int z = 1; z<= ord; z++)
        H(y,z) = unifRandom();

return(H);
}
//---------------------------------------------------

void main()
{
int exponent, dimension;

cout<<"Insert exponent:"<<endl;
cin>>exponent;
cout<< "Insert dimension:"<<endl;   
cin>>dimension;


cout<<"The number of rows/columns in the square matrix is: "<<dimension<<endl;
cout<<"The exponent is: "<<exponent<<endl;

Matrix      A(dimension,dimension),B(dimension,dimension);
    Matrix C(dimension,dimension),D(dimension,dimension);

B= initialize_mat(A,dimension);

cout<<"Initial Matrix: "<<endl;

cout<<setw(5)<<setprecision(2)<<B<<endl;
//-----------------------------------------------------------------------------

cout<<"Repeated Squaring Result: "<<endl;

clock_t time_before1 = clock();

C = repeated_squaring (B, exponent , dimension);
cout<< setw(5) <<setprecision(2) <<C;

clock_t time_after1 = clock();
float diff1 = ((float) time_after1 - (float) time_before1);
cout << "It took " << diff1/CLOCKS_PER_SEC << " seconds to complete" << endl<<endl;

//---------------------------------------------------------------------------------

cout<<"Direct Squaring Result:"<<endl;

clock_t time_before2 = clock();

D = direct_squaring (B, exponent , dimension);
cout<<setw(5)<<setprecision(2)<<D;

clock_t time_after2 = clock();
float diff2 = ((float) time_after2 - (float) time_before2);
cout << "It took " << diff2/CLOCKS_PER_SEC << " seconds to complete" << endl<<endl;

}

I face the following problems:

  1. The random number generator returns only "-5" as each element in the output.
  2. The Matrix multiplication yield different results with brute force multiplication and using the repeated squaring algorithm.

I'm timing the execution time of my code to compare the times taken by brute force multiplication and by repeated squaring.

Could someone please find out what's wrong with the recursion and with the matrix initialization?

NOTE: While compiling this program, make sure you've imported the NEWMAT library.

Thanks in advance!

© Stack Overflow or respective owner

Related posts about matrix

Related posts about matrix-multiplication