CppAD: A C++ Algorithmic Differentiation Package  20130918
sub_eq.hpp
Go to the documentation of this file.
00001 /* $Id$ */
00002 # ifndef CPPAD_SUB_EQ_INCLUDED
00003 # define CPPAD_SUB_EQ_INCLUDED
00004 
00005 /* --------------------------------------------------------------------------
00006 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-12 Bradley M. Bell
00007 
00008 CppAD is distributed under multiple licenses. This distribution is under
00009 the terms of the 
00010                     Eclipse Public License Version 1.0.
00011 
00012 A copy of this license is included in the COPYING file of this distribution.
00013 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
00014 -------------------------------------------------------------------------- */
00015 
00016 //  BEGIN CppAD namespace
00017 namespace CppAD {
00018 
00019 template <class Base>
00020 AD<Base>& AD<Base>::operator -= (const AD<Base> &right)
00021 {
00022      // compute the Base part
00023      Base left;
00024      left    = value_;
00025      value_ -= right.value_;
00026 
00027      // check if there is a recording in progress
00028      ADTape<Base>* tape = AD<Base>::tape_ptr();
00029      if( tape == CPPAD_NULL )
00030           return *this;
00031      tape_id_t tape_id = tape->id_;
00032 
00033      // tape_id cannot match the default value for tape_id_; i.e., 0
00034      CPPAD_ASSERT_UNKNOWN( tape_id > 0 );
00035      bool var_left  = tape_id_       == tape_id;
00036      bool var_right = right.tape_id_ == tape_id;
00037 
00038      if( var_left )
00039      {    if( var_right )
00040           {    // this = variable - variable
00041                CPPAD_ASSERT_UNKNOWN( NumRes(SubvvOp) == 1 );
00042                CPPAD_ASSERT_UNKNOWN( NumArg(SubvvOp) == 2 );
00043 
00044                // put operand addresses in tape
00045                tape->Rec_.PutArg(taddr_, right.taddr_);
00046                // put operator in the tape
00047                taddr_ = tape->Rec_.PutOp(SubvvOp);
00048                // make this a variable
00049                CPPAD_ASSERT_UNKNOWN( tape_id_ == tape_id );
00050           }
00051           else if( IdenticalZero( right.value_ ) )
00052           {    // this = variable - 0
00053           }
00054           else
00055           {    // this = variable - parameter
00056                CPPAD_ASSERT_UNKNOWN( NumRes(SubvpOp) == 1 );
00057                CPPAD_ASSERT_UNKNOWN( NumArg(SubvpOp) == 2 );
00058 
00059                // put operand addresses in tape
00060                addr_t p = tape->Rec_.PutPar(right.value_);
00061                tape->Rec_.PutArg(taddr_, p);
00062                // put operator in the tape
00063                taddr_ = tape->Rec_.PutOp(SubvpOp);
00064                // make this a variable
00065                CPPAD_ASSERT_UNKNOWN( tape_id_ == tape_id );
00066           }
00067      }
00068      else if( var_right  )
00069      {    // this = parameter - variable
00070           CPPAD_ASSERT_UNKNOWN( NumRes(SubpvOp) == 1 );
00071           CPPAD_ASSERT_UNKNOWN( NumArg(SubpvOp) == 2 );
00072 
00073           // put operand addresses in tape
00074           addr_t p = tape->Rec_.PutPar(left);
00075           tape->Rec_.PutArg(p, right.taddr_);
00076           // put operator in the tape
00077           taddr_ = tape->Rec_.PutOp(SubpvOp);
00078           // make this a variable
00079           tape_id_ = tape_id;
00080      }
00081      return *this;
00082 }
00083 
00084 CPPAD_FOLD_ASSIGNMENT_OPERATOR(-=)
00085 
00086 } // END CppAD namespace
00087 
00088 # endif 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines