void adj_extreme(int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[]) { dd_PolyhedraPtr P; dd_ErrorType err; dd_MatrixPtr H,V; dd_SetFamilyPtr A; if (nrhs == 1 && nlhs == 2 && mxIsStruct(prhs[0])) { dd_set_global_constants(); /* First, this must be called. */ H = FT_get_H_MatrixPtr(prhs[0]); P = dd_DDMatrix2Poly(H, &err); /* compute the second representation */ if (err == dd_NoError) { V = dd_CopyGenerators(P); A = dd_CopyAdjacency(P); plhs[0] = FT_set_V_MatrixPtr(V); plhs[1] = FT_set_SetFamilyPtr(A); dd_FreeMatrix(V); dd_FreeSetFamily(A); } else { dd_WriteErrorMessages(stdout,err); mexErrMsgTxt("CDD returned an error, see above(!) for details"); } dd_FreeMatrix(H); dd_FreePolyhedra(P); return; } else { mexErrMsgTxt("adj_extreme expects an H input struct and produces a V output struct and the adjacency struct"); } }
void hull(int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[]) { dd_PolyhedraPtr P; dd_ErrorType err; dd_MatrixPtr H,V; if (nrhs == 1 && nlhs == 1 && mxIsStruct(prhs[0])) { V = FT_get_V_MatrixPtr(prhs[0]); dd_set_global_constants(); /* First, this must be called. */ P = dd_DDMatrix2Poly(V, &err); /* compute the second representation */ if (err == dd_NoError) { H = dd_CopyInequalities(P); plhs[0] = FT_set_H_MatrixPtr(H); dd_FreeMatrix(H); } else { dd_WriteErrorMessages(stdout,err); mexErrMsgTxt("CDD returned an error, see above(!) for details"); } dd_FreeMatrix(V); dd_FreePolyhedra(P); return; } else { mexErrMsgTxt("hull expects a V input struct and produces an H output struct"); } }
void reduce_v(int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[]) { dd_ErrorType err; dd_MatrixPtr V,V1; dd_rowset red; if (nrhs == 1 && nlhs >= 1 && nlhs <= 2 && mxIsStruct(prhs[0])) { dd_set_global_constants(); /* First, this must be called. */ V = FT_get_V_MatrixPtr(prhs[0]); red = dd_RedundantRows(V, &err); /* find redundant rows */ if (err == dd_NoError) { /* remove the red rows */ V1 = dd_MatrixSubmatrix(V, red); plhs[0] = FT_set_V_MatrixPtr(V1); dd_FreeMatrix(V1); if (nlhs == 2) { plhs[1] = FT_set_Set(red); } } else { dd_WriteErrorMessages(stdout,err); mexErrMsgTxt("CDD returned an error, see above(!) for details"); } dd_FreeMatrix(V); set_free(red); return; } else { mexErrMsgTxt("reduce_v expects an V input struct and produces a V output struct and an optional vector of removed vertices"); } }
void solve_lp(int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[]) { /* The original LP data m x n matrix = | b -A | | c0 c^T |, where the LP to be solved is to maximize c^T x + c0 subj. to A x <= b. */ dd_ErrorType error=dd_NoError; dd_LPSolverType solver=dd_CrissCross; /* either DualSimplex or CrissCross */ dd_LPPtr lp; /* pointer to LP data structure that is not visible by user. */ dd_set_global_constants(); /* First, this must be called once to use cddlib. */ /* Input an LP using the cdd library */ lp = MB_get_LP_MatrixPtr(prhs[0]); /* Solve the LP by cdd LP solver. */ dd_LPSolve(lp,solver,&error); if (error!=dd_NoError) dd_WriteErrorMessages(stdout, error); /* Take the solution. */ plhs[0] = MB_set_LPsol_MatrixPtr(lp); /* Free allocated spaces. */ dd_FreeLPData(lp); }
void adjacency(int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[]) { dd_PolyhedraPtr P; dd_ErrorType err; dd_MatrixPtr V; dd_SetFamilyPtr A; if (mxIsStruct(prhs[0])) { V = FT_get_V_MatrixPtr(prhs[0]); dd_set_global_constants(); /* First, this must be called. */ P = dd_DDMatrix2Poly(V, &err); /* compute the second representation */ if (err == dd_NoError) { A = dd_CopyInputAdjacency(P); plhs[0] = FT_set_SetFamilyPtr(A); dd_FreeSetFamily(A); } else { dd_WriteErrorMessages(stdout,err); mexErrMsgTxt("CDD returned an error, see above(!) for details"); } dd_FreeMatrix(V); dd_FreePolyhedra(P); } }
void implicit_linear(int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[]) { dd_MatrixPtr H; dd_rowset rows; dd_ErrorType err; if (nrhs == 1 && nlhs <= 2 && mxIsStruct(prhs[0])) { H = FT_get_H_MatrixPtr(prhs[0]); dd_set_global_constants(); /* First, this must be called. */ rows = dd_ImplicitLinearityRows(H, &err); if (err == dd_NoError) { int total, element, i; double* pr; total = set_card(rows); plhs[0] = mxCreateDoubleMatrix(total, 1, mxREAL); pr = mxGetPr(plhs[0]); for (i=1, element=0; i<=rows[0]; i++){ if (set_member(i, rows)) { pr[element++] = (double)i; } } } else { dd_WriteErrorMessages(stdout,err); mexErrMsgTxt("CDD returned an error, see above(!) for details"); } dd_FreeMatrix(H); set_free(rows); return; } else { mexErrMsgTxt("hull expects a V input struct and produces an H output struct"); } }
dd_LPPtr MB_get_LP_MatrixPtr(const mxArray * in) { mxArray * tmpobj; dd_MatrixPtr A; int j; double * value; dd_ErrorType error=dd_NoError; dd_LPPtr lp; dd_set_global_constants(); /* First, this must be called once to use cddlib. */ if (A=FT_get_H_MatrixPtr(in)) { /* set objective */ if ((tmpobj = mxGetField(in, 0, "obj")) && (mxGetNumberOfDimensions(tmpobj) <= 2) && (mxGetM(tmpobj) == 1)) { value = mxGetPr(tmpobj); A->objective = dd_LPmin; dd_set_d(A->rowvec[0], 0.0); /* there is no constant term */ for (j = 1; j < A->colsize; j++) dd_set_d(A->rowvec[j],value[j-1]); lp=dd_Matrix2LP(A, &error); dd_FreeMatrix(A); return lp; }else{ mexErrMsgTxt("Error in the setting of LP objective."); } }else{ mexErrMsgTxt("Error in the setting of LP matrix."); } return 0; }
int main( int argc, char* argv[]) { /* Due to a bug in some standard C libraries that have shipped with * MPW, zero is passed to MLMain below. (If you build this program * as an MPW tool, you can change the zero to argc.) */ dd_set_global_constants(); /* First, this must be called to use cddlib. */ argc = argc; /* suppress warning */ return MLMain( 0, argv); }
/* * Find the extreme vertices between lower_bound and upper_bound of the convex * hull of ineqs. * * ineqs: row-major order matrix of total length nrow*ncols * nrows: Number of rows in matrix * ncols: Number of columns in matrix * lower_bound: lower bound of solution space on x-axis * upper_bound: upper bound of solution space on y-axis * output size: Set to size of result * * Returns array representing 3 by (output_size/3) matrix, where each row is: * x_i, y_i, l_i * Where x_i, y_i are the coordinates of the vertex, and l_i is the index of * the input inequality that constrains the solution space *to the right* of * vertex i. */ double *extreme_vertices(const double *ineqs, const size_t nrows, const size_t ncols, float lower_bound, float upper_bound, /*OUT*/ size_t * output_size) { /* Preconditions */ assert(ineqs != NULL); assert(ncols == 3); assert(output_size != NULL); /* Check for approx equal lower and upper bound */ if (abs(lower_bound - upper_bound) < EPS) return NULL; assert(lower_bound <= upper_bound); /* * Initialize library * TODO: Do we want to do this on every call? */ dd_set_global_constants(); dd_ErrorType err; dd_MatrixPtr generators; dd_MatrixPtr m = init_ineq_doubles(ineqs, nrows, ncols, lower_bound, upper_bound); dd_SetFamilyPtr incidence; /* Outputs */ dd_PolyhedraPtr poly = dd_DDMatrix2Poly(m, &err); if (err != dd_NoError) { return NULL; } /* Get generators */ generators = dd_CopyGenerators(poly); /* Get incidence */ incidence = dd_CopyIncidence(poly); double *result = list_extreme_vertices(generators, incidence, nrows, m->rowsize - 1, output_size); dd_FreeMatrix(m); dd_FreeMatrix(generators); dd_FreePolyhedra(poly); dd_FreeSetFamily(incidence); dd_free_global_constants(); return result; }
void file_ine(int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[]) /* Ine file input, V output, similar to extreme */ { dd_PolyhedraPtr poly; dd_MatrixPtr M; dd_ErrorType err; char *inputfile; FILE *reading=NULL; dd_MatrixPtr A, G; dd_SetFamilyPtr GI,GA; int buflen, status; dd_set_global_constants(); /* First, this must be called. */ if (nrhs == 1 && nlhs <=2 && mxIsChar(prhs[0])) { /* dd_SetInputFile(&reading,inputfile, &err); */ buflen = mxGetN(prhs[0]) + 1; inputfile= mxCalloc(buflen, sizeof(char)); status = mxGetString(prhs[0], inputfile, buflen); if ( (reading = fopen(inputfile,"r") )== NULL) { mxErrMsgTxt("Input file not found\n"); return; } printf(" Input file opened. \n"); M=dd_PolyFile2Matrix(reading, &err); if (err==dd_NoError) { poly=dd_DDMatrix2Poly(M, &err); /* compute the second representation */ if (err!=dd_NoError) { dd_WriteErrorMessages(stdout,err); mxErrMsgTxt("CDD internal error\n"); return; } A=dd_CopyInequalities(poly); G=dd_CopyGenerators(poly); GI=dd_CopyInputIncidence(poly); GA=dd_CopyAdjacency(poly); plhs[0] = FT_set_V_MatrixPtr(G); plhs[1] = ZH_set_Vlist(GI,GA); dd_FreePolyhedra(poly); dd_FreeMatrix(M); return; } } else { mexErrMsgTxt("file-ine expects an file input"); } return; }
void copy_h(int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[]) { dd_MatrixPtr H; if (nrhs == 1 && nlhs == 1 && mxIsStruct(prhs[0])) { dd_set_global_constants(); /* First, this must be called. */ H = FT_get_H_MatrixPtr(prhs[0]); plhs[0] = FT_set_H_MatrixPtr(H); dd_FreeMatrix(H); return; } else { mexErrMsgTxt("copy_h expects a H input struct and produces a H output struct"); } }
int PASCAL WinMain( HINSTANCE hinstCurrent, HINSTANCE hinstPrevious, LPSTR lpszCmdLine, int nCmdShow) { char buff[512]; char FAR * buff_start = buff; char FAR * argv[32]; char FAR * FAR * argv_end = argv + 32; dd_set_global_constants(); /* First, this must be called to use cddlib. */ hinstPrevious = hinstPrevious; /* suppress warning */ if( !MLInitializeIcon( hinstCurrent, nCmdShow)) return 1; MLScanString( argv, &argv_end, &lpszCmdLine, &buff_start); return MLMain( argv_end - argv, argv); }
void find_interior_DS(int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[]) { /* uses Dual Simplex method */ /* We would like to find an iterior point for a polyhedron in H representation A x <= b. */ dd_ErrorType error=dd_NoError; dd_LPSolverType solver=dd_DualSimplex; dd_LPPtr lp, lp1; /* pointer to LP data structure that is not visible by user. */ dd_MatrixPtr A; int j; dd_set_global_constants(); /* First, this must be called once to use cddlib. */ /* Input an LP using the cdd library */ /* lp = MB_get_LP_MatrixPtr(prhs[0]); */ if (A=FT_get_H_MatrixPtr(prhs[0])) { /* set objective */ A->objective = dd_LPmin; for (j = 0; j < A->colsize; j++) dd_set_d(A->rowvec[j],0.0); lp=dd_Matrix2LP(A, &error); dd_FreeMatrix(A); }else{ mexErrMsgTxt("Error in the setting of LP matrix."); } lp1=dd_MakeLPforInteriorFinding(lp); dd_LPSolve(lp1,solver,&error); if (error!=dd_NoError) dd_WriteErrorMessages(stdout, error); /* Take the solution. */ plhs[0] = MB_set_LPsol_MatrixPtr(lp1); /* Free allocated spaces. */ dd_FreeLPData(lp); dd_FreeLPData(lp1); }
int main(int argc, char *argv[]) { dd_MatrixPtr M=NULL; dd_rowrange i,m; dd_ErrorType err=dd_NoError; dd_rowindex newpos; dd_rowset impl_linset,redset; time_t starttime, endtime; dd_DataFileType inputfile; FILE *reading=NULL; dd_set_global_constants(); /* First, this must be called. */ if (argc>1) strcpy(inputfile,argv[1]); if (argc<=1 || !SetInputFile(&reading,argv[1])){ dd_WriteProgramDescription(stdout); fprintf(stdout,"\ncddlib test program to check redundancy of an H/V-representation.\n"); dd_SetInputFile(&reading,inputfile, &err); } if (err==dd_NoError) { M=dd_PolyFile2Matrix(reading, &err); } else { fprintf(stderr,"Input file not found\n"); goto _L99; } if (err!=dd_NoError) goto _L99; m=M->rowsize; fprintf(stdout, "Canonicalize the matrix.\n"); time(&starttime); dd_MatrixCanonicalize(&M, &impl_linset, &redset, &newpos, &err); time(&endtime); if (err!=dd_NoError) goto _L99; fprintf(stdout, "Implicit linearity rows are:"); set_fwrite(stdout, impl_linset); fprintf(stdout, "\nRedundant rows are:"); set_fwrite(stdout, redset); fprintf(stdout, "\n"); fprintf(stdout, "Nonredundant representation:\n"); fprintf(stdout, "The new row positions are as follows (orig:new).\nEach redundant row has the new number 0.\nEach deleted duplicated row has a number nagative of the row that\nrepresents its equivalence class.\n"); for (i=1; i<=m; i++){ fprintf(stdout, " %ld:%ld",i, newpos[i]); } fprintf(stdout, "\n"); dd_WriteMatrix(stdout, M); dd_WriteTimes(stdout,starttime,endtime); set_free(redset); set_free(impl_linset); dd_FreeMatrix(M); free(newpos); _L99:; if (err!=dd_NoError) dd_WriteErrorMessages(stderr,err); return 0; }
int main(int argc, char *argv[]) { dd_PolyhedraPtr poly; dd_LPPtr lp; dd_MatrixPtr M,A; dd_ErrorType err=dd_NoError; dd_DataFileType inputfile,outputfile; FILE *reading=NULL, *writing; dd_set_global_constants(); /* First, this must be called. */ if (argc>1) strcpy(inputfile,argv[1]); if (argc<=1 || !SetInputFile(&reading,argv[1])){ dd_WriteProgramDescription(stdout); dd_SetInputFile(&reading,inputfile, &err); } if (err==dd_NoError) { M=dd_PolyFile2Matrix(reading, &err); } else { printf("Input file not found\n"); goto _L99; } if (err!=dd_NoError) goto _L99; if (M->objective==dd_LPnone){ /* do representation conversion */ poly=dd_DDMatrix2Poly2(M, dd_LexMin, &err); /* equivalent to poly=dd_DDMatrix2Poly2(M, &err) when the second argument is set to dd_LexMin. */ if (err!=dd_NoError) goto _L99; dd_SetWriteFileName(inputfile, outputfile, 'o', poly->representation); SetWriteFile(&writing, outputfile); dd_WriteProgramDescription(writing); dd_WriteRunningMode(writing, poly); switch (poly->representation) { case dd_Inequality: fprintf(writing, "ext_file: Generators\n"); A=dd_CopyGenerators(poly); dd_WriteMatrix(writing,A); dd_FreeMatrix(A); break; case dd_Generator: fprintf(writing, "ine_file: Inequalities\n"); A=dd_CopyInequalities(poly); dd_WriteMatrix(writing,A); dd_FreeMatrix(A); break; default: break; } dd_WriteDDTimes(writing,poly); fclose(writing); dd_SetWriteFileName(inputfile, outputfile, 'a', poly->representation); SetWriteFile(&writing, outputfile); dd_WriteAdjacency(writing,poly); fclose(writing); dd_SetWriteFileName(inputfile, outputfile, 'j', poly->representation); SetWriteFile(&writing, outputfile); dd_WriteInputAdjacency(writing,poly); fclose(writing); dd_SetWriteFileName(inputfile, outputfile, 'i', poly->representation); SetWriteFile(&writing, outputfile); dd_WriteIncidence(writing,poly); fclose(writing); dd_SetWriteFileName(inputfile, outputfile, 'n', poly->representation); SetWriteFile(&writing, outputfile); dd_WriteInputIncidence(writing,poly); fclose(writing); dd_FreeMatrix(M); dd_FreePolyhedra(poly); } else { /* solve the LP */ lp=dd_Matrix2LP(M, &err); if (err!=dd_NoError) goto _L99; dd_LPSolve(lp,dd_DualSimplex,&err); if (err!=dd_NoError) goto _L99; dd_SetWriteFileName(inputfile, outputfile, 's', M->representation); SetWriteFile(&writing, outputfile); dd_WriteLPResult(writing, lp, err); fclose(writing); dd_FreeMatrix(M); dd_FreeLPData(lp); } _L99: if (err!=dd_NoError) dd_WriteErrorMessages(stdout,err); return 0; }
int main(int argc, char *argv[]) { /* The original LP data m x n matrix = | b -A | | c0 c^T |, where the LP to be solved is to maximize c^T x + c0 subj. to A x <= b. */ dd_ErrorType err=dd_NoError; dd_LPSolverType solver=dd_DualSimplex; /* either DualSimplex or CrissCross */ dd_LPPtr lp,lp1; /* pointer to LP data structure that is not visible by user. */ dd_LPSolutionPtr lps,lps1; /* pointer to LP solution data that is visible by user. */ dd_MatrixPtr M; dd_colrange j; dd_DataFileType inputfile; dd_set_global_constants(); printf("\n--- Solving an LP with dd_LPSolve, and Finding an Interior Point ---\n"); /* Input an LP using the cdd library */ dd_SetInputFile(&reading,inputfile,&err); if (err!=dd_NoError) goto _L99; M=dd_PolyFile2Matrix(reading, &err); if (err!=dd_NoError) goto _L99; /* dd_WriteMatrix(stdout, M); */ lp=dd_Matrix2LP(M, &err); if (err!=dd_NoError) goto _L99; /* Solve the LP by cdd LP solver. */ printf("\n--- Running dd_LPSolve ---\n"); solver=dd_DualSimplex; dd_LPSolve(lp, solver, &err); /* Solve the LP */ if (err!=dd_NoError) goto _L99; /* Write the LP solutions by cdd LP reporter. */ /* dd_WriteLPResult(stdout, lp, err); */ /* dd_WriteLPResult(writing, lp, err); */ /* One can access the solutions by loading them. See dd_WriteLPResult for outputing the results correctly. */ lps=dd_CopyLPSolution(lp); if (lps->LPS==dd_Optimal){ printf("Optimal solution found:\n"); printf(" primal_solution\n"); for (j=1; j<lps->d; j++) { printf(" %3ld : ",j); dd_WriteNumber(stdout,lps->sol[j]); printf("\n"); } printf(" dual_solution\n"); for (j=1; j<lps->d; j++){ if (lps->nbindex[j+1]>0) { printf(" %3ld : ",lps->nbindex[j+1]); dd_WriteNumber(stdout,lps->dsol[j]); printf("\n"); } } printf(" optimal_value : "); dd_WriteNumber(stdout,lps->optvalue); printf("\n"); } /* Find an interior point with cdd LP library. */ printf("\n--- Running dd_FindInteriorPoint ---\n"); lp1=dd_MakeLPforInteriorFinding(lp); printf("The LP to be solved for finding an interior point:\n"); dd_WriteLP(stdout,lp1); dd_LPSolve(lp1,solver,&err); if (err!=dd_NoError) goto _L99; /* Write an interior point. */ lps1=dd_CopyLPSolution(lp1); if (dd_Positive(lps1->optvalue)){ printf("\nAn interior point found: ("); for (j=1; j <(lps1->d)-1; j++) { dd_WriteNumber(stdout,lps1->sol[j]); } printf(")\n"); } if (dd_Negative(lps1->optvalue)) printf("\nThe feasible region is empty.\n"); if (dd_EqualToZero(lps1->optvalue)) printf("\nThe feasible region is nonempty but has no interior point.\n"); /* Free allocated spaces. */ dd_FreeLPSolution(lps); dd_FreeLPData(lp); dd_FreeLPSolution(lps1); dd_FreeLPData(lp1); dd_FreeMatrix(M); _L99:; if (err!=dd_NoError) dd_WriteErrorMessages(stdout, err); dd_free_global_constants(); /* At the end, this should be called. */ return 0; }
void init_library() { dd_set_global_constants();dd_debug = false; }
SEXP impliedLinearity(SEXP m, SEXP h) { GetRNGstate(); if (! isMatrix(m)) error("'m' must be matrix"); if (! isLogical(h)) error("'h' must be logical"); if (LENGTH(h) != 1) error("'h' must be scalar"); if (! isString(m)) error("'m' must be character"); SEXP m_dim; PROTECT(m_dim = getAttrib(m, R_DimSymbol)); int nrow = INTEGER(m_dim)[0]; int ncol = INTEGER(m_dim)[1]; UNPROTECT(1); if (nrow <= 1) error("no use if only one row"); if (ncol <= 3) error("no use if only one col"); for (int i = 0; i < nrow; i++) { const char *foo = CHAR(STRING_ELT(m, i)); if (strlen(foo) != 1) error("column one of 'm' not zero-or-one valued"); if (! (foo[0] == '0' || foo[0] == '1')) error("column one of 'm' not zero-or-one valued"); } if (! LOGICAL(h)[0]) for (int i = nrow; i < 2 * nrow; i++) { const char *foo = CHAR(STRING_ELT(m, i)); if (strlen(foo) != 1) error("column two of 'm' not zero-or-one valued"); if (! (foo[0] == '0' || foo[0] == '1')) error("column two of 'm' not zero-or-one valued"); } dd_set_global_constants(); /* note actual type of "value" is mpq_t (defined in cddmp.h) */ mytype value; dd_init(value); dd_MatrixPtr mf = dd_CreateMatrix(nrow, ncol - 1); /* note our matrix has one more column than Fukuda's */ /* representation */ if(LOGICAL(h)[0]) mf->representation = dd_Inequality; else mf->representation = dd_Generator; mf->numbtype = dd_Rational; /* linearity */ for (int i = 0; i < nrow; i++) { const char *foo = CHAR(STRING_ELT(m, i)); if (foo[0] == '1') set_addelem(mf->linset, i + 1); /* note conversion from zero-origin to one-origin indexing */ } /* matrix */ for (int j = 1, k = nrow; j < ncol; j++) for (int i = 0; i < nrow; i++, k++) { const char *rat_str = CHAR(STRING_ELT(m, k)); if (mpq_set_str(value, rat_str, 10) == -1) { dd_FreeMatrix(mf); dd_clear(value); dd_free_global_constants(); error("error converting string to GMP rational"); } mpq_canonicalize(value); dd_set(mf->matrix[i][j - 1], value); /* note our matrix has one more column than Fukuda's */ } dd_ErrorType err = dd_NoError; dd_rowset out = dd_ImplicitLinearityRows(mf, &err); if (err != dd_NoError) { rr_WriteErrorMessages(err); set_free(out); dd_FreeMatrix(mf); dd_clear(value); dd_free_global_constants(); error("failed"); } SEXP foo; PROTECT(foo = rr_set_fwrite(out)); set_free(out); dd_FreeMatrix(mf); dd_clear(value); dd_free_global_constants(); PutRNGstate(); UNPROTECT(1); return foo; }
int main(int argc, char *argv[]) { dd_MatrixPtr M=NULL,M1=NULL,M2=NULL; dd_colrange j,s,d; dd_ErrorType err=dd_NoError; dd_rowset redset,impl_linset; dd_rowindex newpos; mytype val; dd_DataFileType inputfile; FILE *reading=NULL; dd_set_global_constants(); /* First, this must be called. */ dd_init(val); if (argc>1) strcpy(inputfile,argv[1]); if (argc<=1 || !SetInputFile(&reading,argv[1])){ dd_WriteProgramDescription(stdout); fprintf(stdout,"\ncddlib test program to apply Fourier's Elimination to an H-polyhedron.\n"); dd_SetInputFile(&reading,inputfile, &err); } if (err==dd_NoError) { M=dd_PolyFile2Matrix(reading, &err); } else { fprintf(stderr,"Input file not found\n"); goto _L99; } if (err!=dd_NoError) goto _L99; d=M->colsize; M2=dd_CopyMatrix(M); printf("How many variables to elminate? (max %ld): ",d-1); scanf("%ld",&s); if (s>0 && s < d){ for (j=1; j<=s; j++){ M1=dd_FourierElimination(M2, &err); printf("\nRemove the variable %ld. The resulting redundant system.\n",d-j); dd_WriteMatrix(stdout, M1); dd_MatrixCanonicalize(&M1, &impl_linset, &redset, &newpos, &err); if (err!=dd_NoError) goto _L99; fprintf(stdout, "\nRedundant rows: "); set_fwrite(stdout, redset); dd_FreeMatrix(M2); M2=M1; set_free(redset); set_free(impl_linset); free(newpos); } printf("\nNonredundant representation:\n"); dd_WriteMatrix(stdout, M1); } else { printf("Value out of range\n"); } dd_FreeMatrix(M); dd_FreeMatrix(M1); dd_clear(val); _L99:; /* if (err!=dd_NoError) dd_WriteErrorMessages(stderr,err); */ dd_free_global_constants(); /* At the end, this should be called. */ return 0; }
int main(int argc, char *argv[]) { dd_MatrixPtr M1=NULL,M2=NULL,M2row=NULL,M1plus=NULL; dd_colrange d1; dd_rowrange i,m1,m2,m1plus; dd_ErrorType err=dd_NoError,err1=dd_NoError,err2=dd_NoError; dd_rowset delset,rowset2; dd_Arow cvec; /* certificate */ time_t starttime, endtime; dd_DataFileType inputfile1,inputfile2; FILE *reading1=NULL,*reading2=NULL; dd_set_global_constants(); /* First, this must be called. */ dd_WriteProgramDescription(stdout); fprintf(stdout,"\ncddlib test program to check redundancy of additional data.\n"); if (argc>2){ strcpy(inputfile1,argv[1]); strcpy(inputfile2,argv[2]); } /* if (argc<=2){ fprintf(stdout,"\nUsage:\n redexter file1 file2\n"); goto _L99; } */ if (!SetInputFile(&reading1,argv[1])){ fprintf(stdout,"\nSpecify file1.\n"); dd_SetInputFile(&reading1,inputfile1, &err1); } if (!SetInputFile(&reading2,argv[2])){ fprintf(stdout,"\nSpecify the secondary file.\n"); dd_SetInputFile(&reading2,inputfile2, &err2); } if ((err1==dd_NoError) && (err2==dd_NoError)) { M1=dd_PolyFile2Matrix(reading1, &err1); M2=dd_PolyFile2Matrix(reading2, &err2); } else { fprintf(stderr,"Input file(s) not found\n"); goto _L99; } if ((err1!=dd_NoError) || (err2!=dd_NoError)) goto _L99; m1=M1->rowsize; m2=M2->rowsize; set_initialize(&delset,m2); m1plus=m1+1; if (M1->representation==dd_Generator){ d1=(M1->colsize)+1; } else { d1=M1->colsize; } dd_InitializeArow(d1,&cvec); fprintf(stdout, "\nThe first matrix\n"); dd_WriteMatrix(stdout, M1); fprintf(stdout, "\nThe second matrix\n"); dd_WriteMatrix(stdout, M2); printf("\nChecking whether each row of the second matrix is redundant w.r.t. the first.\n"); time(&starttime); for (i=1; i<=m2; i++){ set_initialize(&rowset2,m2); set_addelem(rowset2, i); set_compl(delset, rowset2); M2row=dd_MatrixSubmatrix(M2, delset); M1plus=dd_MatrixAppend(M1,M2row); if (dd_Redundant(M1plus, m1plus, cvec, &err)) { printf("%ld-th row: redundant\n", i); } else { printf("%ld-th row: non-redundant\n A certificate:", i); dd_WriteArow(stdout, cvec, d1); } dd_FreeMatrix(M1plus); dd_FreeMatrix(M2row); set_free(rowset2); } time(&endtime); dd_WriteTimes(stdout,starttime,endtime); set_free(delset); dd_FreeMatrix(M1); dd_FreeMatrix(M2); _L99:; if (err1!=dd_NoError) dd_WriteErrorMessages(stderr,err1); if (err2!=dd_NoError) dd_WriteErrorMessages(stderr,err2); return 0; }
SEXP redundant(SEXP m, SEXP h) { GetRNGstate(); if (! isString(m)) error("'m' must be character"); if (! isMatrix(m)) error("'m' must be matrix"); if (! isLogical(h)) error("'h' must be logical"); if (LENGTH(h) != 1) error("'h' must be scalar"); SEXP m_dim; PROTECT(m_dim = getAttrib(m, R_DimSymbol)); int nrow = INTEGER(m_dim)[0]; int ncol = INTEGER(m_dim)[1]; UNPROTECT(1); #ifdef WOOF printf("nrow = %d\n", nrow); printf("ncol = %d\n", ncol); #endif /* WOOF */ if (nrow < 2) error("less than 2 rows, cannot be redundant"); if (ncol <= 2) error("no cols in m[ , - c(1, 2)]"); for (int i = 0; i < nrow; i++) { const char *foo = CHAR(STRING_ELT(m, i)); if (strlen(foo) != 1) error("column one of 'm' not zero-or-one valued"); if (! (foo[0] == '0' || foo[0] == '1')) error("column one of 'm' not zero-or-one valued"); } if (! LOGICAL(h)[0]) for (int i = nrow; i < 2 * nrow; i++) { const char *foo = CHAR(STRING_ELT(m, i)); if (strlen(foo) != 1) error("column two of 'm' not zero-or-one valued"); if (! (foo[0] == '0' || foo[0] == '1')) error("column two of 'm' not zero-or-one valued"); } dd_set_global_constants(); /* note actual type of "value" is mpq_t (defined in cddmp.h) */ mytype value; dd_init(value); dd_MatrixPtr mf = dd_CreateMatrix(nrow, ncol - 1); /* note our matrix has one more column than Fukuda's */ /* representation */ if(LOGICAL(h)[0]) mf->representation = dd_Inequality; else mf->representation = dd_Generator; mf->numbtype = dd_Rational; /* linearity */ for (int i = 0; i < nrow; i++) { const char *foo = CHAR(STRING_ELT(m, i)); if (foo[0] == '1') set_addelem(mf->linset, i + 1); /* note conversion from zero-origin to one-origin indexing */ } /* matrix */ for (int j = 1, k = nrow; j < ncol; j++) for (int i = 0; i < nrow; i++, k++) { const char *rat_str = CHAR(STRING_ELT(m, k)); if (mpq_set_str(value, rat_str, 10) == -1) ERROR_WITH_CLEANUP_3("error converting string to GMP rational"); mpq_canonicalize(value); dd_set(mf->matrix[i][j - 1], value); /* note our matrix has one more column than Fukuda's */ } dd_rowset impl_linset, redset; dd_rowindex newpos; dd_ErrorType err = dd_NoError; dd_MatrixCanonicalize(&mf, &impl_linset, &redset, &newpos, &err); if (err != dd_NoError) { rr_WriteErrorMessages(err); ERROR_WITH_CLEANUP_6("failed"); } int mrow = mf->rowsize; int mcol = mf->colsize; if (mcol + 1 != ncol) ERROR_WITH_CLEANUP_6("Cannot happen! computed matrix has" " wrong number of columns"); #ifdef WOOF printf("mrow = %d\n", mrow); printf("mcol = %d\n", mcol); #endif /* WOOF */ SEXP bar; PROTECT(bar = allocMatrix(STRSXP, mrow, ncol)); /* linearity output */ for (int i = 0; i < mrow; i++) if (set_member(i + 1, mf->linset)) SET_STRING_ELT(bar, i, mkChar("1")); else SET_STRING_ELT(bar, i, mkChar("0")); /* note conversion from zero-origin to one-origin indexing */ /* matrix output */ for (int j = 1, k = mrow; j < ncol; j++) for (int i = 0; i < mrow; i++, k++) { dd_set(value, mf->matrix[i][j - 1]); /* note our matrix has one more column than Fukuda's */ char *zstr = NULL; zstr = mpq_get_str(zstr, 10, value); SET_STRING_ELT(bar, k, mkChar(zstr)); free(zstr); } if (mf->representation == dd_Inequality) { SEXP attr_name, attr_value; PROTECT(attr_name = ScalarString(mkChar("representation"))); PROTECT(attr_value = ScalarString(mkChar("H"))); setAttrib(bar, attr_name, attr_value); UNPROTECT(2); } if (mf->representation == dd_Generator) { SEXP attr_name, attr_value; PROTECT(attr_name = ScalarString(mkChar("representation"))); PROTECT(attr_value = ScalarString(mkChar("V"))); setAttrib(bar, attr_name, attr_value); UNPROTECT(2); } int impl_size = set_card(impl_linset); int red_size = set_card(redset); int nresult = 1; int iresult = 1; SEXP baz = NULL; if (impl_size > 0) { PROTECT(baz = rr_set_fwrite(impl_linset)); nresult++; } SEXP qux = NULL; if (red_size > 0) { PROTECT(qux = rr_set_fwrite(redset)); nresult++; } SEXP fred = NULL; { PROTECT(fred = allocVector(INTSXP, nrow)); for (int i = 1; i <= nrow; i++) INTEGER(fred)[i - 1] = newpos[i]; nresult++; } #ifdef WOOF fprintf(stderr, "impl_size = %d\n", impl_size); fprintf(stderr, "red_size = %d\n", red_size); fprintf(stderr, "nresult = %d\n", nresult); if (baz) fprintf(stderr, "LENGTH(baz) = %d\n", LENGTH(baz)); if (qux) fprintf(stderr, "LENGTH(qux) = %d\n", LENGTH(qux)); #endif /* WOOF */ SEXP result, resultnames; PROTECT(result = allocVector(VECSXP, nresult)); PROTECT(resultnames = allocVector(STRSXP, nresult)); SET_STRING_ELT(resultnames, 0, mkChar("output")); SET_VECTOR_ELT(result, 0, bar); if (baz) { SET_STRING_ELT(resultnames, iresult, mkChar("implied.linearity")); SET_VECTOR_ELT(result, iresult, baz); iresult++; } if (qux) { SET_STRING_ELT(resultnames, iresult, mkChar("redundant")); SET_VECTOR_ELT(result, iresult, qux); iresult++; } { SET_STRING_ELT(resultnames, iresult, mkChar("new.position")); SET_VECTOR_ELT(result, iresult, fred); iresult++; } namesgets(result, resultnames); set_free(redset); set_free(impl_linset); free(newpos); dd_FreeMatrix(mf); dd_clear(value); dd_free_global_constants(); PutRNGstate(); UNPROTECT(nresult + 2); return result; }
SEXP allfaces(SEXP hrep) { GetRNGstate(); if (! isMatrix(hrep)) error("'hrep' must be matrix"); if (! isString(hrep)) error("'hrep' must be character"); SEXP hrep_dim; PROTECT(hrep_dim = getAttrib(hrep, R_DimSymbol)); int nrow = INTEGER(hrep_dim)[0]; int ncol = INTEGER(hrep_dim)[1]; UNPROTECT(1); if (nrow <= 0) error("no rows in 'hrep'"); if (ncol <= 3) error("three or fewer cols in hrep"); for (int i = 0; i < nrow; ++i) { const char *foo = CHAR(STRING_ELT(hrep, i)); if (strlen(foo) != 1) error("column one of 'hrep' not zero-or-one valued"); if (! (foo[0] == '0' || foo[0] == '1')) error("column one of 'hrep' not zero-or-one valued"); } dd_set_global_constants(); /* note actual type of "value" is mpq_t (defined in cddmp.h) */ mytype value; dd_init(value); dd_MatrixPtr mf = dd_CreateMatrix(nrow, ncol - 1); /* note our matrix has one more column than Fukuda's */ mf->representation = dd_Inequality; mf->numbtype = dd_Rational; /* linearity */ for (int i = 0; i < nrow; ++i) { const char *foo = CHAR(STRING_ELT(hrep, i)); if (foo[0] == '1') set_addelem(mf->linset, i + 1); /* note conversion from zero-origin to one-origin indexing */ } /* matrix */ for (int j = 1, k = nrow; j < ncol; ++j) for (int i = 0; i < nrow; ++i, ++k) { const char *rat_str = CHAR(STRING_ELT(hrep, k)); if (mpq_set_str(value, rat_str, 10) == -1) { dd_FreeMatrix(mf); dd_clear(value); dd_free_global_constants(); error("error converting string to GMP rational"); } mpq_canonicalize(value); dd_set(mf->matrix[i][j - 1], value); /* note our matrix has one more column than Fukuda's */ } SEXP result; PROTECT(result = FaceEnum(mf)); dd_FreeMatrix(mf); dd_clear(value); dd_free_global_constants(); if (result == R_NilValue) error("failed"); PutRNGstate(); UNPROTECT(1); return result; }