ra-dual.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - Using Dual<> with ra:: arrays & expressions.
  3. // (c) Daniel Llorens - 2015
  4. // This library is free software; you can redistribute it and/or modify it under
  5. // the terms of the GNU Lesser General Public License as published by the Free
  6. // Software Foundation; either version 3 of the License, or (at your option) any
  7. // later version.
  8. #include <cassert>
  9. #include <iostream>
  10. #include <algorithm>
  11. #include "ra/test.hh"
  12. #include "ra/dual.hh"
  13. using std::cout, std::endl, std::flush, ra::TestRecorder;
  14. using real = double;
  15. using complex = std::complex<double>;
  16. using ra::dual, ra::Dual, ra::sqr;
  17. // not needed to put Dual<> in containers, but needed to use Dual<>s by themselves as expr terms.
  18. template <class T> constexpr bool ra::is_scalar_def<Dual<T>> = true;
  19. #define DEFINE_CASE(N, F, DF) \
  20. struct JOIN(case, N) \
  21. { \
  22. template <class X> static auto f(X x) { return (F); } \
  23. template <class X> static auto df(X x) { return (DF); } \
  24. };
  25. DEFINE_CASE(0, x*cos(x)/sqrt(x),
  26. cos(x)/(2.*sqrt(x))-sqrt(x)*sin(x))
  27. DEFINE_CASE(1, x,
  28. 1.)
  29. DEFINE_CASE(2, 3.*exp(x*x)/x+8.*exp(2.*x)/x,
  30. -3.*exp(x*x)/(x*x)+6.*exp(x*x)+16.*exp(2.*x)/x-8.*exp(2.*x)/(x*x))
  31. DEFINE_CASE(3, cos(pow(exp(x), 4.5)),
  32. -4.5*exp(4.5*x)*sin(exp(4.5*x)))
  33. DEFINE_CASE(4, 1./(x*x),
  34. -2.*x/sqr(x*x))
  35. DEFINE_CASE(5, 1./(2.-x*x),
  36. +2.*x/sqr(2.-x*x))
  37. DEFINE_CASE(6, sinh(x)/cosh(x),
  38. 1./sqr(cosh(x)))
  39. #undef DEFINE_CASE
  40. template <class Case, class X>
  41. void
  42. test1(TestRecorder & tr, std::string const & info, X && x, real const rspec=2e-15)
  43. {
  44. tr.info(info, ": f vs Dual")
  45. .test_rel(ra::map([](auto && x) { return Case::f(x); }, x),
  46. ra::map([](auto && x) { return Case::f(dual(x, 1.)).re; }, x),
  47. rspec);
  48. tr.info(info, ": df vs Dual")
  49. .test_rel(ra::map([](auto && x) { return Case::df(x); }, x),
  50. ra::map([](auto && x) { return Case::f(dual(x, 1.)).du; }, x),
  51. rspec);
  52. }
  53. template <class Case, class D>
  54. void
  55. test2(TestRecorder & tr, std::string const & info, D && d, real const rspec=2e-15)
  56. {
  57. tr.info(info, ": f vs Dual")
  58. .test_rel(ra::map([](auto && d) { return Case::f(d.re); }, d),
  59. ra::map([](auto && d) { return Case::f(d).re; }, d),
  60. rspec);
  61. tr.info(info, ": df vs Dual")
  62. .test_rel(ra::map([](auto && d) { return Case::df(d.re); }, d),
  63. ra::map([](auto && d) { return Case::f(d).du; }, d),
  64. rspec);
  65. }
  66. int main()
  67. {
  68. TestRecorder tr(std::cout);
  69. tr.test_eq(0., Dual<real>{3}.du);
  70. tr.test_eq(0., dual(3.).du);
  71. #define TESTER(testn, x) \
  72. { \
  73. testn<case0>(tr, "case0", x); \
  74. testn<case1>(tr, "case1", x); \
  75. testn<case2>(tr, "case2", x); \
  76. testn<case3>(tr, "case3", x, 5e-14); \
  77. testn<case4>(tr, "case4", x); \
  78. testn<case5>(tr, "case5", x); \
  79. testn<case6>(tr, "case6", x); \
  80. }
  81. tr.section("args are arrays of real");
  82. TESTER(test1, ra::Big<real>({10}, (ra::_0 + 1) * .1));
  83. tr.section("args are arrays of complex");
  84. TESTER(test1, ra::Big<complex>({10}, (ra::_0 + 1) * .1 + complex(0, 1)));
  85. tr.section("args are arrays of Dual<real>");
  86. TESTER(test2, ra::Big<Dual<real>>({10}, map([](auto x) { return dual(x, 1.); }, (ra::_0 + 1) * .1)));
  87. tr.section("requires is_scalar registration");
  88. TESTER(test2, Dual<real>(1., 1.));
  89. #undef TESTER
  90. tr.section("using ra:: operators on arrays of Dual<real>");
  91. {
  92. auto test3 = [](TestRecorder & tr, std::string const & info, auto && d, real const rspec=2e-15)
  93. {
  94. tr.info(info, ": f vs Dual")
  95. .test_rel(ra::map([](auto && d) { return cos(d.re); }, d),
  96. ra::map([](auto && d) { return d.re; }, cos(d)),
  97. rspec);
  98. tr.info(info, ": df vs Dual")
  99. .test_rel(ra::map([](auto && d) { return -sin(d.re); }, d),
  100. ra::map([](auto && d) { return d.du; }, cos(d)),
  101. rspec);
  102. };
  103. test3(tr, "Dual<real>",
  104. ra::Big<Dual<real>>({10}, map([](auto x) { return dual(x, 1.); }, (ra::_0 + 1) * .1)));
  105. }
  106. tr.section("TODO define ra:: operators for .re and .du, as real_part(), imag_part() do");
  107. {
  108. }
  109. return tr.summary();
  110. }