Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,413 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,430 people online right now. Registration is fast and FREE... Join Now!




Multiplying Matrices // Nasty For Loops

 
Reply to this topicStart new topic

Multiplying Matrices // Nasty For Loops, Code Provided

Sonastylol
15 Oct, 2008 - 07:21 AM
Post #1

D.I.C Head
**

Joined: 15 Dec, 2007
Posts: 124


My Contributions
Hey guys, Hello Martyr2!

I am trying to put the finishing touches on my Matrix project, but my calculation for multiplying matrices appears to be wrong.

Here is my code first of all, then I will explain:
CODE

MATRIX MATRIX::MultMat(MATRIX MatB)
{
       //create a result matrix
       MATRIX result;
       for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    result.Mat[i][j] = 0;
                    for (int k = 0; k < 3; k++) {
                        result.Mat[i][j] = result.Mat[i][j] + Mat[i][k]*MatB.Mat[k][j];
                    }
                }
       }



When multiplying Matrices, we take the sum of the multiplication of the first row's cells and the first column's cells.

An example of this would be:

[1 2] [5 6] = [19 22]
[3 4] [7 8] = [43 50]

To find the first # of matrix 3, we do:
(1*5) + (2*7) = 19
Next value (to the right of 19) would be:
(1*6) + (2*8) = 22
Completing the Result Matrix:
(3*5) + (4*7) = 43
(3*6) + (4*8) = 50

I hope I'm doing this correctly.

Can you all please help me derive the correct formula for computing this?

It involves a bunch of nasty for loops, and I THOUGHT I had it but I don't appear to be correct.

Thank you so much!


User is offlineProfile CardPM
+Quote Post

Sonastylol
RE: Multiplying Matrices // Nasty For Loops
15 Oct, 2008 - 07:26 AM
Post #2

D.I.C Head
**

Joined: 15 Dec, 2007
Posts: 124


My Contributions
Here is my code for working matrix multiplication.

CODE
MATRIX MATRIX::MultMat(MATRIX MatB)
{
       //create a result matrix
       MATRIX result;
       for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    result.Mat[i][j] = 0;
                    for (int k = 0; k < 3; k++) {
                        result.Mat[i][j] = result.Mat[i][j] + Mat[i][k]*MatB.Mat[k][j];
                    }
                }
       }
      
       //return new result matrix
       return result;
}

Now I have to divide Matrices. How do I do this?

This post has been edited by Sonastylol: 15 Oct, 2008 - 11:07 AM
User is offlineProfile CardPM
+Quote Post

Sonastylol
RE: Multiplying Matrices // Nasty For Loops
16 Oct, 2008 - 09:39 AM
Post #3

D.I.C Head
**

Joined: 15 Dec, 2007
Posts: 124


My Contributions
Bump.

I need to know the formula for dividing matrices.

Thanks!
User is offlineProfile CardPM
+Quote Post

Gloin
RE: Multiplying Matrices // Nasty For Loops
16 Oct, 2008 - 11:37 AM
Post #4

On MeD.i.Cation
Group Icon

Joined: 4 Aug, 2008
Posts: 723



Thanked: 47 times
My Contributions
Matrix division in general is not so simple but if you're only about to define matrix division for a 2x2 matrix there are ways to simplify the problem.

If you have Matrices A and B and wish to calculate Matrix C = A/B = A x Inverse of B (which is also a 2x2 matrix).
You already have a way to solve AxB so all you need is a way to calculate the inverse of B.

Which is done as follows:
Let B be the matrix:

[a b]
[c d]

Calculate e = 1 / ((a * d) - (b * c))

(ad-bc) is the determinant of the matrix and has to be nonzero if the matrix is to have an inverse (it's a good idea to test this in the beginning of your division function).

multiply e by the matrix:

[d -b]
[-c a]

ex:
Let a = 2, b = 1, c = 4, d = 3 gives the following matrix B

[2 1]
[4 3]

ad-bc = 6 - 4 = 2 => e = 1/2

Gives the inverse of B:

[1.5 -0.5]
[-2.0 1.0]

The rest is just to perform matrix Multiplication with matrix A and then you have you result matrix C.

I hope this wasn't too complicated.

This post has been edited by Gloin: 16 Oct, 2008 - 11:43 AM
User is offlineProfile CardPM
+Quote Post

Sonastylol
RE: Multiplying Matrices // Nasty For Loops
16 Oct, 2008 - 12:44 PM
Post #5

D.I.C Head
**

Joined: 15 Dec, 2007
Posts: 124


My Contributions
oh wow, seems pretty complicated.

well my matrices are 3x3, and what about if they were even larger than this?

Seems awful.

I havent been able to find any source code of the matter. darn
User is offlineProfile CardPM
+Quote Post

Gloin
RE: Multiplying Matrices // Nasty For Loops
16 Oct, 2008 - 05:15 PM
Post #6

On MeD.i.Cation
Group Icon

Joined: 4 Aug, 2008
Posts: 723



Thanked: 47 times
My Contributions
Are you really supposed to perform division of one matrix by another or are you only supposed to do division by some constant like A/k??

If you have to do Matrix division of 3x3 matrices then you'll have to do Gauss-elimination (to find the inverse). It's a bit more difficult but definitely doable. Althogh if you haven't seen it before, I guess you're in trouble..

This post has been edited by Gloin: 16 Oct, 2008 - 05:17 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 12:38PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month