source: trunk/cppad/local/sign_op.hpp @ 3301

Last change on this file since 3301 was 3301, checked in by bradbell, 6 years ago

merge in multiple forward direcitons from branches/forward_dir

  • Property svn:keywords set to Id
File size: 3.8 KB
Line 
1/* $Id: sign_op.hpp 3301 2014-05-24 05:20:21Z bradbell $ */
2# ifndef CPPAD_SIGN_OP_INCLUDED
3# define CPPAD_SIGN_OP_INCLUDED
4
5/* --------------------------------------------------------------------------
6CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-14 Bradley M. Bell
7
8CppAD is distributed under multiple licenses. This distribution is under
9the terms of the
10                    Eclipse Public License Version 1.0.
11
12A copy of this license is included in the COPYING file of this distribution.
13Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
14-------------------------------------------------------------------------- */
15
16
17namespace CppAD { // BEGIN_CPPAD_NAMESPACE
18/*!
19\file sign_op.hpp
20Forward and reverse mode calculations for z = sign(x).
21*/
22
23/*!
24Compute forward mode Taylor coefficient for result of op = SignOp.
25
26The C++ source code corresponding to this operation is
27\verbatim
28        z = sign(x)
29\endverbatim
30
31\copydetails forward_unary1_op
32*/
33template <class Base>
34inline void forward_sign_op(
35        size_t p           ,
36        size_t q           ,
37        size_t i_z         ,
38        size_t i_x         ,
39        size_t cap_order   , 
40        Base*  taylor      )
41{
42        // check assumptions
43        CPPAD_ASSERT_UNKNOWN( NumArg(SignOp) == 1 );
44        CPPAD_ASSERT_UNKNOWN( NumRes(SignOp) == 1 );
45        CPPAD_ASSERT_UNKNOWN( i_x < i_z );
46        CPPAD_ASSERT_UNKNOWN( q < cap_order );
47        CPPAD_ASSERT_UNKNOWN( p <= q );
48
49        // Taylor coefficients corresponding to argument and result
50        Base* x = taylor + i_x * cap_order;
51        Base* z = taylor + i_z * cap_order;
52
53        if( p == 0 )
54        {       z[0] = sign(x[0]);
55                p++;
56        }
57        for(size_t j = p; j <= q; j++)
58                z[j] = Base(0.);
59}
60/*!
61Multiple direction forward mode Taylor coefficient for op = SignOp.
62
63The C++ source code corresponding to this operation is
64\verbatim
65        z = sign(x)
66\endverbatim
67
68\copydetails forward_unary1_op_dir
69*/
70template <class Base>
71inline void forward_sign_op_dir(
72        size_t q           ,
73        size_t r           ,
74        size_t i_z         ,
75        size_t i_x         ,
76        size_t cap_order   , 
77        Base*  taylor      )
78{
79        // check assumptions
80        CPPAD_ASSERT_UNKNOWN( NumArg(SignOp) == 1 );
81        CPPAD_ASSERT_UNKNOWN( NumRes(SignOp) == 1 );
82        CPPAD_ASSERT_UNKNOWN( i_x < i_z );
83        CPPAD_ASSERT_UNKNOWN( 0 < q );
84        CPPAD_ASSERT_UNKNOWN( q < cap_order );
85
86        // Taylor coefficients corresponding to argument and result
87        size_t num_taylor_per_var = (cap_order-1) * r + 1;
88        size_t m = (q - 1) * r + 1;
89        Base* z = taylor + i_z * num_taylor_per_var;
90
91        for(size_t ell = 0; ell < r; ell++)
92                z[m+ell] = Base(0.);
93}
94
95/*!
96Compute zero order forward mode Taylor coefficient for result of op = SignOp.
97
98The C++ source code corresponding to this operation is
99\verbatim
100        z = sign(x)
101\endverbatim
102
103\copydetails forward_unary1_op_0
104*/
105template <class Base>
106inline void forward_sign_op_0(
107        size_t i_z         ,
108        size_t i_x         ,
109        size_t cap_order   , 
110        Base*  taylor      )
111{
112
113        // check assumptions
114        CPPAD_ASSERT_UNKNOWN( NumArg(SignOp) == 1 );
115        CPPAD_ASSERT_UNKNOWN( NumRes(SignOp) == 1 );
116        CPPAD_ASSERT_UNKNOWN( i_x < i_z );
117        CPPAD_ASSERT_UNKNOWN( 0 < cap_order );
118
119        // Taylor coefficients corresponding to argument and result
120        Base x0 = *(taylor + i_x * cap_order);
121        Base* z = taylor + i_z * cap_order;
122
123        z[0] = sign(x0);
124}
125/*!
126Compute reverse mode partial derivatives for result of op = SignOp.
127
128The C++ source code corresponding to this operation is
129\verbatim
130        z = sign(x)
131\endverbatim
132
133\copydetails reverse_unary1_op
134*/
135
136template <class Base>
137inline void reverse_sign_op(
138        size_t      d            ,
139        size_t      i_z          ,
140        size_t      i_x          ,
141        size_t      cap_order    , 
142        const Base* taylor       ,
143        size_t      nc_partial   ,
144        Base*       partial      )
145{       
146        // check assumptions
147        CPPAD_ASSERT_UNKNOWN( NumArg(SignOp) == 1 );
148        CPPAD_ASSERT_UNKNOWN( NumRes(SignOp) == 1 );
149        CPPAD_ASSERT_UNKNOWN( i_x < i_z );
150        CPPAD_ASSERT_UNKNOWN( d < cap_order );
151        CPPAD_ASSERT_UNKNOWN( d < nc_partial );
152
153        // nothing to do because partials of sign are zero
154        return;
155}
156
157} // END_CPPAD_NAMESPACE
158# endif
Note: See TracBrowser for help on using the repository browser.