# Help in standard C

Joined Dec 29, 2004
83
Hi ,
I want to compute a geomtric series with matrices. I have the following functions:

mult(matirx1,matrix2) =function to multiply two matrices.

I want to create this function in C that computes the following.

I(indentity matrix)

I+A+A^2+A^3+....A^k

The sum should stop when all the entries in A^k are less than .00001.

I tried

sum=I;
for(i=0;i<k;i++)
{
intermediary=mult(I,A);
sum+=intermediary;
intermediary=mult(intermediary,A)
}

In fact I need to use a recursive form but I just dont know how to do it.

Thank you for yout help
B

#### Papabravo

Joined Feb 24, 2006
18,458
Originally posted by braddy@Mar 3 2006, 04:16 PM
Hi ,
I want to compute a geomtric series with matrices. I have the following functions:

mult(matirx1,matrix2) =function to multiply two matrices.

I want to create this function in C that computes the following.

I(indentity matrix)

I+A+A^2+A^3+....A^k

The sum should stop when all the entries in A^k are less than .00001.

I tried

sum=I;
for(i=0;i<k;i++)
{
intermediary=mult(I,A);
sum+=intermediary;
intermediary=mult(intermediary,A)
}

In fact I need to use a recursive form but I just dont know how to do it.

Thank you for yout help
B
[post=14584]Quoted post[/post]​
A couple of observations
1. You don't NEED recursion
2. Multiplying A by I is a complete waste of time. You already know the answer
3. You did not use your add function for sum += intermediary
4. You need a function Ak_small(Ak) that returns TRUE or FALSE, to test each element of A^k for being less than .00001
5. Use a while loop instead of a for loop
Example
notdone = 1 ;
while(notdone)
{
....
if( Ak_small(Ak)) notdone = 0 ;
}

Hope this helps

Joined Dec 29, 2004
83
Originally posted by Papabravo@Mar 3 2006, 04:59 PM
A couple of observations
1. You don't NEED recursion
2. Multiplying A by I is a complete waste of time. You already know the answer
3. You did not use your add function for sum += intermediary
4. You need a function Ak_small(Ak) that returns TRUE or FALSE, to test each element of A^k for being less than .00001
5. Use a while loop instead of a for loop
Example
notdone = 1 ;
while(notdone)
{
....
if( Ak_small(Ak)) notdone = 0 ;
}

Hope this helps
[post=14598]Quoted post[/post]​
Thank you very much for your suggestions I will try to build on them.
B.

#### CoulombMagician

Joined Jan 10, 2006
37
You might want to add a limit counter inside the while loop anyway because

1. The function might not converge for every value of the argument A. The limit counter should terminate the while loop with some error code in this case.

2. During development programmers have been known to create a bug every now and then, it's nice to have a planned exit from a potentially infinite loop.

#### Papabravo

Joined Feb 24, 2006
18,458
Originally posted by CoulombMagician@Mar 4 2006, 02:26 AM
You might want to add a limit counter inside the while loop anyway because

1. The function might not converge for every value of the argument A. The limit counter should terminate the while loop with some error code in this case.

2. During development programmers have been known to create a bug every now and then, it's nice to have a planned exit from a potentially infinite loop.
[post=14629]Quoted post[/post]​
These are both excellent suggestions.

Joined Dec 29, 2004
83
Originally posted by Papabravo@Mar 4 2006, 11:41 AM
These are both excellent suggestions.
[post=14656]Quoted post[/post]​

This is my code:
I get an infinite loop and I dont know why..?

some info first:
matad(A,B,C,size) function where C is the result of matrix addition A+B
matmpy(A,B,C,size) multiplication function where C is the result of A * B
eye is the Identity matrix

double bino(double A[][MAXSIZE],double eye[][MAXSIZE],double Ap1[MAXSIZE][MAXSIZE],int size)
{
int notdone,i,j;
int count1;
int countbino;
double M[MAXSIZE][MAXSIZE];
double S[MAXSIZE][MAXSIZE];
double O[MAXSIZE][MAXSIZE];
double N[MAXSIZE][MAXSIZE];
double H[MAXSIZE][MAXSIZE];

matmpy(A,eye,Ap1,size); /* to make Ap1=A*/

countbino=0;
count1=0;
notdone=1;

while(notdone=1)
{

matmpy(A,Ap1,Ap1,size);

for(i=0;i<size;i++){
for(j=0;j<size;j++)
{ if(Ap1[j]<0.)
Ap1[j]=-(Ap1[j]);}
}

for(i=0;i<size;i++)
{ for(j=0;j<size;j++)
{ if(Ap1[j]<.00001)
countbino+=1;
}
}

if(countbino==(size*size))
{/* if begins*/
return (Ap1[j]);
count1+=1;
notdone=0;
break;
}

else
{
notdone=1;
count1+=1;
}
}
}/* end of bino*/

PLease do you see where the problem is ??
I have been searching since yesterday but nothing

Thank you
B

#### CoulombMagician

Joined Jan 10, 2006
37
Think about while(notdone=1) vs. while(notdone==1).