/* This is a simple C interface to the black-box solver of phc, ** written by Nobuki Takayama. ** Requirements: ** 1) executable version of phc must be on /tmp; ** 2) user of this program has write permissions to create ** the files "input" and "output" in the directory where ** this program is executed. */ #include #include #include /* Definition of class identifiers. */ #define SstringObject 5 #define Sarray 6 /* Definition of Object */ union cell { int ival; char *str; struct object *op; }; struct object{ int tag; /* class identifier */ union cell lc; /* left cell */ union cell rc; /* right cell */ }; /* Memory allocation function. I recommend not to use malloc and to use gc4.14 for large applications. */ #define sGC_malloc(n) 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);\ }}} #define phc_getoa(ob,i) ((ob.rc.op)[i]) #define phc_getoaSize(ob) ((ob).lc.ival) /* prototypes */ struct object phc_newObjectArray(int size); void phc_printObject(FILE *fp,struct object ob); char *phc_generateUniqueFileName(char *s); struct object phc_complexTo(long double r, long double i); struct object phc_longdoubleToStringObject(long double r); int phc_scan_for_string(FILE *fp, char str[], int lenstr); struct object phc_scan_solutions(FILE *fp, int npaths, int dim ); struct object phc_scan_output_of_phc(char *fname); struct object phc_call_phc(char *sys); main() { struct object ob; ob = phc_call_phc("2\n x**2 + y**2 - 1;\n x**2 + y**2 - 8*x - 3;\n"); printf("-----------------------------------------------------------\n"); phc_printObject(stdout,ob); printf("\n"); } int phc_scan_for_string(FILE *fp, char str[], int lenstr) /* ** 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. */ { char buf[lenstr+1]; char ch; int index,i,compare,npaths,dim,found; index = -1; found = 0; while ((fscanf(fp,"%c",&ch)!=EOF) && found == 0) { if (index == -1 && ch == str[0]) { 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 (found == 1) break; } return found; } struct object phc_scan_solutions(FILE *fp, int npaths, int dim ) /* ** Scans the file for the solutions, from a list of length npaths, ** of complex vectors with dim entries. ** The tolerance for the residual to a solution is set to 1.0E-12. ** Returns the number of solutions. */ { struct 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]; nsols = 0; while (fscanf(fp,"%c",&ch)!=EOF) { fnd = phc_scan_for_string(fp,"start residual :",15); 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 0) { op = (struct object *)sGC_malloc(size*sizeof(struct object)); if (op == (struct object *)NULL) {fprintf(stderr,"No memory\n");exit(1);} }else{ op = (struct object *)NULL; } rob.tag = Sarray; rob.lc.ival = size; rob.rc.op = op; return(rob); } void phc_printObject(FILE *fp,struct object ob) { int n,i; if (ob.tag == SstringObject) { fprintf(fp,"%s",ob.lc.str); }else if (ob.tag == Sarray) { n = phc_getoaSize(ob); fprintf(fp,"["); for (i=0; i