Matrix multiplication

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

Join Date: Nov 2009
Posts: 2
Reputation: uncbball is an unknown quantity at this point 
Solved Threads: 0
uncbball uncbball is offline Offline
Newbie Poster

Matrix multiplication

 
0
  #1
25 Days Ago
Hey, so I have to make a program that has to multiply two 3x3 matrices (among other things). I used classes to do the project, but I just can't seem to get this multiplication to work. Here is my code so far:
  1. void matrix::mult(const matrix& u)
  2. {
  3. double c[9];
  4. double a[9];
  5. for (int i=0; i<rows; i++)
  6. {
  7. for (int j=0; j<columns; j++)
  8. {
  9. for(int r=0; r<columns; r++)
  10. {
  11. c[i*columns+j]+=a[i*columns+r]*u.data[r*columns+j];
  12. }
  13. }
  14. }
  15. }

Any advice would be great, and please let me know if there's anything I should clarify. Thank You.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 394
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz
 
0
  #2
25 Days Ago
First thing that comes to mind is that a[9] and c[9] are not initialized.

Next you are using variables columns and row. So check that this->rows == u.columns. Then set the data in this. as c[] is just ignored.

Other than that it looks ok.
Last edited by StuXYZ; 25 Days Ago at 11:35 pm.
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 2
Reputation: uncbball is an unknown quantity at this point 
Solved Threads: 0
uncbball uncbball is offline Offline
Newbie Poster
 
0
  #3
25 Days Ago
Originally Posted by StuXYZ View Post
Next you are using variables columns and row. So check that this->rows == u.columns. Then set the data in this. as c[] is just ignored.
Hey, thanks for replying. I'm not quite sure what you mean with the above though. And if it matters, rows and columns aren't actually variables, they're constants.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,261
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 157
firstPerson's Avatar
firstPerson firstPerson is online now Online
Nearly a Posting Virtuoso
 
0
  #4
24 Days Ago
Why do you have 3 loops for a 1d vector?

If you want to emulate it as a 2d then all you need is 2 for loops.

  1. int A[4] = {1,2,3,4};
  2. int B[4] = {1,2,3,4};
  3. int R[4] = {0};
  4.  
  5. for(int i = 0; i < 4; i++)
  6. {
  7. for(int j = 0; j < 4; j++)
  8. R[ j + 4*i] = A[j + 4*i] * B[j + 4*i];
  9. }

Its better to make a function that converts (j+4*i) into a 2d index so it becomes more readable.
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...[*]
      [*solved by : murtan]
3) What is the 123456789 prime numer?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 113
Reputation: abhi_elementx is an unknown quantity at this point 
Solved Threads: 6
abhi_elementx's Avatar
abhi_elementx abhi_elementx is offline Offline
Junior Poster
 
0
  #5
24 Days Ago
Hi.
Try this. Naive, but should give you some idea.
The way you architect the classes entirely depends on you.
  1. #include<iostream>
  2. using namespace std;
  3. const int SIZE = 3;
  4.  
  5. class Matrix{
  6. public:
  7. int matrix[SIZE][SIZE];
  8. void read_matrix();
  9. };
  10.  
  11. class MatOps{
  12. public:
  13. Matrix mul_matrix(Matrix &, Matrix &);
  14. };
  15.  
  16. void Matrix :: read_matrix(){
  17. cout<<"Enter matrix(3 x 3)"<<endl;
  18. int i = 0;
  19. int j = 0;
  20. for(i = 0; i < SIZE; i++){
  21. for(j = 0; j < SIZE; j++){
  22. cin>>matrix[i][j];
  23. }
  24. }//for ends
  25. }//read_matrix() ends
  26.  
  27.  
  28. Matrix MatOps :: mul_matrix(Matrix & a, Matrix & b){
  29. Matrix result;
  30. int i = 0;
  31. int j = 0;
  32. int k = 0;
  33.  
  34. for(i = 0; i < SIZE; i++){
  35. for(j = 0; j < SIZE; j++){
  36. result.matrix[i][j] = 0;
  37. for(k = 0; k < SIZE; k++){
  38. result.matrix[i][j] += a.matrix[i][k] * b.matrix[k][j];
  39. }
  40. }
  41. }//for ends
  42.  
  43. return result;
  44. }//read_matrix() ends
  45.  
  46.  
  47. int main(){
  48. Matrix a, b;
  49. a.read_matrix();
  50.  
  51. b.read_matrix();
  52.  
  53. MatOps obj;
  54. Matrix result = obj.mul_matrix(a, b);
  55.  
  56. for(int i = 0; i < SIZE; i++){
  57. for(int j = 0; j < SIZE; j++){
  58. cout<<result.matrix[i][j]<<" ";
  59. }
  60. cout<<endl;
  61. }
  62.  
  63. return 0;
  64. }//main() ends

Cheers.
PEACE !
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC