Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

6 by 6 matrix multiplicaiton with C

  1. Sep 11, 2012 #1
    Hi friends.

    I'm trying to define five 6 by 6 matrices with C code. I need to multiply them than. Those matrices are different from each other. Each matrix has fixed elements but some elements of that matrices depends on other mathematical operations.
    For example imagine I have a matrix which is 6 by 6 and its elements are
    M(1,1) = cos α
    M(1,2) = ρ (sin α)
    M(1,3) = 0
    M(1,4) = 0
    M(1,5) = 0
    M(1,6) = ρ (1-cos α)
    M(2,1) = (-sin α) /ρ
    M(2,2) = cos α
    M(2,3) = 0
    M(2,4) = 0
    M(2,5) = 0
    M(2,6) = sin α
    M(3,1) = 0
    M(3,2) = 0
    M(3,3) = 1
    M(3,4) = ρ.α
    M(3,5) = 0
    M(3,6) = 0
    M(4,1) = 0
    M(4,2) = 0
    M(4,3) = 0
    M(4,4) = 1
    M(4,5) = 0
    M(4,6) = 0
    M(5,1) = sin α
    M(5,2) = ρ (1-cos α)
    M(5,3) = 0
    M(5,4) = 0
    M(5,5) = 1
    M(5,6) = ρ (α- sin α)
    M(6,1) = 0
    M(6,2) = 0
    M(6,3) = 0
    M(6,4) = 0
    M(6,5) = 0
    M(6,6) = 1

    In here ρ and α are calculated by other mathematical operations.
    For example ;

    The program will ask to the user about p and b, than calculate ρ and put that value to the matrix. All five matrices have some elements like that. They need to be defined like that and multiplied to obtain a final matrix.

    Can anybody help me ?
    Last edited: Sep 11, 2012
  2. jcsd
  3. Sep 11, 2012 #2
    What exactly is your question?

    A way to define the matrix is to first assign zeros to all elements with a 'for' loop, then assign values to particular elements according to the formulas you have got there.
  4. Sep 11, 2012 #3
    My question is how to define the matrices and how to assign that fixed and calculated values as their specified elements.
  5. Sep 11, 2012 #4
    Okay then I answered your question in my previous post.
  6. Sep 11, 2012 #5
    please explain it in an explanatory way. I do not understand how to do it.
    Last edited: Sep 11, 2012
  7. Sep 11, 2012 #6
    Okay, write down step by step what it is that you need to do. Then I help you with how to do them.
  8. Sep 11, 2012 #7
    First, I need to define 6 by 6 matrix and give a name to it and do the same thing for other four matrices. In the end I need to have five 6 by 6 matrices.

    Second, I need to define the matrices variables. Some are fixed as 0 or 1 however some are derived from other mathematical operations.
  9. Sep 11, 2012 #8
    Are you familiar with coding in C?

    Do you know how to define variables, in particular matrices, in C?

    Are you familiar with a "for" loop?
  10. Sep 11, 2012 #9
    Not perfect but I know something.

    I do not know how to define a matrix.

    Like I said before, I know something but not perfectly.
  11. Sep 11, 2012 #10

    First you should learn about loops. Play around with a couple of for loops to get some understanding. Let me know if you have any trouble/are comfortable.
  12. Sep 11, 2012 #11
    A matrix in C is represented by a 2 dimensional array. Here's a tutorial on 2 dimensional arrays. Try out the example given there. Once you have this done, we can apply all this to your question.
  13. Sep 11, 2012 #12
    I think I got it. In that example "int a[3][3]" provides a definition of 3x3 matrix with integer elements and named as "a"

    Is that right? If so, since my matrices are 6x6 and the elements are double, I need to rewrite it as "double matrixname [6][6]" Is that right ?

    One more thing. In here -with a for loop- the elements of the matrix entered by the user. However, in my case, user enters nothing directly related about matrices. As I posted on my first message, user will enter some variables and the mathematical operations of that results will be the element of matrices. Also some elements are pre-defined.

    How am I pass this ?
  14. Sep 11, 2012 #13
    Maybe I should define them in the code rather than asking from the user ?
    Something like that works ?

    double a,b,c;

    printf("enter b : "%d);

    printf("enter c : "%d);

    double matrixname [6][6] ={{1, 2, 3, a, 5, 6},{2, 3, 4, 5, 6, 1}, {3, 4, 5, 6, 1, 2}, {4, 5, 6, 1, 2, 3}, {5, 6, 1, 2, 3, 4}, {6, 1, 2, 3, 4, 5}};

    printf ("double matrixname [6][6]") ;
  15. Sep 11, 2012 #14


    Staff: Mentor

    There are several problems with the code here.
    1. printf("enter b : "%d);
    This should be printf("enter b: ");
    2. scanf("%d",b);
    This should be scanf("%lf",&b);
    The conversion specifier for a double is %lf. Also, when you use scanf, you need to provide the address of the variable you are inputting to.
    3. printf ("double matrixname [6][6]") ;
    This will print the string "double matrixname [6][6]". It will NOT print the value of matrix[6][6].
  16. Sep 11, 2012 #15
    You're on the right track (but have syntax errors, as Mark44 points out).

    Since most values in your matrix are 0's, assign 0 to all of them (build up on the example given in the link).

    Now ask the user for 'p' and 'b', then calculate ρ and a, and assign each non-zero matrix element the corresponding value, like this:
    M[1][1] = cos a;
    M[6][6] = 1;
  17. Sep 12, 2012 #16
    Thanks guys.
    Let me try to continue. So now, I need to define nine 6 by 6 matrices since I want to multiply five 6 by 6 matrices.

    Maybe it's better to write it like that ;
    Name of Matrix 1: dsi
    Name of Matrix 2: mein
    Name of Matrix 3: mbdy
    Name of Matrix 4: meex
    Name of Matrix 5: dsf
    Name of Matrix 6: dm ==> dm=mein.dsi
    Name of Matrix 7: dmm ==> dmm=dm.mbdy
    Name of Matrix 8: dmx ==> dmx=dmm.meex
    Name of Matrix 9: dmf ==> dmf=dmx.dsf

    My final matrix after all multiplication process will be dmf, right ?

    And as Sourabh N said, I need to define all that matrices initillay as zero matrices with the following code ;

    Code (Text):

    #include <stdio.h>
    #include <conio.h>
    void main()
        double dsi[6][6], i, j;
        for(i=0; i<6; i++)
            for(j=0; j<6; j++)
    And the others will be defined like that, am I right?

    Than I need to define non-zero elements of the matrices like,
    Code (Text):
    dsi[1][2]=si ;
    dsi[1][5]=cos α;
    where si is an input entered by user and α is a calculated variable which depends other inputs mathematical operations. Of course I need to define that values in the code first like,
    Code (Text):
    int si ;
    double α;
    an so on.

    So far right or am I wrong ?
  18. Sep 12, 2012 #17
    I'm still waiting for some help.

    Can anybody help me ?
  19. Sep 12, 2012 #18


    User Avatar

    Staff: Mentor

    Some parts are OK, but you are mostly wrong.

    You don't need nine matrices, you can store some of the intermediate results in one helper matrix. This way you need less memory. In the case of 6*6 matrices it doesn't matter much, but you should learn to think this way always, memory and processor time are resources that you want to save.

    It is not doing what you think it is doing. You want to zero matrix elements, not indices.

    No such thing as α, you can use only standard ASCII characters.

    Also note dsi[1][5] is most likely not the element you are interested in, indices start with 0 in C (so to get first element from the array a you need a[0], not a[1] - that would be the second element).
  20. Sep 12, 2012 #19
    Thank you Borek.

    Then how am I suppose to get it ?

    I know at least that one. That is just for representation. Is that allowed to use more than one letter to define a variable ?
  21. Sep 12, 2012 #20


    User Avatar

    Staff: Mentor

    You can use i and j as indices - dsi[j].

    So you have named your matrix dsi but you wonder if variable name can have more than one letter? Matrix is a variable, just of different type.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: 6 by 6 matrix multiplicaiton with C