Skip to content

Commit 803dfde

Browse files
committed
Refactor of autodiff_expression_handler.
The expression handler no longer generates the assignments. It stores the primal and forward expression. The assignments can now be generated with helper functions or by accessing the expressions.
1 parent 6d2659c commit 803dfde

File tree

6 files changed

+1062
-939
lines changed

6 files changed

+1062
-939
lines changed

regression-tests/test-results/pure2-autodiff-higher-order.cpp

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,9 @@ auto main() -> int;
544544
double r {0.0};
545545
cpp2::taylor<double,6> r_d {0.0};
546546
auto temp_1_d {CPP2_UFCS(mul)(x_d, y_d, x, y)};
547-
auto temp_1 {x * y}; r_d = CPP2_UFCS(mul)(cpp2::move(temp_1_d), x_d, temp_1, x);
547+
548+
auto temp_1 {x * y};
549+
r_d = CPP2_UFCS(mul)(cpp2::move(temp_1_d), x_d, temp_1, x);
548550
r = cpp2::move(temp_1) * x;
549551
return { std::move(r), std::move(r_d) };
550552
}
@@ -560,7 +562,9 @@ auto temp_1 {x * y}; r_d = CPP2_UFCS(mul)(cpp2::move(temp_1_d), x_d, temp_1, x);
560562
double r {0.0};
561563
cpp2::taylor<double,6> r_d {0.0};
562564
auto temp_1_d {CPP2_UFCS(div)(x_d, y_d, x, y)};
563-
auto temp_1 {x / CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(x),y)}; r_d = CPP2_UFCS(div)(cpp2::move(temp_1_d), y_d, temp_1, y);
565+
566+
auto temp_1 {x / CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(x),y)};
567+
r_d = CPP2_UFCS(div)(cpp2::move(temp_1_d), y_d, temp_1, y);
564568
r = cpp2::move(temp_1) / CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(cpp2::move(temp_1)),y);
565569
return { std::move(r), std::move(r_d) };
566570
}
@@ -569,17 +573,19 @@ auto temp_1 {x / CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(x),y)}; r_d = CPP2_UFCS(div)(c
569573
double r {0.0};
570574
cpp2::taylor<double,6> r_d {0.0};
571575
auto temp_1_d {CPP2_UFCS(mul)(x_d, y_d, x, y)};
572-
auto temp_1 {x * y}; r_d = CPP2_UFCS(div)(cpp2::move(temp_1_d), x_d, temp_1, x);
576+
577+
auto temp_1 {x * y};
578+
r_d = CPP2_UFCS(div)(cpp2::move(temp_1_d), x_d, temp_1, x);
573579
r = cpp2::move(temp_1) / CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(cpp2::move(temp_1)),x);
574580
return { std::move(r), std::move(r_d) };
575581
}
576582

