source: trunk/example/base_alloc.hpp @ 3680

Last change on this file since 3680 was 3680, checked in by bradbell, 5 years ago

merge to branch: trunk
from repository: https://github.com/coin-or/CppAD
start hash code: 071875a4beba3363e5fa9752426aec4762cd1caa
end hash code: 0bef506513a519e1073c6279d5c4cba9e5c3b180

commit 0bef506513a519e1073c6279d5c4cba9e5c3b180
Author: Brad Bell <bradbell@…>
Date: Thu May 7 12:14:32 2015 -0700

Add the acosh function (as an atomic operation when defined by compiler).

commit b3264fa17b2f65b65800423a0e243c9c3ccfe06a
Author: Brad Bell <bradbell@…>
Date: Wed May 6 20:25:38 2015 -0700

CMakeLists.txt: Change so test only check for compliation.

commit dcbac4d4f20cc383f2bd9edb02036659df40b791
Author: Brad Bell <bradbell@…>
Date: Wed May 6 15:06:28 2015 -0700

asinh.cpp: check higher orders, relax accuracy on test.

commit 5f8881993fedd18cccc3c74831133a8f8a9d17b0
Author: Brad Bell <bradbell@…>
Date: Wed May 6 14:36:18 2015 -0700

Change Acos to acos.
acos.cpp: remove trailing white space.

commit e828fa1f7c4c3848c727f14b1b7a8030071ee705
Author: Brad Bell <bradbell@…>
Date: Wed May 6 12:07:35 2015 -0700

Change Acos to acos.
acos.cpp: remove redundant index commands, remove trailing with space.

commit 3d16e5b9fe1bdafa4ad01d1d466bb72b792650fa
Author: Brad Bell <bradbell@…>
Date: Wed May 6 11:30:49 2015 -0700

op_code.hpp: Minor edits to AcosOp? commnets.

commit 58beaaad149b4ac29fae44589d7f8900bf8f4c40
Author: Brad Bell <bradbell@…>
Date: Wed May 6 10:51:43 2015 -0700

for_jac_sweep.hpp: Add missing AsinhOp? case.

commit 623c134870c522ae5e80bcf0f89d230902594c80
Author: Brad Bell <bradbell@…>
Date: Wed May 6 10:27:39 2015 -0700

Fix comment about AsinhOp? operator.

commit 226b14f6f4810f5abf1ca247aae541963efaf4d6
Author: Brad Bell <bradbell@…>
Date: Wed May 6 10:14:08 2015 -0700

Add derivative of F to make order zero case clearer.
acos_reverse.omh: Fix some sign errors.
asin_reverse.omh: Fix typo.
acos_forward.omh: Simplify by distributing minus sign.

commit 4682f4ee73e33b600b180086576e986f636a24dc
Author: Brad Bell <bradbell@…>
Date: Wed May 6 08:15:50 2015 -0700

acos_forward.omh: fix sign that depends on acos versus acosh.

commit 906ae10adf019ddda7f57dd165aab08fc55289c4
Author: Brad Bell <bradbell@…>
Date: Wed May 6 07:09:47 2015 -0700

  1. Fix inclusion of some temporary files in package (e.g., git_commit.sh).
  2. Simplify and improve using git ls-files and ls bin/check_*.
  3. Remove trailing white space.

commit 5096f4706a547bd76caa3766aa2c62802ef7f0bf
Author: Brad Bell <bradbell@…>
Date: Wed May 6 06:41:20 2015 -0700

Combine base type documentation for erf, asinh
(will add more functions to this list list).

commit b3535db5ad95bee90672abcaa686032d23bce2fc
Author: Brad Bell <bradbell@…>
Date: Tue May 5 18:01:11 2015 -0700

  1. Change Arc Cosine/Sine? to Inverse Cosine/Sine?.
  2. Change arcsin-> asin and arccos->acos.
  3. Remove index commands that are duplicates of words in titles.


acos_reverse.omh: Add acosh case to this page.

  • Property svn:keywords set to Id
