#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define USE_GMP
#define USE_MPFR
#include "bnc.h"

#define MAX_LENGTH 1024

#define MPFDEG 20

int main(int argc, char *argv[])
{
	long int i, mpftimes;
	FILE * mpfcoef;

	double start, endtime;
	CMPFArray cmpfans, cmpfinit;
	MPFPoly mpff;
	mpf_t mpfabs_eps, mpfrel_eps;

#define PREC 128
	set_bnc_default_prec(PREC);

	/* init */
	mpf_init_set_d(mpfabs_eps, 1.0e-100);
	mpf_init_set_d(mpfrel_eps, 1.0e-7);

	mpff = init_mpfpoly(MAX_LENGTH);
	cmpfans = init_cmpfarray(MPFDEG);
	cmpfinit = init_cmpfarray(MPFDEG);

	cmpfans = init_cmpfarray(MPFDEG);
	cmpfinit = init_cmpfarray(MPFDEG);


	mpfcoef = fopen("polycoef20.dat", "r");
	fread_mpfpolycoef(mpfcoef, mpff, MPFDEG);
	fclose(mpfcoef);

	print_mpfpoly(mpff);

	/* set Aberth's initial value */
	mpf_dka_init(cmpfinit, mpff);
//	print_cmpfarray(local_cmpfinit);

	/* DKA method */
	start = get_secv();
	mpftimes = mpf_dka(cmpfans, cmpfinit, mpff, 1000, mpfabs_eps, mpfrel_eps);
	endtime = get_secv() - start;

	/* print answer */
	printf("Iterative times: %d\n", mpftimes);
	print_cmpfarray(cmpfans);

	/* clear */
	free_mpfpoly(mpff);
	free_cmpfarray(cmpfans);
	free_cmpfarray(cmpfinit);

	printf("mpf_DKA(%dbits): %f sec\n", PREC, endtime);

	return EXIT_SUCCESS;
}
