473,416 Members | 1,858 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,416 software developers and data experts.

Recursive Matrix Multiplication

1
Hi guys. I have a problem with writing the base case for the following matrix multiplication function I have implemented. Please help.
Expand|Select|Wrap|Line Numbers
  1. #define index(i,j,power) (((i)<<(power))+(j))
  2.  
  3. void recMultiply(int i, int j, float a[], int k, int l, float b[], int x, int y, float c[], int s);
  4.  
  5.     int i, j, k, s, matrixsize, blocksize, jj, kk, power, bsize;
  6.     float sum, maxr, total=0.0, startmult, finishmult, multtime;
  7.     float* A = NULL;
  8.     float* B = NULL;
  9.     float* C = NULL;
  10.  
  11. int main(int argc, char *argv[])
  12. {
  13.         //functions for producing random numbers
  14.     srand(99999);
  15.     maxr = (float)RAND_MAX;
  16.  
  17.         //store user parameters into variables
  18.     power = atoi( argv[1] );
  19.     bsize = atoi( argv[2] );
  20.  
  21.     matrixsize = int(pow(2,power)); 
  22.     blocksize = int(pow(2,bsize));
  23.  
  24.     cout<<"Matrix Size = 2^"<<power<<" = "<<"("<<matrixsize<<"*"<<matrixsize<<")"<<" matrix"<<endl<<endl;
  25.  
  26.        A = new float[matrixsize*matrixsize];
  27.        B = new float[matrixsize*matrixsize];
  28.        C = new float[matrixsize*matrixsize];
  29.  
  30.     //fill matrices A and B with pseudo-random float numbers
  31.     for (i = 0; i < matrixsize; i++)
  32.         for (j = 0; j < matrixsize; j++)
  33.         {
  34.             A[index(i,j,power)] = rand()/maxr;
  35.             B[index(i,j,power)] = rand()/maxr;
  36.             C[index(i,j,power)] = 0.0;
  37.         }
  38.  
  39.     //start clock - multiplication
  40.     startmult = clock();
  41.  
  42.        //call matrix multiplication function
  43.       recMultiply(0, 0, A, 0, 0, B, 0, 0, C, matrixsize);
  44.  
  45.     //Stop clock - multiplication    
  46.     finishmult = clock();
  47.  
  48.                 //Calculate time needed to execute multiplication
  49.     multtime = (finishmult-startmult)/CLOCKS_PER_SEC;
  50.  
  51.     cout<<"Sum of the elements of matrix C: "<<total<<endl<<endl;
  52.  
  53.     cout<<"Time for matrix multiplication: "<<multtime<<" seconds"<<endl<<endl;
  54.  
  55. cin>>s;
  56.  
  57.     return 0;
  58. }
  59.  
  60. void recMultiply(int i, int j, float a[], int k, int l, float b[], int x, int y, float c[], int s){
  61.  
  62.      //recursive matrix multiplication algorithm
  63.      if (s == 2) 
  64.      {
  65.  
  66. //What base case code should go here
  67.  
  68.          }
  69.  
  70.      else {
  71.  
  72.           recMultiply(0, 0, a, 0, 0, b, 0, 0, c, s/2);
  73.           recMultiply(0, s/2, a, s/2, 0, b, 0, 0, c, s/2);
  74.  
  75.           recMultiply(0, 0, a, 0, s/2, b, 0, s/2, c, s/2);
  76.           recMultiply(0, s/2, a, s/2, s/2, b, 0, s/2, c, s/2);
  77.  
  78.           recMultiply(s/2, 0, a, 0, 0, b, s/2, 0, c, s/2);
  79.           recMultiply(s/2, s/2, a, s/2, 0, b, s/2, 0, c, s/2);
  80.  
  81.           recMultiply(s/2, 0, a, 0, s/2, b, s/2, s/2, c, s/2);
  82.           recMultiply(s/2, s/2, a, s/2, s/2, b, s/2, s/2, c, s/2);
  83.  
  84.           }
  85. }
Aug 1 '08 #1
1 9117
sicarie
4,677 Expert Mod 4TB
Wha'ts the problem? Is there an error message with the problem, or is it a logical issue?
Aug 2 '08 #2

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

Similar topics

6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
15
by: christopher diggins | last post by:
Here is some code I wrote for Matrix multiplication for arbitrary dimensionality known at compile-time. I am curious how practical it is. For instance, is it common to know the dimensionality of...
3
by: robix | last post by:
Hi again. I'm now asking your help because of a smal problem i'm getting with my multiplication matrix code. I'll try to give you as many details as possible. My matrix structure: typedef...
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"....
20
by: Frank-O | last post by:
Hi , Recently I have been commited to the task of "translating" some complex statistical algorithms from Matlab to C++. The goal is to be three times as fast as matlab ( the latest) . I've...
0
by: lituncse | last post by:
dear friends, i have come across a problem which is difficult to solve for me.it's about starssen's matrix multiplication.in general matrix multiplication we need 8 multiplications and 4 additions...
6
by: amitsoni.1984 | last post by:
Hi, Is there any direct function for matrix multiplication in Python or any of its packages? or do we have to multiply element by element? Thank you, Amit
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"...
8
by: joegao1 | last post by:
can some one give me a hint? I want to program the code for matrix multiplication with as less arithmetical / multiplication operations as possible. my task is to calculate the matrix...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.