json_get_started.cpp

View page source

Json Get Started: Example and Test

Notation

Notation

Description

size

p

vector of dynamic parameters

1

x

vector of independent variables

1

c

vector of constants

1

y

vector of dependent variables

1

Node Table

index

value

1

p[0]

2

x[0]

3

c[0]

4

sin(p[0])

5

sin(x[0])

6

sin(c[0])

7

sin(p[0]) + sin(x[0]) + sin(c[0])

y[0]

sin(p[0]) + sin(x[0]) + sin(c[0])

Include

Include the CppAD core functions:

# include <cppad/cppad.hpp>

Syntax

ok = get_started ()

bool get_started(void)
{

Setup

   bool ok = true;
   using CppAD::vector;
   using CppAD::AD;
   double eps99 = 99.0 * std::numeric_limits<double>::epsilon();

Function

Begin Function

See function :

   std::string json =
      "{\n"
      "   'function_name'  : 'get_started example',\n"

Begin op_define_vec

see op_define_vec :

      "   'op_define_vec'  : [ 2, [\n"

Define Unary

see Unary Operators :

      "       { 'op_code':1, 'name':'sin', 'n_arg':1 } ,\n"

Define Sum

see sum :

      "       { 'op_code':2, 'name':'sum'            } ]\n"

End op_define_vec

      "   ],\n"

n_dynamic_ind

see n_dynamic_ind :

      "   'n_dynamic_ind'  : 1,\n"

n_variable_ind

see n_variable_ind :

      "   'n_variable_ind' : 1,\n"

constant_vec

see constant_vec :

      "   'constant_vec'   : [ 1, [ -0.1 ] ],\n" // c[0]

Begin op_usage_vec

see op_usage_vec :

      "   'op_usage_vec'   : [ 4, [\n"

op_usage

see op_usage with n_arg In Definition :

      "       [ 1, 1]                ,\n" // sin(p[0])
      "       [ 1, 2]                ,\n" // sin(x[0])
      "       [ 1, 3]                ,\n" // sin(c[0])

see op_usage with n_arg Not In Definition :

      "       [ 2, 1, 3, [4, 5, 6] ] ]\n" // sin(p[0])+sin(x[0])+sin(c[0])

End op_usage_vec

      "   ],\n"

dependent_vec

see dependent_var

      "   'dependent_vec' : [ 1, [7] ] \n"

End Function

      "}\n";

Convert Single to Double Quotes

   for(size_t i = 0; i < json.size(); ++i)
      if( json[i] == '\'' ) json[i] = '"';

double f(x, p)

   CppAD::ADFun<double> f;
   f.from_json(json);

Check f(x, p)

   vector<double> c(1), p(1), x(1), y(1);
   c[0] = -0.1; // must match value in graph
   p[0] = 0.2;  // can be any value
   x[0] = 0.3;  // can be any value
   //
   // compute y = f(x, p)
   f.new_dynamic(p);
   y = f.Forward(0, x);
   //
   // f(x, p) = sin(p_0) + sin(x_0) + sin(c_0)
   double check = std::sin(p[0]) + std::sin(x[0]) + std::sin(c[0]);
   ok &= CppAD::NearEqual(y[0], check, eps99, eps99);

AD<double> f(x, p)

   CppAD::ADFun< AD<double>, double > af( f.base2ad() );

Evaluate Derivative

   // set independent variables and parameters
   vector< AD<double> > ap(1), ax(1);
   ap[0] = 0.2;
   ax[0] = 0.3;
   //
   // record computation of z = d/dx f(x, p)
   CppAD::Independent(ax, ap);
   af.new_dynamic(ap);
   vector< AD<double> > az = af.Jacobian(ax);

double g(x, p) = d/dx f(x, p)

   CppAD::ADFun<double> g(ax, az);

Convert to Json and Back

   json = g.to_json();
   g.from_json(json);

Check g(x, p)

   c[0] = -0.1; // must match value in graph
   p[0] = 0.3;  // can be any value
   x[0] = 0.4;  // can be any value
   //
   // compute z = g(x, p)
   g.new_dynamic(p);
   vector<double> z = g.Forward(0, x);
   //
   // g(x, p) = d/dx f(x, p) = d/dx sin(x) = cos(x)
   check = std::cos(x[0]);
   ok &= CppAD::NearEqual(z[0], check, eps99, eps99);
   //
   return ok;
}