PIPS
io.c
Go to the documentation of this file.
1 /*
2 
3  $Id: io.c 1671 2019-06-26 19:14:11Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of Linear/C3 Library.
8 
9  Linear/C3 Library is free software: you can redistribute it and/or modify it
10  under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  any later version.
13 
14  Linear/C3 Library is distributed in the hope that it will be useful, but WITHOUT ANY
15  WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  FITNESS FOR A PARTICULAR PURPOSE.
17 
18  See the GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with Linear/C3 Library. If not, see <http://www.gnu.org/licenses/>.
22 
23 */
24 
25  /*
26  * LECTURE ET AFFICHAGE ET DIMENSION D'UN VECTEUR
27  *
28  * Malik Imadache, Corinne Ancourt, Neil Butler, Francois Irigoin
29  *
30  * Modifications:
31  * - suppression de l'ancienne version de vect_print() qui faisait
32  * l'hypothese que le type Variable etait egal au type int; remplacement
33  * par un appel a vect_fprint(); (FI, 27/11/89)
34  * - ajout de vect_dump() qui utilise implicitement la fonction
35  * variable_dump_name()
36  */
37 
38 /*LINTLIBRARY*/
39 #ifdef HAVE_CONFIG_H
40  #include "config.h"
41 #endif
42 
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include "linear_assert.h"
46 #include <string.h>
47 
48 #include "boolean.h"
49 #include "arithmetique.h"
50 #include "vecteur.h"
51 
52 /* Pvecteur vect_read(Pbase * b): lecture interactive d'un vecteur sur stdin;
53  * la base b est modifiee de maniere a ce que chaque composante y figure bien;
54  *
55  * La representation d'un vecteur dont la troisieme composante vaut 2,
56  * la dixieme 6 et les autres 0 peut etre:
57  * 3 2 1 10 6 0
58  * ou
59  * x3 2 1 x10 6 0
60  * Le 1 veut dire qu'il faut continuer les lectures, le zero, qu'il faut
61  * arreter
62  *
63  * Ce format est malheureusement incompatible avec celui de vect_fprint()
64  */
66 {
67  Pvecteur v1, v = VECTEUR_NUL;
68  // Let's assume that no variable name will be longer than 9 characters
69  char buffer[10];
70  int c; // flag de continuation
71 
72  c = 1;
73  while (c == 1) {
74  Variable var;
75  Value val;
76  // vect_chain() n'est pas utilise pour conserver l'ordre des couples
77  (void) printf ("valeur de variable :");
78  int n = scanf("%9s",buffer);
79  assert(n==1);
80  (void) printf ("valeur du coefficient de la variable :");
81  (void) scan_Value(&val);
83  var = variable_make(buffer);
84  *b = vect_add_variable(*b,var);
85  }
86  else {
87  var = base_find_variable(*b, (Variable) buffer);
88  }
89  v1 = vect_new(var, val);
90  v1->succ = v;
91  v = v1;
92  (void) printf ("'1' -->rentrer d'autres valeurs,(0-2..9)"
93  "sinon. votre choix:");
94  n = scanf("%d", &c);
95  assert(n==1);
96  }
97  return v;
98 }
99 
100 /* void vect_fprint(FILE * f, Pvecteur v, char * (*variable_name)()):
101  * impression d'un vecteur creux v sur le fichier f; le nom de chaque
102  * coordonnee est donne par la fonction variable_name()
103  *
104  * Par exemple, le vecteur v
105  * -> -> -> ->
106  * v == 2 i - 3 j + k
107  * est imprime sous la forme
108  * 2 * i - 3 * j + k LF
109  * ou les symboles i, j et k sont obtenus via la fonction pointee par
110  * variable_name()
111  *
112  * Le vecteur nul est represente par
113  * vecteur nul LF
114  *
115  * Note: attention au linefeed final
116  * il n'existe pas de fonction relisant des vecteurs sous cette forme
117  * (pour le moment...)
118  *
119  * Modifications:
120  * - suppression du cas special du terme constant
121  * Resultat : core dump (FC, 28/11/94)
122  * Fixed, (BC, 6/12/94)
123  */
125  Pvecteur p;
126 
127  if(v==NULL)
128  (void) fprintf(f,"nul vector\n");
129  else
130  for (p = v; p != NULL; p = p->succ)
131  {
132  if (p->var != TCST) {
133  fprint_Value(f, p->val);
134  (void) fprintf(f," * %s ", variable_name(p->var));
135  }
136  else {
137  (void) fprint_Value(f, p->val);
138  fprintf(f, " ");
139  }
140 
141  if (p->succ != NULL) {
142  (void) fprintf(f,"+ ");}
143  else {
144  (void) fprintf(f,"\n");}
145  }
146 }
147 
148 /* void vect_fprint_as_dense(FILE * f, Pvecteur v, Pbase b):
149  *
150  * Par exemple, le vecteur v
151  * -> -> -> ->
152  * v == 2 i - 3 j + k
153  * est imprime sous la forme
154  * ( 2 -3 1)
155  * dans la base (i j k)
156  *
157  * No constant term TCST is expected.
158  */
160 FILE * f;
161 Pvecteur v;
162 Pbase b;
163 {
164  if(vect_in_basis_p(v, b)) {
165  Pvecteur coord;
166 
167  fputc('(', f);
168 
169  for(coord = b; !VECTEUR_NUL_P(coord); coord = coord->succ) {
170  Variable var = vecteur_var(coord);
171 
172  if(VARIABLE_DEFINED_P(var)) {
173  fprint_Value(f, vect_coeff(var, v));
174  if(VECTEUR_NUL_P(coord->succ)) {
175  fputc(')', f);
176  }
177  else {
178  fputc(',', f);
179  }
180  }
181  else {
182  /* I do not know what should be done for constant terms... */
183  abort();
184  }
185  }
186  }
187  else
188  abort();
189 }
190 
191 /* void vect_fprint_as_monome(FILE * f, Pvecteur v, Pbase b,
192  * char * (*variable_name)(), char *mult_symbol):
193  * impression d'un vecteur creux considere comme un monome sans coefficient.
194  * Par ex.: le vecteur 2 * i - 3 * j + k est ecrit: "i^2 * j^(-3) * k".
195  * Le nom de chaque variable est donne par la fonction variable_name().
196  * L'ordre dans lequel sont ecrites les inconnues du monome est fixe par la base b.
197  * Le symbole "multiplication" est passe dans (char *) mult_symbol: " * ", ".", "x", "", ...
198  * le vecteur de base special TCST n'est pas affiche: seulement son coefficient.
199  * Pas de \n a la fin de l'affichage.
200  */
202  Pvecteur v,
203  Pbase b,
205  char * mult_symbol) {
206  char *s = vect_sprint_as_monome(v, b, variable_name, mult_symbol);
207 
208  fprintf(f, "%s", s);
209  free(s);
210 }
211 
212 /* char *vect_sprint_as_monome(Pvecteur v, Pbase b,
213  * char * (*variable_name)(), char *mult_symbol):
214  * Retourne dans une chaine le Pvecteur considere comme un monome sans coefficient.
215  * (voir ci-dessus)
216  */
218  Pbase b,
220  char * mult_symbol) {
221  Pvecteur p;
222  char t[99];
223  char *r = t;
224  char *s = NULL;
225 
226  if (VECTEUR_NUL_P(v))
227  strcpy (&t[0], "0");
228  else if (VECTEUR_NUL_P(b)) {
229  /* si la base est vide: affiche comme ca vient */
230  for (p = v; p != NULL; p = p->succ) {
231  if (value_one_p(p->val)) {
232  (void) sprintf(r, "%s", variable_name(p->var));
233  r = strchr(r, '\0');
234  }
235  else {
236  (void) sprintf(r, "%s^", variable_name(p->var));
237  r = strchr(r, '\0');
238  sprint_Value(r, p->val);
239  r = strchr(r, '\0');
240  }
241  if (p->succ != NULL) {
242  (void) sprintf(r, "%s", mult_symbol);
243  r = strchr(r, '\0');
244  }
245  }
246  }
247  else {
248  /* si la base n'est pas vide, affiche selon l'ordre
249  * qu'elle definit */
250  bool first_var = true;
251  Value exp;
252  for ( ; !VECTEUR_NUL_P(b); b = b->succ) {
253  exp = vect_coeff(b->var, v);
254  if(exp!=0) {
255 
256  if (!first_var) {
257  (void) sprintf(r, "%s", mult_symbol);
258  r = strchr(r, '\0');
259  }
260  else
261  first_var = false;
262 
263  if (value_pos_p(exp)) {
264  if (value_one_p(exp)) {
265  (void) sprintf(r, "%s", variable_name(b->var));
266  r = strchr(r, '\0');
267  }
268  else {
269  (void) sprintf(r,"%s^", variable_name(b->var));
270  r = strchr(r, '\0');
271  sprint_Value(r, exp);
272  r = strchr(r, '\0');
273  }
274  }
275  else /* exp < 0 */ {
276  /* inutile pour les polynomes */
277  first_var = false;
278 
279  (void) sprintf(r, "%s^(", variable_name(b->var));
280  r = strchr(r, '\0');
281  sprint_Value(r, exp);
282  r = strchr(r, '\0');
283  (void) sprintf(r, ")");
284  r = strchr(r, '\0');
285  }
286  }
287  }
288  }
289  s = (char*) strdup(t);
290  assert(strlen(s)<99); /* (un peu tard:-) */
291  return s;
292 }
293 
294 
295 /* void vect_dump(Pvecteur v): print sparse vector v on stderr. By
296  * default, each dimension/variable is represented by an X followed by
297  * its hexadeximal address.
298  *
299  * Intended for debug purposes. Its behavior depends on the setting of
300  * pointer variable_debug_name by
301  * init_variable_debug_name(). Different names can be returned for
302  * each variable, more useful than a pointer value.
303  */
305  vect_fprint(stderr, v, variable_debug_name);
306 }
307 
308 /* void vect_print(Pvecteur v, char * (*variable_name)()):
309  * impression d'un vecteur creux v sur stdout; le nom de chaque
310  * coordonnee est donne par la fonction variable_name(); voir vect_fprint()
311  */
313 {
314  vect_fprint(stdout, v, variable_name);
315 }
316 
317 /* void vect_fdump(FILE * f, Pvecteur v): impression d'un vecteur creux
318  * par vect_fprint() avec passage de la fonction variable_debug_name()
319  */
320 void vect_fdump(FILE * f, Pvecteur v) {
322 }
323 
324 /* void base_fprint(FILE * f, Pbase b, char * (*variable_name)()):
325  * impression d'une base sur le fichier f; le nom de chaque
326  * coordonnee est donne par la fonction variable_name()
327  *
328  * Par exemple, la base b
329  * -> -> -> ->
330  * b == ( i j k )
331  * est imprime sous la forme
332  * i j k LF
333  * ou les symboles i, j et k sont obtenus via la fonction pointee par
334  * variable_name()
335  *
336  * Le base vide est represente par
337  * base vide LF
338  *
339  * Note: attention au linefeed final
340  * il n'existe pas de fonction relisant une base sous cette forme
341  */
342 void base_fprint(FILE * f,
343  Pbase b,
345  if(VECTEUR_NUL_P(b))
346  (void) fprintf(f,"base vide\n");
347  else
348  for ( ; !VECTEUR_NUL_P(b); b = b->succ) {
349  (void) fprintf(f,"%s", variable_name(b->var));
350  if (!VECTEUR_NUL_P(b->succ)) {
351  (void) fprintf(f,", ");
352  }
353  }
354  (void) fprintf(f," \n");
355 }
#define value_pos_p(val)
#define value_one_p(val)
int Value
bool vect_in_basis_p(Pvecteur v, Pbase b)
Pvecteur vect_in_basis_p(Pvecteur v, Pbase b): check that all coordinates in v are in b,...
Definition: base.c:342
Variable base_find_variable(Pbase b, Variable v)
Variable base_find_variable(Pbase b, Variable v): returns variable v if variable v is one of b's elem...
Definition: base.c:155
bool base_contains_variable_p(Pbase b, Variable v)
bool base_contains_variable_p(Pbase b, Variable v): returns true if variable v is one of b's elements...
Definition: base.c:136
Pbase vect_add_variable(Pbase b, Variable v)
package vecteur - routines sur les bases
Definition: base.c:61
void free(void *)
void sprint_Value(char *s, Value v)
Definition: io.c:53
int scan_Value(Value *pv)
Definition: io.c:63
void fprint_Value(FILE *f, Value v)
Definition: io.c:42
void vect_fprint_as_dense(FILE *f, Pvecteur v, Pbase b)
void vect_fprint_as_dense(FILE * f, Pvecteur v, Pbase b):
Definition: io.c:159
void vect_fprint_as_monome(FILE *f, Pvecteur v, Pbase b, get_variable_name_t variable_name, char *mult_symbol)
void vect_fprint_as_monome(FILE * f, Pvecteur v, Pbase b, char * (*variable_name)(),...
Definition: io.c:201
void vect_dump(Pvecteur v)
void vect_dump(Pvecteur v): print sparse vector v on stderr.
Definition: io.c:304
void base_fprint(FILE *f, Pbase b, get_variable_name_t variable_name)
void base_fprint(FILE * f, Pbase b, char * (*variable_name)()): impression d'une base sur le fichier ...
Definition: io.c:342
char * vect_sprint_as_monome(Pvecteur v, Pbase b, get_variable_name_t variable_name, char *mult_symbol)
char *vect_sprint_as_monome(Pvecteur v, Pbase b, char * (*variable_name)(), char *mult_symbol): Retou...
Definition: io.c:217
Pvecteur vect_read(Pbase *b)
INTLIBRARY.
Definition: io.c:65
void vect_fdump(FILE *f, Pvecteur v)
void vect_fdump(FILE * f, Pvecteur v): impression d'un vecteur creux par vect_fprint() avec passage d...
Definition: io.c:320
void vect_print(Pvecteur v, get_variable_name_t variable_name)
void vect_print(Pvecteur v, char * (*variable_name)()): impression d'un vecteur creux v sur stdout; l...
Definition: io.c:312
void vect_fprint(FILE *f, Pvecteur v, get_variable_name_t variable_name)
void vect_fprint(FILE * f, Pvecteur v, char * (*variable_name)()): impression d'un vecteur creux v su...
Definition: io.c:124
Variable variable_make(char *name)
Variable variable_make(char * name): defines a new variable of a given name.
Definition: variable.c:129
char *(* variable_debug_name)(Variable)
Debug support: pointer to the function used by debug print outs.
Definition: variable.c:114
#define abort()
Definition: misc-local.h:53
#define assert(ex)
Definition: newgen_assert.h:41
int f(int off1, int off2, int n, float r[n], float a[n], float b[n])
Definition: offsets.c:15
int fprintf()
test sc_min : ce test s'appelle par : programme fichier1.data fichier2.data ...
char * strdup()
int printf()
char * variable_name(Variable v)
polynome_ri.c
Definition: polynome_ri.c:73
static string buffer
Definition: string.c:113
le type des coefficients dans les vecteurs: Value est defini dans le package arithmetique
Definition: vecteur-local.h:89
Value val
Definition: vecteur-local.h:91
Variable var
Definition: vecteur-local.h:90
struct Svecteur * succ
Definition: vecteur-local.h:92
#define exp
Avoid some warnings from "gcc -Wshadow".
Definition: vasnprintf.c:207
#define VARIABLE_DEFINED_P(v)
Definition: vecteur-local.h:66
#define TCST
VARIABLE REPRESENTANT LE TERME CONSTANT.
#define vecteur_var(v)
#define VECTEUR_NUL
DEFINITION DU VECTEUR NUL.
char *(* get_variable_name_t)(Variable)
Definition: vecteur-local.h:62
#define VECTEUR_NUL_P(v)
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
Pvecteur vect_new(Variable var, Value coeff)
Pvecteur vect_new(Variable var,Value coeff): allocation d'un vecteur colineaire au vecteur de base va...
Definition: alloc.c:110
Value vect_coeff(Variable var, Pvecteur vect)
Variable vect_coeff(Variable var, Pvecteur vect): coefficient de coordonnee var du vecteur vect —> So...
Definition: unaires.c:228