#include <v_lib.h>
#include "../utils/math_u.h"

int main(int argc,char *argv[])
{
	matrix_t **mset, *inv;
	int i,order,n,lower,upper;
	file_t out, an;
	char *out_name;
	puts(
		"Normal & inversed matrices generator ver. 2.0.\n"
		"Copyright (C) Vladimir A. Smirnov, 1997-2000.");
	if(argc<5)
	{
		puts("Usage: imgen <order> <lower limit> <upper limit> "
			"<number of pairs> [output file name] [file name for answers]");
		return -1;
	}
	order=atoi(argv[1]);
	lower=atoi(argv[2]);
	upper=atoi(argv[3]);
	n=atoi(argv[4]);
	out_name=argc<6 ? "CON" : argv[5];
	out=fileOpenWrite(out_name);
	if(!out)
	{
		printf("Unable to open '%s' for output.\n",out_name);
		return -1;
	}
	if(argc < 7)
		an=0;
	else
	{
		an=fileOpenWrite(argv[6]);
		if(!an)
		{
			printf("Unable to open '%s' for output.\n",out_name);
			fileClose(out);
			return -1;
		}
	}
	umCreateRandomMatrixSet(&mset,n,order,order,lower,upper,false,true);
	inv=matAlloc(order,order);
	filePrintf(out,";\t%d pairs of matrices of order %d\n;\tproduced by %s\n",
		n,order,argv[0]);
	if(an)
	{
		for(i=0;i<n;i++)
		{
			inv=matInverse(mset[i],true);
			filePrintf(out,"M[%d]=\n",i+1);
			umDumpMatrix(out,mset[i],NULL,NULL,NULL,"| ","  "," |\n",true);
			filePrintf(an,"M[%d]^(-1)=\n",i+1);
			umDumpMatrixAsFractions(an,inv,NULL,NULL,NULL,"| ","  "," |\n",true);
		}
		fileClose(an);
	}
	else
	{
		for(i=0;i<n;i++)
		{
			inv=matInverse(mset[i],true);
			filePrintf(out,"M[%d]=\n",i+1);
			umDumpMatrix(out,mset[i],NULL,NULL,NULL,"| ","  "," |\n",true);
			filePrintf(out,"M[%d]^(-1)=\n",i+1);
			umDumpMatrixAsFractions(out,inv,NULL,NULL,NULL,"| ","  "," |\n",true);
		}
	}
	fileClose(out);
	umDestroyRandomMatrixSet(mset,n);
	free(inv);
	return 0;
}
