blob: de287b43b3197a47e21d71be76ae0b11a70525c5 [file] [log] [blame]
Austin Schuh405fa6c2015-09-06 18:13:55 -07001/* adjacency.c: Test program to call the cdd library cddlib
2 written by Komei Fukuda, fukuda@ifor.math.ethz.ch
3 Version 0.93, July 18, 2003
4 Standard ftp site: ftp.ifor.math.ethz.ch, Directory: pub/fukuda/cdd
5*/
6
7/* This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20*/
21
22#include "setoper.h"
23#include "cdd.h"
24#include <stdio.h>
25#include <stdlib.h>
26#include <time.h>
27#include <math.h>
28#include <string.h>
29
30dd_boolean SetInputFile(FILE **f, dd_DataFileType fname)
31{
32 dd_boolean success=dd_FALSE;
33 success=dd_FALSE;
34
35 if ( ( *f = fopen(fname, "r") )!= NULL) {
36 printf("input file %s is open\n", fname);
37 success=dd_TRUE;
38 }
39 else{
40 printf("The input file %s not found\n",fname);
41 }
42 return success;
43}
44
45dd_boolean SetWriteFile(FILE **f, dd_DataFileType fname)
46{
47 dd_boolean success=dd_FALSE;
48
49 if ( (*f = fopen(fname, "w")) != NULL){
50 printf("output file %s is open\n",fname);
51 success=dd_TRUE;
52 }
53 else{
54 printf("The output file %s cannot be opened\n",fname);
55 }
56 return success;
57}
58
59
60int main(int argc, char *argv[])
61{
62 dd_MatrixPtr M=NULL,M2=NULL,M3=NULL;
63 dd_SetFamilyPtr A=NULL;
64 dd_colrange d;
65 dd_ErrorType err=dd_NoError;
66 dd_rowset redrows,linrows,ignoredrows, basisrows;
67 dd_colset ignoredcols, basiscols;
68 long rank;
69 mytype val;
70 time_t starttime, endtime;
71 dd_DataFileType inputfile;
72 FILE *reading=NULL;
73
74 dd_set_global_constants(); /* First, this must be called. */
75
76 dd_init(val);
77 if (argc>1) strcpy(inputfile,argv[1]);
78 if (argc<=1 || !SetInputFile(&reading,argv[1])){
79 dd_WriteProgramDescription(stdout);
80 fprintf(stdout,"\ncddlib test program to remove redundancy and compute adjacency of the resulting representation.\n");
81 dd_SetInputFile(&reading,inputfile, &err);
82 }
83 if (err==dd_NoError) {
84 M=dd_PolyFile2Matrix(reading, &err);
85 }
86 else {
87 fprintf(stderr,"Input file not found\n");
88 goto _L99;
89 }
90
91 if (err!=dd_NoError) goto _L99;
92
93 if (M->representation==dd_Generator) d=M->colsize+1; else d=M->colsize;
94
95 fprintf(stdout, "redundant rows:\n");
96 time(&starttime);
97 redrows=dd_RedundantRows(M, &err);
98 time(&endtime);
99 set_fwrite(stdout, redrows);
100 dd_WriteTimes(stdout,starttime,endtime);
101
102 M2=dd_MatrixSubmatrix(M, redrows);
103
104 fprintf(stdout, "Implicit linearity (after removal of redundant rows): ");
105 linrows=dd_ImplicitLinearityRows(M2, &err);
106
107 if (M->representation==dd_Generator)
108 fprintf(stdout," %ld ", set_card(linrows));
109 else
110 fprintf(stdout," %ld ", set_card(linrows));
111 set_fwrite(stdout,linrows);
112 set_uni(M2->linset, M2->linset, linrows);
113 /* add the implicit linrows to the explicit linearity rows */
114
115 printf("\nNonredundant representation (except possibly for the linearity part):\n");
116 dd_WriteMatrix(stdout, M2);
117
118 /* To remove redundancy of the linearity part,
119 we need to compute the rank and a basis of the linearity part. */
120 set_initialize(&ignoredrows, M2->rowsize);
121 set_initialize(&ignoredcols, M2->colsize);
122 set_compl(ignoredrows, M2->linset);
123 rank=dd_MatrixRank(M2,ignoredrows,ignoredcols, &basisrows, &basiscols);
124 set_diff(ignoredrows, M2->linset, basisrows);
125 M3=dd_MatrixSubmatrix(M2, ignoredrows);
126 if (rank>0){
127 if (set_card(ignoredrows)) {
128 fprintf(stdout,"\nThe following %ld linearity rows are dependent and unnecessary:", set_card(ignoredrows));
129 set_fwrite(stdout,ignoredrows);
130 }
131 } else
132 fprintf(stdout,"\nThe linearity rows are independent and thus minimal\n");
133
134 printf("Nonredundant representation (= minimal representation):\n");
135 dd_WriteMatrix(stdout, M3);
136
137 printf("\nAdjacency of the minimal representation:\n");
138 A=dd_Matrix2Adjacency(M3, &err);
139 dd_WriteSetFamily(stdout, A);
140
141 dd_clear(val);
142 set_free(linrows);
143 set_free(basisrows);
144 set_free(basiscols);
145 set_free(ignoredrows);
146 set_free(ignoredcols);
147 set_free(redrows);
148
149 if (A!=NULL) dd_FreeSetFamily(A);
150 dd_FreeMatrix(M);
151 dd_FreeMatrix(M2);
152 dd_FreeMatrix(M3);
153
154_L99:;
155 if (err!=dd_NoError) dd_WriteErrorMessages(stderr,err);
156 dd_free_global_constants(); /* At the end, this should be called. */
157 return 0;
158}
159
160
161/* end of adjacency.c */