PIPS
pnome-io.c File Reference
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "linear_assert.h"
#include <ctype.h>
#include "boolean.h"
#include "arithmetique.h"
#include "vecteur.h"
#include "polynome.h"
+ Include dependency graph for pnome-io.c:

Go to the source code of this file.

Macros

#define POLYNOME_BUFFER_SIZE   20480
 

Functions

void float_to_frac (float x, char **ps)
 void float_to_frac(float x, char** ps) PRIVATE returns the simplest representation of floating-point number x tries to make an integer, then a small fraction, then a floating-point, then a floating-point with exponent. More...
 
void monome_fprint (FILE *fd, Pmonome pm, Pbase pb, bool plus_sign, char *(*variable_name)(Variable))
 void monome_fprint(FILE* fd, Pmonome pm, Pbase pb, bool plus_sign, char* (*variable_name)()) PRIVATE Outputs to file fd an ASCII form of monomial pm, naming variables with the "variable-name" function, ordering them with the basis pb. More...
 
char * monome_sprint (Pmonome pm, Pbase pb, bool plus_sign, char *(*variable_name)(Variable))
 char monome_sprint(Pmonome pm, Pbase pb, bool plus_sign, char (*variable_name)()) PRIVATE Outputs a string representing monomial pm, naming variables with the "variable-name" function, ordering them with the basis pb. More...
 
void polynome_fprint (FILE *fd, Ppolynome pp, char *(*variable_name)(Variable), int *is_inferior_var)
 void polynome_fprint(FILE* fd, Ppolynome pp, char* (*variable_name)(), bool (*is_inferior_var)()) Outputs to file fd an ASCII form of polynomial pp, using the user-provided function variable_name(Variable var) to associate the "Variable" pointers with the variable names. More...
 
char * polynome_sprint (Ppolynome pp, char *(*variable_name)(Variable), int *is_inferior_var)
 char polynome_sprint(Ppolynome pp, char (*variable_name)(), bool (*is_inferior_var)()) Outputs to file fd an ASCII form of polynomial pp, using the user-provided function variable_name(Variable var) to associate the "Variable" pointers with the variable names. More...
 
char * default_variable_name (Variable var)
 char *default_variable_name(Variable var) returns for variable var the name "Vxxxx" where xxxx are four letters computed from (int) var. More...
 
int default_is_inferior_var (Variable var1, Variable var2)
 bool default_is_inferior_var(Variable var1, Variable var2) return true if var1 is before var2, lexicographically, according to the "default_variable_name" naming. More...
 
int default_is_inferior_varval (Pvecteur varval1, Pvecteur varval2)
 bool default_is_inferior_varval(Pvecteur varval1, Pvecteur varval2) return true if var1 is before var2, lexicographically, according to the "default_variable_name" naming. More...
 
int default_is_inferior_pvarval (Pvecteur *pvarval1, Pvecteur *pvarval2)
 bool default_is_inferior_pvarval(Pvecteur * pvarval1, Pvecteur * pvarval2) return true if var1 is before var2, lexicographically, according to the "default_variable_name" naming. More...
 
static void remove_blanks (char **ps)
 
static float parse_coeff (char **ps)
 
static char * parse_var_name (char **ps)
 
Ppolynome polynome_sscanf (char *sp, Variable(*name_to_variable)(Variable))
 Ppolynome polynome_sscanf(char *sp, (*name_to_variable)()) converts into polynomial structure the expression passed in ASCII form in string sp. More...
 

Macro Definition Documentation

◆ POLYNOME_BUFFER_SIZE

#define POLYNOME_BUFFER_SIZE   20480

Function Documentation

◆ default_is_inferior_pvarval()

int default_is_inferior_pvarval ( Pvecteur pvarval1,
Pvecteur pvarval2 
)

bool default_is_inferior_pvarval(Pvecteur * pvarval1, Pvecteur * pvarval2) return true if var1 is before var2, lexicographically, according to the "default_variable_name" naming.

Parameters
pvarval1varval1
pvarval2varval2

Definition at line 286 of file pnome-io.c.

