CppAD: A C++ Algorithmic Differentiation Package 20110419
|
00001 /* $Id$ */ 00002 # ifndef CPPAD_AD_COPY_INCLUDED 00003 # define CPPAD_AD_COPY_INCLUDED 00004 00005 /* -------------------------------------------------------------------------- 00006 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-11 Bradley M. Bell 00007 00008 CppAD is distributed under multiple licenses. This distribution is under 00009 the terms of the 00010 Common 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 /* 00017 ------------------------------------------------------------------------------ 00018 00019 $begin ad_copy$$ 00020 $spell 00021 Vec 00022 const 00023 $$ 00024 00025 $index AD, copy$$ 00026 $index AD, assignment$$ 00027 00028 $index constructor, AD$$ 00029 $index assignment, AD$$ 00030 00031 $index convert, to AD$$ 00032 $index Base, convert to AD$$ 00033 $index double, convert to AD$$ 00034 $index VecAD, convert to AD$$ 00035 00036 $section AD Copy Constructor and Assignment Operator$$ 00037 00038 $head Syntax$$ 00039 00040 $subhead Constructor$$ 00041 $syntax%AD<%Base%> %y%(%x%) 00042 %$$ 00043 $syntax%AD<%Base%> %y% = %x%$$ 00044 00045 $subhead Assignment$$ 00046 $syntax%%y% = %x%$$ 00047 00048 $head Purpose$$ 00049 The constructor creates a new $syntax%AD<%Base%>%$$ object $italic y$$ 00050 and the assignment operator uses an existing $italic y$$. 00051 In either case, 00052 $italic y$$ has the same value as $italic x$$, 00053 and the same dependence on the 00054 $cref/independent variables/glossary/Tape/Independent Variable/$$ 00055 ($italic y$$ is a 00056 $xref/glossary/Variable/variable/$$ if and only if $italic x$$ is a variable). 00057 00058 $head x$$ 00059 The argument $italic x$$ has prototype 00060 $syntax% 00061 const %Type% &%x% 00062 %$$ 00063 where $italic Type$$ is 00064 $syntax%VecAD<%Base%>::reference%$$, 00065 $syntax%AD<%Base%>%$$, 00066 $italic Base$$, or 00067 $code double$$. 00068 00069 $head y$$ 00070 The target $italic y$$ has prototype 00071 $syntax% 00072 AD<%Base%> &%y% 00073 %$$ 00074 00075 $head Example$$ 00076 $children% 00077 example/copy_ad.cpp% 00078 example/copy_base.cpp% 00079 example/eq.cpp 00080 00081 %$$ 00082 The following files contain examples and tests of these operations. 00083 Each test returns true if it succeeds and false otherwise. 00084 $table 00085 $rref CopyAD.cpp$$ 00086 $rref CopyBase.cpp$$ 00087 $rref Eq.cpp$$ 00088 $tend 00089 00090 $end 00091 ------------------------------------------------------------------------------ 00092 */ 00093 00094 // BEGIN CppAD namespace 00095 namespace CppAD { 00096 00097 /* 00098 // use default copy constructor and assignment operator 00099 // because they may be optimized better by the compiler 00100 template <class Base> 00101 inline AD<Base>::AD(const AD &x) 00102 { 00103 value_ = x.value_; 00104 id_ = x.id_; 00105 taddr_ = x.taddr_; 00106 00107 return; 00108 } 00109 template <class Base> 00110 inline AD<Base>& AD<Base>::operator=(const AD<Base> &right) 00111 { value_ = right.value_; 00112 id_ = right.id_; 00113 taddr_ = right.taddr_; 00114 00115 // check that all variables are parameters while tape is empty 00116 CPPAD_ASSERT_UNKNOWN( 00117 Parameter(*this) || (tape_this() != CPPAD_NULL) 00118 ); 00119 00120 return *this; 00121 } 00122 */ 00123 00124 // Constructor and assignment from Base type 00125 // Initilaize id_ to CPPAD_MAX_NUM_THREADS, so that following conditions hold 00126 // id_ != 0 , id_ % CPPAD_MAX_NUM_THREADS == 0, id_ != any recording tape id. 00127 // taddr_ is not used, set anyway to avoid compile warning. 00128 template <class Base> 00129 inline AD<Base>::AD(const Base &b) 00130 : value_(b) 00131 , id_(CPPAD_MAX_NUM_THREADS) 00132 , taddr_(0) 00133 { } 00134 template <class Base> 00135 inline AD<Base>& AD<Base>::operator=(const Base &b) 00136 { value_ = b; 00137 id_ = CPPAD_MAX_NUM_THREADS; 00138 00139 // check that this is a parameter 00140 CPPAD_ASSERT_UNKNOWN( Parameter(*this) ); 00141 00142 return *this; 00143 } 00144 00145 // constructor and assignment from VecAD_reference<Base> 00146 template <class Base> 00147 inline AD<Base>::AD(const VecAD_reference<Base> &x) 00148 { *this = x.ADBase(); } 00149 template <class Base> 00150 inline AD<Base>& AD<Base>::operator=(const VecAD_reference<Base> &x) 00151 { return *this = x.ADBase(); } 00152 00153 // constructor and assignment from any other type 00154 // taddr_ is not used, set anyway to avoid compile warning. 00155 template <class Base> 00156 template <class T> 00157 inline AD<Base>::AD(const T &t) 00158 : value_(Base(t)) 00159 , id_(CPPAD_MAX_NUM_THREADS) 00160 , taddr_(0) 00161 { } 00162 template <class Base> 00163 template <class T> 00164 inline AD<Base>& AD<Base>::operator=(const T &t) 00165 { return *this = Base(t); } 00166 00167 00168 } // END CppAD namespace 00169 00170 # endif