PIPS
sc_proj.c
Go to the documentation of this file.
1 /*
2  $Id: sc_proj.c 1671 2019-06-26 19:14:11Z coelho $
3 
4  Copyright 1989-2016 MINES ParisTech
5 
6  This file is part of Linear/C3 Library.
7 
8  Linear/C3 Library is free software: you can redistribute it and/or modify it
9  under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  any later version.
12 
13  Linear/C3 Library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 
17  See the GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with Linear/C3 Library. If not, see <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "linear_assert.h"
28 #include <getopt.h>
29 
30 #include "arithmetique.h"
31 #include "vecteur.h"
32 #include "contrainte.h"
33 #include "sc.h"
34 
35 extern char * sc_internal_symbol_table(char *);
36 
37 int main(int argc, char * const argv[])
38 {
39  // option management
40  bool reverse = false, bound = false;
41  int debug = 0, opt;
42  while ((opt = getopt(argc, argv, "bhrD")) != -1) {
43  switch (opt) {
44  case 'r': reverse = true; break;
45  case 'b': bound = true; break;
46  case 'D': debug++; break;
47  case 'h':
48  fprintf(stdout,
49  "usage: %s [-r or -b] variables... < system\n"
50  "\tdefault: project listed variables\n"
51  "\t-r: project all but the listed variables\n"
52  "\t-b: show bounds on listed variables\n",
53  argv[0]);
54  exit(0);
55  default: exit(1);
56  }
57  }
58  // only one of -r and -b
59  assert(!(reverse && bound));
60 
61  // get system from stdin
62  Psysteme s;
63  bool ok = sc_fscan(stdin, &s);
64  assert(ok);
65  if (debug >= 2) sc_fprint(stderr, s, *variable_default_name);
66 
67  // get command arguments as a base
68  Pbase arg_base = BASE_NULLE;
69  for (int i = optind; i < argc; i++)
70  arg_base = base_add_variable(arg_base, sc_internal_symbol_table(argv[i]));
71 
72  Pbase proj_base;
73  if (reverse || bound) {
74  proj_base = vect_copy(sc_base(s));
75  for (Pbase b = arg_base; b != BASE_NULLE; b = vecteur_succ(b))
76  proj_base = base_remove_variable(proj_base, vecteur_var(b));
77  }
78  else {
79  proj_base = vect_copy(arg_base);
80  }
81 
82  // project all "proj_base" variables
83  for (Pbase b = proj_base; b != BASE_NULLE; b = vecteur_succ(b))
84  s = sc_projection(s, vecteur_var(b));
85 
86  // cleanup
87  sc_nredund(&s);
88 
89  if (debug) sc_fprint(stderr, s, *variable_default_name);
90 
91  // print out result
92  if (bound) {
93  // we must bound each remaining variables
94  for (Pbase b = arg_base; b != BASE_NULLE; b = vecteur_succ(b)) {
96  Psysteme sb = sc_copy(s);
97  // YES, we use *s* base
98  for (Pbase c = sc_base(s); c != BASE_NULLE; c = vecteur_succ(c)) {
99  if (vecteur_var(c) != keep)
100  sb = sc_projection(sb, vecteur_var(c));
101  }
102  sc_nredund(&sb);
104  sc_rm(sb);
105  }
106  }
107  else
108  sc_fprint(stdout, s, *variable_default_name);
109 
110  base_rm(arg_base);
111  base_rm(proj_base);
112  sc_rm(s);
113  return 0;
114 }
Pbase base_add_variable(Pbase b, Variable var)
Pbase base_add_variable(Pbase b, Variable v): add variable v as a new dimension to basis b at the end...
Definition: base.c:88
Pbase base_remove_variable(Pbase b, Variable v)
Pbase base_remove_variable(b, v): remove basis vector relative to v from b; abort if v is not in b;.
Definition: base.c:122
char * variable_default_name(Variable v)
char * variable_default_name(Variable v): returns the name of variable v
Definition: variable.c:81
#define exit(code)
Definition: misc-local.h:54
void debug(const int the_expected_debug_level, const char *calling_function_name, const char *a_message_format,...)
ARARGS0.
Definition: debug.c:189
#define assert(ex)
Definition: newgen_assert.h:41
void sc_rm(Psysteme ps)
void sc_rm(Psysteme ps): liberation de l'espace memoire occupe par le systeme de contraintes ps;
Definition: sc_alloc.c:277
Psysteme sc_copy(Psysteme ps)
Psysteme sc_copy(Psysteme ps): duplication d'un systeme (allocation et copie complete des champs sans...
Definition: sc_alloc.c:230
void sc_fprint(FILE *fp, Psysteme ps, get_variable_name_t nom_var)
void sc_fprint(FILE * f, Psysteme ps, char * (*nom_var)()): cette fonction imprime dans le fichier po...
Definition: sc_io.c:220
bool sc_fscan(FILE *f, Psysteme *ps)
bool sc_fscan(FILE * f, Psysteme * ps): construit un systeme d'inegalites et d'egalites lineaires a p...
Definition: sc_io.c:121
int fprintf()
test sc_min : ce test s'appelle par : programme fichier1.data fichier2.data ...
char * sc_internal_symbol_table(char *)
int main(int argc, char *const argv[])
Definition: sc_proj.c:37
static bool ok
le type des coefficients dans les vecteurs: Value est defini dans le package arithmetique
Definition: vecteur-local.h:89
Definition: statement.c:4047
int optind
@ keep
bj > b1 -> h1/hj = h1
Definition: union-local.h:61
#define vecteur_var(v)
#define vecteur_succ(v)
#define base_rm(b)
void * Variable
arithmetique is a requirement for vecteur, but I do not want to inforce it in all pips files....
Definition: vecteur-local.h:60
#define BASE_NULLE
MACROS SUR LES BASES.
Pbase vect_copy(Pvecteur b)
direct duplication.
Definition: alloc.c:240