fun_from_json_xam.cpp#

View page source

C++: from_json: Example and Test#

bool from_json_xam(void) {
   using cppad_py::a_double;
   using cppad_py::vec_double;
   using cppad_py::vec_a_double;
   using cppad_py::d_fun;
   using cppad_py::a_fun;
   //
   // initialize return variable
   bool ok = true;
   // ----------------------------------------------------------------------
   // AD graph repersentation of f(x) = sin(x) / cos(x)
   //
   // node_1 : x[0]
   // node_2 : sin( x[0] )
   // node_3 : cos( x[0] )
   // node_4 : sin( x[0] ) / cos( x[0] )
   // y[0]   = sin( x[0] ) / cos( x[0] )
   // use single quote to avodi having to escape double quote
   std::string json =
      "{\n"
      "   'function_name' : 'tangent function',\n"
      "   'op_define_vec' : [ 3, [\n"
      "       { 'op_code':1, 'name':'sin', 'n_arg':1 } ,\n"
      "       { 'op_code':2, 'name':'cos', 'n_arg':1 } ,\n"
      "       { 'op_code':3, 'name':'div', 'n_arg':2 } ]\n"
      "   ],\n"
      "   'n_dynamic_ind'  : 0,\n"
      "   'n_variable_ind' : 1,\n"
      "   'constant_vec'   : [ 0, [ ] ],\n"
      "   'op_usage_vec'   : [ 3, [\n"
      "       [ 1, 1 ]   ,\n"  // node_2: sin(x[0])
      "       [ 2, 1 ]   ,\n"  // node_3: cos(x[0])
      "       [ 3, 2, 3] ]\n"  // node_4: sin(x[0]) / cos(x[0])
      "   ],\n"
      "   'dependent_vec' : [ 1, [4] ]\n"
      "}\n";
   // convert the single quote to double quote
   for(size_t i = 0; i < json.size(); ++i)
      if( json[i] == '\'' )
         json[i] = '"';
   //
   // convert json to a fucntion object
   vec_a_double ax(0), ay(0); // construct an empty function
   d_fun f(ax, ay);
   f.from_json(json);
   //
   // compute y = f(x)
   vec_double x(1), y(1);
   x[0]  = 1.0;
   y     = f.forward(0, x);
   //
   // check the function value
   double eps99     = std::numeric_limits<double>::epsilon();
   double check     = std::tan(x[0]);
   double rel_error = y[0] / check - 1.0;
   ok              &= std::fabs( rel_error ) < eps99;
   //
   return ok;
}