monomial.c
Go to the documentation of this file.
1 #include "monomial.h"
2 
3 #include "error.h"
4 #include "defines.h"
5 
6 #include <math.h>
7 
18 {
19  f->empty=TRUE;
20  InitVarSet(&(f->vars));
21  f->ct=0.0;
22 }
23 
25 {
26  f->empty=TRUE;
27  ResetVarSet(&(f->vars));
28  f->ct=0.0;
29 }
30 
31 void FixVariableInMonomial(unsigned int nv,double v,Tmonomial *f)
32 {
33  f->ct*=FixVariableInVarSet(nv,v,&(f->vars));
34 
35  if (fabs(f->ct)<=ZERO)
36  ResetMonomial(f);
37 
38  /*the indexes for variables with ID above 'nv' should be decreased by one*/
39  ShiftVarIndexes(nv,&(f->vars)); /*has no effects for empty var sets*/
40 }
41 
42 void ReplaceVariableInMonomial(unsigned int nv,double ct,unsigned int nvNew,Tmonomial *f)
43 {
44  f->ct*=ReplaceVariableInVarSet(nv,ct,nvNew,&(f->vars));
45 
46  if (fabs(f->ct)<=ZERO)
47  ResetMonomial(f);
48 
49  /*the indexes for variables with ID above 'nv' should be decreased by one*/
50  ShiftVarIndexes(nv,&(f->vars)); /*has no effects for empty var sets*/
51 }
52 
54 {
55  return(f->empty);
56 }
57 
58 boolean CtMonomial(Tmonomial *f)
59 {
60  return((f->empty)||
61  (VariableSetSize(&(f->vars))==0));
62 }
63 
65 {
66  return((VariableSetSize(&(f->vars))==1)&&
67  (GetVariablePowerN(0,&(f->vars))==2)&&
68  (PolynomialVarSet(&(f->vars))));
69 }
70 
72 {
73  return((VariableSetSize(&(f->vars))==2)&&
74  (GetVariablePowerN(0,&(f->vars))==1)&&
75  (GetVariablePowerN(1,&(f->vars))==1)&&
76  (PolynomialVarSet(&(f->vars))));
77 }
78 
80 {
81  return((VariableSetSize(&(f->vars))==1)&&
82  (GetVariablePowerN(0,&(f->vars))==1)&&
83  (PolynomialVarSet(&(f->vars))));
84 }
85 
87 {
88  return(PolynomialVarSet(&(f->vars)));
89 }
90 
91 unsigned int MonomialOrder(Tmonomial *f)
92 {
93  return(VarSetOrder(&(f->vars)));
94 }
95 
96 void CopyMonomial(Tmonomial *f_dst,Tmonomial *f_orig)
97 {
98  f_dst->empty=f_orig->empty;
99  f_dst->ct=f_orig->ct;
100  CopyVarSet(&(f_dst->vars),&(f_orig->vars));
101 }
102 
103 unsigned int CmpMonomial(Tmonomial *f1,Tmonomial *f2)
104 {
105  unsigned int r;
106 
107  if ((f1->empty)&&(f2->empty))
108  r=0;
109  else
110  {
111  if (f1->empty)
112  r=2;
113  else
114  {
115  if (f2->empty)
116  r=1;
117  else
118  {
119  r=CmpVarSet(&(f1->vars),&(f2->vars));
120  if (r==0)
121  {
122  if (f1->ct>f2->ct)
123  r=1;
124  else
125  {
126  if (f1->ct<f2->ct)
127  r=2;
128  }
129  }
130  }
131  }
132  }
133  return(r);
134 }
135 
137 {
138  if (f->empty)
139  return(0);
140  else
141  return(f->ct);
142 }
143 
144 void SetMonomialCt(double k,Tmonomial *f)
145 {
146  f->empty=FALSE;
147  f->ct=k;
148 
149  if (fabs(f->ct)<=ZERO)
150  ResetMonomial(f);
151 }
152 
154 {
155  return(&(f->vars));
156 }
157 
158 void AddCt2Monomial(double k,Tmonomial *f)
159 {
160  if (f->empty)
161  f->ct=k;
162  else
163  f->ct*=k;
164 
165  f->empty=FALSE;
166 
167  if (fabs(f->ct)<=ZERO)
168  f->ct=0.0;
169 }
170 
171 void AddVariable2Monomial(unsigned int fn,unsigned int varid,unsigned int p,Tmonomial *f)
172 {
173  if (f->empty)
174  {
175  f->empty=FALSE;
176  f->ct=1;
177  AddVariable2Set(fn,varid,p,&(f->vars));
178  }
179  else
180  AddVariable2Set(fn,varid,p,&(f->vars));
181 }
182 
184 {
185  InitMonomial(f);
186  f->empty=((f1->empty)&&(f2->empty));
187  if (!f->empty)
188  {
189  f->ct=f1->ct*f2->ct;
190  if (fabs(f->ct)>ZERO)
191  ProductVarSet(&(f1->vars),&(f2->vars),&(f->vars));
192  else
193  f->ct=0.0;
194  }
195 }
196 
197 inline double EvaluateMonomial(double *varValues,Tmonomial *f)
198 {
199  if (f->empty)
200  return(0.0);
201  else
202  {
203  if (VariableSetSize(&(f->vars))>0)
204  return(f->ct*EvaluateVarSet(varValues,&(f->vars)));
205  else
206  return(f->ct);
207  }
208 }
209 
211 {
212  if (f->empty)
213  NewInterval(0,0,i_out);
214  else
215  {
216  if (VariableSetSize(&(f->vars))>0)
217  {
218  Tinterval ct;
219 
220  EvaluateVarSetInt(varValues,i_out,&(f->vars));
221 
222  /* The coefficients of the equations can be affected by some noise
223  due to floating point roundings when operating them. We add a small
224  range (1e-11) to compensate for those possible errors. */
225  NewInterval(f->ct-ZERO,f->ct+ZERO,&ct);
226  IntervalProduct(&ct,i_out,i_out);
227  }
228  else
229  {
230  /* The coefficients of the equations can be affected by some noise
231  due to floating point roundings when operating them. We add a small
232  range (1e-11) to compensate for those possible errors. */
233  NewInterval(f->ct-ZERO,f->ct+ZERO,i_out);
234  }
235  }
236 }
237 
238 void DeriveMonomial(unsigned int nv,Tmonomial *df,Tmonomial *f)
239 {
240  if ((f->empty)||(!VarIncluded(nv,&(f->vars))))
241  InitMonomial(df);
242  else
243  {
244  double s;
245 
246  /* The output of 'DeriveVarSet' is >0 since we now for sure the
247  var set includes variable 'nv;*/
248  s=DeriveVarSet(nv,&(df->vars),&(f->vars));
249  df->ct=f->ct*s;
250  df->empty=(s==0.0);
251  }
252 }
253 
254 
255 void PrintMonomial(FILE *file,boolean first,char **varNames,Tmonomial *f)
256 {
257  if (!f->empty)
258  {
259  if (first)
260  {
261  if (fabs(f->ct-1)>ZERO)
262  {
263  fprintf(file,"%.16g",f->ct);
264  if (!EmptyVarSet(&(f->vars)))
265  fprintf(file,"*");
266  }
267  }
268  else
269  {
270  if (fabs(f->ct-1)<ZERO)
271  fprintf(file,"+");
272  else
273  {
274  if (fabs(f->ct+1)<ZERO)
275  fprintf(file,"-");
276  else
277  {
278  fprintf(file,"%+.16g",f->ct);
279  if (!EmptyVarSet(&(f->vars)))
280  fprintf(file,"*");
281  }
282  }
283  }
284 
285  PrintVarSet(file,varNames,&(f->vars));
286  }
287 }
288 
290 {
291  DeleteVarSet(&(f->vars));
292 }
void EvaluateMonomialInt(Tinterval *varValues, Tinterval *i_out, Tmonomial *f)
Evaluates a monomial for a given set of ranges for the variables.
Definition: monomial.c:210
void PrintMonomial(FILE *file, boolean first, char **varNames, Tmonomial *f)
Prints a monomial.
Definition: monomial.c:255
void DeleteVarSet(Tvariable_set *vs)
Destructor.
Definition: variable_set.c:852
void AddCt2Monomial(double k, Tmonomial *f)
Scales a monomial.
Definition: monomial.c:158
#define FALSE
FALSE.
Definition: boolean.h:30
double ct
Definition: monomial.h:34
double EvaluateMonomial(double *varValues, Tmonomial *f)
Evaluates a monomial for a given set of value for the variables.
Definition: monomial.c:197
void EvaluateVarSetInt(Tinterval *varValues, Tinterval *i_out, Tvariable_set *vs)
Evaluates a variable set for a given set of ranges for the variables.
Definition: variable_set.c:564
double FixVariableInVarSet(unsigned int varid, double ct, Tvariable_set *vs)
Replaces a variable by a constant.
Definition: variable_set.c:323
double GetMonomialCt(Tmonomial *f)
Gets the scale factor of a monomial.
Definition: monomial.c:136
void CopyVarSet(Tvariable_set *vs_dst, Tvariable_set *vs_orig)
Copy constructor.
Definition: variable_set.c:84
double DeriveVarSet(unsigned int nv, Tvariable_set *dvs, Tvariable_set *vs)
Derives an variable set.
Definition: variable_set.c:657
boolean empty
Definition: monomial.h:33
boolean PolynomialMonomial(Tmonomial *f)
Identifies monimials not involving any kind of (trigonomitric function).
Definition: monomial.c:86
unsigned int CmpMonomial(Tmonomial *f1, Tmonomial *f2)
Monomial comparison.
Definition: monomial.c:103
#define TRUE
TRUE.
Definition: boolean.h:21
void PrintVarSet(FILE *f, char **varNames, Tvariable_set *vs)
Prints a variable set.
Definition: variable_set.c:774
void AddVariable2Monomial(unsigned int fn, unsigned int varid, unsigned int p, Tmonomial *f)
Adds a power variable to the monomial.
Definition: monomial.c:171
void ResetVarSet(Tvariable_set *vs)
Resets the information stored in a variable set.
Definition: variable_set.c:79
void InitMonomial(Tmonomial *f)
Constructor.
Definition: monomial.c:17
#define ZERO
Floating point operations giving a value below this constant (in absolute value) are considered 0...
Definition: defines.h:37
unsigned int CmpVarSet(Tvariable_set *vs1, Tvariable_set *vs2)
Variable set comparison.
Definition: variable_set.c:112
void SetMonomialCt(double k, Tmonomial *f)
Changes the scale factor of a monomial.
Definition: monomial.c:144
void AddVariable2Set(unsigned int f, unsigned int varid, unsigned int p, Tvariable_set *vs)
Adds an element to a variable set.
Definition: variable_set.c:265
boolean VarIncluded(unsigned int id, Tvariable_set *vs)
Checks if a variable index is included in a set of variable indexes.
Definition: variable_set.c:183
boolean LinearMonomial(Tmonomial *f)
Checks if a monomial is lienal: K*x, with K a constant.
Definition: monomial.c:79
void DeleteMonomial(Tmonomial *f)
Destructor.
Definition: monomial.c:289
Error and warning functions.
boolean CtMonomial(Tmonomial *f)
Checks if a monomial is constant.
Definition: monomial.c:58
boolean PolynomialVarSet(Tvariable_set *vs)
Identifies polynomial variable sets.
Definition: variable_set.c:153
unsigned int GetVariablePowerN(unsigned int n, Tvariable_set *vs)
Gets a variable power from a variable set.
Definition: variable_set.c:463
Definitions of constants and macros used in several parts of the cuik library.
void DeriveMonomial(unsigned int nv, Tmonomial *df, Tmonomial *f)
Derives an monomial.
Definition: monomial.c:238
void ShiftVarIndexes(unsigned int nv, Tvariable_set *vs)
Reduces the variable indexes above a given index.
Definition: variable_set.c:99
A set of variable indexes with powers.
Definition: variable_set.h:131
A scaled product of powers of variables.
Definition: monomial.h:32
void ReplaceVariableInMonomial(unsigned int nv, double ct, unsigned int nvNew, Tmonomial *f)
Replaces a variable.
Definition: monomial.c:42
boolean BilinearMonomial(Tmonomial *f)
Checks if a monomial is bilienal: K*x*y, with K a constant.
Definition: monomial.c:71
unsigned int VariableSetSize(Tvariable_set *vs)
Gets the number of elements of a variable set.
Definition: variable_set.c:442
void ResetMonomial(Tmonomial *f)
Reset the monomial information.
Definition: monomial.c:24
boolean EmptyMonomial(Tmonomial *f)
Checks if a monomial is empty.
Definition: monomial.c:53
boolean EmptyVarSet(Tvariable_set *vs)
Checks if a variable set is empty.
Definition: variable_set.c:148
void CopyMonomial(Tmonomial *f_dst, Tmonomial *f_orig)
Copy constructor.
Definition: monomial.c:96
boolean QuadraticMonomial(Tmonomial *f)
Checks if a monomial is quadratic: K*x^2, with K a constant.
Definition: monomial.c:64
double EvaluateVarSet(double *varValues, Tvariable_set *vs)
Evaluates a variable set for a given set of value for the variables.
Definition: variable_set.c:490
void ProductVarSet(Tvariable_set *vs1, Tvariable_set *vs2, Tvariable_set *v_out)
Product of two variable sets.
Definition: variable_set.c:282
Tvariable_set * GetMonomialVariables(Tmonomial *f)
Gets the variables of a monomial.
Definition: monomial.c:153
void IntervalProduct(Tinterval *i1, Tinterval *i2, Tinterval *i_out)
Product of two intervals.
Definition: interval.c:384
void NewInterval(double lower, double upper, Tinterval *i)
Constructor.
Definition: interval.c:47
Definition of the Tmonomial type and the associated functions.
double ReplaceVariableInVarSet(unsigned int varid, double ct, unsigned int newID, Tvariable_set *vs)
Replaces a variable by another variable.
Definition: variable_set.c:398
Defines a interval.
Definition: interval.h:33
unsigned int VarSetOrder(Tvariable_set *vs)
Gets the order of a variable set.
Definition: variable_set.c:423
void MonomialProduct(Tmonomial *f1, Tmonomial *f2, Tmonomial *f)
Product of two monomials.
Definition: monomial.c:183
void FixVariableInMonomial(unsigned int nv, double v, Tmonomial *f)
Replaces a variable by a constant.
Definition: monomial.c:31
Tvariable_set vars
Definition: monomial.h:35
unsigned int MonomialOrder(Tmonomial *f)
Gets the order of a monomial.
Definition: monomial.c:91
void InitVarSet(Tvariable_set *vs)
Constructor.
Definition: variable_set.c:70