/* invers - invert a square matrix
   written by Marco in 1994
   and revised in 2021 */

/* This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details. */


#include<stdio.h>
#include<stdlib.h>
#include"./matrices.h"

int main(int argc, char **argv)
{
 FILE *fp;
 MAT mat=MATNULL;
 MAT sol=MATNULL;
 int i,j;

 if(argc!=2)
    {fprintf(stderr,"%s usage: %s square_matrix\n",argv[0],argv[0]);
     exit(EXIT_FAILURE);}

 /* open square_matrix file for reading */
 if(!(fp=fopen(argv[1],"r")))
             {fprintf(stderr,"Can't open file %s\n",argv[1]);
              exit(EXIT_FAILURE);}
 
 /* read square_matrix file and then close the file */
 freadm(fp,&mat);
 fclose(fp);

 /* invert the square matrix */
 inv(&sol,mat);

 /* print the inverse on stdin */
 printf("Inverse matrix of %s:\n\n",argv[1]);
 writem(sol);

 /* free the memory allocated for the matrices */
 freem(&mat);
 freem(&sol);

 /* end of the program */
 return(EXIT_SUCCESS);
}