287 {
288  return strcmp(default_variable_name(vecteur_var(* pvarval1)),
289  default_variable_name(vecteur_var(* pvarval2)));
290 }
char * default_variable_name(Variable var)
char *default_variable_name(Variable var) returns for variable var the name "Vxxxx" where xxxx are fo...
Definition: pnome-io.c:242
#define vecteur_var(v)

References default_variable_name(), and vecteur_var.

Referenced by block_to_complexity(), final_statement_to_complexity_evaluation(), and polynome_equal().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ default_is_inferior_var()

int default_is_inferior_var ( Variable  var1,
Variable  var2 
)

bool default_is_inferior_var(Variable var1, Variable var2) return true if var1 is before var2, lexicographically, according to the "default_variable_name" naming.

Parameters
var1ar1
var2ar2

Definition at line 265 of file pnome-io.c.

267 {
268  return strcmp(default_variable_name(var1),
269  default_variable_name(var2));
270 }

References default_variable_name().

+ Here is the call graph for this function:

◆ default_is_inferior_varval()

int default_is_inferior_varval ( Pvecteur  varval1,
Pvecteur  varval2 
)

bool default_is_inferior_varval(Pvecteur varval1, Pvecteur varval2) return true if var1 is before var2, lexicographically, according to the "default_variable_name" naming.

Parameters
varval1arval1
varval2arval2

Definition at line 276 of file pnome-io.c.

