matrix program

This is a discussion on matrix program within the C Programming forums, part of the General Programming Boards category; I made matrix multiplication program that uses file command. it supposed to read input from matrix.in and writes matrix.out but ...

  1. #1
    J
    Guest

    matrix program

    I made matrix multiplication program that uses file command.
    it supposed to read input from matrix.in and writes matrix.out
    but somehow it does not calculate multiplication and calculates matrix inverse wrong.
    Where can be the error?

    -----------------------------main.c-------------------------------------------
    #include "matrix.h"
    #include <stdio.h>

    int main(){
    FILE *fin, *fout;
    Matrix R, S, P, Rinv, Sinv, Pinv;
    int i, j, k;
    fin = fopen("matrix.in", "r");
    fout = fopen("matrix.out", "w");

    mat_read(R, fin);
    mat_read(S, fin);
    fclose(fin);

    /* Compute the product, P = R*S. */
    mat_mult(R,S,P);

    /* Write the results. */
    mat_write(R, "R", fout);
    mat_write(S, "S", fout);
    mat_write(P, "P", fout);

    /* compute Rinv, if it is defined. */
    i = mat_inverse(R, Rinv);
    if(i == 1)
    fprintf(fout, "\nRinv", Rinv);
    else
    fprintf(fout, "Rinv[%d][%d]= Undefined", MATSIZE, MATSIZE);

    /* compute Sinv, if it is defined. */
    j = mat_inverse(S, Sinv);
    if(j == 1)
    fprintf(fout, "\nSinv", Sinv);
    else
    fprintf(fout, "\nSinv[%d][%d]= Undefined", MATSIZE, MATSIZE);

    /* compute Pinv, if it is defined. */
    k = mat_inverse(P, Pinv);
    if(k == 1)
    fprintf(fout, "\nPinv", Pinv);
    else
    fprintf(fout, "\nPinv[%d][%d]= Undefined", MATSIZE, MATSIZE);

    if (fin == NULL)
    {
    fprintf(stderr,"unable to open %s, exiting program\n", "matrix.in");
    exit (1);
    }

    fclose(fout);
    }/* end main */



    --------------------------------matrix.c------------------------------------------
    #include "matrix.h"
    #include <stdio.h>

    void mat_read(Matrix A, FILE *fin)
    {
    int i, j, k, offset, sr;
    double dnum;
    char buffer[100];

    for(i=0; i< MATSIZE; i++)
    {
    offset = 0;
    fgets(buffer, 100, fin);

    for(j=0; j < MATSIZE; j++)
    {
    sr = sscanf(buffer + offset, "%lf%n", &dnum, &k);
    offset += k;
    printf(" %lf", dnum);
    }
    }
    }/* end mat_read */

    void mat_write( Matrix A, char name[], FILE *fout)
    {
    /* This function that will write the contents of the matrix, A, in tabular format, one row per line, on external file (fout). */

    int i, j, k;
    double dnum;

    for(i=0; i< MATSIZE; i++)
    {
    fprintf(fout, "%c[%d][%d]= ",name,MATSIZE,MATSIZE);
    for(j=0; j < MATSIZE; j++)
    {
    dnum = A[i][j];
    printf("\n %lf", dnum);
    fprintf(fout, "%lf", dnum);
    }
    fprintf(fout, "\n");
    printf("\n");
    }
    }/* end mat_write */

    void mat_mult(Matrix A, Matrix B, Matrix P)
    {
    /* This function computes P as the matrix product of A with B. */

    int i, j, k;


    for (i=0; i<MATSIZE; i++)
    {
    for (j=0; j<MATSIZE; j++)
    {
    P[i][j]=0;
    for (k=0; k<4; k++)
    {
    P[i][j]+=(A[i][k] * B[k][j]);
    printf("%lf", P[i][j]);
    }
    }
    }
    }/*end mat_mult */

    int mat_inverse(Matrix A, Matrix Ainv)
    {
    /* This function computes the inverse matrix, Ainv, from matrix A, if it is defined. */
    double a1, b1, a2, b2;
    double k, c, d, e, f;

    a1 = A[0][0];
    b1 = A[0][1];
    a2 = A[1][0];
    b2 = A[1][1];

    k = a1*b2 - a2*b1;

    Ainv[0][0] = (a1*b2) / (a1*b2 - a2*b1);
    Ainv[0][1] = (b1*b2) / (a2*b1 - a1*b2);
    Ainv[1][0] = (a1*a2) / (a2*b1 - a1*b2);
    Ainv[1][1] = (a2*b1) / (a1*b2 - a2*b1);
    c = Ainv[0][0];
    d = Ainv[0][1];
    e = Ainv[1][0];
    f = Ainv[1][1];
    printf("\n Ainv %lf %lf \n%lf %lf", c, d, e, f);
    if(k!=0)
    return 1;
    else
    return 0;
    }/*end mat_inverse */



    ------------------------------input-----------------------------------------------
    -3.2 5.6
    -4.1 -0.5
    -1.0 1.0
    3.0 -3.0

    ----------------------------output-----------------------------------------------
    ][2][2]= 148041113090085879808.000000148019105265344315392. 000000
    ][2][2]= 246735053610050125824.0000000.000000
    _[2][2]= 154088004830388813824.0000000.000000
    _[2][2]= 159118508021975613440.0000000.000000
    a[2][2]= InfInf
    a[2][2]= InfInf

    Rinv
    Sinv
    Pinv

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    If you could use the code tags that would be nice.

    -Prelude
    My best code is written with the delete key.

  3. #3
    J
    Guest
    I made matrix multiplication program that uses file command.
    it supposed to read input from matrix.in and writes matrix.out
    but somehow it does not calculate multiplication and calculates matrix inverse wrong.
    Where can be the error?

    -----------------------------main.c-------------------------------------------
    PHP Code:

    #include "matrix.h" 
    #include <stdio.h> 

    int main(){ 
    FILE *fin, *fout
    Matrix RSPRinvSinvPinv
    int ijk
    fin fopen("matrix.in""r"); 
    fout fopen("matrix.out""w"); 

    mat_read(Rfin); 
    mat_read(Sfin); 
    fclose(fin); 

    /* Compute the product, P = R*S. */ 
    mat_mult(R,S,P); 

    /* Write the results. */ 
    mat_write(R"R"fout); 
    mat_write(S"S"fout); 
    mat_write(P"P"fout); 

    /* compute Rinv, if it is defined. */ 
    mat_inverse(RRinv); 
    if(
    == 1
    fprintf(fout"\nRinv"Rinv); 
    else 
    fprintf(fout"Rinv[%d][%d]= Undefined"MATSIZEMATSIZE); 

    /* compute Sinv, if it is defined. */ 
    mat_inverse(SSinv); 
    if(
    == 1
    fprintf(fout"\nSinv"Sinv); 
    else 
    fprintf(fout"\nSinv[%d][%d]= Undefined"MATSIZEMATSIZE); 

    /* compute Pinv, if it is defined. */ 
    mat_inverse(PPinv); 
    if(
    == 1
    fprintf(fout"\nPinv"Pinv); 
    else 
    fprintf(fout"\nPinv[%d][%d]= Undefined"MATSIZEMATSIZE); 

    if (
    fin == NULL

    fprintf(stderr,"unable to open %s, exiting program\n""matrix.in"); 
    exit (
    1); 


    fclose(fout); 
    }
    /* end main */ 



    --------------------------------matrix.c------------------------------------------ 
    #include "matrix.h" 
    #include <stdio.h> 

    void mat_read(Matrix AFILE *fin

    int ijkoffsetsr
    double dnum
    char buffer[100]; 

    for(
    i=0iMATSIZEi++) 

    offset 0
    fgets(buffer100fin); 

    for(
    j=0MATSIZEj++) 

    sr sscanf(buffer offset"%lf%n", &dnum, &k); 
    offset += k
    printf(" %lf"dnum); 


    }
    /* end mat_read */ 

    void mat_writeMatrix Achar name[], FILE *fout

    /* This function that will write the contents of the matrix, A, in tabular format, one row per line, on external file (fout). */ 

    int ijk
    double dnum

    for(
    i=0iMATSIZEi++) 

    fprintf(fout"%c[%d][%d]= ",name,MATSIZE,MATSIZE); 
    for(
    j=0MATSIZEj++) 

    dnum A[i][j]; 
    printf("\n %lf"dnum); 
    fprintf(fout"%lf"dnum); 

    fprintf(fout"\n"); 
    printf("\n"); 

    }
    /* end mat_write */ 

    void mat_mult(Matrix AMatrix BMatrix P

    /* This function computes P as the matrix product of A with B. */ 

    int ijk


    for (
    i=0i<MATSIZEi++) 

    for (
    j=0j<MATSIZEj++) 

    P[i][j]=0
    for (
    k=0k<4k++) 

    P[i][j]+=(A[i][k] * B[k][j]); 
    printf("%lf"P[i][j]); 



    }
    /*end mat_mult */ 

    int mat_inverse(Matrix AMatrix Ainv

    /* This function computes the inverse matrix, Ainv, from matrix A, if it is defined. */ 
    double a1b1a2b2
    double kcdef

    a1 A[0][0]; 
    b1 A[0][1]; 
    a2 A[1][0]; 
    b2 A[1][1]; 

    a1*b2 a2*b1

    Ainv[0][0] = (a1*b2) / (a1*b2 a2*b1); 
    Ainv[0][1] = (b1*b2) / (a2*b1 a1*b2); 
    Ainv[1][0] = (a1*a2) / (a2*b1 a1*b2); 
    Ainv[1][1] = (a2*b1) / (a1*b2 a2*b1); 
    Ainv[0][0]; 
    Ainv[0][1]; 
    Ainv[1][0]; 
    Ainv[1][1]; 
    printf("\n Ainv %lf %lf \n%lf %lf"cdef); 
    if(
    k!=0
    return 
    1
    else 
    return 
    0
    }
    /*end mat_inverse */ 



    ------------------------------input----------------------------------------------- 
    -
    3.2 5.6 
    -4.1 -0.5 
    -1.0 1.0 
    3.0 
    -3.0 

    ----------------------------output----------------------------------------------- 
    ][
    2][2]= 148041113090085879808.000000148019105265344315392.000000 
    ][2][2]= 246735053610050125824.0000000.000000 
    _
    [2][2]= 154088004830388813824.0000000.000000 
    _
    [2][2]= 159118508021975613440.0000000.000000 
    a
    [2][2]= InfInf 
    a
    [2][2]= InfInf 

    Rinv 
    Sinv 
    Pinv 

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    sigh...

    -Prelude
    My best code is written with the delete key.

  5. #5
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    go here this explains the math pretty well...

    http://www.geocities.com/SiliconVall.../matrices.html

    >sigh...

    you said something there.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program 2D matrix multiplication using malloc
    By college_kid in forum C Programming
    Replies: 5
    Last Post: 04-03-2009, 10:04 AM
  2. Matrix c program help!!!
    By kclew in forum C Programming
    Replies: 21
    Last Post: 02-25-2009, 03:46 AM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  4. Weird errors.
    By Desolation in forum C++ Programming
    Replies: 20
    Last Post: 05-09-2007, 01:10 PM
  5. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21