master: json: add atanh_json_op unary operator.
Brad Bell
4 years ago
17 | 17 | add_op.cpp |
18 | 18 | asinh_op.cpp |
19 | 19 | asin_op.cpp |
20 | atanh_op.cpp | |
20 | 21 | atan_op.cpp |
21 | 22 | atom_op.cpp |
22 | 23 | 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++ |
32 | 32 | extern bool add_op(void); |
33 | 33 | extern bool asinh_op(void); |
34 | 34 | extern bool asin_op(void); |
35 | extern bool atanh_op(void); | |
35 | 36 | extern bool atan_op(void); |
36 | 37 | extern bool atom_op(void); |
37 | 38 | extern bool cosh_op(void); |
92 | 93 | # if CPPAD_USE_CPLUSPLUS_2011 |
93 | 94 | Run( acosh_op, "acosh_op" ); |
94 | 95 | Run( asinh_op, "asinh_op" ); |
96 | Run( atanh_op, "atanh_op" ); | |
95 | 97 | # endif |
96 | 98 | // END_SORT_THIS_LINE_MINUS_2 |
97 | 99 |
428 | 428 | CPPAD_ASSERT_UNKNOWN( NumArg(local::AsinhOp) == 1 ); |
429 | 429 | break; |
430 | 430 | |
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 | ||
431 | 437 | case local::json::acos_json_op: |
432 | 438 | i_result = rec.PutOp(local::AcosOp); |
433 | 439 | rec.PutArg( arg[0] ); |
527 | 533 | CPPAD_ASSERT_UNKNOWN( isnan( parameter[i_result] ) ); |
528 | 534 | break; |
529 | 535 | |
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 | ||
530 | 541 | case local::json::acos_json_op: |
531 | 542 | i_result = rec.put_dyn_par(nan, local::acos_dyn, arg[0] ); |
532 | 543 | CPPAD_ASSERT_UNKNOWN( isnan( parameter[i_result] ) ); |
612 | 623 | |
613 | 624 | case local::json::asinh_json_op: |
614 | 625 | 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] ] ); | |
615 | 632 | i_result = rec.put_con_par(result); |
616 | 633 | CPPAD_ASSERT_UNKNOWN( parameter[i_result] == result ); |
617 | 634 | break; |
10 | 10 | -------------------------------------------------------------------------- */ |
11 | 11 | $begin json_op_define$$ |
12 | 12 | $spell |
13 | atanh | |
13 | 14 | asinh |
14 | 15 | acosh |
15 | 16 | acos |
68 | 69 | |
69 | 70 | $subhead C++11$$ |
70 | 71 | The following unary operators require C++11 or higher to work with CppAD: |
71 | $code acosh$$, asinh$$. | |
72 | $code acosh$$, $code asinh$$, $code atanh$$. | |
72 | 73 | |
73 | 74 | $subhead abs$$ |
74 | 75 | The result node value is the absolute value of the argument. |
77 | 78 | $subhead asinh$$ |
78 | 79 | The result node value is the inverse hyperbolic sine of the argument. |
79 | 80 | 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. | |
80 | 85 | |
81 | 86 | $subhead acosh$$ |
82 | 87 | The result node value is the inverse hyperbolic cosine of the argument. |
255 | 260 | example/json/tan_op.cpp% |
256 | 261 | example/json/acosh_op.cpp% |
257 | 262 | example/json/asinh_op.cpp% |
263 | example/json/atanh_op.cpp% | |
258 | 264 | example/json/add_op.cpp% |
259 | 265 | example/json/atom_op.cpp% |
260 | 266 | example/json/div_op.cpp% |
134 | 134 | |
135 | 135 | case local::asinh_dyn: |
136 | 136 | 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; | |
137 | 141 | break; |
138 | 142 | |
139 | 143 | case local::acos_dyn: |
267 | 271 | |
268 | 272 | case local::AsinhOp: |
269 | 273 | 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; | |
270 | 279 | ++n_usage; |
271 | 280 | break; |
272 | 281 | |
503 | 512 | op_code = graph_code[ local::json::asinh_json_op ]; |
504 | 513 | break; |
505 | 514 | |
515 | case local::atanh_dyn: | |
516 | op_code = graph_code[ local::json::atanh_json_op ]; | |
517 | break; | |
518 | ||
506 | 519 | case local::acos_dyn: |
507 | 520 | op_code = graph_code[ local::json::acos_json_op ]; |
508 | 521 | break; |
641 | 654 | is_var[0] = true; |
642 | 655 | break; |
643 | 656 | |
657 | case local::AtanhOp: | |
658 | fixed_n_arg = 1; | |
659 | is_var[0] = true; | |
660 | break; | |
661 | ||
644 | 662 | case local::AcosOp: |
645 | 663 | fixed_n_arg = 1; |
646 | 664 | is_var[0] = true; |
756 | 774 | |
757 | 775 | case local::AsinhOp: |
758 | 776 | 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 ]; | |
759 | 781 | break; |
760 | 782 | |
761 | 783 | case local::AcosOp: |