Skip to content

Commit 4cd35d0

Browse files
committed
Add an AD unit test
1 parent ece0c9b commit 4cd35d0

File tree

4 files changed

+163
-1
lines changed

4 files changed

+163
-1
lines changed

regression-tests/pure2-autodiff.cpp2

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,23 @@ write_output_reverse: (func: std::string, x: double, inout x_b: double, y: doubl
277277
y_b = 0.0;
278278
}
279279

280+
ad_test_2: @autodiff<"order=2"> @print type = {
281+
f: (x: double) -> (y: double) = {
282+
if x < -3
283+
{
284+
y = x * x;
285+
}
286+
else if x < 3
287+
{
288+
y = x + sin(x) + 10;
289+
}
290+
else
291+
{
292+
y = sin(x) * x*x;
293+
}
294+
}
295+
}
296+
280297
main: () = {
281298

282299
x: double = 2.0;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
11
pure2-autodiff.cpp
2+
C:\github\cppfront\include\cpp2util.h(1250): error C7595: 'std::source_location::current': call to immediate function is not a constant expression
3+
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.43.34808\include\source_location(37): note: failure was caused by attempting to access a member on an object of dynamic type 'std::source_location' in which the member is not defined
4+
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.43.34808\include\source_location(37): note: see usage of 'std::source_location::source_location'
5+
C:\github\cppfront\include\cpp2util.h(1250): note: the call stack of the evaluation (the oldest call first) is
6+
C:\github\cppfront\include\cpp2util.h(1250): note: while evaluating function 'std::source_location std::source_location::current(const uint_least32_t,const uint_least32_t,const char *const ,const char *const ) noexcept'
7+
C:\github\cppfront\include\cpp2util.h(1250): note: the template instantiation context (the oldest one first) is
8+
pure2-autodiff.cpp2(282): note: see reference to class template instantiation 'cpp2::taylor<double,2>' being compiled
9+
cpp2taylor.h2(16): note: while compiling class template member function 'cpp2::taylor<double,2>::taylor(const std::initializer_list<R> &)'
10+
with
11+
[
12+
R=double
13+
]
14+
pure2-autodiff.cpp2(298): note: see the first reference to 'cpp2::taylor<double,2>::taylor' in 'ad_test_2::f_d'
15+
cpp2taylor.h2(35): note: while compiling class template member function 'void cpp2::taylor<double,2>::set(const int,const double) &'

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

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
#define CPP2_IMPORT_STD Yes
33
#include "cpp2ad_stack.h"
4+
#include "cpp2taylor.h"
45

56
//=== Cpp2 type declarations ====================================================
67

@@ -28,6 +29,10 @@ class ad_test_reverse;
2829
class ad_test_twice;
2930

3031

32+
#line 280 "pure2-autodiff.cpp2"
33+
class ad_test_2;
34+
35+
3136
//=== Cpp2 type definitions and function declarations ===========================
3237

3338
#line 1 "pure2-autodiff.cpp2"
@@ -532,6 +537,24 @@ auto write_output(cpp2::impl::in<std::string> func, cpp2::impl::in<double> x, cp
532537
auto write_output_reverse(cpp2::impl::in<std::string> func, cpp2::impl::in<double> x, double& x_b, cpp2::impl::in<double> y, double& y_b, double& r_b, auto const& ret) -> void;
533538

534539
#line 280 "pure2-autodiff.cpp2"
540+
class ad_test_2 {
541+
using f_ret = double;
542+
543+
#line 281 "pure2-autodiff.cpp2"
544+
public: [[nodiscard]] static auto f(cpp2::impl::in<double> x) -> f_ret;
545+
struct f_d_ret { double y; cpp2::taylor<double,2> y_d; };
546+
547+
548+
public: [[nodiscard]] static auto f_d(cpp2::impl::in<double> x, cpp2::impl::in<cpp2::taylor<double,2>> x_d) -> f_d_ret;
549+
550+
public: ad_test_2() = default;
551+
public: ad_test_2(ad_test_2 const&) = delete; /* No 'that' constructor, suppress copy */
552+
public: auto operator=(ad_test_2 const&) -> void = delete;
553+
554+
555+
#line 295 "pure2-autodiff.cpp2"
556+
};
557+
535558
auto main() -> int;
536559

537560
//=== Cpp2 function definitions =================================================
@@ -1552,7 +1575,56 @@ auto write_output_reverse(cpp2::impl::in<std::string> func, cpp2::impl::in<doubl
15521575
y_b = 0.0;
15531576
}
15541577

1555-
#line 280 "pure2-autodiff.cpp2"
1578+
#line 281 "pure2-autodiff.cpp2"
1579+
[[nodiscard]] auto ad_test_2::f(cpp2::impl::in<double> x) -> f_ret{
1580+
cpp2::impl::deferred_init<double> y;
1581+
#line 282 "pure2-autodiff.cpp2"
1582+
if (cpp2::impl::cmp_less(x,-3))
1583+
{
1584+
y.construct(x * x);
1585+
}
1586+
else {if (cpp2::impl::cmp_less(x,3))
1587+
{
1588+
y.construct(x + sin(x) + 10);
1589+
}
1590+
else
1591+
{
1592+
y.construct(sin(x) * x * x);
1593+
}}return std::move(y.value());
1594+
}
1595+
1596+
[[nodiscard]] auto ad_test_2::f_d(cpp2::impl::in<double> x, cpp2::impl::in<cpp2::taylor<double,2>> x_d) -> f_d_ret{
1597+
double y {0.0};
1598+
cpp2::taylor<double,2> y_d {0.0};if (cpp2::impl::cmp_less(x,-3)) {
1599+
y_d = x_d.mul(x_d, x, x);
1600+
y = x * x;
1601+
}
1602+
else {
1603+
if (cpp2::impl::cmp_less(x,3)) {
1604+
1605+
cpp2::taylor<double,2> temp_1_d {CPP2_UFCS(sin)(x_d, x)};
1606+
1607+
double temp_1 {sin(x)};
1608+
y_d = x_d + cpp2::move(temp_1_d);
1609+
y = x + cpp2::move(temp_1) + 10;
1610+
}
1611+
else {
1612+
1613+
cpp2::taylor<double,2> temp_3_d {CPP2_UFCS(sin)(x_d, x)};
1614+
1615+
double temp_3 {sin(x)};
1616+
1617+
auto temp_4_d {cpp2::move(temp_3_d).mul(x_d, temp_3, x)};
1618+
1619+
auto temp_4 {cpp2::move(temp_3) * x};
1620+
y_d = cpp2::move(temp_4_d).mul(x_d, temp_4, x);
1621+
y = cpp2::move(temp_4) * x;
1622+
}
1623+
}
1624+
return { std::move(y), std::move(y_d) };
1625+
}
1626+
1627+
#line 297 "pure2-autodiff.cpp2"
15561628
auto main() -> int{
15571629

15581630
double x {2.0};

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,5 +1386,64 @@ ad_test_twice:/* @autodiff @autodiff<"suffix=_d2"> @print */ type =
13861386
return;
13871387
}
13881388
}
1389+
1390+
1391+
ad_test_2:/* @autodiff<"order=2"> @print */ type =
1392+
{
1393+
f:(in x: double, ) -> (out y: double, ) =
1394+
{
1395+
if x < -3
1396+
{
1397+
y = x * x;
1398+
}
1399+
else
1400+
{
1401+
if x < 3
1402+
{
1403+
y = x + sin(x) + 10;
1404+
}
1405+
else
1406+
{
1407+
y = sin(x) * x * x;
1408+
}
1409+
}
1410+
return;
1411+
}
1412+
1413+
f_d:(
1414+
in x: double,
1415+
in x_d: cpp2::taylor<double, 2>,
1416+
) -> (
1417+
out y: double = 0.0,
1418+
out y_d: cpp2::taylor<double, 2> = 0.0,
1419+
) =
1420+
{
1421+
if x < -3
1422+
{
1423+
y_d = x_d..mul(x_d, x, x);
1424+
y = x * x;
1425+
}
1426+
else
1427+
{
1428+
if x < 3
1429+
{
1430+
temp_1_d: cpp2::taylor<double, 2> = x_d.sin(x);
1431+
temp_1: double = sin(x);
1432+
y_d = x_d + temp_1_d;
1433+
y = x + temp_1 + 10;
1434+
}
1435+
else
1436+
{
1437+
temp_3_d: cpp2::taylor<double, 2> = x_d.sin(x);
1438+
temp_3: double = sin(x);
1439+
temp_4_d: _ = temp_3_d..mul(x_d, temp_3, x);
1440+
temp_4: _ = temp_3 * x;
1441+
y_d = temp_4_d..mul(x_d, temp_4, x);
1442+
y = temp_4 * x;
1443+
}
1444+
}
1445+
return;
1446+
}
1447+
}
13891448
ok (all Cpp2, passes safety checks)
13901449

0 commit comments

Comments
 (0)