577583
[[nodiscard]] auto ad_test::mul_add_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) -> mul_add_d_ret{
578584
double r {0.0};
579585
cpp2::taylor<double,6> r_d {0.0};
580-
auto temp_1_d {x_d + y_d};
586+
cpp2::taylor<double,6> temp_1_d {x_d + y_d};
581587

582-
auto temp_1 {x + y};
588+
double temp_1 {x + y};
583589
r_d = CPP2_UFCS(mul)(x_d, cpp2::move(temp_1_d), x, temp_1);
584590
r = x * cpp2::move(temp_1);
585591
return { std::move(r), std::move(r_d) };
@@ -588,9 +594,9 @@ auto temp_1_d {x_d + y_d};
588594
[[nodiscard]] auto ad_test::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{
589595
double r {0.0};
590596
cpp2::taylor<double,6> r_d {0.0};
591-
auto temp_1_d {CPP2_UFCS(mul)(x_d, y_d, x, y)};
597+
cpp2::taylor<double,6> temp_1_d {CPP2_UFCS(mul)(x_d, y_d, x, y)};
592598

593-
auto temp_1 {x * y};
599+
double temp_1 {x * y};
594600
r_d = x_d + cpp2::move(temp_1_d);
595601
r = x + cpp2::move(temp_1);
596602
return { std::move(r), std::move(r_d) };
@@ -606,22 +612,22 @@ auto temp_1_d {CPP2_UFCS(mul)(x_d, y_d, x, y)};
606612
[[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{
607613
double r {0.0};
608614
cpp2::taylor<double,6> r_d {0.0};
609-
auto temp_2 {func_d(x, x_d, y, y_d)};
615+
auto temp_1 {func_d(x, x_d, y, y_d)};
610616

611-
auto temp_1 {temp_2.ret};
617+
cpp2::taylor<double,6> temp_2_d {temp_1.ret_d};
612618

613-
auto temp_1_d {cpp2::move(temp_2).ret_d};
614-
r_d = CPP2_UFCS(mul)(x_d, cpp2::move(temp_1_d), x, temp_1);
615-
r = x * cpp2::move(temp_1);
619+
double temp_2 {cpp2::move(temp_1).ret};
620+
r_d = CPP2_UFCS(mul)(x_d, cpp2::move(temp_2_d), x, temp_2);
621+
r = x * cpp2::move(temp_2);
616622
return { std::move(r), std::move(r_d) };
617623
}
618624

619625
[[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{
620626
double r {0.0};
621627
cpp2::taylor<double,6> r_d {0.0};
622-
auto temp_1_d {x_d - y_d};
628+
cpp2::taylor<double,6> temp_1_d {x_d - y_d};
623629

624-
auto temp_1 {x - y};
630+
double temp_1 {x - y};
625631
r_d = CPP2_UFCS(sin)(cpp2::move(temp_1_d), temp_1);
626632
r = sin(cpp2::move(temp_1));
627633
return { std::move(r), std::move(r_d) };
@@ -765,10 +771,10 @@ int i_d {};
765771
std::vector<cpp2::taylor<double,6>> v_d {};
766772

767773
std::vector<double> v {};
768-
CPP2_UFCS(push_back)(v, x);
769774
CPP2_UFCS(push_back)(v_d, x_d);
770-
CPP2_UFCS(push_back)(v, y);
775+
CPP2_UFCS(push_back)(v, x);
771776
CPP2_UFCS(push_back)(v_d, y_d);
777+
CPP2_UFCS(push_back)(v, y);
772778
r_d = { };
773779
r = 0.0;
774780
{

regression-tests/test-results/pure2-autodiff-higher-order.cpp2.output

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,8 @@ ad_test:/* @autodiff<"order=6"> @print */ type =
443443
out r_d: cpp2::taylor<double, 6> = 0.0,
444444
) =
445445
{
446-
temp_1_d: _ = x_d + y_d;
447-
temp_1: _ = x + y;
446+
temp_1_d: cpp2::taylor<double, 6> = x_d + y_d;
447+
temp_1: double = x + y;
448448
r_d = x_d.mul(temp_1_d, x, temp_1);
449449
r = x * temp_1;
450450
return;
@@ -460,8 +460,8 @@ ad_test:/* @autodiff<"order=6"> @print */ type =
460460
out r_d: cpp2::taylor<double, 6> = 0.0,
461461
) =
462462
{
463-
temp_1_d: _ = x_d.mul(y_d, x, y);
464-
temp_1: _ = x * y;
463+
temp_1_d: cpp2::taylor<double, 6> = x_d.mul(y_d, x, y);
464+
temp_1: double = x * y;
465465
r_d = x_d + temp_1_d;
466466
r = x + temp_1;
467467
return;
@@ -492,11 +492,11 @@ ad_test:/* @autodiff<"order=6"> @print */ type =
492492
out r_d: cpp2::taylor<double, 6> = 0.0,
493493
) =
494494
{
495-
temp_2: _ = func_d(x, x_d, y, y_d);
496-
temp_1: _ = temp_2.ret;
497-
temp_1_d: _ = temp_2.ret_d;
498-
r_d = x_d.mul(temp_1_d, x, temp_1);
499-
r = x * temp_1;
495+
temp_1: _ = func_d(x, x_d, y, y_d);
496+
temp_2_d: cpp2::taylor<double, 6> = temp_1.ret_d;
497+
temp_2: double = temp_1.ret;
498+
r_d = x_d.mul(temp_2_d, x, temp_2);
499+
r = x * temp_2;
500500
return;
501501
}
502502

@@ -510,8 +510,8 @@ ad_test:/* @autodiff<"order=6"> @print */ type =
510510
out r_d: cpp2::taylor<double, 6> = 0.0,
511511
) =
512512
{
513-
temp_1_d: _ = x_d - y_d;
514-
temp_1: _ = x - y;
513+
temp_1_d: cpp2::taylor<double, 6> = x_d - y_d;
514+
temp_1: double = x - y;
515515
r_d = temp_1_d.sin(temp_1);
516516
r = sin(temp_1);
517517
return;
@@ -732,10 +732,10 @@ ad_test:/* @autodiff<"order=6"> @print */ type =
732732
{
733733
v_d: std::vector<cpp2::taylor<double, 6>> = ();
734734
v: std::vector<double> = ();
735-
v.push_back(x);
736735
v_d.push_back(x_d);
737-
v.push_back(y);
736+
v.push_back(x);
738737
v_d.push_back(y_d);
738+
v.push_back(y);
739739
r_d = ();
740740
r = 0.0;
741741
(copy t_d_iter: _ = v_d.begin(), )

regression-tests/test-results/pure2-autodiff.cpp

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,9 @@ return { std::move(ret), std::move(ret_d) };
615615
double r {0.0};
616616
double r_d {0.0};
617617
auto temp_1_d {x * y_d + y * x_d};
618-
auto temp_1 {x * y}; r_d = temp_1 * x_d + x * cpp2::move(temp_1_d);
618+
619+
auto temp_1 {x * y};
620+
r_d = temp_1 * x_d + x * cpp2::move(temp_1_d);
619621
r = cpp2::move(temp_1) * x;
620622
return { std::move(r), std::move(r_d) };
621623
}
@@ -631,7 +633,9 @@ auto temp_1 {x * y}; r_d = temp_1 * x_d + x * cpp2::move(temp_1_d);
631633
double r {0.0};
632634
double r_d {0.0};
633635
auto temp_1_d {x_d / CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(x_d),y) - x * y_d / CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(y_d),(y * y))};
634-
auto temp_1 {x / CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(x),y)}; r_d = cpp2::move(temp_1_d) / CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(cpp2::move(temp_1_d)),y) - temp_1 * y_d / CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(y_d),(y * y));
636+
637+
auto temp_1 {x / CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(x),y)};
638+
r_d = cpp2::move(temp_1_d) / CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(cpp2::move(temp_1_d)),y) - temp_1 * y_d / CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(y_d),(y * y));
635639
r = cpp2::move(temp_1) / CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(cpp2::move(temp_1)),y);
636640
return { std::move(r), std::move(r_d) };
637641
}
@@ -640,17 +644,19 @@ auto temp_1 {x / CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(x),y)}; r_d = cpp2::move(temp_
640644
double r {0.0};
641645
double r_d {0.0};
642646
auto temp_1_d {x * y_d + y * x_d};
643-
auto temp_1 {x * y}; r_d = cpp2::move(temp_1_d) / CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(cpp2::move(temp_1_d)),x) - temp_1 * x_d / CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(x_d),(x * x));
647+
648+
auto temp_1 {x * y};
649+
r_d = cpp2::move(temp_1_d) / CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(cpp2::move(temp_1_d)),x) - temp_1 * x_d / CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(x_d),(x * x));
644650
r = cpp2::move(temp_1) / CPP2_ASSERT_NOT_ZERO(CPP2_TYPEOF(cpp2::move(temp_1)),x);
645651
return { std::move(r), std::move(r_d) };
646652
}
647653

