CppAD: A C++ Algorithmic Differentiation Package
20130918
|
00001 /* $Id$ */ 00002 # ifndef CPPAD_ADD_INCLUDED 00003 # define CPPAD_ADD_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> operator + (const AD<Base> &left , const AD<Base> &right) 00021 { 00022 // compute the Base part of this AD object 00023 AD<Base> result; 00024 result.value_ = left.value_ + right.value_; 00025 CPPAD_ASSERT_UNKNOWN( Parameter(result) ); 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 result; 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 = left.tape_id_ == tape_id; 00036 bool var_right = right.tape_id_ == tape_id; 00037 00038 if( var_left ) 00039 { if( var_right ) 00040 { // result = 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(left.taddr_, right.taddr_); 00046 // put operator in the tape 00047 result.taddr_ = tape->Rec_.PutOp(AddvvOp); 00048 // make result a variable 00049 result.tape_id_ = tape_id; 00050 } 00051 else if( IdenticalZero(right.value_) ) 00052 { // result = variable + 0 00053 result.make_variable(left.tape_id_, left.taddr_); 00054 } 00055 else 00056 { // result = variable + parameter 00057 // = parameter + variable 00058 CPPAD_ASSERT_UNKNOWN( NumRes(AddpvOp) == 1 ); 00059 CPPAD_ASSERT_UNKNOWN( NumArg(AddpvOp) == 2 ); 00060 00061 // put operand addresses in tape 00062 addr_t p = tape->Rec_.PutPar(right.value_); 00063 tape->Rec_.PutArg(p, left.taddr_); 00064 // put operator in the tape 00065 result.taddr_ = tape->Rec_.PutOp(AddpvOp); 00066 // make result a variable 00067 result.tape_id_ = tape_id; 00068 } 00069 } 00070 else if( var_right ) 00071 { if( IdenticalZero(left.value_) ) 00072 { // result = 0 + variable 00073 result.make_variable(right.tape_id_, right.taddr_); 00074 } 00075 else 00076 { // result = parameter + variable 00077 CPPAD_ASSERT_UNKNOWN( NumRes(AddpvOp) == 1 ); 00078 CPPAD_ASSERT_UNKNOWN( NumArg(AddpvOp) == 2 ); 00079 00080 // put operand addresses in tape 00081 addr_t p = tape->Rec_.PutPar(left.value_); 00082 tape->Rec_.PutArg(p, right.taddr_); 00083 // put operator in the tape 00084 result.taddr_ = tape->Rec_.PutOp(AddpvOp); 00085 // make result a variable 00086 result.tape_id_ = tape_id; 00087 } 00088 } 00089 return result; 00090 } 00091 00092 // convert other cases into the case above 00093 CPPAD_FOLD_AD_VALUED_BINARY_OPERATOR(+) 00094 00095 } // END CppAD namespace 00096 00097 # endif