1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-24 Bradley M. Bell
// ----------------------------------------------------------------------------
/*
{xrst_begin stack_machine.cpp}
Example Differentiating a Stack Machine Interpreter
###################################################
{xrst_literal
// BEGIN C++
// END C++
}
{xrst_end stack_machine.cpp}
*/
// BEGIN C++
# include <cstring>
# include <cstddef>
# include <cstdlib>
# include <cctype>
# include <cassert>
# include <stack>
# include <cppad/cppad.hpp>
namespace {
// Begin empty namespace ------------------------------------------------
bool is_number( const std::string &s )
{ char ch = s[0];
bool number = (std::strchr("0123456789.", ch) != 0);
return number;
}
bool is_binary( const std::string &s )
{ char ch = s[0];
bool binary = (strchr("+-*/.", ch) != 0);
return binary;
}
bool is_variable( const std::string &s )
{ char ch = s[0];
bool variable = ('a' <= ch) && (ch <= 'z');
return variable;
}
void StackMachine(
std::stack< std::string > &token_stack ,
CppAD::vector< CppAD::AD<double> > &variable )
{ using std::string;
using std::stack;
using CppAD::AD;
stack< AD<double> > value_stack;
string token;
AD<double> value_one;
AD<double> value_two;
while( ! token_stack.empty() )
{ string s = token_stack.top();
token_stack.pop();
if( is_number(s) )
{ value_one = std::atof( s.c_str() );
value_stack.push( value_one );
}
else if( is_variable(s) )
{ value_one = variable[ size_t(s[0]) - size_t('a') ];
value_stack.push( value_one );
}
else if( is_binary(s) )
{ assert( value_stack.size() >= 2 );
value_one = value_stack.top();
value_stack.pop();
value_two = value_stack.top();
value_stack.pop();
switch( s[0] )
{
case '+':
value_stack.push(value_one + value_two);
break;
case '-':
value_stack.push(value_one - value_two);
break;
case '*':
value_stack.push(value_one * value_two);
break;
case '/':
value_stack.push(value_one / value_two);
break;
default:
assert(0);
}
}
else if( s[0] == '=' )
{ assert( value_stack.size() >= 1 );
assert( token_stack.size() >= 1 );
//
s = token_stack.top();
token_stack.pop();
//
assert( is_variable( s ) );
value_one = value_stack.top();
value_stack.pop();
//
variable[ size_t(s[0]) - size_t('a') ] = value_one;
}
else assert(0);
}
return;
}
// End empty namespace -------------------------------------------------------
}
bool StackMachine(void)
{ bool ok = true;
using std::string;
using std::stack;
using CppAD::AD;
using CppAD::NearEqual;
double eps99 = 99.0 * std::numeric_limits<double>::epsilon();
using CppAD::vector;
// The users program in that stack machine language
const char *program[] = {
"1.0", "a", "+", "=", "b", // b = a + 1
"2.0", "b", "*", "=", "c", // c = b * 2
"3.0", "c", "-", "=", "d", // d = c - 3
"4.0", "d", "/", "=", "e" // e = d / 4
};
size_t n_program = sizeof( program ) / sizeof( program[0] );
// put the program in the token stack
stack< string > token_stack;
size_t i = n_program;
while(i--)
token_stack.push( program[i] );
// domain space vector
size_t n = 1;
vector< AD<double> > X(n);
X[0] = 0.;
// declare independent variables and start tape recording
CppAD::Independent(X);
// x[0] corresponds to a in the stack machine
vector< AD<double> > variable(26);
variable[0] = X[0];
// calculate the resutls of the program
StackMachine( token_stack , variable);
// range space vector
size_t m = 4;
vector< AD<double> > Y(m);
Y[0] = variable[1]; // b = a + 1
Y[1] = variable[2]; // c = (a + 1) * 2
Y[2] = variable[3]; // d = (a + 1) * 2 - 3
Y[3] = variable[4]; // e = ( (a + 1) * 2 - 3 ) / 4
// create f : X -> Y and stop tape recording
CppAD::ADFun<double> f(X, Y);
// use forward mode to evaluate function at different argument value
size_t p = 0;
vector<double> x(n);
vector<double> y(m);
x[0] = 1.;
y = f.Forward(p, x);
// check function values
ok &= (y[0] == x[0] + 1.);
ok &= (y[1] == (x[0] + 1.) * 2.);
ok &= (y[2] == (x[0] + 1.) * 2. - 3.);
ok &= (y[3] == ( (x[0] + 1.) * 2. - 3.) / 4.);
// Use forward mode (because x is shorter than y) to calculate Jacobian
p = 1;
vector<double> dx(n);
vector<double> dy(m);
dx[0] = 1.;
dy = f.Forward(p, dx);
ok &= NearEqual(dy[0], 1., eps99, eps99);
ok &= NearEqual(dy[1], 2., eps99, eps99);
ok &= NearEqual(dy[2], 2., eps99, eps99);
ok &= NearEqual(dy[3], .5, eps99, eps99);
// Use Jacobian routine (which automatically decides which mode to use)
dy = f.Jacobian(x);
ok &= NearEqual(dy[0], 1., eps99, eps99);
ok &= NearEqual(dy[1], 2., eps99, eps99);
ok &= NearEqual(dy[2], 2., eps99, eps99);
ok &= NearEqual(dy[3], .5, eps99, eps99);
return ok;
}
// END C++
|