Thread: power operation on a matrix question..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    power operation on a matrix question..

    i got a square matrix
    and i need to calculate the matrix when it set by some power
    i tried this one
    it didnt work
    ??
    Code:
    for (tndex=0;power>=tndex;tndex++){
            for (index=0;index<rows;index++){
    
               for (kndex=0;kndex<cols;kndex++){
    
                 sum[index][kndex]=sum[index][kndex]+matrix[index][kndex]*matrix[kndex][index]
               }
               
            }
    }
                                              //end i multiplication

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you know how to do matrix multiplication?

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    yes
    i read this
    http://en.wikipedia.org/wiki/Matrix_multiplication

    but how to keep the sum matrix?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, given that what you've posted bears no resemblance to the wiki link you gave, I would suggest reading the wiki link again. The (i,j) element of your answer (I guess that's what you want sum to be?) will be the sum of the products of the elements of the i'th row of the first matrix and the j'th column of the second matrix.

    Note that these matrices will be identical to your original matrix only in the case of finding a square; if you need to find a cube, for instance, you would first find the square of your matrix, and then multiply your matrix by that squared matrix.

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i know the algorithm

    i tried again its not working

    Code:
            for (index=0;kndex<cols;index++){
    
               for (kndex=0;kndex<cols;kndex++){
    
                 sum2=sum2+matrix[index][kndex]*matrix[kndex][index];
                 sum[index][kndex]=sum2;
               }
               printf("\n");
            }

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i need to put a square matrix on some input power

    i tried to copy the algoritm
    Code:
    http://en.wikipedia.org/wiki/Matrix_multiplication
    Code:
    #include <stdio.h>
    
    
    
    
    int main(){//start
        int rows,cols;
        int sum2=0;
        printf("enter rows and cols [1..50] ==> ");
        scanf("&#37;d %d",&rows,&cols);
        int matrix[rows][cols];
        int sum[rows][cols];
        int transpose [cols][rows];
        int index,kndex;
    
        for (index=0;index<rows;index++){
    
            for (kndex=0;kndex<cols;kndex++){
               matrix[index][kndex]=0;
               sum[index][kndex]=0;
               transpose[index][kndex]=0;
            }//end inner for
        }//end outer for
    
                                                     //stat input
    
          for (index=rows-1;index>=0;index--){
    
               for (kndex=cols-1;kndex>=0;kndex--){
    
                 scanf("%d",&matrix[index][kndex]);
    
                 transpose [kndex][index]=matrix[index][kndex];
               }
    
            }
    
    getchar();  //needed because of scanf()
                                                 //end input
    
    
    
    
    
    
    
    
    
    
                                                 //start power multiplication
    
       for (index=0;kndex<power;index++){
            for (index=0;kndex<cols;index++){
    
               for (kndex=0;kndex<cols;kndex++){
    
                 sum2=sum2+matrix[index][kndex]*matrix[kndex][index];
                 sum[index][kndex]=sum2;
               }
               
            }
    }
                                              //end power multiplication
    
    
                    //start print
    
            for (index=0;index<rows;index++){
    
               for (kndex=0;kndex<cols;kndex++){
    
                 printf("%d ",sum[index][kndex]);
               }
               printf("\n");
            }
    
                                              //end print
    
                    printf("\n");
    
    
    
    
    
        return 0;
    }//end main func

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You probably should compute every element of the answer array, not just the diagonal elements [0][0], [1][1], etc. Note that it is not possible to do this without three nested for-loops (two to figure out which entry in the answer matrix we're trying to find, and one to do the sum) -- right now you're trying to make one for-loop go through the entire matrix, which is why you're only getting the diagonal entries.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit operation question
    By mr_coffee in forum C Programming
    Replies: 3
    Last Post: 04-07-2009, 05:00 PM
  2. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  3. Gauss-Jordan Matrix Inversion in C++
    By Max_Power82 in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2006, 08:31 PM
  4. Replies: 3
    Last Post: 04-29-2005, 02:03 AM
  5. Question about view vector, position, and matrix speed
    By Silvercord in forum Game Programming
    Replies: 1
    Last Post: 02-03-2003, 12:37 PM