Institut de Robòtica i Informàtica Industrial
KRD Group

The CuikSuite Project

monomial.c

Go to the documentation of this file.
00001 #include "monomial.h"
00002 
00003 #include "error.h"
00004 #include "defines.h"
00005 
00006 #include <math.h>
00007 
00017 void InitMonomial(Tmonomial *f)
00018 {
00019   f->empty=TRUE;
00020   InitVarSet(&(f->vars));
00021   f->ct=0.0;
00022 }
00023 
00024 void ResetMonomial(Tmonomial *f)
00025 {
00026   f->empty=TRUE;
00027   ResetVarSet(&(f->vars));
00028   f->ct=0.0;
00029 }
00030 
00031 void FixVariableInMonomial(unsigned int nv,double v,Tmonomial *f)
00032 {
00033   unsigned int pos,power;
00034 
00035   pos=GetPlaceinSet(nv,&(f->vars)); /*This returns NO_UINT for empty var sets*/
00036 
00037   if (pos!=NO_UINT)
00038     {
00039       power=GetVariablePowerN(pos,&(f->vars));
00040       RemoveVariableFromSet(nv,&(f->vars));
00041       f->ct*=pow(v,power);
00042       if (fabs(f->ct)<=ZERO)
00043         ResetMonomial(f); 
00044     }
00045 
00046   /*the indexes for variables with ID above 'nv' should be decreased by one*/
00047   ShiftVarIndexes(nv,&(f->vars)); /*has no effects for empty var sets*/
00048 }
00049 
00050 void ReplaceVariableInMonomial(unsigned int nv,double ct,unsigned int nvNew,Tmonomial *f)
00051 {
00052   unsigned int pos,power;
00053 
00054   pos=GetPlaceinSet(nv,&(f->vars));
00055 
00056   if (pos!=NO_UINT)
00057     {
00058       power=GetVariablePowerN(pos,&(f->vars));
00059       RemoveVariableFromSet(nv,&(f->vars));
00060       AddVariable2Set(nvNew,power,&(f->vars));
00061       
00062       f->ct*=pow(ct,power);
00063       if (fabs(f->ct)<=ZERO)
00064         ResetMonomial(f);
00065     }
00066 
00067   /*the indexes for variables with ID above 'nv' should be decreased by one*/
00068   ShiftVarIndexes(nv,&(f->vars));
00069 }
00070 
00071 boolean EmptyMonomial(Tmonomial *f)
00072 {
00073   return(f->empty);
00074 }
00075 
00076 boolean CtMonomial(Tmonomial *f)
00077 {
00078   return((f->empty)||
00079          (VariableSetSize(&(f->vars))==0));
00080 }
00081 
00082 boolean QuadraticMonomial(Tmonomial *f)
00083 {
00084   return((VariableSetSize(&(f->vars))==1)&&
00085          (GetVariablePowerN(0,&(f->vars))==2));
00086 }
00087 
00088 boolean BilinearMonomial(Tmonomial *f)
00089 {
00090   return((VariableSetSize(&(f->vars))==2)&&
00091          (GetVariablePowerN(0,&(f->vars))==1)&&
00092          (GetVariablePowerN(1,&(f->vars))==1));
00093 }
00094 
00095 boolean LinearMonomial(Tmonomial *f)
00096 {
00097   return((VariableSetSize(&(f->vars))==1)&&
00098          (GetVariablePowerN(0,&(f->vars))==1));
00099 }
00100 
00101 unsigned int MonomialOrder(Tmonomial *f)
00102 {
00103   return(VarSetOrder(&(f->vars)));
00104 }
00105 
00106 void CopyMonomial(Tmonomial *f_dst,Tmonomial *f_orig)
00107 {
00108   f_dst->empty=f_orig->empty;
00109   f_dst->ct=f_orig->ct;
00110   CopyVarSet(&(f_dst->vars),&(f_orig->vars));
00111 }
00112 
00113 unsigned int CmpMonomial(Tmonomial *f1,Tmonomial *f2)
00114 {
00115   unsigned int r;
00116 
00117   if ((f1->empty)&&(f2->empty))
00118     r=0;
00119   else
00120     {
00121       if (f1->empty)
00122         r=2;
00123       else
00124         {
00125           if (f2->empty)
00126             r=1;
00127           else
00128             {
00129               r=CmpVarSet(&(f1->vars),&(f2->vars));
00130               if (r==0)
00131                 {
00132                   if (f1->ct>f2->ct)
00133                     r=1;
00134                   else
00135                     {
00136                       if (f1->ct<f2->ct)
00137                         r=2;
00138                     }
00139                 }
00140             }
00141         }
00142     }
00143   return(r);
00144 }
00145 
00146 double GetMonomialCt(Tmonomial *f)
00147 {
00148   if (f->empty)
00149     return(0);
00150   else
00151     return(f->ct);
00152 }
00153 
00154 void SetMonomialCt(double k,Tmonomial *f)
00155 {
00156   f->empty=FALSE;
00157   f->ct=k;
00158 
00159   if (fabs(f->ct)<=ZERO)
00160     ResetMonomial(f);
00161 }
00162 
00163 Tvariable_set *GetMonomialVariables(Tmonomial *f)
00164 {
00165   return(&(f->vars));
00166 }
00167 
00168 void AddCt2Monomial(double k,Tmonomial *f)
00169 {
00170   if (f->empty)
00171     f->ct=k;
00172   else
00173     f->ct*=k;
00174 
00175   f->empty=FALSE;
00176 
00177   if (fabs(f->ct)<=ZERO)
00178     f->ct=0.0;
00179 }
00180 
00181 void AddVariable2Monomial(unsigned int varid,unsigned int p,Tmonomial *f)
00182 {
00183   if (f->empty)
00184     {
00185       f->empty=FALSE;
00186       f->ct=1;
00187       AddVariable2Set(varid,p,&(f->vars));
00188     }
00189   else
00190     AddVariable2Set(varid,p,&(f->vars));
00191 }
00192 
00193 double EvaluateMonomial(double *varValues,Tmonomial *f)
00194 {
00195   if (f->empty)
00196     return(0.0);
00197   else
00198     {
00199       if (VariableSetSize(&(f->vars))>0)
00200         return(f->ct*EvaluateVarSet(varValues,&(f->vars)));
00201       else
00202         return(f->ct);
00203     }
00204 }
00205 
00206 void EvaluateMonomialInt(Tinterval *varValues,Tinterval *i_out,Tmonomial *f)
00207 {  
00208   if (f->empty)
00209     NewInterval(0,0,i_out);
00210   else
00211     {
00212       if (VariableSetSize(&(f->vars))>0)
00213         {
00214           Tinterval ct;
00215 
00216           EvaluateVarSetInt(varValues,i_out,&(f->vars));
00217 
00218           /* The coefficients of the equations can be affected by some noise
00219              due to floating point roundings when operating them. We add a small
00220              range (1e-11) to compensate for those possible errors. */
00221           NewInterval(f->ct-ZERO,f->ct+ZERO,&ct);
00222           IntervalProduct(&ct,i_out,i_out);
00223         }
00224       else
00225         {
00226           /* The coefficients of the equations can be affected by some noise
00227              due to floating point roundings when operating them. We add a small
00228              range (1e-11) to compensate for those possible errors. */
00229           NewInterval(f->ct-ZERO,f->ct+ZERO,i_out);
00230         }
00231     }
00232 }
00233 
00234 void DeriveMonomial(unsigned int nv,Tmonomial *df,Tmonomial *f)
00235 {
00236   if ((f->empty)||(!VarIncluded(nv,&(f->vars))))
00237     InitMonomial(df);
00238   else
00239     {
00240       /* The output of 'DeriveVarSet' is >0 since we now for sure the
00241          var set includes variable 'nv;*/
00242       df->ct=f->ct*(double)DeriveVarSet(nv,&(df->vars),&(f->vars));
00243       df->empty=FALSE;
00244     }
00245 }
00246 
00247 
00248 void PrintMonomial(FILE *file,boolean first,char **varNames,Tmonomial *f)
00249 {
00250   if (!f->empty)
00251     {
00252       if (first)
00253         {
00254           if (fabs(f->ct-1)>ZERO)
00255             {
00256               fprintf(file,"%.12g",f->ct);
00257               if (!EmptyVarSet(&(f->vars)))
00258                 fprintf(file,"*");
00259             }
00260         }
00261       else
00262         {
00263           if (fabs(f->ct-1)<ZERO)
00264             fprintf(file,"+");
00265           else
00266             {
00267               if (fabs(f->ct+1)<ZERO)
00268                 fprintf(file,"-");
00269               else
00270                 {
00271                   fprintf(file,"%+.12g",f->ct);
00272                   if (!EmptyVarSet(&(f->vars)))
00273                     fprintf(file,"*");
00274                 }
00275             }
00276         }
00277 
00278       PrintVarSet(file,varNames,&(f->vars));
00279     }
00280 }
00281 
00282 void DeleteMonomial(Tmonomial *f)
00283 {
00284   DeleteVarSet(&(f->vars));
00285 }