CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Faltering Windows support
  • Internet Explorer 8 Click Clever Click Safe
  • Release Candidate 2 for ASP.NET MVC 2
  • Learn How to Create Dual Mode Windows Services

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ (Non Visual C++ Issues)
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    C++ (Non Visual C++ Issues) Ask or answer C and C++ questions not related to Visual C++. This includes Console programming, Linux programming, or general ANSI C++.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 9th, 2009, 09:58 PM
    dukebball dukebball is offline
    Junior Member
     
    Join Date: Oct 2009
    Posts: 7
    dukebball is an unknown quantity at this point (<10)
    Matrix multiplication

    Hey, so I have to make a program that has to multiply two 3x3 matrices (among other things). I used classes to do the project, but I just can't seem to get this multiplication to work. Here is my code so far:

    Code:
    void matrix::mult(const matrix& u)
    {
        double c[9];
        double a[9];
        for (int i=0; i<rows; i++)
        {
            for (int j=0; j<columns; j++)
            {
                for(int r=0; r<columns; r++)
                {
                    c[i*columns+j]+=a[i*columns+r]*u.data[r*columns+j];
                }
            }
        }
    }
    Any advice would be great, and please let me know if there's anything I should clarify. Thank You.

    Last edited by dukebball; November 9th, 2009 at 10:03 PM.
    Reply With Quote
      #2    
    Old November 9th, 2009, 11:54 PM
    Lindley Lindley is offline
    Elite Member
    Power Poster
     
    Join Date: Oct 2007
    Location: Fairfax, VA
    Posts: 6,898
    Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+)
    Re: Matrix multiplication

    You don't seem to be initializing the c array prior to using it, and I have no idea what you think the a array contains, because it appears to be uninitialized as well from that code.
    Reply With Quote
      #3    
    Old November 10th, 2009, 02:12 AM
    deightm deightm is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 2
    deightm is an unknown quantity at this point (<10)
    Re: Matrix multiplication

    void matrix::mult(const matrix& u)
    {
    unsigned char i=0,j=0,k=0;

    double c[9];
    for(i=0;i<9;i++) c[i] = 0;

    for(i=0;i<3;i++)
    for(j=0;j<3);j++)
    for(k=0;k<3;k++)
    c[i*3+j] += this->data[i*columns+j] * u.data[k*columns+j];
    }

    This should set c = "this matrix" * u, assuming that "data" is double[9] array which stores matrix value;
    Reply With Quote
      #4    
    Old November 10th, 2009, 03:04 AM
    bitshifter420's Avatar
    bitshifter420 bitshifter420 is offline
    Member
     
    Join Date: Jun 2007
    Location: MA-USA
    Posts: 227
    bitshifter420 has a spectacular aura about (100+) bitshifter420 has a spectacular aura about (100+)
    Re: Matrix multiplication

    Rule #1) Use single dimensional array for speed and clarity.
    Rule #2) Dont use a loop, even if your compiler can unroll them.
    Rule #3) Use SIMD for this type of operation (do 4 muls at once)

    Tomorrow when i get to my other PC i will have some code to share.
    I have recently written an entire matrix library in assembly language for SSE2.
    This library was developed by me and a friend and each procedure
    was carefully coded and clocked using high resolution uops counters.
    If you dont like assembly language then use intrinsics instead.

    Edit:
    Ooh, i found a copy of my C version right here on this HDD
    Code:
    void MultiplyMatrices(float dst[16], const float a[16], const float b[16])
    {
       dst[0]  = a[0] * b[0]  + a[4] * b[1]  + a[8]  * b[2]  + a[12] * b[3];
       dst[1]  = a[1] * b[0]  + a[5] * b[1]  + a[9]  * b[2]  + a[13] * b[3];
       dst[2]  = a[2] * b[0]  + a[6] * b[1]  + a[10] * b[2]  + a[14] * b[3];
       dst[3]  = a[3] * b[0]  + a[7] * b[1]  + a[11] * b[2]  + a[15] * b[3];
    
       dst[4]  = a[0] * b[4]  + a[4] * b[5]  + a[8]  * b[6]  + a[12] * b[7];
       dst[5]  = a[1] * b[4]  + a[5] * b[5]  + a[9]  * b[6]  + a[13] * b[7];
       dst[6]  = a[2] * b[4]  + a[6] * b[5]  + a[10] * b[6]  + a[14] * b[7];
       dst[7]  = a[3] * b[4]  + a[7] * b[5]  + a[11] * b[6]  + a[15] * b[7];
    
       dst[8]  = a[0] * b[8]  + a[4] * b[9]  + a[8]  * b[10] + a[12] * b[11];
       dst[9]  = a[1] * b[8]  + a[5] * b[9]  + a[9]  * b[10] + a[13] * b[11];
       dst[10] = a[2] * b[8]  + a[6] * b[9]  + a[10] * b[10] + a[14] * b[11];
       dst[11] = a[3] * b[8]  + a[7] * b[9]  + a[11] * b[10] + a[15] * b[11];
    
       dst[12] = a[0] * b[12] + a[4] * b[13] + a[8]  * b[14] + a[12] * b[15];
       dst[13] = a[1] * b[12] + a[5] * b[13] + a[9]  * b[14] + a[13] * b[15];
       dst[14] = a[2] * b[12] + a[6] * b[13] + a[10] * b[14] + a[14] * b[15];
       dst[15] = a[3] * b[12] + a[7] * b[13] + a[11] * b[14] + a[15] * b[15];
    }
    If you want the SSE2 assembly code version just let me know...

    Last edited by bitshifter420; November 10th, 2009 at 03:18 AM.
    Reply With Quote
      #5    
    Old November 10th, 2009, 03:23 AM
    potatoCode potatoCode is offline
    Member +
     
    Join Date: Jan 2008
    Location: California, USA
    Posts: 750
    potatoCode will become famous soon enough (80+)
    Re: Matrix multiplication

    never mind.
    __________________
    Hello CG members! I'm currently seeking an employment as a C++ software developer.
    If your company is hiring,
    please be kind-hearted and help me find work by putting in a referral,
    or simply notify me of the opportunity by dropping me a Private Message.
    Many thanks

    Check out Geany for a lightweight and fast PHP/C++ IDE.
    ------------------------------------------------------------------------------------------------
    Please use [code]...[/code] tags when posting code, and please rate the posts you find it helpful

    Last edited by potatoCode; November 10th, 2009 at 03:26 AM.
    Reply With Quote
      #6    
    Old November 10th, 2009, 10:40 AM
    Lindley Lindley is offline
    Elite Member
    Power Poster
     
    Join Date: Oct 2007
    Location: Fairfax, VA
    Posts: 6,898
    Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+)
    Re: Matrix multiplication

    See, if I needed that degree of optimization, I'd just use one of the optimized BLAS implementations.
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ (Non Visual C++ Issues)


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 10:57 AM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.