1 # ifndef CPPAD_CORE_AD_CTOR_HPP
2 # define CPPAD_CORE_AD_CTOR_HPP
3 /* --------------------------------------------------------------------------
4 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-18 Bradley M. Bell
5 
6 CppAD is distributed under the terms of the
7              Eclipse Public License Version 2.0.
8 
9 This Source Code may also be made available under the following
10 Secondary License when the conditions for such availability set forth
11 in the Eclipse Public License, Version 2.0 are satisfied:
12       GNU General Public License, Version 2.0 or later.
13 ---------------------------------------------------------------------------- */
14 
15 /*
16 ------------------------------------------------------------------------------
17 
18 $begin ad_ctor$$
19 $spell
20     cppad
21     ctor
22     initializes
23     Vec
24     const
25 $$
26 
27 
28 $section AD Constructors $$
29 
30 $head Syntax$$
31 $codei%AD<%Base%> %y%()
32 %$$
33 $codei%AD<%Base%> %y%(%x%)
34 %$$
35 
36 $head Purpose$$
37 creates a new $codei%AD<%Base%>%$$ object $icode y$$
38 and initializes its value as equal to $icode x$$.
39 
40 $head x$$
41 
42 $subhead implicit$$
43 There is an implicit constructor where $icode x$$ has one of the following
44 prototypes:
45 $codei%
46     const %Base%&        %x%
47     const VecAD<%Base%>& %x%
48 %$$
49 
50 $subhead explicit$$
51 There is an explicit constructor where $icode x$$ has prototype
52 $codei%
53     const %Type%&        %x%
54 %$$
55 for any type that has an explicit constructor of the form
56 $icode%Base%(%x%)%$$.
57 
58 $head y$$
59 The target $icode y$$ has prototype
60 $codei%
61     AD<%Base%> %y%
62 %$$
63 
64 $head Example$$
65 $children%
66     example/general/ad_ctor.cpp
67 %$$
68 The files $cref ad_ctor.cpp$$ contain examples and tests of these operations.
69 It test returns true if it succeeds and false otherwise.
70 
71 $end
72 ------------------------------------------------------------------------------
73 */
74 
75 namespace CppAD { // BEGIN_CPPAD_NAMESPACE
76 
77 /*!
78 \file ad_ctor.hpp
79 AD<Base> constructors and and copy operations.
80 */
81 
82 /*!
83 \page AD_default_ctor
84 Use default copy constructor
85 because they may be optimized better than the code below:
86 \code
87 template <class Base>
88 AD<Base>::AD(const AD &x)
89 {
90     value_    = x.value_;
91     tape_id_  = x.tape_id_;
92     taddr_    = x.taddr_;
93     ad_type_  = x.ad_type_;
94 
95     return;
96 }
97 \endcode
98 */
99 
100 /*!
101 Default Constructor.
102 
103 \tparam Base
104 Base type for this AD object.
105 */
106 template <class Base>
AD(void)107 AD<Base>::AD(void)
108 : value_()
109 , tape_id_(0)
110 , taddr_(0)
111 , ad_type_(constant_enum)
112 { }
113 
114 // --------------------------------------------------------------------------
115 # ifdef CPPAD_FOR_TMB
116 /*!
117 Constructor from double.
118 
119 \param d
120 is value corresponding to this AD object.
121 The tape identifier will be an invalid tape identifier,
122 so this object is initially a parameter.
123 
124 \par CPPAD_FOR_TMB
125 This constructor is defined when CPPAD_FOR_TMB is defined.
126 */
127 template <class Base>
AD(const double & d)128 AD<Base>::AD(const double &d)
129 : value_( Base(d) )
130 , tape_id_(0)
131 , taddr_(0)
132 , ad_type_(constant_enum)
133 {   // check that this is a parameter
134     CPPAD_ASSERT_UNKNOWN( Parameter(*this) );
135 }
136 // --------------------------------------------------------------------------
137 # else
138 // --------------------------------------------------------------------------
139 /*!
140 Constructor from Base type.
141 
142 \tparam Base
143 Base type for this AD object.
144 
145 \param b
146 is the Base type value corresponding to this AD object.
147 The tape identifier will be an invalid tape identifier,
148 so this object is initially a parameter.
149 
150 \par CPPAD_FOR_TMB
151 This constructor is defined when CPPAD_FOR_TMB is not defined.
152 */
153 template <class Base>
AD(const Base & b)154 AD<Base>::AD(const Base &b)
155 : value_(b)
156 , tape_id_(0)
157 , taddr_(0)
158 , ad_type_(constant_enum)
159 {   // check that this is a parameter
160     CPPAD_ASSERT_UNKNOWN( Parameter(*this) );
161 }
162 # endif
163 // --------------------------------------------------------------------------
164 
165 /*!
166 Constructor from an ADVec<Base> element drops the vector information.
167 
168 \tparam Base
169 Base type for this AD object.
170 */
171 template <class Base>
AD(const VecAD_reference<Base> & x)172 AD<Base>::AD(const VecAD_reference<Base> &x)
173 {   *this = x.ADBase(); }
174 
175 /*!
176 Constructor from any other type, converts to Base type, and uses constructor
177 from Base type.
178 
179 \tparam Base
180 Base type for this AD object.
181 
182 \tparam T
183 is the the type that is being converted to AD<Base>.
184 There must be a constructor for Base from Type.
185 
186 \param t
187 is the object that is being converted from T to AD<Base>.
188 */
189 template <class Base>
190 template <class T>
AD(const T & t)191 AD<Base>::AD(const T &t)
192 : value_(Base(t))
193 , tape_id_(0)
194 , taddr_(0)
195 , ad_type_(constant_enum)
196 { }
197 
198 } // END_CPPAD_NAMESPACE
199 # endif
200