@@ -82,6 +82,21 @@ using add_mul_ret = double;
8282
8383#line 51 "pure2-autodiff-higher-order.cpp2"
8484 public: [[nodiscard]] static auto add_mul (cpp2::impl::in<double > x, cpp2::impl::in<double > y) -> add_mul_ret;
85+ using func_ret = double ;
86+
87+
88+ #line 55 "pure2-autodiff-higher-order.cpp2"
89+ public: [[nodiscard]] static auto func (cpp2::impl::in<double > x, cpp2::impl::in<double > y) -> func_ret;
90+ using func_call_ret = double ;
91+
92+
93+ #line 59 "pure2-autodiff-higher-order.cpp2"
94+ public: [[nodiscard]] static auto func_call (cpp2::impl::in<double > x, cpp2::impl::in<double > y) -> func_call_ret;
95+ using sin_call_ret = double ;
96+
97+
98+ #line 63 "pure2-autodiff-higher-order.cpp2"
99+ public: [[nodiscard]] static auto sin_call (cpp2::impl::in<double > x, cpp2::impl::in<double > y) -> sin_call_ret;
85100struct add_1_d_ret { double r; cpp2::taylor<double ,6 > r_d; };
86101
87102
@@ -131,17 +146,29 @@ struct add_mul_d_ret { double r; cpp2::taylor<double,6> r_d; };
131146
132147public: [[nodiscard]] static auto add_mul_d (cpp2::impl::in<double > x, cpp2::impl::in<cpp2::taylor<double ,6 >> x_d, cpp2::impl::in<double > y, cpp2::impl::in<cpp2::taylor<double ,6 >> y_d) -> add_mul_d_ret;
133148
149+ struct func_d_ret { double r; cpp2::taylor<double ,6 > r_d; };
150+
151+ public: [[nodiscard]] static auto func_d (cpp2::impl::in<double > x, cpp2::impl::in<cpp2::taylor<double ,6 >> x_d, cpp2::impl::in<double > y, cpp2::impl::in<cpp2::taylor<double ,6 >> y_d) -> func_d_ret;
152+
153+ struct func_call_d_ret { double r; cpp2::taylor<double ,6 > r_d; };
154+
155+ public: [[nodiscard]] static auto func_call_d (cpp2::impl::in<double > x, cpp2::impl::in<cpp2::taylor<double ,6 >> x_d, cpp2::impl::in<double > y, cpp2::impl::in<cpp2::taylor<double ,6 >> y_d) -> func_call_d_ret;
156+
157+ struct sin_call_d_ret { double r; cpp2::taylor<double ,6 > r_d; };
158+
159+ public: [[nodiscard]] static auto sin_call_d (cpp2::impl::in<double > x, cpp2::impl::in<cpp2::taylor<double ,6 >> x_d, cpp2::impl::in<double > y, cpp2::impl::in<cpp2::taylor<double ,6 >> y_d) -> sin_call_d_ret;
160+
134161 public: ad_test() = default ;
135162 public: ad_test(ad_test const &) = delete ; /* No 'that' constructor, suppress copy */
136163 public: auto operator =(ad_test const &) -> void = delete ;
137164
138165
139- #line 54 "pure2-autodiff-higher-order.cpp2"
166+ #line 66 "pure2-autodiff-higher-order.cpp2"
140167};
141168
142169auto write_output (cpp2::impl::in<std::string> func, cpp2::impl::in<double > x, cpp2::impl::in<ad_type> x_d, cpp2::impl::in<double > y, cpp2::impl::in<ad_type> y_d, auto const & ret) -> void;
143170
144- #line 65 "pure2-autodiff-higher-order.cpp2"
171+ #line 77 "pure2-autodiff-higher-order.cpp2"
145172auto main () -> int;
146173
147174// === Cpp2 function definitions =================================================
@@ -232,6 +259,27 @@ auto main() -> int;
232259 r.construct (x + x * y);
233260 return std::move (r.value ()); }
234261
262+ #line 55 "pure2-autodiff-higher-order.cpp2"
263+ [[nodiscard]] auto ad_test::func (cpp2::impl::in<double > x, cpp2::impl::in<double > y) -> func_ret{
264+ cpp2::impl::deferred_init<double > r;
265+ #line 56 "pure2-autodiff-higher-order.cpp2"
266+ r.construct (x + y);
267+ return std::move (r.value ()); }
268+
269+ #line 59 "pure2-autodiff-higher-order.cpp2"
270+ [[nodiscard]] auto ad_test::func_call (cpp2::impl::in<double > x, cpp2::impl::in<double > y) -> func_call_ret{
271+ cpp2::impl::deferred_init<double > r;
272+ #line 60 "pure2-autodiff-higher-order.cpp2"
273+ r.construct (x * func (x, y));
274+ return std::move (r.value ()); }
275+
276+ #line 63 "pure2-autodiff-higher-order.cpp2"
277+ [[nodiscard]] auto ad_test::sin_call (cpp2::impl::in<double > x, cpp2::impl::in<double > y) -> sin_call_ret{
278+ cpp2::impl::deferred_init<double > r;
279+ #line 64 "pure2-autodiff-higher-order.cpp2"
280+ r.construct (sin (x - y));
281+ return std::move (r.value ()); }
282+
235283 [[nodiscard]] auto ad_test::add_1_d (cpp2::impl::in<double > x, cpp2::impl::in<cpp2::taylor<double ,6 >> x_d, cpp2::impl::in<double > y, cpp2::impl::in<cpp2::taylor<double ,6 >> y_d) -> add_1_d_ret{
236284 double r {0.0 };
237285 cpp2::taylor<double ,6 > r_d {0.0 };r_d = x_d + y_d;
@@ -330,25 +378,56 @@ auto temp_1_d {CPP2_UFCS(mul)(x_d, y_d, x, y)};
330378 return { std::move (r), std::move (r_d) };
331379 }
332380
333- #line 56 "pure2-autodiff-higher-order.cpp2"
381+ [[nodiscard]] auto ad_test::func_d (cpp2::impl::in<double > x, cpp2::impl::in<cpp2::taylor<double ,6 >> x_d, cpp2::impl::in<double > y, cpp2::impl::in<cpp2::taylor<double ,6 >> y_d) -> func_d_ret{
382+ double r {0.0 };
383+ cpp2::taylor<double ,6 > r_d {0.0 };r_d = x_d + y_d;
384+ r = x + y;
385+ return { std::move (r), std::move (r_d) };
386+ }
387+
388+ [[nodiscard]] auto ad_test::func_call_d (cpp2::impl::in<double > x, cpp2::impl::in<cpp2::taylor<double ,6 >> x_d, cpp2::impl::in<double > y, cpp2::impl::in<cpp2::taylor<double ,6 >> y_d) -> func_call_d_ret{
389+ double r {0.0 };
390+ cpp2::taylor<double ,6 > r_d {0.0 };
391+ auto temp_2 {func_d (x, x_d, y, y_d)};
392+
393+ auto temp_1 {temp_2.r };
394+
395+ auto temp_1_d {cpp2::move (temp_2).r_d };
396+ r_d = CPP2_UFCS (mul)(x_d, cpp2::move (temp_1_d), x, temp_1);
397+ r = x * cpp2::move (temp_1);
398+ return { std::move (r), std::move (r_d) };
399+ }
400+
401+ [[nodiscard]] auto ad_test::sin_call_d (cpp2::impl::in<double > x, cpp2::impl::in<cpp2::taylor<double ,6 >> x_d, cpp2::impl::in<double > y, cpp2::impl::in<cpp2::taylor<double ,6 >> y_d) -> sin_call_d_ret{
402+ double r {0.0 };
403+ cpp2::taylor<double ,6 > r_d {0.0 };
404+ auto temp_1_d {x_d - y_d};
405+
406+ auto temp_1 {x - y};
407+ r_d = CPP2_UFCS (sin)(cpp2::move (temp_1_d), temp_1);
408+ r = sin (cpp2::move (temp_1));
409+ return { std::move (r), std::move (r_d) };
410+ }
411+
412+ #line 68 "pure2-autodiff-higher-order.cpp2"
334413auto write_output (cpp2::impl::in<std::string> func, cpp2::impl::in<double > x, cpp2::impl::in<ad_type> x_d, cpp2::impl::in<double > y, cpp2::impl::in<ad_type> y_d, auto const & ret) -> void{
335414 std::cout << " diff(" + cpp2::to_string (func) + " ) at (x = " + cpp2::to_string (x) + " , x_d = " + cpp2::to_string (x_d) + " , y = " + cpp2::to_string (y) + " , y_d = " + cpp2::to_string (y_d) + " ):" << std::endl;
336415 std::cout << " r = " + cpp2::to_string (ret.r ) + " " << std::endl;
337416{
338417auto i{1 };
339418
340- #line 60 "pure2-autodiff-higher-order.cpp2"
419+ #line 72 "pure2-autodiff-higher-order.cpp2"
341420 for ( ; cpp2::impl::cmp_less_eq (i,ad_order); i += 1 ) {
342421 std::cout << " d" + cpp2::to_string (i) + " = " + cpp2::to_string (CPP2_ASSERT_IN_BOUNDS (ret.r_d , i)) + " " << std::endl;
343422 }
344423}
345- #line 63 "pure2-autodiff-higher-order.cpp2"
424+ #line 75 "pure2-autodiff-higher-order.cpp2"
346425}
347426
348- #line 65 "pure2-autodiff-higher-order.cpp2"
427+ #line 77 "pure2-autodiff-higher-order.cpp2"
349428auto main () -> int{
350429
351- #line 68 "pure2-autodiff-higher-order.cpp2"
430+ #line 80 "pure2-autodiff-higher-order.cpp2"
352431 double x {2.0 };
353432 ad_type x_d {1.0 };
354433 double y {3.0 };
@@ -365,9 +444,9 @@ auto main() -> int{
365444 write_output (" x / y / y" , x, x_d, y, y_d, ad_test::div_2_d (x, x_d, y, y_d));
366445 write_output (" x * y / x" , x, x_d, y, y_d, ad_test::mul_div_2_d (x, x_d, y, y_d));
367446 write_output (" x * (x + y)" , x, x_d, y, y_d, ad_test::mul_add_d (x, x_d, y, y_d));
368- write_output (" x + x * y" , x, x_d, y, y_d, ad_test::add_mul_d (cpp2::move (x), cpp2::move ( x_d), cpp2::move (y), cpp2::move ( y_d) ));
369- // write_output("x * func(x, y)", x, x_d, y, y_d, ad_test::func_call_d(x, x_d, y, y_d));
370- // write_output("sin(x + y)", x, x_d, y, y_d, ad_test::sin_call_d(x, x_d, y, y_d));
447+ write_output (" x + x * y" , x, x_d, y, y_d, ad_test::add_mul_d (x, x_d, y, y_d));
448+ write_output (" x * func(x, y)" , x, x_d, y, y_d, ad_test::func_call_d (x, x_d, y, y_d));
449+ write_output (" sin(x - y)" , x, x_d, y, y_d, ad_test::sin_call_d (cpp2::move (x), cpp2::move ( x_d), cpp2::move (y), cpp2::move ( y_d) ));
371450// write_output("if branch", x, x_d, y, y_d, ad_test::if_branch_d(x, x_d, y, y_d));
372451// write_output("if else branch", x, x_d, y, y_d, ad_test::if_else_branch_d(x, x_d, y, y_d));
373452// write_output("direct return", x, x_d, y, y_d, ad_test::direct_return_d(x, x_d, y, y_d));
0 commit comments