source: trunk/bug/opt_atomic.sh @ 3366

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

opt_atomic.sh: simplify example source code.

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 2.8 KB
Line 
1#! /bin/bash -e
2# $Id: opt_atomic.sh 3366 2014-09-27 09:43:15Z bradbell $
3# -----------------------------------------------------------------------------
4# CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-13 Bradley M. Bell
5#
6# CppAD is distributed under multiple licenses. This distribution is under
7# the terms of the
8#                     Eclipse Public License Version 1.0.
9#
10# A copy of this license is included in the COPYING file of this distribution.
11# Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
12# -----------------------------------------------------------------------------
13cat << EOF
14Assert during optimization of an atomic function.
15EOF
16cat << EOF > bug.$$
17# include <cppad/cppad.hpp>
18
19using CppAD::vector;
20using CppAD::AD;
21using CppAD::ADFun;
22
23class atomic_reciprocal : public CppAD::atomic_base<double> {
24public:
25        atomic_reciprocal(const std::string& name) :
26        CppAD::atomic_base<double>(name)
27        { }
28private:
29        virtual bool forward(
30                size_t                    p ,
31                size_t                    q ,
32                const vector<bool>&      vx ,
33                      vector<bool>&      vy ,
34                const vector<double>&    tx ,
35                      vector<double>&    ty
36        )
37        {       size_t n = tx.size() / (q + 1);
38                size_t m = ty.size() / (q + 1);
39                assert( n == 1 );
40                assert( m == 1 );
41                assert( p <= q );
42
43                // return flag
44                bool ok = true;
45
46                // check for defining variable information
47                // This case must always be implemented
48                if( vx.size() > 0 )
49                        vy[0] = vx[0];
50
51                // Order zero forward mode.
52                // This case must always be implemented
53                // y^0 = f( x^0 ) = 1 / x^0
54                double f = 1. / tx[0];
55                if( p <= 0 )
56                        ty[0] = f;
57                if( q <= 0 )
58                        return ok;
59                //
60                assert( vx.size() == 0 );
61                ok = false;
62                return ok;
63        }
64        // reverse Jacobian set sparsity routine called by CppAD
65        virtual bool rev_sparse_jac(
66                size_t                                p  ,
67                const vector< std::set<size_t> >&     rt ,
68                      vector< std::set<size_t> >&     st )
69        {       // This function needed if using RevSparseJac or optimize
70                // with afun.option( CppAD::atomic_base<double>::set_sparsity_enum )
71                size_t n = st.size();
72                size_t m = rt.size();
73                assert( n == 1 );
74                assert( m == 1 );
75
76                // sparsity for S(x)^T = f'(x)^T * R^T is same as sparsity for R^T
77                st[0] = rt[0];
78
79                return true;
80        }
81};
82
83
84int main(){
85        // Create the atomic function
86        atomic_reciprocal afun("atomic_reciprocal");
87
88        vector< AD<double> > ax(1), ay(1);
89        ax[0]=1;
90        Independent(ax);
91        afun(ax, ay);
92        AD<double> az = 0.0; 
93        ay[0] = CondExpGt(ay[0], az, az, ay[0]);
94        ADFun<double> F(ax, ay);
95 
96        F.optimize();
97}
98EOF
99# -----------------------------------------------------------------------------
100if [ ! -e build ]
101then
102        mkdir build
103fi
104cd build
105echo "$0"
106name=`echo $0 | sed -e 's|.*/||' -e 's|\..*||'`
107mv ../bug.$$ $name.cpp
108echo "g++ -I../.. --std=c++11 -g $name.cpp -o $name"
109g++ -I../.. --std=c++11 -g $name.cpp -o $name
110#
111echo "./$name"
112./$name
Note: See TracBrowser for help on using the repository browser.