ra-dual.cc 4.7 KB

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