Codebase list cppad / e539058
master: json: add atanh_json_op unary operator. Brad Bell 4 years ago
8 changed file(s) with 156 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
1717 add_op.cpp
1818 asinh_op.cpp
1919 asin_op.cpp
20 atanh_op.cpp
2021 atan_op.cpp
2122 atom_op.cpp
2223 cosh_op.cpp
0 /* --------------------------------------------------------------------------
1 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-19 Bradley M. Bell
2
3 CppAD is distributed under the terms of the
4 Eclipse Public License Version 2.0.
5
6 This Source Code may also be made available under the following
7 Secondary License when the conditions for such availability set forth
8 in the Eclipse Public License, Version 2.0 are satisfied:
9 GNU General Public License, Version 2.0 or later.
10 ---------------------------------------------------------------------------- */
11 /*
12 $begin json_atanh_op.cpp$$
13 $spell
14 Json
15 $$
16
17 $section Using The Json Addition Operator: Example and Test$$
18
19 $head Source Code$$
20 $srcfile%example/json/atanh_op.cpp%0%// BEGIN C++%// END C++%1%$$
21
22 $end
23 */
24 // BEGIN C++
25 # include <cppad/cppad.hpp>
26
27 bool atanh_op(void)
28 { bool ok = true;
29 using CppAD::vector;
30 using CppAD::AD;
31 double eps99 = 99.0 * std::numeric_limits<double>::epsilon();
32 //
33 // AD graph example
34 // node_1 : p[0]
35 // node_2 : x[0]
36 // node_3 : c[0]
37 // node_4 : atanh(p[0])
38 // node_5 : atanh(x[0])
39 // node_6 : atanh(c[0])
40 // node_7 : atanh(p[0]) + atanh(x[0]) + atanh(c[0])
41 // y[0] = atanh(p[0]) + atanh(x[0]) + atanh(c[0])
42 // use single quote to avoid having to escape double quote
43 std::string graph =
44 "{\n"
45 " 'function_name' : 'atanh_op example',\n"
46 " 'op_define_vec' : [ 2, [\n"
47 " { 'op_code':1, 'name':'atanh', 'n_arg':1 } ,\n"
48 " { 'op_code':2, 'name':'sum' } ]\n"
49 " ],\n"
50 " 'n_dynamic_ind' : 1,\n"
51 " 'n_independent' : 1,\n"
52 " 'string_vec' : 0, [ ],\n"
53 " 'constant_vec' : 1, [ 0.3 ],\n" // c[0]
54 " 'op_usage_vec' : 4, [\n"
55 " [ 1, 1] ,\n" // atanh(p0)
56 " [ 1, 2] ,\n" // atanh(x0)
57 " [ 1, 3] ,\n" // atanh(c0)
58 " [ 2, 1, 3, [4, 5, 6] ] ]\n" // atanh(p0)+atanh(x0)+atanh(c0)
59 " ,\n"
60 " 'dependent_vec' : 1, [7]\n"
61 "}\n";
62 // Convert the single quote to double quote
63 for(size_t i = 0; i < graph.size(); ++i)
64 if( graph[i] == '\'' ) graph[i] = '"';
65 //
66 // f(x, p) = atanh(p0) + atanh(x0) + atanh(c0)
67 CppAD::ADFun<double> f;
68 f.from_json(graph);
69 ok &= f.Domain() == 1;
70 ok &= f.Range() == 1;
71 ok &= f.size_dyn_ind() == 1;
72 //
73 // value of constant in function
74 vector<double> c(1);
75 c[0] = 0.3;
76 //
77 // set independent variables and parameters
78 vector<double> p(1), x(1);
79 p[0] = -0.1;
80 x[0] = 0.2;
81 //
82 // compute y = f(x, p)
83 f.new_dynamic(p);
84 vector<double> y = f.Forward(0, x);
85 //
86 // check result
87 double check = CppAD::atanh(p[0]) + CppAD::atanh(x[0]) + CppAD::atanh(c[0]);
88 ok &= CppAD::NearEqual(y[0], check, eps99, eps99);
89 //
90 // Convert to Json graph and back again
91 graph = f.to_json();
92 f.from_json(graph);
93 //
94 // compute y = f(x, p)
95 f.new_dynamic(p);
96 y = f.Forward(0, x);
97 //
98 // check result
99 ok &= CppAD::NearEqual(y[0], check, eps99, eps99);
100 //
101 return ok;
102 }
103 // END C++
3232 extern bool add_op(void);
3333 extern bool asinh_op(void);
3434 extern bool asin_op(void);
35 extern bool atanh_op(void);
3536 extern bool atan_op(void);
3637 extern bool atom_op(void);
3738 extern bool cosh_op(void);
9293 # if CPPAD_USE_CPLUSPLUS_2011
9394 Run( acosh_op, "acosh_op" );
9495 Run( asinh_op, "asinh_op" );
96 Run( atanh_op, "atanh_op" );
9597 # endif
9698 // END_SORT_THIS_LINE_MINUS_2
9799
428428 CPPAD_ASSERT_UNKNOWN( NumArg(local::AsinhOp) == 1 );
429429 break;
430430
431 case local::json::atanh_json_op:
432 i_result = rec.PutOp(local::AtanhOp);
433 rec.PutArg( arg[0] );
434 CPPAD_ASSERT_UNKNOWN( NumArg(local::AtanhOp) == 1 );
435 break;
436
431437 case local::json::acos_json_op:
432438 i_result = rec.PutOp(local::AcosOp);
433439 rec.PutArg( arg[0] );
527533 CPPAD_ASSERT_UNKNOWN( isnan( parameter[i_result] ) );
528534 break;
529535
536 case local::json::atanh_json_op:
537 i_result = rec.put_dyn_par(nan, local::atanh_dyn, arg[0] );
538 CPPAD_ASSERT_UNKNOWN( isnan( parameter[i_result] ) );
539 break;
540
530541 case local::json::acos_json_op:
531542 i_result = rec.put_dyn_par(nan, local::acos_dyn, arg[0] );
532543 CPPAD_ASSERT_UNKNOWN( isnan( parameter[i_result] ) );
612623
613624 case local::json::asinh_json_op:
614625 result = CppAD::asinh( parameter[ arg[0] ] );
626 i_result = rec.put_con_par(result);
627 CPPAD_ASSERT_UNKNOWN( parameter[i_result] == result );
628 break;
629
630 case local::json::atanh_json_op:
631 result = CppAD::atanh( parameter[ arg[0] ] );
615632 i_result = rec.put_con_par(result);
616633 CPPAD_ASSERT_UNKNOWN( parameter[i_result] == result );
617634 break;
1010 -------------------------------------------------------------------------- */
1111 $begin json_op_define$$
1212 $spell
13 atanh
1314 asinh
1415 acosh
1516 acos
6869
6970 $subhead C++11$$
7071 The following unary operators require C++11 or higher to work with CppAD:
71 $code acosh$$, asinh$$.
72 $code acosh$$, $code asinh$$, $code atanh$$.
7273
7374 $subhead abs$$
7475 The result node value is the absolute value of the argument.
7778 $subhead asinh$$
7879 The result node value is the inverse hyperbolic sine of the argument.
7980 The file $cref json_asinh_op.cpp$$ is an example and test of this operation.
81
82 $subhead atanh$$
83 The result node value is the inverse hyperbolic sine of the argument.
84 The file $cref json_atanh_op.cpp$$ is an example and test of this operation.
8085
8186 $subhead acosh$$
8287 The result node value is the inverse hyperbolic cosine of the argument.
255260 example/json/tan_op.cpp%
256261 example/json/acosh_op.cpp%
257262 example/json/asinh_op.cpp%
263 example/json/atanh_op.cpp%
258264 example/json/add_op.cpp%
259265 example/json/atom_op.cpp%
260266 example/json/div_op.cpp%
134134
135135 case local::asinh_dyn:
136136 is_json_op_used[local::json::asinh_json_op] = true;
137 break;
138
139 case local::atanh_dyn:
140 is_json_op_used[local::json::atanh_json_op] = true;
137141 break;
138142
139143 case local::acos_dyn:
267271
268272 case local::AsinhOp:
269273 is_json_op_used[local::json::asinh_json_op] = true;
274 ++n_usage;
275 break;
276
277 case local::AtanhOp:
278 is_json_op_used[local::json::atanh_json_op] = true;
270279 ++n_usage;
271280 break;
272281
503512 op_code = graph_code[ local::json::asinh_json_op ];
504513 break;
505514
515 case local::atanh_dyn:
516 op_code = graph_code[ local::json::atanh_json_op ];
517 break;
518
506519 case local::acos_dyn:
507520 op_code = graph_code[ local::json::acos_json_op ];
508521 break;
641654 is_var[0] = true;
642655 break;
643656
657 case local::AtanhOp:
658 fixed_n_arg = 1;
659 is_var[0] = true;
660 break;
661
644662 case local::AcosOp:
645663 fixed_n_arg = 1;
646664 is_var[0] = true;
756774
757775 case local::AsinhOp:
758776 op_code = graph_code[ local::json::asinh_json_op ];
777 break;
778
779 case local::AtanhOp:
780 op_code = graph_code[ local::json::atanh_json_op ];
759781 break;
760782
761783 case local::AcosOp:
1111
1212 $begin whats_new_19$$
1313 $spell
14 atanh
1415 asinh
1516 acosh
1617 acos
5859 $code abs$$,
5960 $code acosh$$,
6061 $code asinh$$,
62 $code atanh$$,
6163 $code acos$$,
6264 $code asin$$,
6365 $code atan$$,
153153 $rref json_add_op.cpp$$
154154 $rref json_asinh_op.cpp$$
155155 $rref json_asin_op.cpp$$
156 $rref json_atanh_op.cpp$$
156157 $rref json_atan_op.cpp$$
157158 $rref json_atom_op.cpp$$
158159 $rref json_cosh_op.cpp$$