CppAD: A C++ Algorithmic Differentiation Package
20130102
|
00001 /* $Id$ */ 00002 # ifndef CPPAD_SUB_INCLUDED 00003 # define CPPAD_SUB_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 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(SubvvOp) == 1 ); 00042 CPPAD_ASSERT_UNKNOWN( NumArg(SubvvOp) == 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(SubvvOp); 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 CPPAD_ASSERT_UNKNOWN( NumRes(SubvpOp) == 1 ); 00058 CPPAD_ASSERT_UNKNOWN( NumArg(SubvpOp) == 2 ); 00059 00060 // put operand addresses in tape 00061 addr_t p = tape->Rec_.PutPar(right.value_); 00062 tape->Rec_.PutArg(left.taddr_, p); 00063 // put operator in the tape 00064 result.taddr_ = tape->Rec_.PutOp(SubvpOp); 00065 // make result a variable 00066 result.tape_id_ = tape_id; 00067 } 00068 } 00069 else if( var_right ) 00070 { // result = parameter - variable 00071 CPPAD_ASSERT_UNKNOWN( NumRes(SubpvOp) == 1 ); 00072 CPPAD_ASSERT_UNKNOWN( NumArg(SubpvOp) == 2 ); 00073 00074 // put operand addresses in tape 00075 addr_t p = tape->Rec_.PutPar(left.value_); 00076 tape->Rec_.PutArg(p, right.taddr_); 00077 // put operator in the tape 00078 result.taddr_ = tape->Rec_.PutOp(SubpvOp); 00079 // make result a variable 00080 result.tape_id_ = tape_id; 00081 } 00082 return result; 00083 } 00084 00085 // convert other cases into the case above 00086 CPPAD_FOLD_AD_VALUED_BINARY_OPERATOR(-) 00087 00088 } // END CppAD namespace 00089 00090 # endif