Help in standard C

Discussion in 'Programmer's Corner' started by braddy, Mar 3, 2006.

  1. braddy

    Thread Starter Well-Known Member

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

    add (matrix1,matix2)=function to add 2 matrices.
    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
     
  2. Papabravo

    Expert

    Feb 24, 2006
    10,179
    1,800
    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
     
  3. braddy

    Thread Starter Well-Known Member

    Dec 29, 2004
    83
    0
    Thank you very much for your suggestions I will try to build on them.
    B.
     
  4. CoulombMagician

    Active Member

    Jan 10, 2006
    37
    0
    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.
     
  5. Papabravo

    Expert

    Feb 24, 2006
    10,179
    1,800
    These are both excellent suggestions.
     
  6. braddy

    Thread Starter Well-Known Member

    Dec 29, 2004
    83
    0

    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];

    matad(A,eye,S,size);

    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;
    matad(S,Ap1,S,size);
    count1+=1;
    }
    }
    }/* end of bino*/


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

    Thank you
    B
     
  7. CoulombMagician

    Active Member

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