I have an algorithm, which I'm not too pleased with, that does the job.
Does anybody know a faster method, or even how to improve my algorithm?
My algorithm takes three n-x-n symmetric matrices, A, B, C
It multiplies the first two in the usual way to give a matrix D = A*B.
It then calculates the product D*C and stores the result in A.
I am not happy with this as it takes far too long.
Can anybody speed it up?
The first function is the standard way in which matrix multiplication is performed
Expand|Select|Wrap|Line Numbers
- void Matrix_Multiply(double *mat_one,double *mat_two,double *mat_result,double &size)
- {
- for(int i=0;i<(int)(size);i++){
- for(int j=0;j<(int)(size);j++){
- for(int k=0;k<(int)(size);k++){
- *(mat_result+(int)(i*size)+j)+=*(mat_one+(int)(i*size)+k)**(mat_two+(int)(k*size)+j);
- }
- }
- }
- }
Expand|Select|Wrap|Line Numbers
- void Matrix_Triple_Product(double *mat_one,double *mat_two,double *mat_three,void (*g)(double *,double *,double *,double &),double &size)
- {
- double *mat_temp=new(double [(int)(size)*(int)(size)]);//Array to hold the result of the first multiplication
- for(int i=0;i<(int)(size);i++){
- for(int j=0;j<(int)(size);j++){
- *(mat_temp+(int)(i*size)+j)=0.0;
- }
- }
- //Calculate the product of the first two matrices and store the result in mat_temp
- g(mat_one,mat_two,mat_temp,size);
- //Assign the second matrix to zero
- for(int i=0;i<(int)(size);i++){
- for(int j=0;j<(int)(size);j++){
- *(mat_two+(int)(i*size)+j)=0.0;
- }
- }
- //Calculate the product of mat_temp and the third matrix and store the result in the second matrix
- g(mat_temp,mat_three,mat_two,size);
- cout<<"The matrix product has been computed and is stored in memory\n";
- delete[] mat_temp;
- }