Matrices multiplicarion algorithm

This is a discussion on Matrices multiplicarion algorithm within the C++ Programming forums, part of the General Programming Boards category; Hi folks, i'm doing a program that recieves two matrices from the user and multiply them together. All my code ...

  1. #1
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186

    Matrices multiplicarion algorithm

    Hi folks, i'm doing a program that recieves two matrices from the user and multiply them together. All my code is done except for the multiplication function. I know that i must use "for loops" but how many :S

    i'm passint object to be the operand while the the calling object is having the "this" pointer, it goes like this

    let A be the first matrice
    let B be the second matrice

    A.operator*(B);

    i have absolutely no idea, this is the function:
    Code:
    Matrix Matrix::operator*(const Matrix& rhs)
    {
           Matrix result;
           
           //is this correct ?
           result = *(this)*rhs;
           
           if (this == &rhs)
           {
                    return *this;
           }
           else 
           {
                //Checks if matrices are able to be multiplied 
                if (this->m_Columns == rhs.m_Rows)
                {
                                    //Do multiplication                  
                }
                else
                {
                    cout << "Cannot perform multiplication." << endl;
                    cout << "Columns of first matrix don't ";
                    cout << "match the rows of the second matrix" << endl;
                }
           }
           
           return result;
    }
    ca any one help please
    Last edited by Hussain Hani; 04-03-2007 at 01:58 AM.
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    Well have you looked up how to do matrix multiplication on paper?
    Say
    http://mathworld.wolfram.com/MatrixMultiplication.html
    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
    Registered User
    Join Date
    Apr 2007
    Location
    Sweden
    Posts
    41
    Code:
           //is this correct ?
           result = *(this)*rhs;
    I wouldn't believe so? That just calls the operator* method recursively, right? Over and over again...

    The simplest algorithm for matrix multiplication is O(n^3), and as such would require three for-loops. Saying anything more than that would probably give away the solution (I suspect this is homework). If you can do it on paper, you should be able to translate it to a computer program with a little effort.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    There are more complicated algorithms, though, which are asymptotically more efficient than O(n^3) - see

    http://en.wikipedia.org/wiki/Matrix_multiplication

    (though implementing them is way beyond a homework assignment).

  5. #5
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    it's not any homework i'm just doing that to improve my operator overloading skills + improving my dynamic memory use

    also i've taken vow that i will never take C++ to the next level (API + DirectX) until i make decent dos math program
    Last edited by Hussain Hani; 04-03-2007 at 11:53 AM.
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 01:30 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Problem multiplying rotation matrices together
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 03-04-2003, 08:20 AM

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