Question about 2D-array notation and matrices

This is a discussion on Question about 2D-array notation and matrices within the C++ Programming forums, part of the General Programming Boards category; I have written a program that does matrix multiplication as in the example below and which gives me correct results ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    22

    Question about 2D-array notation and matrices

    I have written a program that does matrix multiplication as in the example below and which gives me correct results (compared to online calculators).

    Name:  MatrixAB.png
Views: 210
Size:  2.1 KB

    Code:
    int A[3][3] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };
    int B[3][3] = {
        {a, b, c},
        {d, e, f},
        {g, h, i}
    };
    
    // multiplication along the lines of
    // (1 * a) + (2 * d) + (3 * g)
    C[0][0] = A[0][0] * B[0][0] + A[0][1] * B[1][0] + A[0][2] * B[2][0];
    ...
    However I'm a little unsure whether I can write matrices that I find in math texts in the order in which I see them.

    Taking this matrix for right-handed, CCW rotation around the x-axis for example:

    Name:  MatrixRotX.png
Views: 177
Size:  1.5 KB

    Can i write this as
    Code:
    // c, s are symbolic
    double Rx[3][3] = {
        {1, 0, 0},
        {0, c, s},
        {0,-s, c}
    };
    Or do the C++ rows represent the columns in the picture above, such that the definition becomes:
    Code:
    double Rx[3][3] = {
        {1, 0, 0},
        {0, c,-s},
        {0, s, c}
    };
    To summarize, I'm unsure whether matrices, they way they are written in math texts, can simply be written into C++ arrays "as they appear".

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    > To summarize, I'm unsure whether matrices, they way they are written in math texts, can simply be written into C++ arrays "as they appear".
    Surely your initial experiment tells you the answer to this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,820
    C++ has no concept of matrices. Therefore, there is no "real" way to convert a mathematical matrix to C++.
    C++ does, however, have two dimensional arrays where you can use two indexes, say r and c, to index through it. However, because C++ has no concept of matrices, there is nothing saying that the first index shall be the row and the second the column. You can invert them if you want. What matters is how you iterate them. Does that make sense?
    There is one thing, though. C++ lays out arrays "row wise" in memory. That means that the array m[0][0...n] comes before m[1][0...n] in memory. Therefore, it is more efficient to iterate the inner dimension first, and the outer last. Although, big matrix multiplication tend to play havoc with caches unless they are optimized.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    22
    I think I get what you're saying. Whether I should use C++ "rows" or "columns" to represent the columns of a matrix is dependent on the order in which I compute with the array later on.

    The reason my question arose is that the mathematical notation, when written with the rows = rows approach, does seem to be in the wrong order (using my multiplication algorithm). So I guess I'll just work with the transpose of the matrix internally (e.g. in a matrix-class constructor) and see if that gives me the desired result while preserving the "look" of textbook matrices in my code.

    Thanks.
    Last edited by -Adrian; 05-09-2013 at 07:43 AM.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,820
    Quote Originally Posted by -Adrian View Post
    I think I get what you're saying. Whether I should use C++ rows or columns to represent the columns of a matrix is dependent on the order in which I compute with the array later on.
    Exactly.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    1,314
    You should follow the normal mathematical conventions, such as the one shown in this wiki article:

    Rotation matrix - Wikipedia, the free encyclopedia

    The layout of a matrix in C / C++ implies that the indices are [row][col]. The offset to data within a matrix will be ((row x number_of_columns) + col) (times size of each element of the matrix).

    When matrices are used for "mapping", the mapping matrix goes on the left, and the data to be mapped goes on the right. This means that mapping of vectors requires you "transpose" them into "vertical" matrices, and end up with a vertical result (1 column, multiple rows), as seen in the wiki article.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expression: Convert infix notation to postfix notation.
    By Nutshell in forum C Programming
    Replies: 7
    Last Post: 02-27-2010, 06:44 AM
  2. big O notation question
    By l2u in forum C++ Programming
    Replies: 7
    Last Post: 11-08-2008, 02:53 PM
  3. a question about matrices
    By trekintouretter in forum C Programming
    Replies: 1
    Last Post: 10-21-2007, 07:24 PM
  4. Question to do with matrices
    By Wiretron in forum C Programming
    Replies: 14
    Last Post: 12-21-2006, 06:16 AM
  5. realloc and array notation?
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-05-2002, 05:02 PM

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