File size: 9.1 KB
Line 
1/* $Id: base_alloc.hpp 3680 2015-05-07 19:17:37Z bradbell $ */
2# ifndef CPPAD_BASE_ALLOC_INCLUDED
3# define CPPAD_BASE_ALLOC_INCLUDED
4/* --------------------------------------------------------------------------
5CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-15 Bradley M. Bell
6
7CppAD is distributed under multiple licenses. This distribution is under
8the terms of the
9                    Eclipse Public License Version 1.0.
10
11A copy of this license is included in the COPYING file of this distribution.
12Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
13-------------------------------------------------------------------------- */
14/*
15$begin base_alloc.hpp$$
16$spell
17        acosh
18        asinh
19        Rel
20        Lt Le Eq Ge Gt
21        Cond
22        enum
23        geq
24        cos
25        sqrt
26        cppad.hpp
27        alloc
28        op
29        const
30        ptrdbl
31        bool
32        CppAD
33        sizeof
34        inline
35        atan
36        ostream
37        namespace
38        exp
39        tanh
40        acos
41        asin
42        std
43        fabs
44        erf
45        endif
46$$
47$section Example AD<Base> Where Base Constructor Allocates Memory$$
48
49$head Purpose$$
50Demonstrate use of $codei%AD<%Base%>%$$
51where memory is allocated for each element of the type $icode Base$$.
52In addition, this is a complete example where all the
53$cref/required Base/base_require/$$ type
54operations are defined (as apposed to other examples where
55some of the operations for the Base type are already defined).
56
57$head Include File$$
58This file uses some of the definitions in $cref base_require$$
59and $cref thread_alloc$$.
60$codep */
61# include <cppad/base_require.hpp>
62# include <cppad/thread_alloc.hpp>
63/* $$
64
65$head Computed Assignment Macro$$
66This macro is used for the
67$code base_alloc$$ computed assignment operators; to be specific,
68used with $icode op $$ equal to
69$code +=$$,
70$code -=$$,
71$code *=$$,
72$code /=$$.
73$codep */
74# define BASE_ALLOC_ASSIGN_OPERATOR(op) \
75        void operator op (const base_alloc& x) \
76        {       *ptrdbl_ op *x.ptrdbl_; }
77/* $$
78
79$head Binary Operator Macro$$
80This macro is used for the
81$code base_alloc$$ binary operators (as member functions); to be specific,
82used with $icode op $$ equal to
83$code +$$,
84$code -$$,
85$code *$$,
86$code /$$.
87$codep */
88# define BASE_ALLOC_BINARY_OPERATOR(op) const \
89        base_alloc operator op (const base_alloc& x) const \
90        {       base_alloc result; \
91                double   dbl = *ptrdbl_; \
92                double x_dbl = *x.ptrdbl_; \
93                *result.ptrdbl_ = dbl op x_dbl; \
94                return result; \
95        }
96/* $$
97
98$head Boolean Operator Macro$$
99This macro can be used for the
100$code base_alloc$$ binary operators that have a
101$code bool$$ result; to be specific,
102used with $icode op $$ equal to
103$code ==$$,
104$code !=$$,
105$code <$$,
106$code <=$$,
107$code >=$$, and
108$code >$$,
109$codep */
110# define BASE_ALLOC_BOOL_OPERATOR(op) const \
111        bool operator op (const base_alloc& x) const \
112        {       double   dbl = *ptrdbl_; \
113                double x_dbl = *x.ptrdbl_; \
114                return dbl op x_dbl; \
115        }
116/* $$
117
118$head Class Definition$$
119The following example class
120defines the necessary $cref base_member$$ functions.
121It is made more complicated by storing a pointer to a $code double$$
122instead of the $code double$$ value itself.
123$codep */
124
125class base_alloc {
126public:
127        double* ptrdbl_;
128
129        base_alloc(void)
130        {       size_t cap;
131                void* v  = CppAD::thread_alloc::get_memory(sizeof(double), cap);
132                ptrdbl_  = static_cast<double*>(v);
133        }
134        base_alloc(double dbl)
135        {       size_t cap;
136                void *= CppAD::thread_alloc::get_memory(sizeof(double), cap);
137                ptrdbl_  = static_cast<double*>(v);
138                *ptrdbl_ = dbl;
139        }
140        base_alloc(const base_alloc& x)
141        {       size_t cap;
142                void *= CppAD::thread_alloc::get_memory(sizeof(double), cap);
143                ptrdbl_  = static_cast<double*>(v);
144                *ptrdbl_ = *x.ptrdbl_;
145        }
146        ~base_alloc(void)
147        {       void* v  = static_cast<void*>(ptrdbl_);
148                CppAD::thread_alloc::return_memory(v);
149        }
150        base_alloc operator-(void) const
151        {       base_alloc result;
152                *result.ptrdbl_ = - *ptrdbl_;
153                return result;
154        }
155        base_alloc operator+(void) const
156        {       return *this; }
157        void operator=(const base_alloc& x)
158        {       *ptrdbl_ = *x.ptrdbl_; }
159        BASE_ALLOC_ASSIGN_OPERATOR(+=)
160        BASE_ALLOC_ASSIGN_OPERATOR(-=)
161        BASE_ALLOC_ASSIGN_OPERATOR(*=)
162        BASE_ALLOC_ASSIGN_OPERATOR(/=)
163        BASE_ALLOC_BINARY_OPERATOR(+)
164        BASE_ALLOC_BINARY_OPERATOR(-)
165        BASE_ALLOC_BINARY_OPERATOR(*)
166        BASE_ALLOC_BINARY_OPERATOR(/)
167        BASE_ALLOC_BOOL_OPERATOR(==)
168        BASE_ALLOC_BOOL_OPERATOR(!=)
169        // The <= operator is not necessary for the base type requirements
170        // (needed so we can use NearEqual with base_alloc arguments).
171        BASE_ALLOC_BOOL_OPERATOR(<=)
172};
173/* $$
174
175$head CondExpOp$$
176The type $code base_alloc$$ does not use $cref CondExp$$ operations.
177Hence its $code CondExpOp$$ function is defined by
178$codep */
179namespace CppAD {
180        inline base_alloc CondExpOp(
181                enum CompareOp     cop          ,
182                const base_alloc&       left         ,
183                const base_alloc&       right        ,
184                const base_alloc&       exp_if_true  ,
185                const base_alloc&       exp_if_false )
186        {       // not used
187                assert(false);
188
189                // to void compiler error
190                return base_alloc();
191        }
192}
193/* $$
194
195$head CondExpRel$$
196The $cref/CPPAD_COND_EXP_REL/base_cond_exp/CondExpRel/$$ macro invocation
197$codep */
198namespace CppAD {
199        CPPAD_COND_EXP_REL(base_alloc)
200}
201/* $$
202uses $code CondExpOp$$ above to
203define $codei%CondExp%Rel%$$ for $code base_alloc$$ arguments
204and $icode%Rel%$$ equal to
205$code Lt$$, $code Le$$, $code Eq$$, $code Ge$$, and $code Gt$$.
206
207$head EqualOpSeq$$
208The type $code base_alloc$$ is simple (in this respect) and so we define
209$codep */
210namespace CppAD {
211        inline bool EqualOpSeq(const base_alloc& x, const base_alloc& y)
212        {       return *x.ptrdbl_ == *y.ptrdbl_; }
213}
214/* $$
215
216$head Identical$$
217The type $code base_alloc$$ is simple (in this respect) and so we define
218$codep */
219namespace CppAD {
220        inline bool IdenticalPar(const base_alloc& x)
221        {       return true; }
222        inline bool IdenticalZero(const base_alloc& x)
223        {       return (*x.ptrdbl_ == 0.0); }
224        inline bool IdenticalOne(const base_alloc& x)
225        {       return (*x.ptrdbl_ == 1.0); }
226        inline bool IdenticalEqualPar(const base_alloc& x, const base_alloc& y)
227        {       return (*x.ptrdbl_ == *y.ptrdbl_); }
228}
229/* $$
230
231$head Output Operator$$
232$codep */
233namespace CppAD {
234        std::ostream& operator << (std::ostream &os, const base_alloc& x)
235        {       os << *x.ptrdbl_;
236                return os;
237        }
238}
239/* $$
240
241$head Integer$$
242$codep */
243namespace CppAD {
244        inline int Integer(const base_alloc& x)
245        {       return static_cast<int>(*x.ptrdbl_); }
246}
247/* $$
248
249$head Ordered$$
250The $code base_alloc$$ type supports ordered comparisons
251$codep */
252namespace CppAD {
253        inline bool GreaterThanZero(const base_alloc& x)
254        {       return *x.ptrdbl_ > 0.0; }
255        inline bool GreaterThanOrZero(const base_alloc& x)
256        {       return *x.ptrdbl_ >= 0.0; }
257        inline bool LessThanZero(const base_alloc& x)
258        {       return *x.ptrdbl_ < 0.0; }
259        inline bool LessThanOrZero(const base_alloc& x)
260        {       return *x.ptrdbl_ <= 0.f; }
261        inline bool abs_geq(const base_alloc& x, const base_alloc& y)
262        {       return std::fabs(*x.ptrdbl_) >= std::fabs(*y.ptrdbl_); }
263}
264/* $$
265
266$head Unary Standard Math$$
267The macro
268$cref/CPPAD_STANDARD_MATH_UNARY/base_std_math/CPPAD_STANDARD_MATH_UNARY/$$
269would not work with the type $code base_alloc$$ so we define
270a special macro for this type:
271$codep */
272# define BASE_ALLOC_STD_MATH(fun) \
273        inline base_alloc fun (const base_alloc& x) \
274        { return   std::fun(*x.ptrdbl_); }
275/* $$
276The following invocations of the macro above define the
277$cref/unary standard math/base_std_math/Unary Standard Math/$$ functions
278(except for $code abs$$):
279$codep */
280namespace CppAD {
281        BASE_ALLOC_STD_MATH(acos)
282        BASE_ALLOC_STD_MATH(asin)
283        BASE_ALLOC_STD_MATH(atan)
284        BASE_ALLOC_STD_MATH(cos)
285        BASE_ALLOC_STD_MATH(cosh)
286        BASE_ALLOC_STD_MATH(exp)
287        BASE_ALLOC_STD_MATH(log)
288        BASE_ALLOC_STD_MATH(log10)
289        BASE_ALLOC_STD_MATH(sin)
290        BASE_ALLOC_STD_MATH(sinh)
291        BASE_ALLOC_STD_MATH(sqrt)
292        BASE_ALLOC_STD_MATH(tan)
293        BASE_ALLOC_STD_MATH(tanh)
294}
295/* $$
296The absolute value function is special because its $code std$$ name is
297$code fabs$$
298$codep */
299namespace CppAD {
300        inline base_alloc abs(const base_alloc& x)
301        {       return std::fabs(*x.ptrdbl_); }
302}
303/* $$
304
305$head erf, asinh, acosh$$
306The following defines the
307$cref/erf, asinh, acosh/base_std_math/erf, asinh, acosh/$$ functions
308required by $code AD<base_alloc>$$:
309$codep */
310# if CPPAD_COMPILER_HAS_ERF
311        BASE_ALLOC_STD_MATH(erf)
312# endif
313# if CPPAD_COMPILER_HAS_ACOSH
314        BASE_ALLOC_STD_MATH(acosh)
315# endif
316# if CPPAD_COMPILER_HAS_ASINH
317        BASE_ALLOC_STD_MATH(asinh)
318# endif
319/* $$
320
321$head sign$$
322The following defines the $code CppAD::sign$$ function that
323is required to use $code AD<base_alloc>$$:
324$codep */
325namespace CppAD {
326        inline base_alloc sign(const base_alloc& x)
327        {       if( *x.ptrdbl_ > 0.0 )
328                        return 1.0;
329                if( *x.ptrdbl_ == 0.0 )
330                        return 0.0;
331                return -1.0;
332        }
333}
334/* $$
335
336$head pow $$
337The following defines a $code CppAD::pow$$ function that
338is required to use $code AD<base_alloc>$$:
339$codep */
340namespace CppAD {
341        inline base_alloc pow(const base_alloc& x, const base_alloc& y)
342        { return std::pow(*x.ptrdbl_, *y.ptrdbl_); }
343}
344/* $$
345
346$head limits$$
347$codep */
348namespace CppAD {
349        template <>
350        class numeric_limits<base_alloc> {
351        public:
352                // machine epsilon
353                static base_alloc epsilon(void)
354                {       return std::numeric_limits<base_alloc>::epsilon(); }
355                // minimum positive normalized value
356                static base_alloc min(void)
357                {       return std::numeric_limits<base_alloc>::min(); }
358                // maximum finite value
359                static base_alloc max(void)
360                {       return std::numeric_limits<base_alloc>::max(); }
361        };
362        // deprecated machine epsilon
363        template <>
364        inline base_alloc epsilon<base_alloc>(void)
365        {       return numeric_limits<base_alloc>::epsilon(); }
366}
367/* $$
368
369$end
370*/
371# endif
Note: See TracBrowser for help on using the repository browser.