ra-dual.cc 4.9 KB

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