473,414 Members | 1,665 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Fast Multiplication of Three n-x-n matrices

emaghero
Does anybody know an algorithm for the fast multiplication of three n-x-n symmetric matrices?

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
  1. void Matrix_Multiply(double *mat_one,double *mat_two,double *mat_result,double &size)
  2. {
  3.     for(int i=0;i<(int)(size);i++){
  4.         for(int j=0;j<(int)(size);j++){
  5.             for(int k=0;k<(int)(size);k++){
  6.                 *(mat_result+(int)(i*size)+j)+=*(mat_one+(int)(i*size)+k)**(mat_two+(int)(k*size)+j);
  7.             }
  8.         }
  9.     }
  10. }
  11.  
The second function calls the first to perform two matrix multiplications and then stores the result
Expand|Select|Wrap|Line Numbers
  1. void Matrix_Triple_Product(double *mat_one,double *mat_two,double *mat_three,void (*g)(double *,double *,double *,double &),double &size)
  2. {
  3.     double *mat_temp=new(double [(int)(size)*(int)(size)]);//Array to hold the result of the first multiplication
  4.     for(int i=0;i<(int)(size);i++){
  5.         for(int j=0;j<(int)(size);j++){
  6.             *(mat_temp+(int)(i*size)+j)=0.0;
  7.         }
  8.     }
  9.     //Calculate the product of the first two matrices and store the result in mat_temp
  10.     g(mat_one,mat_two,mat_temp,size);
  11.     //Assign the second matrix to zero
  12.     for(int i=0;i<(int)(size);i++){
  13.         for(int j=0;j<(int)(size);j++){
  14.             *(mat_two+(int)(i*size)+j)=0.0;
  15.         }
  16.     }
  17.     //Calculate the product of mat_temp and the third matrix and store the result in the second matrix
  18.     g(mat_temp,mat_three,mat_two,size);
  19.  
  20.     cout<<"The matrix product has been computed and is stored in memory\n";
  21.  
  22.     delete[] mat_temp;
  23.  
  24. }
  25.  
Nov 23 '06 #1
1 4565
Try Strassen Algorithm for fast matrix multiplication
Nov 28 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Ralf Hildebrandt | last post by:
Hi all! First of all: I am a C-newbie. I have noticed a "strange" behavior with the standart integer multiplication. The code is: void main(void)
14
by: amitnanda | last post by:
Hi Guys, I have a matrix multiplication program in C that multiplies two matrices. When their size is 3*3 or 800*800, the program runs fine. But above that size, I get a "segmentation fault"....
7
by: VijaKhara | last post by:
Hi all, Is there any method which can implememt the matrix multiplication faster than using the formula as we often do by hand? I am writing the following code and my matrice: one is 3x40000 and...
3
by: matheen | last post by:
i want java code for multiplicaion of two matrices,so please write the complete code for my question thankyou.
3
by: crazygrey | last post by:
Hello, I'm a newbie to C++ so excuse me if my question was trivial but it is important to me. I'm implementing a simple code to find the forward kinematics of a robot: #include "stdafx.h"...
1
by: dcatunlucky | last post by:
Ok, I have an assignment to write a program that multiplies two matrices. The matrices dimensions will be user defined as well as the numbers (floating point values) inside of them. The program must...
1
by: glitchized | last post by:
hi, can anyone help me with matrices multiplication function. how do i go about doing it if i wanna do multiplication of 2 square matrices? i'll first take in the size of the matrix example: cin...
1
by: Sozos | last post by:
Hi guys. I have a problem with writing the base case for the following matrix multiplication function I have implemented. Please help. #define index(i,j,power) (((i)<<(power))+(j)) void...
1
by: siggy key | last post by:
I have the following definition typedef struct matrix_t { int rows, columns; Complex** nextElement;// TODO: Add fields }*Matrix; and I have to implement /** * Performs matrix...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.