Matrix Multiplication using Multi-Dimensional Arrays

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2009
Posts: 38
Reputation: ARYT is an unknown quantity at this point 
Solved Threads: 0
ARYT's Avatar
ARYT ARYT is offline Offline
Light Poster

Matrix Multiplication using Multi-Dimensional Arrays

 
0
  #1
Apr 3rd, 2009
Hi everyone

This should be one of our last assignments. Well, not sure, but most probably.
As the title implies, we're supposed to write a program using multi-dimensional arrays for a 6x6 matrix multiplication.

Here's what I've done. It's almost OK; except the most important part which is the final result. It's wrong.

What's the algorithm for matrix multiplication?

  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. int main()
  8.  
  9. {
  10.  
  11. ifstream infile("MATRIX.dat");
  12. ofstream outfile ("RESULT.dat");
  13.  
  14. int m1[6][6], m2[6][6], M[6][6];
  15. int i=0, j=0;
  16.  
  17. for (i=0; i<6; i++)
  18. {
  19. for (j=0; j<6; j++)
  20. {infile>>m1[i][j]
  21. >>m2[i][j];
  22. M[i][j] = m1[0][j]*m2[i][0];
  23. cout<<M[i][j]<<" ";
  24. } cout<<endl;
  25. }
  26.  
  27.  
  28. infile.close ();
  29. outfile.close ();
  30.  
  31. return 0;
  32. }


BTW, Ancient Dragon, Our tutor said, I asked you to use End Of File function. Why you invent sth different when you can't solve it? Insist on it, and solve it that way.
WTF!!!
Religion is a way of austerity and a mental self-tranquilizer which is associated with numerous cultures. One cannot deny that Moses predominate Jewish over other believers, while Mohammad calls Muslims to be the ones who excel in humanity and still they are the messengers sent from one God, postulating the impacts of religion and ideology on genetics?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,311
Reputation: vishesh is on a distinguished road 
Solved Threads: 36
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso

Re: Matrix Multiplication using Multi-Dimensional Arrays

 
0
  #2
Apr 3rd, 2009
What you have done is multiply corresponding elements of matrices. However its not so. First of all the two matrices should not be necessarily of same dimensions. It should be rather of form NxP and PxM whose product is of form nxm. I guess your teacher wants you to multiply two square matrices of dimension 6.

Take a look at this article at wii describing how to multiply matrices, and then try to implement it on code, in case of problem post.

http://en.wikipedia.org/wiki/Matrix_multiplication
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 309
Reputation: NicAx64 will become famous soon enough NicAx64 will become famous soon enough 
Solved Threads: 18
NicAx64's Avatar
NicAx64 NicAx64 is offline Offline
Posting Whiz

Re: Matrix Multiplication using Multi-Dimensional Arrays

 
0
  #3
Apr 3rd, 2009
The multiplication of matrices defines this rule.

If M = A X B , suppose that A is mxn and B is nxp
then , M is a m x p agree !

M(i,j) = ( k=0 to n ) Σ { A (i,k) x B (k,j) } ----------- (1)

are you agree with the (1) , if not you have to re read this and draw matrices and think again and again until you clear this.it's important.
Nothing like a kernel pannic !
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 309
Reputation: NicAx64 will become famous soon enough NicAx64 will become famous soon enough 
Solved Threads: 18
NicAx64's Avatar
NicAx64 NicAx64 is offline Offline
Posting Whiz

Re: Matrix Multiplication using Multi-Dimensional Arrays

 
0
  #4
Apr 3rd, 2009
Originally Posted by ARYT View Post
BTW, Ancient Dragon, Our tutor said, I asked you to use End Of File function. Why you invent sth different when you can't solve it? Insist on it, and solve it that way.
WTF!!!

Hmm , be patient bro ! don't bug the lectures like that , they can implement it they never give something that you can't or he can't
slove. But the problem is they think in their level. This is mostly true
for teaching the mathematics like subjects.

I also have this experience , I understood nothing my lectures said. I pass that subject using these resources. If you feeling the
same confusion , then may be these will be useful.

http://justmathtutoring.com/
http://tutorial.math.lamar.edu/
http://www.sosmath.com/
http://www.purplemath.com/modules/index.htm

I know how you feeling now. But the only solution is just be patient and try to understand ! you will surely get it.

This post is not offense with the lectures ! but This is the true in most uni's and only the smartest 1% of the students will only get what the lecture told. May be this post also will be helpful them to
think in a different way. Look at the first site , it contains math tutor and it optimized for the student's understand capacity.
Nothing like a kernel pannic !
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 38
Reputation: ARYT is an unknown quantity at this point 
Solved Threads: 0
ARYT's Avatar
ARYT ARYT is offline Offline
Light Poster

Re: Matrix Multiplication using Multi-Dimensional Arrays

 
0
  #5
Apr 3rd, 2009
Originally Posted by NicAx64 View Post
Hmm , be patient bro ! don't bug the lectures like that , they can implement it they never give something that you can't or he can't
slove. But the problem is they think in their level. This is mostly true
for teaching the mathematics like subjects.

I also have this experience , I understood nothing my lectures said. I pass that subject using these resources. If you feeling the
same confusion , then may be these will be useful.

http://justmathtutoring.com/
http://tutorial.math.lamar.edu/
http://www.sosmath.com/
http://www.purplemath.com/modules/index.htm

I know how you feeling now. But the only solution is just be patient and try to understand ! you will surely get it.

This post is not offense with the lectures ! but This is the true in most uni's and only the smartest 1% of the students will only get what the lecture told. May be this post also will be helpful them to
think in a different way. Look at the first site , it contains math tutor and it optimized for the student's understand capacity.
well, thanks. I've also taken Engineering Calculus this semester, but I was talking about our Programming tutor.