277 {
278  return strcmp(default_variable_name(vecteur_var(varval1)),
280 }

References default_variable_name(), and vecteur_var.

+ Here is the call graph for this function:

◆ default_variable_name()

char* default_variable_name ( Variable  var)

char *default_variable_name(Variable var) returns for variable var the name "Vxxxx" where xxxx are four letters computed from (int) var.

I guess that many variables can have the same name since the naming is done modulo 26^4 ? RK. To be fixed...

Parameters
varar

Definition at line 242 of file pnome-io.c.

244 {
245  char *s = (char *) malloc(6);
246  int i = (intptr_t) var;
247 
248  if (var != TCST) {
249  sprintf(s, "V%c%c%c%c",
250  (char) 93 + (i % 26),
251  (char) 93 + ((i / 26) % 26),
252  (char) 93 + ((i / 26 / 26) % 26),
253  (char) 93 + ((i / 26 / 26 / 26) % 26));
254  }
255  else
256  sprintf(s, "TCST");
257  return(s);
258 }
void * malloc(YYSIZE_T)
#define intptr_t
Definition: stdint.in.h:294
#define TCST
VARIABLE REPRESENTANT LE TERME CONSTANT.

References intptr_t, malloc(), and TCST.

Referenced by default_is_inferior_pvarval(), default_is_inferior_var(), and default_is_inferior_varval().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ float_to_frac()

void float_to_frac ( float  x,
char **  ps 
)

void float_to_frac(float x, char** ps) PRIVATE returns the simplest representation of floating-point number x tries to make an integer, then a small fraction, then a floating-point, then a floating-point with exponent.

if too little or too big print it with an exponent

default printing: as a float

if x is close enough up to a little fraction

rint it as a fraction

Definition at line 54 of file pnome-io.c.

57 {
58  int i;
59  float fprecision = (float) intpower(10.0, -PNOME_FLOAT_N_DECIMALES);
60 
61  if (((x <= fprecision) && (x >= -fprecision)) ||
63  /* if too little or too big print it with an exponent */
64  sprintf(*ps, "%.*E", PNOME_FLOAT_N_DECIMALES, x);
65  else {
66  /* default printing: as a float */
67  sprintf(*ps, "%.*f", PNOME_FLOAT_N_DECIMALES, x);
68  for (i=1; i<PNOME_FLOAT_TO_FRAC_LEVEL; i++)
69  if ((((x*i) - ((int) (x*i+0.5))) < (fprecision)) &&
70  (((x*i) - ((int) (x*i+0.5))) > (-fprecision))) {
71  /* if x is close enough up to a little fraction */
72  if ((((int) (x*i+0.5)) < PNOME_FLOAT_TO_FRAC_LEVEL) || (i==1)) {
73  /*print it as a fraction */
74  if (i==1)
75  sprintf(*ps, "%d", (int) (x*i+0.5));
76  else
77  sprintf(*ps, "%d/%d", (int) (x*i+0.5), i);
78  }
79  break;
80  }
81  }
82 }
double intpower(double d, int n)
double intpower(double d, int n) returns d^n for all integers n
#define PNOME_FLOAT_TO_EXP_LEVEL
#define PNOME_FLOAT_TO_FRAC_LEVEL
#define PNOME_FLOAT_N_DECIMALES
static char * x
Definition: split_file.c:159

References intpower(), PNOME_FLOAT_N_DECIMALES, PNOME_FLOAT_TO_EXP_LEVEL, PNOME_FLOAT_TO_FRAC_LEVEL, and x.

Referenced by monome_sprint().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ monome_fprint()

void monome_fprint ( FILE *  fd,
Pmonome  pm,
Pbase  pb,
bool  plus_sign,
char * (*)(Variable variable_name 
)

void monome_fprint(FILE* fd, Pmonome pm, Pbase pb, bool plus_sign, char* (*variable_name)()) PRIVATE Outputs to file fd an ASCII form of monomial pm, naming variables with the "variable-name" function, ordering them with the basis pb.

the "+" sign is printed if plus_sign == true.

Definition at line 90 of file pnome-io.c.

96 {
97  char *s = monome_sprint(pm, pb, plus_sign, variable_name);
98 
99  fprintf(fd, "%s", s);
100  free(s);
101 }
void free(void *)
char * monome_sprint(Pmonome pm, Pbase pb, bool plus_sign, char *(*variable_name)(Variable))
char monome_sprint(Pmonome pm, Pbase pb, bool plus_sign, char (*variable_name)()) PRIVATE Outputs a s...
Definition: pnome-io.c:109
int fprintf()
test sc_min : ce test s'appelle par : programme fichier1.data fichier2.data ...
char * variable_name(Variable v)
polynome_ri.c
Definition: polynome_ri.c:73

References fprintf(), free(), monome_sprint(), and variable_name().

+ Here is the call graph for this function:

◆ monome_sprint()

char* monome_sprint ( Pmonome  pm,
Pbase  pb,
bool  plus_sign,
char * (*)(Variable variable_name 
)

char monome_sprint(Pmonome pm, Pbase pb, bool plus_sign, char (*variable_name)()) PRIVATE Outputs a string representing monomial pm, naming variables with the "variable-name" function, ordering them with the basis pb.

the "+" sign is printed if plus_sign == true.

Definition at line 109 of file pnome-io.c.

114 {
115  float x;
116  char t[99];
117  char *r, *s;
118  char *u;
119 
120  u = (char *) malloc(99);
121  r = t;
122 
123  if (MONOME_UNDEFINED_P(pm))
124  sprintf(r, "%s", MONOME_UNDEFINED_SYMBOL);
125  else {
126  x = monome_coeff(pm);
127  if (x==0)
128  sprintf(r, "%s", MONOME_NUL_SYMBOL);
129  else {
130  if (x<0) {
131  *(r++) = '-';
132  if (plus_sign)
133  *(r++) = ' ';
134  x = - x;
135  }
136  else if (plus_sign) {
137  *(r++) = '+';
138  *(r++) = ' ';
139  }
140 
141  float_to_frac(x, &u);
142 
143  if (vect_coeff(TCST, monome_term(pm)) == 0) {
144  if (x != 1) {
145  sprintf(r, "%s%s", u, MONOME_COEFF_MULTIPLY_SYMBOL);
146  r = strchr(r, '\0');
147  }
150  strcpy(r, s);
151  free(s);
152  }
153  else
154  sprintf(r, "%s", u);
155  }
156  }
157  free(u);
158  return (char*) strdup((char *) t);
159 }
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
void float_to_frac(float x, char **ps)
void float_to_frac(float x, char** ps) PRIVATE returns the simplest representation of floating-point ...
Definition: pnome-io.c:54
#define MONOME_UNDEFINED_SYMBOL
#define MONOME_NUL_SYMBOL
#define monome_term(pm)
#define MONOME_UNDEFINED_P(pm)
#define monome_coeff(pm)
Macros definitions.
#define MONOME_VAR_MULTIPLY_SYMBOL
#define MONOME_COEFF_MULTIPLY_SYMBOL
char * strdup()
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

References float_to_frac(), free(), malloc(), monome_coeff, MONOME_COEFF_MULTIPLY_SYMBOL, MONOME_NUL_SYMBOL, monome_term, MONOME_UNDEFINED_P, MONOME_UNDEFINED_SYMBOL, MONOME_VAR_MULTIPLY_SYMBOL, strdup(), TCST, variable_name(), vect_coeff(), vect_sprint_as_monome(), and x.

Referenced by monome_fprint(), and polynome_sprint().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_coeff()

static float parse_coeff ( char **  ps)
static

handle fractionnal coefficients

Definition at line 307 of file pnome-io.c.

309 {
310  float coeff = 0;
311 
312  if (isdigit(**ps) || (**ps == '.') || (**ps == '+') || (**ps == '-')) {
313  sscanf(*ps, "%f", &coeff);
314  if ((coeff == 0) && (**ps == '+'))
315  coeff = 1;
316  if ((coeff == 0) && (**ps == '-'))
317  coeff = -1;
318  }
319  else
320  coeff = 1;
321 
322  if ((**ps == '-') || (**ps == '+'))
323  (*ps)++;
324  while (isdigit(**ps) || (**ps == '.') || (**ps == '*'))
325  (*ps)++;
326 
327  if (**ps == '/') { /* handle fractionnal coefficients */
328  float denom;
329  (*ps)++;
330  denom = parse_coeff(ps);
331  coeff /= denom;
332  }
333 
334  return(coeff);
335 }
static float parse_coeff(char **ps)
Definition: pnome-io.c:307

Referenced by polynome_sscanf().

+ Here is the caller graph for this function:

◆ parse_var_name()

static char* parse_var_name ( char **  ps)
static

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

THE ':' STANDS FOR MODULE_SEP_STRING OF Linear/C3 Library

TO BE ABLE TO HANDLE VARIABLES SUCH AS P:I

Definition at line 337 of file pnome-io.c.

339 {
340  char *name, *n;
341 
342  name = (char *) malloc(MAX_NAME_LENGTH);
343  n = name;
344 
345  if (isalpha(**ps)) /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
346  do { *n++ = *((*ps)++); }
347  while (isalnum(**ps) || (**ps == ':')); /* THE ':' STANDS FOR MODULE_SEP_STRING OF Linear/C3 Library */
348  /* TO BE ABLE TO HANDLE VARIABLES SUCH AS P:I */
349  *n = '\0';
350 
351  return(name);
352 }
#define MAX_NAME_LENGTH

References malloc(), and MAX_NAME_LENGTH.

Referenced by polynome_sscanf().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ polynome_fprint()

void polynome_fprint ( FILE *  fd,
Ppolynome  pp,
char * (*)(Variable variable_name,
int is_inferior_var 
)

void polynome_fprint(FILE* fd, Ppolynome pp, char* (*variable_name)(), bool (*is_inferior_var)()) Outputs to file fd an ASCII form of polynomial pp, using the user-provided function variable_name(Variable var) to associate the "Variable" pointers with the variable names.

is_inferior_var(Variable var1, Variable var2) is also given by the user: it must return true if var1 must be printed before var2 in monomials.

For the moment, monomials are not sorted. No "\n" is printed after the polynomial.

Definition at line 173 of file pnome-io.c.

178 {
180 
181  fprintf(fd, "%s", s);
182  free(s);
183 }
bool is_inferior_var(Variable, Variable)
Definition: polynome_ri.c:118
char * polynome_sprint(Ppolynome pp, char *(*variable_name)(Variable), int *is_inferior_var)
char polynome_sprint(Ppolynome pp, char (*variable_name)(), bool (*is_inferior_var)()) Outputs to fil...
Definition: pnome-io.c:197

References fprintf(), free(), is_inferior_var(), polynome_sprint(), and variable_name().

Referenced by fprint_pla_pp_dims(), is_not_trivial_p(), mapping_on_broadcast(), nullify_factors(), partition_unknowns(), plc_fprint_distance(), plc_fprint_proto(), plc_make_distance(), prgm_mapping(), prototype_dimension(), prp(), and valuer().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ polynome_sprint()

char* polynome_sprint ( Ppolynome  pp,
char * (*)(Variable variable_name,
int is_inferior_var 
)

char polynome_sprint(Ppolynome pp, char (*variable_name)(), bool (*is_inferior_var)()) Outputs to file fd an ASCII form of polynomial pp, using the user-provided function variable_name(Variable var) to associate the "Variable" pointers with the variable names.

is_inferior_var(Variable var1, Variable var2) is also given by the user: it must return true if var1 must be printed before var2 in monomials.

For the moment, monomials are not sorted. No "\n" is printed after the polynomial.

The following line is added by L.Zhou Mar. 26, 91

Definition at line 197 of file pnome-io.c.

201 {
202 #define POLYNOME_BUFFER_SIZE 20480
203  static char t[POLYNOME_BUFFER_SIZE];
204  char *r, *s;
205 
206  r = t;
207 
208  if (POLYNOME_UNDEFINED_P(pp))
209  sprintf(r, "%s", POLYNOME_UNDEFINED_SYMBOL);
210  else if (POLYNOME_NUL_P(pp))
211  sprintf(r, "%s", POLYNOME_NUL_SYMBOL);
212  else {
214  bool print_plus_sign = false;
215 
216  /* The following line is added by L.Zhou Mar. 26, 91 */
217  pp = polynome_sort(&pp, is_inferior_var);
218 
219  while (!POLYNOME_NUL_P(pp)) {
220  s = monome_sprint(polynome_monome(pp), pb,
221  print_plus_sign, variable_name);
222  strcpy(r, s);
223  r = strchr(r, '\0');
224  pp = polynome_succ(pp);
225  print_plus_sign = true;
226  if (!POLYNOME_NUL_P(pp)) *(r++) = ' ';
227  free(s);
228  }
229  }
230  assert( strlen(t) < POLYNOME_BUFFER_SIZE);
231  return (char*) strdup((char *) t);
232 }
#define assert(ex)
Definition: newgen_assert.h:41
#define POLYNOME_BUFFER_SIZE
Pbase polynome_used_var(Ppolynome pp, int *is_inferior_var)
Pbase polynome_used_var(Ppolynome pp, bool *is_inferior_var()) PRIVATE Returns, in a Pbase,...
Definition: pnome-reduc.c:204
Ppolynome polynome_sort(Ppolynome *ppp, int *is_inferior_var)
Ppolynome polynome_sort((Ppolynome *) ppp, bool (*is_inferior_var)()) Sorts the polynomial *ppp: mono...
#define POLYNOME_UNDEFINED_SYMBOL
#define POLYNOME_UNDEFINED_P(pp)
#define polynome_monome(pp)
#define POLYNOME_NUL_P(pp)
#define POLYNOME_NUL_SYMBOL
#define polynome_succ(pp)
le type des coefficients dans les vecteurs: Value est defini dans le package arithmetique
Definition: vecteur-local.h:89
struct Svecteur * Pbase

References assert, free(), is_inferior_var(), monome_sprint(), POLYNOME_BUFFER_SIZE, polynome_monome, POLYNOME_NUL_P, POLYNOME_NUL_SYMBOL, polynome_sort(), polynome_succ, POLYNOME_UNDEFINED_P, POLYNOME_UNDEFINED_SYMBOL, polynome_used_var(), strdup(), and variable_name().

Referenced by complexity_sprint(), and polynome_fprint().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ polynome_sscanf()

Ppolynome polynome_sscanf ( char *  sp,
Variable (*)(Variable name_to_variable 
)

Ppolynome polynome_sscanf(char *sp, (*name_to_variable)()) converts into polynomial structure the expression passed in ASCII form in string sp.

(for debug only) pas vraiment quick mais bien dirty

printf(stderr, "\ns='%s'\n", s);

printf(stderr, "au milieu: s='%s'\n", s);

Parameters
spp

Definition at line 359 of file pnome-io.c.

362 {
363  Ppolynome pp = POLYNOME_NUL;
364  Pmonome curpm;
365  bool constructing_monome = false;
366  float coeff = 0.;
367  char *varname;
368  Value power;
369  char *s;
370 
371  s = (char*) strdup(sp);
372  remove_blanks(&s);
373 
374  while (*s != '\0')
375  {
376  /*fprintf(stderr, "\ns='%s'\n", s);*/
377  power = VALUE_ONE;
378  if (!constructing_monome) { coeff = parse_coeff(&s);
379  }
380  varname = parse_var_name(&s);
381  if (strlen(varname)!=0) {
382  if (*s == '^') {
383  s++;
384  power = float_to_value(parse_coeff(&s));
385  }
386  else
387  while ((*s == '.') || (*s == '*')) s++;
388  }
389  else varname = (char*) strdup("TCST");
390 
391  if (constructing_monome) {
392  vect_add_elem(&(monome_term(curpm)),
393  name_to_variable(varname),
394  power);
395  }
396  else {
397  curpm = make_monome(coeff, name_to_variable(varname), power);
398  constructing_monome = true;
399  }
400  /*fprintf(stderr, "au milieu: s='%s'\n", s);*/
401 
402  if ((*s == '+') || (*s == '-'))
403  {
404  polynome_monome_add(&pp, curpm);
405  monome_rm(&curpm);
406  constructing_monome = false;
407  }
408  }
409  if (!MONOME_NUL_P(curpm)) {
410  polynome_monome_add(&pp, curpm);
411  monome_rm(&curpm);
412  }
413 
414  return (pp);
415 }
#define float_to_value(f)
int Value
#define VALUE_ONE
Variable name_to_variable(char *)
Definition: polynome_ri.c:172
Pmonome make_monome(float coeff, Variable var, Value expo)
Pmonome make_monome(float coeff, Variable var, Value expo) PRIVATE allocates space for,...
Definition: pnome-alloc.c:81
void monome_rm(Pmonome *ppm)
void monome_rm(Pmonome* ppm) PRIVATE frees space occupied by monomial *ppm returns *ppm pointing to M...
Definition: pnome-alloc.c:154
void polynome_monome_add(Ppolynome *ppp, Pmonome pm)
void polynome_monome_add(Ppolynome* ppp, Pmonome pm) PRIVATE Add monomial pm to polynomial *ppp,...
Definition: pnome-bin.c:50
static void remove_blanks(char **ps)
Definition: pnome-io.c:292
static char * parse_var_name(char **ps)
Definition: pnome-io.c:337
#define POLYNOME_NUL
#define MONOME_NUL_P(pm)
void vect_add_elem(Pvecteur *pvect, Variable var, Value val)
void vect_add_elem(Pvecteur * pvect, Variable var, Value val): addition d'un vecteur colineaire au ve...
Definition: unaires.c:72

References float_to_value, make_monome(), MONOME_NUL_P, monome_rm(), monome_term, name_to_variable(), parse_coeff(), parse_var_name(), polynome_monome_add(), POLYNOME_NUL, remove_blanks(), strdup(), VALUE_ONE, and vect_add_elem().

+ Here is the call graph for this function:

◆ remove_blanks()

static void remove_blanks ( char **  ps)
static

Definition at line 292 of file pnome-io.c.

294 {
295  char *s = *ps,
296  *t = *ps;
297 
298  do {
299  while (isspace(**ps))
300  (*ps)++;
301  *t++ = *(*ps)++;
302  } while (**ps != '\0');
303  *t++ = '\0';
304  *ps = s;
305 }

Referenced by polynome_sscanf().

+ Here is the caller graph for this function: