/* syslin - a linear system solver
   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 con=MATNULL;
 MAT sol=MATNULL;

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

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

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

 /* solve the linear system */
 linsys(&sol,mat,con);

 /* write solution to file */
 if(!(fp=fopen(argv[3],"w")))
                {fprintf(stderr,"Can't open file %s\n",argv[3]);
                 exit(EXIT_FAILURE);}

 fwritem(fp,sol);
 fclose(fp);

 printf("Solution in file %s\n",argv[3]);

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

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