CppAD: A C++ Algorithmic Differentiation Package
20130918
|
00001 /* $Id$ */ 00002 # ifndef CPPAD_MUL_INCLUDED 00003 # define CPPAD_MUL_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(MulvvOp) == 1 ); 00042 CPPAD_ASSERT_UNKNOWN( NumArg(MulvvOp) == 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(MulvvOp); 00048 // make result a variable 00049 result.tape_id_ = tape_id; 00050 } 00051 else if( IdenticalZero(right.value_) ) 00052 { // result = variable * 0 00053 } 00054 else if( IdenticalOne(right.value_) ) 00055 { // result = variable * 1 00056 result.make_variable(left.tape_id_, left.taddr_); 00057 } 00058 else 00059 { // result = variable * parameter 00060 CPPAD_ASSERT_UNKNOWN( NumRes(MulpvOp) == 1 ); 00061 CPPAD_ASSERT_UNKNOWN( NumArg(MulpvOp) == 2 ); 00062 00063 // put operand addresses in tape 00064 addr_t p = tape->Rec_.PutPar(right.value_); 00065 tape->Rec_.PutArg(p, left.taddr_); 00066 // put operator in the tape 00067 result.taddr_ = tape->Rec_.PutOp(MulpvOp); 00068 // make result a variable 00069 result.tape_id_ = tape_id; 00070 } 00071 } 00072 else if( var_right ) 00073 { if( IdenticalZero(left.value_) ) 00074 { // result = 0 * variable 00075 } 00076 else if( IdenticalOne(left.value_) ) 00077 { // result = 1 * variable 00078 result.make_variable(right.tape_id_, right.taddr_); 00079 } 00080 else 00081 { // result = parameter * variable 00082 CPPAD_ASSERT_UNKNOWN( NumRes(MulpvOp) == 1 ); 00083 CPPAD_ASSERT_UNKNOWN( NumArg(MulpvOp) == 2 ); 00084 00085 // put operand addresses in tape 00086 addr_t p = tape->Rec_.PutPar(left.value_); 00087 tape->Rec_.PutArg(p, right.taddr_); 00088 // put operator in the tape 00089 result.taddr_ = tape->Rec_.PutOp(MulpvOp); 00090 // make result a variable 00091 result.tape_id_ = tape_id; 00092 } 00093 } 00094 return result; 00095 } 00096 00097 // convert other cases into the case above 00098 CPPAD_FOLD_AD_VALUED_BINARY_OPERATOR(*) 00099 00100 } // END CppAD namespace 00101 00102 # endif