# Thread: 1 to n multiplication of matrices(For Loop Question)

1. ## 1 to n multiplication of matrices(For Loop Question)

hi again, i have the following code which gives m1^2 (m1 is a user defined square matrix).

Code:
```  for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
mult[i][j]=0;
for(r=0;r<n;r++)
{
mult[i][j] += m1[i][r]*m1[r][j];
}
}
}```
My whole program should compute the m1 + m1^2 + m1^3 +..+ m1^n
My question here is how can i add another m1 in this loop?

2. I don't really know about matrices, but why don't you define a function matrix_pow(int **matrix, int n) which returns m1 to the power of n? Then create a new matrix, add m1 to it and for every number until n add matrix_pow(m1, n) to the matrix.

3. thanks for ur answer, i dont know if i can do it on ur way but i still dont know how to get m1^3 or m1^4

4. Easy. You again need a function matrix_mul(int **left, int **right), since pow is a series of multiplications.

Code:
```int i;
for (i = 0; i < n; i++)
result = matrix_mul(result, matrix);```

5. Yeah. This is what I use to mulitply:
Code:
```int **multiply (int *A[], int *B[], int Ar, int Ac, int Br, int Bc) { /* The product AB has as many rows as A */
int **AB=malloc(Ar*sizeof(int*)), i, ii, iii;           /* and as many columns as B. */
if (Ac!=Br) return NULL;
for (i=0; i<Ar; i++) {                  /* i is a new row for AB */
AB[i]=malloc(sizeof(int)*Bc);           /* ii is a column for i */
for (ii=0; ii<Bc; ii++)                         /* iii is a column from A and a row from B */
for (iii=0; iii<Br; iii++) AB[i][ii]+=A[i][iii]*B[iii][ii];
}
return AB;
}```
There's a lot of stuff in there you could get rid of if you are dealing with square matrixes.

Then like Brafil says, you multiply the matrix by itself, and keep multiplying the result by the original matrix for each power.

6. Actually it looks very complicated to me as im on very early phase of programming,

Isnt there another way to do it with For loop ?

7. Originally Posted by QuaX
Actually it looks very complicated to me as im on very early phase of programming,

Isnt there another way to do it with For loop ?
Look closer; if you know how to multiply a matrix you should be able to follow this easily (the reason I added those notes originally was because I'd never done matrix multiplication before, so I learned the procedure (rows by columns) from a website).

You are pretty much doing the same thing in your Original Post. Here's the prototype again:
Code:
`int **multiply (int *A[], int *B[], int Ar, int Ac, int Br, int Bc)`
It returns a **matrix AB. The first two parameters are the the matrices to multiply; AB is the product. Since these are not necessarily square and could be of any size, rather than try and measure by dividing with sizeof, I just include the dimensions (Ar = rows in A, Ac = columns in A, etc). So "if (Ac!=Br)" is just a check to make sure that B has as many rows as A has columns -- otherwise the matrices cannot be multiplied

If you are using square matrices that are of the same set size, and don't want to use malloc, you could simplify it a lot:
Code:
```void multiply (int A[3][3], int B[3][3], int AB[3][3]) {
int i, ii, iii;
for (i=0; i<3; i++) {
for (ii=0; ii<3; ii++)
for (iii=0; iii<3; iii++) AB[i][ii]+=A[i][iii]*B[iii][ii];
}
}```
Make sure AB is all zeros first!

Here's a demo that multiplies by an "identity matrix" (B), so the product (AB) should be identical to A:
Code:
```#include <stdio.h>

void show (int matrix[3][3]) {
int i, ii;
for (i=0; i<3; i++) {
for (ii=0; ii<3; ii++) printf("%5d",matrix[i][ii]);
printf("\n");
}
}

void multiply (int A[3][3], int B[3][3], int AB[3][3]) {
int i, ii, iii;
for (i=0; i<3; i++) {
for (ii=0; ii<3; ii++)
for (iii=0; iii<3; iii++) AB[i][ii]+=A[i][iii]*B[iii][ii];
}
}

int main() {
int mx1[3][3]={ {3,-1,-2}, {2,-2,-1}, {6, 6, 6} },
mx2[3][3]={ {1,0,0}, {0,1,0}, {0,0,1} },
product[3][3]={{0,0,0}};
puts("__A__");
show(mx1);
puts("__B__");
show(mx2);
multiply(mx1,mx2,product);
puts("__AB__");
show(product);
return 0;
}```

8. Now its more understandable for me thanks for ur answer. Now theres another problem; the matrix size is defined by user and when i make it just 'n' in the function, program begins infinite loop and crashes.

9. Originally Posted by QuaX
the matrix size is defined by user
Well, so now you see the logic go back and have a look at the dynamic allocation version. If the array size is set by the user, the best way to go is to use malloc() (and free()!). If you're not supposed to be using malloc yet, keep playing around with the parameters and such (getting this stuff straight was maybe the hardest part of learning basic C); if you have code you think should work but doesn't post it.

Eventually, I realized I was only frustrating myself by trying to do things "for the first time" in actual project code. It's better to to distil the problem down to its essential components and do separate little experiments, then apply the logic back into your project -- even if it SEEMS like that will be more typing

10. Thanks for ur answers and advice, but for now i have to finish this project asap so i will try For Loop to make this working , at least it looks a bit easier than messing with malloc etc.