/home/coin/SVN-release/CoinAll-1.1.0/cppad/cppad/local/op_code.hpp

Go to the documentation of this file.
00001 # ifndef CPPAD_OP_CODE_INCLUDED
00002 # define CPPAD_OP_CODE_INCLUDED
00003 
00004 /* --------------------------------------------------------------------------
00005 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-06 Bradley M. Bell
00006 
00007 CppAD is distributed under multiple licenses. This distribution is under
00008 the terms of the 
00009                     Common Public License Version 1.0.
00010 
00011 A copy of this license is included in the COPYING file of this distribution.
00012 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
00013 -------------------------------------------------------------------------- */
00014 /*
00015 ----------------------------------------------------------------------------
00016 $begin OpCode$$ $comment CppAD Developer Documentation$$
00017 $aindex subhead$$
00018 $spell
00019         Addpv
00020         Addvp
00021         Addvv
00022         Subpv
00023         Subvp
00024         Subvv
00025         Mulpv
00026         Mulvp
00027         Mulvv
00028         Divpv
00029         Divvp
00030         Divvv
00031         namespace
00032         Powvp
00033         Powpv
00034         Powvv
00035         Prip
00036         Priv
00037         CExp
00038         Stpp
00039         Stvp
00040         Inv
00041         Ldv
00042         Ldp
00043         Stvp
00044         Stpp
00045         Stvv
00046         Stpv
00047         Sto
00048         enum
00049         Mul
00050         Div
00051         CondExp
00052         Cos
00053         Cosh
00054         Ind
00055         Acos
00056         Asin
00057         Atan
00058         Sqrt
00059         op
00060         zs
00061         zst
00062         zt
00063         xy
00064         yy
00065         Cpp
00066 $$
00067 
00068 $mindex tape operator$$
00069 $section The Tape Operator Codes$$
00070 
00071 $head Syntax$$
00072 $syntax%OpCode% %op%$$ $pre
00073 $$
00074 
00075 
00076 $head Description$$
00077 The value $italic op$$ is one of the following enum type values:
00078 $codep */
00079 
00080 namespace CppAD {
00081 
00082         // alphabetical order (ignoring the Op at the end)
00083         enum OpCode {
00084                 AbsOp,    //  abs(variable)
00085                 AcosOp,   // asin(variable)
00086                 AddpvOp,  //      parameter  + variable
00087                 AddvpOp,  //      variable   + parameter
00088                 AddvvOp,  //      variable   + variable
00089                 AsinOp,   // asin(variable)
00090                 AtanOp,   // atan(variable)
00091                 CExpOp,   // CondExp(cop, left, right, trueCase, falseCase)
00092                 ComOp,    // Compare(cop, result, left, right)
00093                 CosOp,    //  cos(variable)
00094                 CoshOp,   // cosh(variable)
00095                 DisOp,    //  dis(variable,    index)
00096                 DivpvOp,  //      parameter  / variable
00097                 DivvpOp,  //      variable   / parameter
00098                 DivvvOp,  //      variable   / variable
00099                 ExpOp,    //  exp(variable)
00100                 LdpOp,    //    z[parameter]
00101                 LdvOp,    //    z[variable]
00102                 InvOp,    //                             independent variable
00103                 LogOp,    //  log(variable)
00104                 MulpvOp,  //      parameter  * variable
00105                 MulvpOp,  //      variable   * parameter
00106                 MulvvOp,  //      variable   * variable
00107                 NonOp,    //                             space holder
00108                 ParOp,    //      parameter
00109                 PowvpOp,  //  pow(variable,    parameter)
00110                 PowpvOp,  //  pow(parameter,   variable)
00111                 PowvvOp,  //  pow(variable,    variable)
00112                 PripOp,   //      text         parameter
00113                 PrivOp,   //      text         parameter
00114                 SinOp,    //  sin(variable)
00115                 SinhOp,   // sinh(variable)
00116                 SqrtOp,   // sqrt(variable)
00117                 StppOp,   //    z[parameter] = parameter
00118                 StvpOp,   //    z[variable]  = parameter
00119                 StpvOp,   //    z[parameter] = variable
00120                 StvvOp,   //    z[variable]  = variable
00121                 SubpvOp,  //      parameter  - variable
00122                 SubvpOp,  //      variable   - parameter
00123                 SubvvOp   //      variable   - variable
00124         };
00125 }
00126 /* $$
00127 
00128 $end
00129 ------------------------------------------------------------------------ 
00130 $begin printOp$$ $comment CppAD Developer Documentation$$
00131 $spell
00132         var
00133         Num
00134         ind
00135         Op
00136         std
00137         ostream
00138         const
00139         nfz
00140         fz
00141         rz
00142         nrz
00143         Taylor
00144 $$
00145 
00146 $index printOp$$
00147 $index operator, print$$
00148 $index trace, tape operation$$
00149 $section Print the Information Corresponding to One Tape Operation$$
00150 
00151 $head Syntax$$
00152 $syntax%void printOp(
00153         std::ostream &%os% ,
00154         const TapeRec<%Base%> *%Rec% ,
00155         size_t %i_var% ,
00156         OpCode %op% ,
00157         const size_t *%ind% ,
00158         size_t %nfz% ,
00159         const %Value% *%fz% ,
00160         size_t %nrz% ,
00161         const %Value% *%rz% )%$$
00162 
00163 
00164 $head Base$$
00165 Determines the type of tape we are printing from.
00166 
00167 $head Value$$
00168 Determines the type of the values that we are printing
00169 (expected to be either $italic Base$$ or $italic Pack$$ where
00170 $italic Pack$$ is the type used to pace sparsity patterns).
00171 
00172 $head os$$
00173 is the output stream that the information is printed on.
00174 
00175 $head Rec$$
00176 Is the entire recording for the tape that this operator is in.
00177 
00178 $head i_var$$
00179 is the variable index for the result of this operation
00180 (ignored if $syntax%NumVar(%op%) == 0%$$).
00181 
00182 $head op$$
00183 The operator code for this operation.
00184 
00185 $head ind$$
00186 is the vector of indices for this operation
00187 (must have  $syntax%NumInd(%op%)%$$ elements).
00188 
00189 $head nfz$$
00190 is the number of forward calculated values of type $italic Value$$
00191 that correspond to this operation
00192 (ignored if $syntax%NumVar(%op%) == 0%$$).
00193 
00194 $head fz$$
00195 points to the first forward calculated value
00196 that correspond to this operation
00197 (ignored if $syntax%NumVar(%op%) == 0%$$).
00198 
00199 $head nrz$$
00200 is the number of reverse calculated values of type $italic Value$$
00201 that correspond to this operation
00202 (ignored if $syntax%NumVar(%op%) == 0%$$).
00203 
00204 $head rz$$
00205 points to the first reverse calculated value
00206 that correspond to this operation
00207 (ignored if $syntax%NumVar(%op%) == 0%$$).
00208 
00209 $end
00210 ---------------------------------------------------------------------------
00211 $begin NumInd$$ $comment CppAD Developer Documentation$$
00212 $spell
00213         NumInd
00214         Op
00215 $$
00216 
00217 $index NumInd$$
00218 $section Number of Ind field Values Corresponding to an Op Code$$
00219 
00220 $head Syntax$$
00221 $syntax%size_t NumInd(OpCode %op%)%$$
00222 
00223 
00224 $head Description$$
00225 The syntax
00226 $syntax%
00227         size_t NumInd(operator %op%)
00228 %$$
00229 returns the number of Ind field values corresponding to each operator value.
00230 
00231 $end
00232 ------------------------------------------------------------------------
00233 $begin NumVar$$ $comment CppAD Developer Documentation$$
00234 $spell
00235         NumVar
00236         Op
00237 $$
00238 
00239 $index NumVar$$
00240 $section Number of Variables Corresponding to an Op Code$$
00241 
00242 $head Syntax$$
00243 $syntax%size_t NumVar(OpCode %op%)%$$
00244 
00245 
00246 $head Description$$
00247 The syntax
00248 $syntax%
00249         size_t NumVar(operator %op%)
00250 %$$
00251 returns the number of variable
00252 field values corresponding to each operator value.
00253 
00254 $end
00255 ------------------------------------------------------------------------ */
00256 
00257 # include <string>
00258 # include <sstream>
00259 # include <iomanip>
00260 
00261 namespace CppAD {
00262 
00263 // alphabetical order (ignoring the Op at the end)
00264 const size_t NumIndTable[] = {
00265         1, // AbsOp
00266         1, // AcosOp
00267         2, // AddpvOp
00268         2, // AddvpOp
00269         2, // AddvvOp
00270         1, // AsinOp
00271         1, // AtanOp
00272         6, // CExpOp
00273         4, // ComOp
00274         1, // CosOp
00275         1, // CoshOp
00276         2, // DisOp
00277         2, // DivpvOp
00278         2, // DivvpOp
00279         2, // DivvvOp
00280         1, // ExpOp
00281         3, // LdpOp
00282         3, // LdvOp
00283         0, // InvOp
00284         1, // LogOp
00285         2, // MulvvOp
00286         2, // MulpvOp
00287         2, // MulvpOp
00288         0, // NonOp
00289         1, // ParOp
00290         2, // PowvpOp
00291         2, // PowpvOp
00292         2, // PowvvOp
00293         2, // PripOp
00294         2, // PrivOp
00295         1, // SinOp
00296         1, // SinhOp
00297         1, // SqrtOp
00298         3, // StppOp
00299         3, // StvpOp
00300         3, // StpvOp
00301         3, // StvvOp
00302         2, // SubpvOp
00303         2, // SubvpOp
00304         2  // SubvvOp
00305 };
00306 
00307 inline size_t NumInd(OpCode op)
00308 {
00309         CPPAD_ASSERT_UNKNOWN( size_t(SubvvOp) == 
00310                 sizeof(NumIndTable) / sizeof(NumIndTable[0]) - 1
00311         );
00312         CPPAD_ASSERT_UNKNOWN( size_t(op) <= size_t(SubvvOp) );
00313 
00314         return NumIndTable[(size_t) op];
00315 }
00316 
00317 // alphabetical order (ignoring the Op at the end)
00318 const size_t NumVarTable[] = {
00319         1, // AbsOp
00320         2, // AcosOp
00321         1, // AddpvOp
00322         1, // AddvpOp
00323         1, // AddvvOp
00324         2, // AsinOp
00325         2, // AtanOp
00326         1, // CExpOp
00327         0, // ComOp
00328         2, // CosOp
00329         2, // CoshOp
00330         1, // DisOp
00331         1, // DivpvOp
00332         1, // DivvpOp
00333         1, // DivvvOp
00334         1, // ExpOp
00335         1, // LdpOp
00336         1, // LdvOp
00337         1, // InvOp
00338         1, // LogOp
00339         1, // MulvvOp
00340         1, // MulpvOp
00341         1, // MulvpOp
00342         1, // NonOp
00343         1, // ParOp
00344         3, // PowvpOp
00345         3, // PowpvOp
00346         3, // PowvvOp
00347         0, // PripOp
00348         1, // PrivOp
00349         2, // SinOp
00350         2, // SinhOp
00351         1, // SqrtOp
00352         0, // StppOp
00353         0, // StvpOp
00354         0, // StpvOp
00355         0, // StvvOp
00356         1, // SubpvOp
00357         1, // SubvpOp
00358         1  // SubvvOp
00359 };
00360 
00361 inline size_t NumVar(OpCode op)
00362 {
00363         CPPAD_ASSERT_UNKNOWN( size_t(SubvvOp) == 
00364                 sizeof(NumVarTable) / sizeof(NumVarTable[0]) - 1
00365         );
00366         CPPAD_ASSERT_UNKNOWN( size_t(op) <= size_t(SubvvOp) );
00367 
00368         return NumVarTable[(size_t) op];
00369 }
00370 
00371 template <class Type>
00372 void printOpField(
00373         std::ostream      &os , 
00374         const char *   leader ,
00375         const Type     &value , 
00376         size_t          width )
00377 {
00378         std::ostringstream buffer;
00379         std::string        str;
00380 
00381         // first print the leader
00382         os << leader;
00383 
00384         // print the value into an internal buffer
00385         buffer << std::setw(width) << value;
00386         str = buffer.str();
00387 
00388         // length of the string
00389         size_t len = str.size();
00390         if( len > width )
00391         {       size_t i;
00392                 for(i = 0; i < width-1; i++)
00393                         os << str[i];
00394                 os << "*";
00395                 return;
00396         }
00397 
00398         // count number of spaces at begining
00399         size_t nspace = 0; 
00400         while(str[nspace] == ' ' && nspace < len)
00401                 nspace++;
00402 
00403         // left justify the string
00404         size_t i = nspace;
00405         while( i < len )
00406                 os << str[i++];
00407 
00408         i = width - len + nspace;
00409         while(i--)
00410                 os << " "; 
00411 }
00412 
00413 template <class Base, class Value>
00414 void printOp(
00415         std::ostream          &os     , 
00416         const TapeRec<Base>   *Rec    ,
00417         size_t                 i_var  , 
00418         OpCode                 op     ,
00419         const size_t          *ind    ,
00420         size_t                 nfz    ,
00421         const  Value          *fz     ,
00422         size_t                 nrz    ,
00423         const  Value          *rz     )
00424 {       
00425         static char *CompareOpName[] = { "Lt", "Le", "Eq", "Ge", "Gt", "Ne" };
00426         static char *OpName[] = {
00427                 "Abs"   ,
00428                 "Acos"  ,
00429                 "Addpv" ,
00430                 "Addvp" ,
00431                 "Addvv" ,
00432                 "Asin"  ,
00433                 "Atan"  ,
00434                 "CExp"  ,
00435                 "Com"   ,
00436                 "Cos"   ,
00437                 "Cosh"  ,
00438                 "DisOp" ,
00439                 "Divpv" ,
00440                 "Divvp" ,
00441                 "Divvv" ,
00442                 "Exp"   ,
00443                 "Ldp"   ,
00444                 "Ldv"   ,
00445                 "Inv"   ,
00446                 "Log"   ,
00447                 "Mulpv" ,
00448                 "Mulvp" ,
00449                 "Mulvv" ,
00450                 "Non"   ,
00451                 "Par"   ,
00452                 "Powvp" ,
00453                 "Powpv" ,
00454                 "Powvv" ,
00455                 "Prip"  ,
00456                 "Priv"  ,
00457                 "Sin"   ,
00458                 "Sinh"  ,
00459                 "Sqrt"  ,
00460                 "Stpp"  ,
00461                 "Stvp"  ,
00462                 "Stpv"  ,
00463                 "Stvv"  ,
00464                 "Subpv" ,
00465                 "Subvp" ,
00466                 "Subvv"
00467         };
00468         CPPAD_ASSERT_UNKNOWN( 
00469                 size_t(SubvvOp) == sizeof(OpName) / sizeof(OpName[0]) - 1
00470         );
00471 
00472         // print operator
00473         printOpField(os,  "i=",      i_var, 5);
00474         if( op == CExpOp )
00475         {       printOpField(os, "op=", OpName[op], 4); 
00476                 printOpField(os, "", CompareOpName[ ind[0] ], 3);
00477         }
00478         else if( op == ComOp )
00479         {       printOpField(os, "op=", OpName[op], 3); 
00480                 printOpField(os, "", CompareOpName[ ind[0] ], 4);
00481         }
00482         else    printOpField(os, "op=", OpName[op], 7); 
00483 
00484         // print other fields
00485         size_t ncol = 5;
00486         switch( op )
00487         {
00488                 case LdpOp:
00489                 CPPAD_ASSERT_UNKNOWN( NumInd(op) == 3 );
00490                 printOpField(os, "off=", ind[0], ncol);
00491                 printOpField(os, "  p=", *(Rec->GetPar(ind[1])), ncol);
00492                 break;
00493 
00494                 case LdvOp:
00495                 CPPAD_ASSERT_UNKNOWN( NumInd(op) == 3 );
00496                 printOpField(os, "off=", ind[0], ncol);
00497                 printOpField(os, "  v=", ind[1], ncol);
00498                 break;
00499 
00500                 case StppOp:
00501                 CPPAD_ASSERT_UNKNOWN( NumInd(op) == 3 );
00502                 printOpField(os, "off=", ind[0], ncol);
00503                 printOpField(os, " pl=", *(Rec->GetPar(ind[1])), ncol);
00504                 printOpField(os, " pr=", *(Rec->GetPar(ind[2])), ncol);
00505                 break;
00506 
00507                 case StpvOp:
00508                 CPPAD_ASSERT_UNKNOWN( NumInd(op) == 3 );
00509                 printOpField(os, "off=", ind[0], ncol);
00510                 printOpField(os, " pl=", *(Rec->GetPar(ind[1])), ncol);
00511                 printOpField(os, " vr=", ind[2], ncol);
00512                 break;
00513 
00514                 case StvpOp:
00515                 CPPAD_ASSERT_UNKNOWN( NumInd(op) == 3 );
00516                 printOpField(os, "off=", ind[0], ncol);
00517                 printOpField(os, " vl=", ind[1], ncol);
00518                 printOpField(os, " pr=", *(Rec->GetPar(ind[2])), ncol);
00519                 break;
00520 
00521                 case StvvOp:
00522                 CPPAD_ASSERT_UNKNOWN( NumInd(op) == 3 );
00523                 printOpField(os, "off=", ind[0], ncol);
00524                 printOpField(os, " vl=", ind[1], ncol);
00525                 printOpField(os, " vr=", ind[2], ncol);
00526                 break;
00527 
00528                 case AddvvOp:
00529                 case DivvvOp:
00530                 case MulvvOp:
00531                 case PowvvOp:
00532                 case SubvvOp:
00533                 CPPAD_ASSERT_UNKNOWN( NumInd(op) == 2 );
00534                 printOpField(os, " vl=", ind[0], ncol);
00535                 printOpField(os, " vr=", ind[1], ncol);
00536                 break;
00537 
00538                 case AddpvOp:
00539                 case SubpvOp:
00540                 case MulpvOp:
00541                 case PowpvOp:
00542                 case DivpvOp:
00543                 CPPAD_ASSERT_UNKNOWN( NumInd(op) == 2 );
00544                 printOpField(os, " pl=", *(Rec->GetPar(ind[0])), ncol);
00545                 printOpField(os, " vr=", ind[1], ncol);
00546                 break;
00547 
00548                 case AddvpOp:
00549                 case DivvpOp:
00550                 case MulvpOp:
00551                 case PowvpOp:
00552                 case SubvpOp:
00553                 CPPAD_ASSERT_UNKNOWN( NumInd(op) == 2 );
00554                 printOpField(os, " vl=", ind[0], ncol);
00555                 printOpField(os, " pr=", *(Rec->GetPar(ind[1])), ncol);
00556                 break;
00557 
00558                 case AbsOp:
00559                 case AcosOp:
00560                 case AsinOp:
00561                 case AtanOp:
00562                 case CosOp:
00563                 case CoshOp:
00564                 case ExpOp:
00565                 case LogOp:
00566                 case SinOp:
00567                 case SinhOp:
00568                 case SqrtOp:
00569                 CPPAD_ASSERT_UNKNOWN( NumInd(op) == 1 );
00570                 printOpField(os, "  v=", ind[0], ncol);
00571                 break;
00572 
00573                 case ParOp:
00574                 CPPAD_ASSERT_UNKNOWN( NumInd(op) == 1 );
00575                 printOpField(os, "  p=", *(Rec->GetPar(ind[0])), ncol);
00576                 break;
00577 
00578                 case PripOp:
00579                 CPPAD_ASSERT_UNKNOWN( NumInd(op) == 2 );
00580                 printOpField(os, "txt=", *(Rec->GetTxt(ind[0])), ncol);
00581                 printOpField(os, "  p=", *(Rec->GetPar(ind[1])), ncol);
00582                 break;
00583 
00584                 case PrivOp:
00585                 CPPAD_ASSERT_UNKNOWN( NumInd(op) == 2 );
00586                 printOpField(os, "txt=", *(Rec->GetTxt(ind[0])), ncol);
00587                 printOpField(os, "  v=", ind[1], ncol);
00588                 break;
00589 
00590                 case InvOp:
00591                 case NonOp:
00592                 CPPAD_ASSERT_UNKNOWN( NumInd(op) == 0 );
00593                 break;
00594 
00595                 case DisOp:
00596                 CPPAD_ASSERT_UNKNOWN( NumInd(op) == 2 );
00597                 printOpField(os, "  v=", ind[0], ncol);
00598                 printOpField(os, "  f=", ind[1], ncol);
00599                 break;
00600         
00601 
00602                 case CExpOp:
00603                 CPPAD_ASSERT_UNKNOWN(ind[1] != 0);
00604                 CPPAD_ASSERT_UNKNOWN( NumInd(op) == 6 );
00605                 if( ind[1] & 1 )
00606                         printOpField(os, " vl=", ind[2], ncol);
00607                 else    printOpField(os, " pl=", *(Rec->GetPar(ind[2])), ncol);
00608                 if( ind[1] & 2 )
00609                         printOpField(os, " vr=", ind[3], ncol);
00610                 else    printOpField(os, " pr=", *(Rec->GetPar(ind[3])), ncol);
00611                 if( ind[1] & 4 )
00612                         printOpField(os, " vt=", ind[4], ncol);
00613                 else    printOpField(os, " pt=", *(Rec->GetPar(ind[4])), ncol);
00614                 if( ind[1] & 8 )
00615                         printOpField(os, " vf=", ind[5], ncol);
00616                 else    printOpField(os, " pf=", *(Rec->GetPar(ind[5])), ncol);
00617                 break;
00618 
00619                 case ComOp:
00620                 CPPAD_ASSERT_UNKNOWN(ind[1] != 0);
00621                 CPPAD_ASSERT_UNKNOWN( NumInd(op) == 4 );
00622                 if( ind[1] & 1 )
00623                         printOpField(os, "res=", 1, ncol);
00624                 else    printOpField(os, "res=", 0, ncol);
00625                 if( ind[1] & 2 )
00626                         printOpField(os, " vl=", ind[2], ncol);
00627                 else    printOpField(os, " pl=", *(Rec->GetPar(ind[2])), ncol);
00628                 if( ind[1] & 4 )
00629                         printOpField(os, " vr=", ind[3], ncol);
00630                 else    printOpField(os, " pr=", *(Rec->GetPar(ind[3])), ncol);
00631                 break;
00632 
00633                 default:
00634                 CPPAD_ASSERT_UNKNOWN(0);
00635         }
00636         size_t k;
00637         if( NumVar(op) > 0 )
00638         { 
00639                 for(k = 0; k < nfz; k++)
00640                         std::cout << "| fz[" << k << "]=" << fz[k];
00641                 for(k = 0; k < nrz; k++)
00642                         std::cout << "| rz[" << k << "]=" << rz[k];
00643         }
00644         std::cout << std::endl;
00645 }
00646 
00647 } // END CppAD namespace
00648 
00649 # endif

Generated on Sun Nov 14 14:06:33 2010 for Coin-All by  doxygen 1.4.7