So, Can't you give me a code for that multiplication?


BTW, I have some problems with Chain rule (For differentiation), Both partial differentiation and integration and Jacobian.
Can you help me with that too?
Religion is a way of austerity and a mental self-tranquilizer which is associated with numerous cultures. One cannot deny that Moses predominate Jewish over other believers, while Mohammad calls Muslims to be the ones who excel in humanity and still they are the messengers sent from one God, postulating the impacts of religion and ideology on genetics?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 38
Reputation: ARYT is an unknown quantity at this point 
Solved Threads: 0
ARYT's Avatar
ARYT ARYT is offline Offline
Light Poster

Re: Matrix Multiplication using Multi-Dimensional Arrays

 
0
  #6
Apr 3rd, 2009
Originally Posted by vishesh View Post
What you have done is multiply corresponding elements of matrices. However its not so. First of all the two matrices should not be necessarily of same dimensions. It should be rather of form NxP and PxM whose product is of form nxm. I guess your teacher wants you to multiply two square matrices of dimension 6.

Take a look at this article at wii describing how to multiply matrices, and then try to implement it on code, in case of problem post.

http://en.wikipedia.org/wiki/Matrix_multiplication
You know what?! I actually couldn't remember the matrix multiplication from high school math; so, right there at the tutorial class, I took my iPhone out of my pocket and went to Wikipedia to find it out. The tutor was looking at me, somehow like saying, "What the hell are you doing there?".

I just can't figure out the code for that.

Can you please give a code, algorithm or sth for that?
Religion is a way of austerity and a mental self-tranquilizer which is associated with numerous cultures. One cannot deny that Moses predominate Jewish over other believers, while Mohammad calls Muslims to be the ones who excel in humanity and still they are the messengers sent from one God, postulating the impacts of religion and ideology on genetics?
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 309
Reputation: NicAx64 will become famous soon enough NicAx64 will become famous soon enough 
Solved Threads: 18
NicAx64's Avatar
NicAx64 NicAx64 is offline Offline
Posting Whiz

Re: Matrix Multiplication using Multi-Dimensional Arrays

 
0
  #7
Apr 3rd, 2009
M(i,j) = ( k=0 to n ) Σ { A (i,k) x B (k,j) }

so if you can underestand this then you can code you'r own.
This is the so called algorithm , the real implementation is like this.

1. first fetch your two input matrices
2. take the size's of the matrices into int's am , an , bm , bn
3. chek whether an = bm if not say error message
4. your answer matrix should be in the from amx bn therefore
allocate a memory space for that . and using the for loops run that
M(i,j) = ( k=0 to n ) Σ { A (i,k) x B (k,j) } to until j=1 to bn for each
i=1 to an.
note :you can implement the k=0 to n using also a for loop.

Just three for loops ! Just give up a try !
Nothing like a kernel pannic !
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,676
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 262
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Matrix Multiplication using Multi-Dimensional Arrays

 
0
  #8
Apr 3rd, 2009
In math:
M(i,j) = ( k = 1 to n ) Σ { A (i,k) x B (k,j) }

In C++:
There are 8 variables to deal with:
M, A, and B are matrixes (multidimensional arrays in C++ speak)

rows is the number of rows in M and the number of rows in A

columns is the number of columns in M and the number of columns in B.

n is the number of columns in A and the number of rows in B, which must be equal to do a dot product multiplication for matrixes.

i is a given row

j is a given column

D(i, j) is a given element of a matrix with indices i representing the row and j representing the column.

Σ in C++ is essentially +=
  1. i ranges from 0 to rows - 1
  2. j ranges from 0 to columns - 1
  3. k ranges from 0 to n - 1
  4. M(i, j) += A(i, k) x B(k, j)
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 38
Reputation: ARYT is an unknown quantity at this point 
Solved Threads: 0
ARYT's Avatar
ARYT ARYT is offline Offline
Light Poster

Re: Matrix Multiplication using Multi-Dimensional Arrays

 
0
  #9
Apr 5th, 2009
No, you're making it more complicated than it is. It's a multiplication of two 6x6 matrices.
Religion is a way of austerity and a mental self-tranquilizer which is associated with numerous cultures. One cannot deny that Moses predominate Jewish over other believers, while Mohammad calls Muslims to be the ones who excel in humanity and still they are the messengers sent from one God, postulating the impacts of religion and ideology on genetics?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 38
Reputation: ARYT is an unknown quantity at this point 
Solved Threads: 0
ARYT's Avatar
ARYT ARYT is offline Offline
Light Poster

Re: Matrix Multiplication using Multi-Dimensional Arrays

 
0
  #10
Apr 5th, 2009
OK. Here I've tried to define another variable which is k, but came up with a load of garbage.

  1. int m1[6][6], m2[6][6], M[6][6];
  2. int i=0, j=0, k=0;
  3.  
  4. for (i=0; i<6; i++)
  5. {
  6. for (j=0; j<6; j++)
  7. {
  8. for (k=0; k<6; k++)
  9. {infile>>m1[i][j]
  10. >>m2[i][j];
  11. M[i][j] = m1[i][k]*m2[k][j];
  12. cout<<M[i][j]<<" ";
  13. }} cout<<endl;
  14. }
Last edited by ARYT; Apr 5th, 2009 at 2:32 am.
Religion is a way of austerity and a mental self-tranquilizer which is associated with numerous cultures. One cannot deny that Moses predominate Jewish over other believers, while Mohammad calls Muslims to be the ones who excel in humanity and still they are the messengers sent from one God, postulating the impacts of religion and ideology on genetics?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC