Question

i want to potentiate a Matrix but i dont workings how it should work.

m ist the Matrix i want to potentiate

long double pro[100][100]; // product after each step
long double res[100][100]; // the Matrix with the exponent n

for (int n = 1; n < nVal; n++) // exponent
{
    for (int i = 0; i < mVal; i++) // row
    {
        for (int j = 0; j < mVal; j++) // col
        {
            res[i][j] = 0;
            for (int k = 0; k < mVal; k++) // inner
            {
                res[i][j] += pro[i][k] * m[k][j]; // multiply the product with the default matrix
            }
        }
    }
}

// array Output - working
for (int i = 0; i<mVal; i++)
{
    for (int j = 0; j<mVal; j++)
        cout << res[i][j] << "\t";
    cout << endl;
}

in the output i see some crazy numbers and i dont know why :( Can anyone help me?

Was it helpful?

Solution 2

As Willll said you shouldn't forget to initialize.

Another suggestion would be to erase the exponent loop and just use the pow() function from math library. It´ll make it more simple and easier to visualize.

OTHER TIPS

You should

  1. initialise the pro matrix to the identity at the beginning of loop on n
  2. copy the res matrix into the pro matrix the end of each loop on n.

In pseudo code

 pro = Identity matrix
 for (int n = 1; n < nVal; n++) {
     res = pro * m // using two loops
     pro = res
 }
 result is in pro. 

Note that there are much faster way to compute powers: http://en.wikipedia.org/wiki/Exponentiation_by_squaring

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top