ra-dual.C 4.9 KB

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