1 to n multiplication of matrices(For Loop Question)

This is a discussion on 1 to n multiplication of matrices(For Loop Question) within the C Programming forums, part of the General Programming Boards category; hi again, i have the following code which gives m1^2 (m1 is a user defined square matrix). Code: for(i=0;i<n;i++) { ...

  1. #1
    Registered User QuaX's Avatar
    Join Date
    Apr 2009
    Posts
    12

    1 to n multiplication of matrices(For Loop Question)

    hi again, i have the following code which gives m1^2 (m1 is a user defined square matrix).

    Code:
      for(i=0;i<n;i++)
      {
                      for(j=0;j<n;j++)
                      {
                                      mult[i][j]=0;
                                      for(r=0;r<n;r++)
                                      {
                                      mult[i][j] += m1[i][r]*m1[r][j];
                                      }
                      }
      }
    My whole program should compute the m1 + m1^2 + m1^3 +..+ m1^n
    My question here is how can i add another m1 in this loop?

  2. #2
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    I don't really know about matrices, but why don't you define a function matrix_pow(int **matrix, int n) which returns m1 to the power of n? Then create a new matrix, add m1 to it and for every number until n add matrix_pow(m1, n) to the matrix.

  3. #3
    Registered User QuaX's Avatar
    Join Date
    Apr 2009
    Posts
    12
    thanks for ur answer, i dont know if i can do it on ur way but i still dont know how to get m1^3 or m1^4

  4. #4
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Easy. You again need a function matrix_mul(int **left, int **right), since pow is a series of multiplications.

    Code:
    int i;
    for (i = 0; i < n; i++)
        result = matrix_mul(result, matrix);

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Yeah. This is what I use to mulitply:
    Code:
    int **multiply (int *A[], int *B[], int Ar, int Ac, int Br, int Bc) { /* The product AB has as many rows as A */
            int **AB=malloc(Ar*sizeof(int*)), i, ii, iii;           /* and as many columns as B. */
            if (Ac!=Br) return NULL;        
            for (i=0; i<Ar; i++) {                  /* i is a new row for AB */
                    AB[i]=malloc(sizeof(int)*Bc);           /* ii is a column for i */
                    for (ii=0; ii<Bc; ii++)                         /* iii is a column from A and a row from B */
                            for (iii=0; iii<Br; iii++) AB[i][ii]+=A[i][iii]*B[iii][ii];
            }
            return AB;
    }
    There's a lot of stuff in there you could get rid of if you are dealing with square matrixes.

    Then like Brafil says, you multiply the matrix by itself, and keep multiplying the result by the original matrix for each power.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User QuaX's Avatar
    Join Date
    Apr 2009
    Posts
    12
    Actually it looks very complicated to me as im on very early phase of programming,

    Isnt there another way to do it with For loop ?

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by QuaX View Post
    Actually it looks very complicated to me as im on very early phase of programming,

    Isnt there another way to do it with For loop ?
    Look closer; if you know how to multiply a matrix you should be able to follow this easily (the reason I added those notes originally was because I'd never done matrix multiplication before, so I learned the procedure (rows by columns) from a website).

    You are pretty much doing the same thing in your Original Post. Here's the prototype again:
    Code:
    int **multiply (int *A[], int *B[], int Ar, int Ac, int Br, int Bc)
    It returns a **matrix AB. The first two parameters are the the matrices to multiply; AB is the product. Since these are not necessarily square and could be of any size, rather than try and measure by dividing with sizeof, I just include the dimensions (Ar = rows in A, Ac = columns in A, etc). So "if (Ac!=Br)" is just a check to make sure that B has as many rows as A has columns -- otherwise the matrices cannot be multiplied

    If you are using square matrices that are of the same set size, and don't want to use malloc, you could simplify it a lot:
    Code:
    void multiply (int A[3][3], int B[3][3], int AB[3][3]) { 
            int i, ii, iii;                 
            for (i=0; i<3; i++) {                   
                    for (ii=0; ii<3; ii++)                  
                            for (iii=0; iii<3; iii++) AB[i][ii]+=A[i][iii]*B[iii][ii];
            }
    }
    Make sure AB is all zeros first!

    Here's a demo that multiplies by an "identity matrix" (B), so the product (AB) should be identical to A:
    Code:
    #include <stdio.h>
    
    void show (int matrix[3][3]) {
            int i, ii;
            for (i=0; i<3; i++) {
                    for (ii=0; ii<3; ii++) printf("%5d",matrix[i][ii]);
                    printf("\n");
            }
    }
    
    void multiply (int A[3][3], int B[3][3], int AB[3][3]) { 
            int i, ii, iii;                 
            for (i=0; i<3; i++) {                   
                    for (ii=0; ii<3; ii++)                  
                            for (iii=0; iii<3; iii++) AB[i][ii]+=A[i][iii]*B[iii][ii];
            }
    }
    
    
    int main() {
            int mx1[3][3]={ {3,-1,-2}, {2,-2,-1}, {6, 6, 6} },           
                    mx2[3][3]={ {1,0,0}, {0,1,0}, {0,0,1} }, 
                    product[3][3]={{0,0,0}};
            puts("__A__");
            show(mx1);
            puts("__B__");
            show(mx2);
            multiply(mx1,mx2,product);
            puts("__AB__");
            show(product);
            return 0;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User QuaX's Avatar
    Join Date
    Apr 2009
    Posts
    12
    Now its more understandable for me thanks for ur answer. Now theres another problem; the matrix size is defined by user and when i make it just 'n' in the function, program begins infinite loop and crashes.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by QuaX View Post
    the matrix size is defined by user
    Well, so now you see the logic go back and have a look at the dynamic allocation version. If the array size is set by the user, the best way to go is to use malloc() (and free()!). If you're not supposed to be using malloc yet, keep playing around with the parameters and such (getting this stuff straight was maybe the hardest part of learning basic C); if you have code you think should work but doesn't post it.

    Eventually, I realized I was only frustrating myself by trying to do things "for the first time" in actual project code. It's better to to distil the problem down to its essential components and do separate little experiments, then apply the logic back into your project -- even if it SEEMS like that will be more typing
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User QuaX's Avatar
    Join Date
    Apr 2009
    Posts
    12
    Thanks for ur answers and advice, but for now i have to finish this project asap so i will try For Loop to make this working , at least it looks a bit easier than messing with malloc etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 12:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

Tags for this Thread


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