=================================================================== RCS file: /home/cvs/OpenXM/src/phc/phc6.c,v retrieving revision 1.1.1.1 retrieving revision 1.6 diff -u -p -r1.1.1.1 -r1.6 --- OpenXM/src/phc/phc6.c 1999/10/08 02:12:14 1.1.1.1 +++ OpenXM/src/phc/phc6.c 2019/01/01 07:17:52 1.6 @@ -1,4 +1,5 @@ /* phc6.c , yama:1999/sm1-prog/phc6.c */ +/* $OpenXM: OpenXM/src/phc/phc6.c,v 1.5 2002/05/02 11:54:33 takayama Exp $ */ /* This is a simple C interface to the black-box solver of phc. ** Requirements: ** 1) executable version of phc will be searched in the following order: @@ -13,6 +14,8 @@ #include #include #include +#include +#include "gc.h" /* Definition of class identifiers. */ #define Snull 0 @@ -25,7 +28,7 @@ union cell { int ival; char *str; struct phc_object *op; - long double longdouble; + double longdouble; }; struct phc_object{ int tag; /* class identifier */ @@ -36,17 +39,18 @@ struct phc_object{ /* Memory allocation function. Use your favorite memory allocation function. I recommend not to use malloc and to use gc4.14 for large applications. */ -#define sGC_malloc(n) malloc(n) +//#define sGC_malloc(n) malloc(n) +#define sGC_malloc(n) GC_malloc(n) /********** macros to use Sarray **************/ /* put to Object Array */ #define phc_putoa(ob,i,cc) {\ -if ((ob).tag != Sarray) {fprintf(stderr,"Warning: PUTOA is for an array of objects\n");} else \ -{if ((0 <= (i)) && ((i) < (ob).lc.ival)) {\ - (ob.rc.op)[i] = cc;\ -}else{\ - fprintf(stderr,"Warning: PUTOA, the size is %d.\n",(ob).lc.ival);\ -}}} + if ((ob).tag != Sarray) {fprintf(stderr,"Warning: PUTOA is for an array of objects\n");} else \ + {if ((0 <= (i)) && ((i) < (ob).lc.ival)) {\ + (ob.rc.op)[i] = cc;\ + }else{\ + fprintf(stderr,"Warning: PUTOA, the size is %d.\n",(ob).lc.ival);\ + }}} #define phc_getoa(ob,i) ((ob.rc.op)[i]) #define phc_getoaSize(ob) ((ob).lc.ival) @@ -55,38 +59,67 @@ struct phc_object phc_newObjectArray(int size); void phc_printObject(FILE *fp,struct phc_object ob); char *phc_generateUniqueFileName(char *s); char *phc_which(char *s); /* search a path for the file s */ -struct phc_object phc_complexTo(long double r, long double i); +struct phc_object phc_complexTo(double r, double i); -int phc_scan_for_string(FILE *fp, char str[], int lenstr); +int phc_scan_for_string(FILE *fp, char str[]); struct phc_object phc_scan_solutions(FILE *fp, int npaths, int dim ); -struct phc_object phc_scan_output_of_phc(char *fname); +struct phc_object phc_scan_output_of_phc(FILE *fp); struct phc_object phc_call_phc(char *sys); int phc_verbose = 0; int phc_overwrite = 1; /* Always use tmp.input.0 and tmp.output.0 - for work files. */ + for work files. */ -main(int argc, char *argv[]) { +int main(int argc, char *argv[]) { struct phc_object ob; + struct phc_object ob2; + int n2; int n,i,dim; #define INPUTSIZE 4096 char input[INPUTSIZE]; + char fname[INPUTSIZE]; #define A_SIZE 1024 char a[A_SIZE]; int message = 0; + FILE *fp; for (i=1; i 0)) printf(" ,\n"); else printf(" \n"); + } + for (i=0; i= INPUTSIZE) { - fprintf(stderr,"Too big input for the input buffer input.\n"); exit(10); - } - sprintf(input+strlen(input),"%s\n",a); + if (message) {printf("eq[%d] = ",i); fflush(stdout);} + do { + fgets(a,A_SIZE-1, stdin); + } while (strlen(a) == 0); + if (strlen(a) >= A_SIZE-3) { + fprintf(stderr,"Too big input for the input buffer a.\n"); exit(10); + } + if (strlen(input)+strlen(a) >= INPUTSIZE) { + fprintf(stderr,"Too big input for the input buffer input.\n"); exit(10); + } + sprintf(input+strlen(input),"%s\n",a); } ob = phc_call_phc(input); if (message) { @@ -126,52 +160,91 @@ main(int argc, char *argv[]) { } printf("]\n"); } + return(0); } -int phc_scan_for_string(FILE *fp, char str[], int lenstr) +int phc_scan_for_string(FILE *fp, char s[]) /* ** Scans the file fp for a certain string str of length lenstr+1. ** Reading stops when the string has been found, then the variable ** on return equals 1, otherwise 0 is returned. */ +#define BUF_SIZE 1024 { - char buf[lenstr+1]; + char buf[BUF_SIZE]; + int pt,n,c,i; + pt = 0; + n=strlen(s); + if (n > BUF_SIZE-2) { + fprintf(stderr,"Too long string for scan_for_string.\n"); + exit(1); + } + for (i=0; i= BUF_SIZE-1) { + fprintf(stderr,"Too long string in phc_scan_for_string\n"); + exit(-1); + } index = -1; found = 0; - while ((fscanf(fp,"%c",&ch)!=EOF) && found == 0) + while (((ch = fgetc(fp))!=EOF) && found == 0) { if (index == -1 && ch == str[0]) - { - index = 0; - buf[index] = ch; - } + { + index = 0; + buf[index] = ch; + } else - { - if (index == lenstr) - { - compare = 0; - for (i=0; i -1 && index < lenstr) - { - index = index+1; - buf[index] = ch; - } - } + { + if (index == lenstr) + { + compare = 0; + for (i=0; str[i] != '\0'; i++) + { + if (buf[i]!=str[i]) + { + compare = compare+1; + } + } + if (compare == 0) + { + found = 1; + } + index = -1; + } + else + if (index > -1 && index < lenstr) + { + index = index+1; + buf[index] = ch; + } + } if (found == 1) break; } return found; @@ -183,57 +256,92 @@ struct phc_object phc_scan_solutions(FILE *fp, int npa ** The tolerance for the residual to a solution is set to 1.0E-12. ** Returns solutions. */ +#define BUFSIZE 1024 { struct phc_object rob,sob; char ch; int fnd,i,j,nsols; - float res; - long double realpart; - long double imagpart; - long double realparts[npaths][dim]; - long double imagparts[npaths][dim]; + double res; + double realpart; + double imagpart; + /* double realparts[npaths][dim]; + double imagparts[npaths][dim]; */ + double *realparts; + double *imagparts; + double *res_list; + char buf[BUFSIZE]; nsols = 0; - while (fscanf(fp,"%c",&ch)!=EOF) + realparts = (double *)sGC_malloc(sizeof(double)*(npaths*dim+1)); + imagparts = (double *)sGC_malloc(sizeof(double)*(npaths*dim+1)); + res_list = (double *)sGC_malloc(sizeof(double)*npaths+1); + while ((ch = fgetc(fp)) != EOF) { - fnd = phc_scan_for_string(fp,"start residual :",15); + fnd = phc_scan_for_string(fp,"start residual :"); if (fnd==1) - { - fscanf(fp,"%E",&res); - /* printf(" residual = "); printf("%E\n",res); */ - if (res < 1.0E-12) nsols = nsols+1; - fnd = phc_scan_for_string(fp,"the solution for t :",19); - for (i=0;i npaths) { + fprintf(stderr,"Something is wrong in phc_scan_solutions\n"); + fprintf(stderr,"npaths=%d, nsols=%d \n",npaths,nsols); + exit(-1); + } + } + */ + fnd = phc_scan_for_string(fp,"the solution for t :"); + res_list[nsols-1] = res; + for (i=0;i= npaths) break; + } } + if (nsols < npaths) { + fprintf(stderr,"Warning: nsols < npaths. Check tmp.output.*\n"); + } if(phc_verbose) fprintf(stderr," number of solutions = %i\n",nsols); rob = phc_newObjectArray(nsols); for (i=0;i