PHP Code:
#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