csmapping.c
Go to the documentation of this file.
1 #include "csmapping.h"
2 #include "defines.h"
3 
4 #include <math.h>
5 
15 {
16  unsigned int i,vID;
17 
18  NEW(m->original,1,Tvariables);
19  CopyVariables(m->original,o);
20  NEW(m->simple,1,Tvariables);
21  CopyVariables(m->simple,s);
22 
23  m->sizeO=NVariables(o); /* The number of variables is cached */
24  m->sizeS=NVariables(s);
25 
26  /*
27  Some of the variables in the simple system are included in the original one.
28  This is a one to one relation and, thus, it can be straighforwardly defined.
29  However this is of little use (only used in GetVarIDInOriginal)
30  */
31  NEW(m->orig,m->sizeS,unsigned int);
32 
33  for(i=0;i<m->sizeS;i++)
34  {
35  /* this can be NO_UINT if the variable in the simplified system is not
36  in the original one (ex. dummies!!)*/
38  }
39 
40  /*
41  The variables in the original system can be not linked to any variable in the
42  simple system or be linearly related with a variable in the simple system.
43  Observe that identity (copy) is a form of linear relation (with ct 1 and offest 0).
44  Only this last relation (identity) can be automatically detected and set.
45  Other type of relations have to be set by upper levels (using 'SetOriginalVarRelation').
46  */
47  NEW(m->related,m->sizeO,boolean);
49 
50  for(i=0;i<m->sizeO;i++)
51  {
53 
54  m->related[i]=(vID!=NO_UINT);
55  InitLinearConstraint(&(m->lc[i]));
56  if (m->related[i])
57  {
58  /* Var 'i' in the original system is var vID in the simplified one */
59  AddTerm2LinearConstraint(vID,1,&(m->lc[i]));
60  }
61  }
62 }
63 
64 void CopyMapping(Tmapping *m_dst,Tmapping *m_src)
65 {
66  unsigned int i;
67 
68  if (m_src->original==NULL)
69  m_dst->original=NULL;
70  else
71  {
72  NEW(m_dst->original,1,Tvariables);
73  CopyVariables(m_dst->original,m_src->original);
74  }
75 
76  if (m_src->simple==NULL)
77  m_dst->simple=NULL;
78  else
79  {
80  NEW(m_dst->simple,1,Tvariables);
81  CopyVariables(m_dst->simple,m_src->simple);
82  }
83 
84  m_dst->sizeO=m_src->sizeO;
85  m_dst->sizeS=m_src->sizeS;
86 
87  NEW(m_dst->orig,m_dst->sizeS,unsigned int);
88  for(i=0;i<m_src->sizeS;i++)
89  m_dst->orig[i]=m_src->orig[i];
90 
91  NEW(m_dst->related,m_dst->sizeO,boolean);
92  NEW(m_dst->lc,m_dst->sizeO,TLinearConstraint);
93  for(i=0;i<m_src->sizeO;i++)
94  {
95  m_dst->related[i]=m_src->related[i];
96  CopyLinearConstraint(&(m_dst->lc[i]),&(m_src->lc[i]));
97  }
98 }
99 
100 void SetOriginalVarRelation(unsigned int nvo,TLinearConstraint *lc,Tmapping *m)
101 {
102  if (nvo<m->sizeO)
103  {
104  m->related[nvo]=TRUE;
105  DeleteLinearConstraint(&(m->lc[nvo]));
106  CopyLinearConstraint(&(m->lc[nvo]),lc);
107  }
108  else
109  Error("Var index out of range in SetOriginalVarRelation");
110 }
111 
112 void GetOriginalVarRelation(unsigned int nvo,TLinearConstraint *lc,Tmapping *m)
113 {
114  if (m->related[nvo])
115  CopyLinearConstraint(lc,&(m->lc[nvo]));
116  else
118 }
119 
120 unsigned int GetSizeOriginal(Tmapping *m)
121 {
122  return(m->sizeO);
123 }
124 
125 unsigned int GetSizeSimple(Tmapping *m)
126 {
127  return(m->sizeS);
128 }
129 
130 boolean IsInSimple(unsigned int v,Tmapping *m)
131 {
133 }
134 
135 unsigned int GetVarIDInOriginal(unsigned int v,Tmapping *m)
136 {
137  if (v<m->sizeS)
138  return(m->orig[v]);
139  else
140  return(NO_UINT);
141 }
142 
144 {
145  /* The same variable in the system */
146  /* All ranges in the simple box change (recall that the
147  simple system is a strict subset of the original system) */
148 
149  /*When applying this mapping the information in 'o' is considered the
150  most up to date and, thus, the one in 's' is discarted*/
151 
152  /*When accumulating (intersecting) the several constraints that define the
153  range for the variables in the simple system we can discover that there
154  is no solution (empty intersection)*/
155  unsigned int i;
156 
157  BoxFromVariables(s,m->simple);
158  for(i=0;i<m->sizeS;i++)
159  {
160  if (m->orig[i]!=NO_UINT)
162  }
163 }
164 
165 void SimplePointFromOriginal(double *o,double **s,Tmapping *m)
166 {
167  unsigned int i;
168 
170  for(i=0;i<m->sizeS;i++)
171  {
172  if (m->orig[i]!=NO_UINT)
173  (*s)[i]=o[m->orig[i]];
174  }
175 }
176 
178 {
179  unsigned int i;
180  Tinterval r,rc,e,*is;
181 
182  is=GetBoxIntervals(s);
183  for(i=0;i<m->sizeO;i++)
184  {
185  if (m->related[i])
186  {
187  EvaluateLinearConstraintInt(is,&r,&(m->lc[i]));
188  GetLinearConstraintError(&e,&(m->lc[i]));
189  IntervalInvert(&e,&e);
190  IntervalAdd(&e,&r,&r);
191  if (Intersection(GetBoxInterval(i,o),&r,&rc))
192  SetBoxInterval(i,&rc,o);
193  else
194  SetBoxInterval(i,&r,o);
195  }
196  }
197 }
198 
199 void UpdateOriginalPointFromSimple(double *s,double *o,Tmapping *m)
200 {
201  unsigned int i;
202  Tinterval e;
203 
204  for(i=0;i<m->sizeO;i++)
205  {
206  if (m->related[i])
207  {
208  GetLinearConstraintError(&e,&(m->lc[i]));
209  o[i]=EvaluateLinearConstraint(s,&(m->lc[i]))-IntervalCenter(&e);
210  }
211  }
212 }
213 
214 /* Since all variables in simple are also in original, the only information
215  relevant to be printed in how the variables in the original system
216  depend on those in the simple one
217 */
218 void PrintMapping(FILE *f,Tmapping *m)
219 {
220  unsigned int i;
221  char *n;
222  char **varNamesO,**varNamesS;
223 
224  if (m->original==NULL)
225  varNamesO=NULL;
226  else
227  {
228  NEW(varNamesO,m->sizeO,char *);
229  GetVariableNames(varNamesO,m->original);
230  }
231 
232  if (m->simple==NULL)
233  varNamesS=NULL;
234  else
235  {
236  NEW(varNamesS,m->sizeS,char *);
237  GetVariableNames(varNamesS,m->simple);
238  }
239 
240  for(i=0;i<m->sizeO;i++)
241  {
242  fprintf(f,"%% ");
243  if (varNamesO==NULL)
244  fprintf(f,"v_%u",i);
245  else
246  {
247  n=varNamesO[i];
248  PRINT_VARIABLE_NAME(f,n);
249  }
250  fprintf(f,"=");
251  if (m->related[i])
252  PrintLinearConstraint(f,FALSE,varNamesS,&(m->lc[i]));
253  else
254  {
255  //PRINT_VARIABLE_NAME(f,n);
256  fprintf(f,"No in simplification\n");
257  }
258  }
259  if (varNamesO!=NULL)
260  free(varNamesO);
261 
262  if (varNamesS!=NULL)
263  free(varNamesS);
264 }
265 
266 void LoadMapping(FILE *f,Tmapping *m)
267 {
268  unsigned int i;
269 
270  m->original=NULL;
271  m->simple=NULL;
272 
273  fscanf(f,"%u",&(m->sizeS));
274  NEW(m->orig,m->sizeS,unsigned int);
275  for(i=0;i<m->sizeS;i++)
276  fscanf(f,"%u ",&(m->orig[i]));
277 
278  fscanf(f,"%u",&(m->sizeO));
279  NEW(m->related,m->sizeO,boolean);
280  NEW(m->lc,m->sizeO,TLinearConstraint);
281  for(i=0;i<m->sizeO;i++)
282  {
283  fprintf(f,"%u",m->related[i]);
284  if (m->related[i])
285  LoadLinearConstraint(f,&(m->lc[i]));
286  else
287  InitLinearConstraint(&(m->lc[i]));
288  }
289 }
290 
291 void SaveMapping(FILE *f,Tmapping *m)
292 {
293  unsigned int i;
294 
295  fprintf(f,"%u\n",m->sizeS);
296  for(i=0;i<m->sizeS;i++)
297  fprintf(f,"%u ",m->orig[i]);
298  fprintf(f,"\n");
299 
300  fprintf(f,"%u\n",m->sizeO);
301  for(i=0;i<m->sizeO;i++)
302  {
303  fprintf(f,"%u ",m->related[i]);
304  if (m->related[i])
305  SaveLinearConstraint(f,&(m->lc[i]));
306  fprintf(f,"\n");
307  }
308 }
309 
311 {
312  unsigned int i;
313 
314  if (m->original!=NULL)
315  {
317  free(m->original);
318  }
319 
320  if (m->simple!=NULL)
321  {
323  free(m->simple);
324  }
325 
326  free(m->orig);
327  for(i=0;i<m->sizeO;i++)
328  DeleteLinearConstraint(&(m->lc[i]));
329  free(m->lc);
330  free(m->related);
331 }
332 
boolean Intersection(Tinterval *i1, Tinterval *i2, Tinterval *i_out)
Computes the intersection of two intervals.
Definition: interval.c:285
Set of variables of a cuiksystem.
Definition: variables.h:38
void DeleteLinearConstraint(TLinearConstraint *lc)
Destructor.
Tinterval * GetBoxInterval(unsigned int n, Tbox *b)
Returns a pointer to one of the intervals defining the box.
Definition: box.c:270
#define FALSE
FALSE.
Definition: boolean.h:30
void SaveMapping(FILE *f, Tmapping *m)
Saves the mapping information into a file.
Definition: csmapping.c:291
unsigned int NVariables(Tvariables *vs)
Gets the number of variables in a set.
Definition: variables.c:69
void GetVariableNames(char **varNames, Tvariables *vs)
Gets the name for all the variables in the set.
Definition: variables.c:234
#define NEW(_var, _n, _type)
Allocates memory space.
Definition: defines.h:385
A linear constraint with an associated error.
boolean * related
Definition: csmapping.h:63
unsigned int GetVarIDInOriginal(unsigned int v, Tmapping *m)
Gets the original identifier of a simplified variable.
Definition: csmapping.c:135
void SetBoxInterval(unsigned int n, Tinterval *is, Tbox *b)
Replaces a particular interval in a box.
Definition: box.c:259
void IntervalAdd(Tinterval *i1, Tinterval *i2, Tinterval *i_out)
Addition of two intervals.
Definition: interval.c:418
void SetOriginalVarRelation(unsigned int nvo, TLinearConstraint *lc, Tmapping *m)
Set a relation for a variable in the original set.
Definition: csmapping.c:100
void InitLinearConstraint(TLinearConstraint *lc)
Constructor.
void LoadLinearConstraint(FILE *f, TLinearConstraint *lc)
Constructor. Loads the linear constraint from a file.
unsigned int GetSizeSimple(Tmapping *m)
Size of the variable set in the simplified system.
Definition: csmapping.c:125
void IntervalInvert(Tinterval *i, Tinterval *i_out)
Change of sign of a given interval.
Definition: interval.c:456
void PrintLinearConstraint(FILE *f, boolean eq, char **varName, TLinearConstraint *lc)
Prints a linear constraint.
#define TRUE
TRUE.
Definition: boolean.h:21
Mapping between the sets of variables in two different cuiksystems.
Definition: csmapping.h:53
void PointFromVariables(double **p, Tvariables *vs)
Creates a point from the center of the ranges of a set of variables.
Definition: variables.c:294
void Error(const char *s)
General error function.
Definition: error.c:80
void AddTerm2LinearConstraint(unsigned int ind, double val, TLinearConstraint *lc)
Adds a scaled variable to the linear constraint.
void CopyInterval(Tinterval *i_dst, Tinterval *i_org)
Copy constructor.
Definition: interval.c:59
void DeleteMapping(Tmapping *m)
Destructor.
Definition: csmapping.c:310
void UpdateOriginalPointFromSimple(double *s, double *o, Tmapping *m)
Gets a original point from an simplified one.
Definition: csmapping.c:199
void CopyVariables(Tvariables *vs_dst, Tvariables *vs_src)
Copy onstructor.
Definition: variables.c:40
void EvaluateLinearConstraintInt(Tinterval *varValues, Tinterval *i_out, TLinearConstraint *lc)
Interval evaluation of a linear constraint.
void InitMapping(Tvariables *o, Tvariables *s, Tmapping *m)
Constructor.
Definition: csmapping.c:14
void CopyLinearConstraint(TLinearConstraint *lc_dst, TLinearConstraint *lc_src)
Copy constructor.
unsigned int GetVariableID(char *name, Tvariables *vs)
Gets the variable identifier (i.e., its position in the set) given a variable name.
Definition: variables.c:201
unsigned int sizeS
Definition: csmapping.h:58
#define PRINT_VARIABLE_NAME(f, name)
Prints a variable name into a file.
Definition: defines.h:427
Definitions of constants and macros used in several parts of the cuik library.
void UpdateOriginalFromSimple(Tbox *s, Tbox *o, Tmapping *m)
Gets a original box from an simplified one.
Definition: csmapping.c:177
unsigned int sizeO
Definition: csmapping.h:57
unsigned int GetSizeOriginal(Tmapping *m)
Size of the variable set in the original system.
Definition: csmapping.c:120
void PrintMapping(FILE *f, Tmapping *m)
Prints a mapping.
Definition: csmapping.c:218
A box.
Definition: box.h:83
void GetLinearConstraintError(Tinterval *error, TLinearConstraint *lc)
Gets the right-hand side interval for the linear constraint.
#define NO_UINT
Used to denote an identifier that has not been initialized.
Definition: defines.h:435
double IntervalCenter(Tinterval *i)
Gets the interval center.
Definition: interval.c:129
Tvariable * GetVariable(unsigned int n, Tvariables *vs)
Gets the information of a variable given its number in the set (i.e., its identifier).
Definition: variables.c:189
void SimplePointFromOriginal(double *o, double **s, Tmapping *m)
Gets a simple point from an original one.
Definition: csmapping.c:165
void SimpleFromOriginal(Tbox *o, Tbox *s, Tmapping *m)
Gets a simple box from an original one.
Definition: csmapping.c:143
void DeleteVariables(Tvariables *vs)
Destructor.
Definition: variables.c:354
Tinterval * GetBoxIntervals(Tbox *b)
Returns a pointer to the array of intervals defining the box.
Definition: box.c:284
char * GetVariableName(Tvariable *v)
Gets the variable name.
Definition: variable.c:65
void LoadMapping(FILE *f, Tmapping *m)
Constructor. Loads the mapping information from a file.
Definition: csmapping.c:266
void CopyMapping(Tmapping *m_dst, Tmapping *m_src)
Copy constructor.
Definition: csmapping.c:64
Tvariables * original
Definition: csmapping.h:54
void GetOriginalVarRelation(unsigned int nvo, TLinearConstraint *lc, Tmapping *m)
Gets the relation for a variable in the original set with the variables in the simple set...
Definition: csmapping.c:112
Definition of the Tmapping type and the associated functions.
TLinearConstraint * lc
Definition: csmapping.h:65
double EvaluateLinearConstraint(double *varValues, TLinearConstraint *lc)
Evaluates a linear combination for a given point.
Defines a interval.
Definition: interval.h:33
boolean IsInSimple(unsigned int v, Tmapping *m)
Identify original variables also present in the simple system.
Definition: csmapping.c:130
Tvariables * simple
Definition: csmapping.h:55
unsigned int * orig
Definition: csmapping.h:60
void BoxFromVariables(Tbox *b, Tvariables *vs)
Creates a box from the ranges of a set of variables.
Definition: variables.c:283
void SaveLinearConstraint(FILE *f, TLinearConstraint *lc)
Saves the linear constraint into a file.