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

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




storing the result of matrix multiplication as a scalar

 
Reply to this topicStart new topic

storing the result of matrix multiplication as a scalar, Easy stuffs to the Experts !!!

m.babanfati
13 Apr, 2008 - 09:24 AM
Post #1

New D.I.C Head
*

Joined: 10 Dec, 2007
Posts: 15


My Contributions
Hello all;

I have tried my best to see to the end of this "TROUBLE", but still I couldn't figure out what the problem is.
I want to multiply a row vector M1(1X3) to a column vector M2(3X1) and store the resultant in a memory location assigned to a float variable M1_M2(scalar).
the M1 = [1, 4, 7]; while M2 = [3, 5, 9]' and the expected result would be M1_M2 = 86. But to my surprise my code below "amazingly" returns 2.70933e+259 in place of the actual value of 86.

Please help me out

CODE

#include <iostream>
#include <vector>
#include <math.h>
#include <cstdlib>
using namespace std;

// nRows and nCols stands for the number of rows and columns for the array
const int nRows1 = 1;
const int nCols1 = 3;
const int nCols2 = 1;
const int nRows2 = 3;
// Method to multiply the two arrays
double multiplyMatrix(double M1[nRows1][nCols1], double M2[nRows2][nCols2], double M1_M2) {
    double X[10][10][10];
    int row,col,m; // counters
    for(row = 0; row < nRows1; row++) {
        for(col=0;col<nCols2;col++) {
          // initialise the resultant variable to 0.
          M1_M2 = 0;
          for(m=0;m<3;m++) {
                X[row][col][m] += M1[row][m]*M2[m][col];
                M1_M2 += X[row][col][m];
                }
        }
    }
}

int main () {
    double M1[nRows1][nCols1] = {1, 4, 7};   // declaration of row vector of order 1 by 3
    double M2[nRows2][nCols2] = {{3},{5},{9}}; // declaration of column vector of order 3 by 1
    double M1_M2;             // declaration of the scalar to store the result of multiplying both vectors
    multiplyMatrix(M1, M2, M1_M2); // function call
    cout << M1_M2 << endl;      // to display the result
    return 0;
}


User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Storing The Result Of Matrix Multiplication As A Scalar
14 Apr, 2008 - 05:45 AM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
your problem would seem to be in using an uninitialized variable:
X[row][col][m] += M1[row][m]*M2[m][col];.

to do this you would need to remove the + in the +=.

But why are you doing that anyway? Why is X even a 3D matrix?

that inner loop could be:
CODE
          M1_M2 = 0;
          for(m=0;m<3;m++) {
                M1_M2 += M1[row][m]*M2[m][col];
                }
and work just as well.
User is offlineProfile CardPM
+Quote Post

m.babanfati
RE: Storing The Result Of Matrix Multiplication As A Scalar
14 Apr, 2008 - 07:45 AM
Post #3

New D.I.C Head
*

Joined: 10 Dec, 2007
Posts: 15


My Contributions
QUOTE(NickDMax @ 14 Apr, 2008 - 06:45 AM) *

your problem would seem to be in using an uninitialized variable:
X[row][col][m] += M1[row][m]*M2[m][col];.

to do this you would need to remove the + in the +=.

But why are you doing that anyway? Why is X even a 3D matrix?

that inner loop could be:
CODE
          M1_M2 = 0;
          for(m=0;m<3;m++) {
                M1_M2 += M1[row][m]*M2[m][col];
                }
and work just as well.


Thanks alot NickDMax, as ur suggestion helped along way in solving the problem.
As for the 3D array, I did it with the intention of giving enough memory for all manipulations, but after your your suggestion, I realised it was a complete nuisance. So everything works as expected.
Once again Thanks and keep up the good job !
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 07:22PM

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