matrix multiplier...

This is a discussion on matrix multiplier... within the C Programming forums, part of the General Programming Boards category; Hey y'all. Right here goes. I'm writing a matrix calculator, i've got add down, i've got subtract down, now I'm ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    41

    matrix multiplier...

    Hey y'all.

    Right here goes. I'm writing a matrix calculator, i've got add down, i've got subtract down, now I'm on multiply and sweet mother of gargantua its giving me hassle. I mean GARRR

    I've the section of the code that i've tried below. It gives the output matrix the right dimensions, just not the right values.

    Code:
    int multiply_matrix(matrix_struct *matrix_a, matrix_struct *matrix_b, matrix_struct *matrix_c)
    {
    
    	int row,col,sweep, position;
    	float total=0, matrix_total=0;
    	
    	if((matrix_a->no_of_rows)!=(matrix_b->no_of_columns))
    	{
    		printf("\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n");
    		printf("= Sorry, the dimensions of your matrix  =\n");
    		printf("= do not allow multilpication           =\n");
    		printf("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n");
    		return 0;
    	}
    
    	else
    	{
    
    		    free(matrix_c->matrix);
    		
    			matrix_c->no_of_rows=matrix_b->no_of_rows;
                matrix_c->no_of_columns=matrix_a->no_of_columns;
            
                //allocate memory for rows
    
                
    			matrix_c->matrix=(float**)(malloc((matrix_c->no_of_rows)*sizeof(float*)));
    
                //allocate memory for each row of the matrix
    
                for(row=0;row<(matrix_c->no_of_rows);row++)
    				matrix_c->matrix[row]=(float*)(malloc((matrix_c->no_of_columns)*sizeof(float)));
    
    
    
    			//performing the muliplication
    
    
    
            for(row=0;row<(matrix_c->no_of_rows); row++)
              for(col=0;col<(matrix_c->no_of_columns);col++)
    		  {
    			  total=0;
    			  matrix_total=0;
    
    			  for(sweep=0; sweep<(matrix_a->no_of_rows); sweep++)
    				  for(position=0; position<(matrix_b->no_of_columns); position++)
    				  {
    					  matrix_total=(matrix_a->matrix[sweep][position])*(matrix_b->matrix[position][sweep]);
    					  total=+matrix_total;
    				  }
    				matrix_c->matrix[row][col] = total;
    
    		  }
    
    		    
    		  
    		  
    		  
    		  
    		  printf("Your answer is:\n\n");
    
              for(row=0;row<(matrix_c->no_of_rows); row++)
              {
                  printf("\n");
                   for(col=0;col<(matrix_c->no_of_columns);col++)
                      printf("%f ",matrix_c->matrix[row][col]);
              }
    
    
    	
    	
    	}
    
    
    
        return 1;
    }
    This is just the function, if you want the rest of the code its in the attatchement below.

    I really cant get my head around it, its driving me nuts, so any help would be appreciated on levels you cant imagine.

    Cheers,

    Mitch
    ~~~~~~~~~~
    Mitchell Kent
    07782383326
    spudtheimpaler@hotmail.com
    ~~~~~~~~~~

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    At a glance:

    >>total = +matrix_total;
    presumably should be
    >>total += matrix_total;
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    41
    Yep, thats one bug now squashed. Now it seems to work, but using the example of a 2x2 matrix multiply by a 2x2 matrix, all the elements being 2 ( [2 2,2 2]*[2 2,2 2] ) rather than get a 2x2 matrix with all answers 8, i get a 2x2 matrix with all answers 16

    also, when i do a 3x3 * 3x3 with all elements 3, rather than get 3x3 all elements being 27, they are all 81..

    ie the 2x2 matrix is off by a factor of 2 and the 3x3 matrix is off by a factor of 3...

    also, it doesnt work when i try a 1x4 matrix or 4x1 matrix

    im still trying but its not going well...
    ~~~~~~~~~~
    Mitchell Kent
    07782383326
    spudtheimpaler@hotmail.com
    ~~~~~~~~~~

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    sounds like you're calculating it too many times. Write it down on paper first in simple form, the follow the logic of your code to see where it's not doing what you want.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    41
    wish it were that simple. i think it's the right number of iterations. though a loop in a loop in a loop in a loop heaven knows. Plus i'm getting errors when the 2d array is of dimension 1 by something.

    I'm going into uni tomorrow, so i'll ask a lecturer, who hopefully has some answers seeign as he set the damned thing. I'll post anything i learn on here. saying that i'm still gonna be on it all night

    Chers for looking though

    Mitch
    ~~~~~~~~~~
    Mitchell Kent
    07782383326
    spudtheimpaler@hotmail.com
    ~~~~~~~~~~

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
            for(row=0;row<(matrix_c->no_of_rows); row++)
              for(col=0;col<(matrix_c->no_of_columns);col++)
    	  {
    		matrix_c->matrix[row][col] = 0;
    
    		for(sweep=0; sweep<(matrix_a->no_of_columns); sweep++)
    			matrix_c->matrix[row][col] = matrix_a->matrix[row][sweep] * matrix_b->matrix[sweep][col];
    
    	  }

  7. #7
    Registered User
    Join Date
    Dec 2003
    Posts
    41
    Thanks, it neatens my code a lot

    I am just gonna leave it and ask my lecturers in the morning. I'll post their answer up here anyways, just in case anyone wanted to know.

    Many thanks everyone

    Mitch
    ~~~~~~~~~~
    Mitchell Kent
    07782383326
    spudtheimpaler@hotmail.com
    ~~~~~~~~~~

  8. #8
    Registered User
    Join Date
    Dec 2003
    Posts
    41

    fin

    Code:
               //performing the muliplication 
    
    
           for(row=0;row<(matrix_c->no_of_rows); row++) 
             for(col=0;col<(matrix_c->no_of_columns);col++) 
             { 
               
                 total=0;
                 matrix_total=0;
                 matrix_c->matrix[row][col] = 0; 
                 
                 
               for(sweep=0; sweep<(matrix_a->no_of_columns); sweep++) 
               {
                   matrix_total = matrix_a->matrix[row][sweep] * matrix_b->matrix[sweep][col]; 
                   total+=matrix_total;
               } 
    
              
    
             matrix_c->matrix[row][col]=total;
    
    
             }
    was the way to do it. I had gotten the rows and columns from which matrices getting muddled from further above and that was what was screwing with it all. Thanks for your help everyone, truly appreciated

    Mitch
    ~~~~~~~~~~
    Mitchell Kent
    07782383326
    spudtheimpaler@hotmail.com
    ~~~~~~~~~~

  9. #9
    Registered User
    Join Date
    Dec 2003
    Posts
    41
    ~~~~~~~~~~
    Mitchell Kent
    07782383326
    spudtheimpaler@hotmail.com
    ~~~~~~~~~~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 04:57 PM
  3. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM
  4. Matrix Reloaded Questions (SPOILERS_
    By Xei in forum A Brief History of Cprogramming.com
    Replies: 73
    Last Post: 10-19-2003, 02:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21