648654
[[nodiscard]] auto ad_test::mul_add_d(cpp2::impl::in<double> x, cpp2::impl::in<double> x_d, cpp2::impl::in<double> y, cpp2::impl::in<double> y_d) -> mul_add_d_ret{
649655
double r {0.0};
650656
double r_d {0.0};
651-
auto temp_1_d {x_d + y_d};
657+
double temp_1_d {x_d + y_d};
652658

653-
auto temp_1 {x + y};
659+
double temp_1 {x + y};
654660
r_d = x * cpp2::move(temp_1_d) + temp_1 * x_d;
655661
r = x * cpp2::move(temp_1);
656662
return { std::move(r), std::move(r_d) };
@@ -659,9 +665,9 @@ auto temp_1_d {x_d + y_d};
659665
[[nodiscard]] auto ad_test::add_mul_d(cpp2::impl::in<double> x, cpp2::impl::in<double> x_d, cpp2::impl::in<double> y, cpp2::impl::in<double> y_d) -> add_mul_d_ret{
660666
double r {0.0};
661667
double r_d {0.0};
662-
auto temp_1_d {x * y_d + y * x_d};
668+
double temp_1_d {x * y_d + y * x_d};
663669

664-
auto temp_1 {x * y};
670+
double temp_1 {x * y};
665671
r_d = x_d + cpp2::move(temp_1_d);
666672
r = x + cpp2::move(temp_1);
667673
return { std::move(r), std::move(r_d) };
@@ -677,35 +683,35 @@ auto temp_1_d {x * y_d + y * x_d};
677683
[[nodiscard]] auto ad_test::func_call_d(cpp2::impl::in<double> x, cpp2::impl::in<double> x_d, cpp2::impl::in<double> y, cpp2::impl::in<double> y_d) -> func_call_d_ret{
678684
double r {0.0};
679685
double r_d {0.0};
680-
auto temp_2 {func_d(x, x_d, y, y_d)};
686+
auto temp_1 {func_d(x, x_d, y, y_d)};
681687

682-
auto temp_1 {temp_2.ret};
688+
double temp_2_d {temp_1.ret_d};
683689

684-
auto temp_1_d {cpp2::move(temp_2).ret_d};
685-
r_d = x * cpp2::move(temp_1_d) + temp_1 * x_d;
686-
r = x * cpp2::move(temp_1);
690+
double temp_2 {cpp2::move(temp_1).ret};
691+
r_d = x * cpp2::move(temp_2_d) + temp_2 * x_d;
692+
r = x * cpp2::move(temp_2);
687693
return { std::move(r), std::move(r_d) };
688694
}
689695

690696
[[nodiscard]] auto ad_test::func_outer_call_d(cpp2::impl::in<double> x, cpp2::impl::in<double> x_d, cpp2::impl::in<double> y, cpp2::impl::in<double> y_d) -> func_outer_call_d_ret{
691697
double r {0.0};
692698
double r_d {0.0};
693-
auto temp_2 {func_outer_d(x, x_d, y, y_d)};
699+
auto temp_1 {func_outer_d(x, x_d, y, y_d)};
694700

695-
auto temp_1 {temp_2.ret};
701+
double temp_2_d {temp_1.ret_d};
696702

697-
auto temp_1_d {cpp2::move(temp_2).ret_d};
698-
r_d = x * cpp2::move(temp_1_d) + temp_1 * x_d;
699-
r = x * cpp2::move(temp_1);
703+
double temp_2 {cpp2::move(temp_1).ret};
704+
r_d = x * cpp2::move(temp_2_d) + temp_2 * x_d;
705+
r = x * cpp2::move(temp_2);
700706
return { std::move(r), std::move(r_d) };
701707
}
702708

703709
[[nodiscard]] auto ad_test::sin_call_d(cpp2::impl::in<double> x, cpp2::impl::in<double> x_d, cpp2::impl::in<double> y, cpp2::impl::in<double> y_d) -> sin_call_d_ret{
704710
double r {0.0};
705711
double r_d {0.0};
706-
auto temp_1_d {x_d - y_d};
712+
double temp_1_d {x_d - y_d};
707713

708-
auto temp_1 {x - y};
714+
double temp_1 {x - y};
709715
r_d = cos(temp_1) * cpp2::move(temp_1_d);
710716
r = sin(cpp2::move(temp_1));
711717
return { std::move(r), std::move(r_d) };
@@ -849,10 +855,10 @@ int i_d {};
849855
std::vector<double> v_d {};
850856

851857
std::vector<double> v {};
852-
CPP2_UFCS(push_back)(v, x);
853858
CPP2_UFCS(push_back)(v_d, x_d);
854-
CPP2_UFCS(push_back)(v, y);
859+
CPP2_UFCS(push_back)(v, x);
855860
CPP2_UFCS(push_back)(v_d, y_d);
861+
CPP2_UFCS(push_back)(v, y);
856862
r_d = { };
857863
r = 0.0;
858864
{
@@ -900,13 +906,13 @@ auto const& t_d{*cpp2::impl::assert_not_null(t_d_iter)};
900906
double r_d2 {0.0};
901907
double r_d {0.0};
902908
double r_d_d2 {0.0};
903-
auto temp_1_d2 {x * x_d_d2 + x_d * x_d2};
909+
double temp_1_d2 {x * x_d_d2 + x_d * x_d2};
904910

905-
auto temp_1 {x * x_d};
911+
double temp_1 {x * x_d};
906912

907-
auto temp_2_d2 {x * x_d_d2 + x_d * x_d2};
913+
double temp_2_d2 {x * x_d_d2 + x_d * x_d2};
908914

909-
auto temp_2 {x * x_d};
915+
double temp_2 {x * x_d};
910916
r_d_d2 = cpp2::move(temp_1_d2) + cpp2::move(temp_2_d2);
911917
r_d = cpp2::move(temp_1) + cpp2::move(temp_2);
912918
r_d2 = x * x_d2 + x * x_d2;

regression-tests/test-results/pure2-autodiff.cpp2.output

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,8 @@ ad_test:/* @autodiff @print */ type =
452452
out r_d: double = 0.0,
453453
) =
454454
{
455-
temp_1_d: _ = x_d + y_d;
456-
temp_1: _ = x + y;
455+
temp_1_d: double = x_d + y_d;
456+
temp_1: double = x + y;
457457
r_d = x * temp_1_d + temp_1 * x_d;
458458
r = x * temp_1;
459459
return;
@@ -469,8 +469,8 @@ ad_test:/* @autodiff @print */ type =
469469
out r_d: double = 0.0,
470470
) =
471471
{
472-
temp_1_d: _ = x * y_d + y * x_d;
473-
temp_1: _ = x * y;
472+
temp_1_d: double = x * y_d + y * x_d;
473+
temp_1: double = x * y;
474474
r_d = x_d + temp_1_d;
475475
r = x + temp_1;
476476
return;
@@ -501,11 +501,11 @@ ad_test:/* @autodiff @print */ type =
501501
out r_d: double = 0.0,
502502
) =
503503
{
504-
temp_2: _ = func_d(x, x_d, y, y_d);
505-
temp_1: _ = temp_2.ret;
506-
temp_1_d: _ = temp_2.ret_d;
507-
r_d = x * temp_1_d + temp_1 * x_d;
508-
r = x * temp_1;
504+
temp_1: _ = func_d(x, x_d, y, y_d);
505+
temp_2_d: double = temp_1.ret_d;
506+
temp_2: double = temp_1.ret;
507+
r_d = x * temp_2_d + temp_2 * x_d;
508+
r = x * temp_2;
509509
return;
510510
}
511511

@@ -519,11 +519,11 @@ ad_test:/* @autodiff @print */ type =
519519
out r_d: double = 0.0,
520520
) =
521521
{
522-
temp_2: _ = func_outer_d(x, x_d, y, y_d);
523-
temp_1: _ = temp_2.ret;
524-
temp_1_d: _ = temp_2.ret_d;
525-
r_d = x * temp_1_d + temp_1 * x_d;
526-
r = x * temp_1;
522+
temp_1: _ = func_outer_d(x, x_d, y, y_d);
523+
temp_2_d: double = temp_1.ret_d;
524+
temp_2: double = temp_1.ret;
525+
r_d = x * temp_2_d + temp_2 * x_d;
526+
r = x * temp_2;
527527
return;
528528
}
529529

@@ -537,8 +537,8 @@ ad_test:/* @autodiff @print */ type =
537537
out r_d: double = 0.0,
538538
) =
539539
{
540-
temp_1_d: _ = x_d - y_d;
541-
temp_1: _ = x - y;
540+
temp_1_d: double = x_d - y_d;
541+
temp_1: double = x - y;
542542
r_d = cos(temp_1) * temp_1_d;
543543
r = sin(temp_1);
544544
return;
@@ -759,10 +759,10 @@ ad_test:/* @autodiff @print */ type =
759759
{
760760
v_d: std::vector<double> = ();
761761
v: std::vector<double> = ();
762-
v.push_back(x);
763762
v_d.push_back(x_d);
764-
v.push_back(y);
763+
v.push_back(x);
765764
v_d.push_back(y_d);
765+
v.push_back(y);
766766
r_d = ();
767767
r = 0.0;
768768
(copy t_d_iter: _ = v_d.begin(), )
@@ -827,10 +827,10 @@ ad_test_twice:/* @autodiff @autodiff<"suffix=_d2"> @print */ type =
827827
out r_d_d2: double = 0.0,
828828
) =
829829
{
830-
temp_1_d2: _ = x * x_d_d2 + x_d * x_d2;
831-
temp_1: _ = x * x_d;
832-
temp_2_d2: _ = x * x_d_d2 + x_d * x_d2;
833-
temp_2: _ = x * x_d;
830+
temp_1_d2: double = x * x_d_d2 + x_d * x_d2;
831+
temp_1: double = x * x_d;
832+
temp_2_d2: double = x * x_d_d2 + x_d * x_d2;
833+
temp_2: double = x * x_d;
834834
r_d_d2 = temp_1_d2 + temp_2_d2;
835835
r_d = temp_1 + temp_2;
836836
r_d2 = x * x_d2 + x * x_d2;

0 commit comments

Comments
 (0)