CppAD: A C++ Algorithmic Differentiation Package
20130102
|
00001 /* $Id$ */ 00002 # ifndef CPPAD_ADD_EQ_INCLUDED 00003 # define CPPAD_ADD_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(AddvvOp) == 1 ); 00042 CPPAD_ASSERT_UNKNOWN( NumArg(AddvvOp) == 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(AddvvOp); 00048 // make this a variable 00049 CPPAD_ASSERT_UNKNOWN( tape_id_ == tape_id ); 00050 } 00051 else if( ! IdenticalZero( right.value_ ) ) 00052 { // this = variable + parameter 00053 // = parameter + variable 00054 CPPAD_ASSERT_UNKNOWN( NumRes(AddpvOp) == 1 ); 00055 CPPAD_ASSERT_UNKNOWN( NumArg(AddpvOp) == 2 ); 00056 00057 // put operand addresses in tape 00058 addr_t p = tape->Rec_.PutPar(right.value_); 00059 tape->Rec_.PutArg(p, taddr_); 00060 // put operator in the tape 00061 taddr_ = tape->Rec_.PutOp(AddpvOp); 00062 // make this a variable 00063 CPPAD_ASSERT_UNKNOWN( tape_id_ == tape_id ); 00064 } 00065 } 00066 else if( var_right ) 00067 { if( IdenticalZero(left) ) 00068 { // this = 0 + right 00069 make_variable(right.tape_id_, right.taddr_); 00070 } 00071 else 00072 { // this = parameter + variable 00073 CPPAD_ASSERT_UNKNOWN( NumRes(AddpvOp) == 1 ); 00074 CPPAD_ASSERT_UNKNOWN( NumArg(AddpvOp) == 2 ); 00075 00076 // put operand addresses in tape 00077 addr_t p = tape->Rec_.PutPar(left); 00078 tape->Rec_.PutArg(p, right.taddr_); 00079 // put operator in the tape 00080 taddr_ = tape->Rec_.PutOp(AddpvOp); 00081 // make this a variable 00082 tape_id_ = tape_id; 00083 } 00084 } 00085 return *this; 00086 } 00087 00088 CPPAD_FOLD_ASSIGNMENT_OPERATOR(+=) 00089 00090 } // END CppAD namespace 00091 00092 # endif