dual.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file dual.cc
  3. /// @brief Tests for dual numbers.
  4. // (c) Daniel Llorens - 2013, 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 <numeric>
  13. #include "ra/format.hh"
  14. #include "ra/complex.hh"
  15. #include "ra/dual.hh"
  16. #include "ra/test.hh"
  17. using std::cout, std::endl, std::flush, ra::TestRecorder;
  18. using real = double;
  19. using complex = std::complex<double>;
  20. #define DEFINE_CASE(N, F, DF) \
  21. struct JOIN(case, N) \
  22. { \
  23. template <class X> static auto f(X x) { return (F); } \
  24. template <class X> static auto df(X x) { return (DF); } \
  25. };
  26. DEFINE_CASE(0, x*cos(x)/sqrt(x),
  27. cos(x)/(2.*sqrt(x))-sqrt(x)*sin(x))
  28. DEFINE_CASE(1, x,
  29. 1.)
  30. DEFINE_CASE(2, 3.*exp(x*x)/x+8.*exp(2.*x)/x,
  31. -3.*exp(x*x)/(x*x)+6.*exp(x*x)+16.*exp(2.*x)/x-8.*exp(2.*x)/(x*x))
  32. DEFINE_CASE(3, cos(pow(exp(x), 4.5)),
  33. -4.5*exp(4.5*x)*sin(exp(4.5*x)))
  34. DEFINE_CASE(4, 1./(x*x),
  35. -2.*x/sqr(x*x))
  36. DEFINE_CASE(5, 1./(2.-x*x),
  37. +2.*x/sqr(2.-x*x))
  38. DEFINE_CASE(6, sinh(x)/cosh(x),
  39. 1./sqr(cosh(x)))
  40. DEFINE_CASE(7, fma(x, x, 3.*x),
  41. 2.*x+3.)
  42. #undef DEFINE_CASE
  43. // repeat case2 using assignment ops.
  44. struct case8
  45. {
  46. template <class X> static auto f(X x)
  47. {
  48. auto y = 3.*exp(x*x);
  49. y /= x;
  50. y += 8.*exp(2.*x)/x;
  51. return y;
  52. }
  53. template <class X> static auto df(X x)
  54. {
  55. auto lo = x;
  56. lo *= lo;
  57. auto dy = -3.*exp(x*x)/lo;
  58. dy += +6.*exp(x*x);
  59. dy += +16.*exp(2.*x)/x;
  60. dy -= 8.*exp(2.*x)/lo;
  61. return dy;
  62. }
  63. };
  64. template <class Case, class X>
  65. void test1(TestRecorder & tr, std::string info, X && x, real const rspec=2e-15)
  66. {
  67. for (unsigned int i=0; i!=x.size(); ++i) {
  68. tr.info(info, " ", i, " f vs Dual").test_rel_error(Case::f(x[i]), Case::f(dual(x[i], 1.)).re, rspec);
  69. tr.info(info, " ", i, " df vs Dual").test_rel_error(Case::df(x[i]), Case::f(dual(x[i], 1.)).du, rspec);
  70. }
  71. }
  72. int main()
  73. {
  74. TestRecorder tr(std::cout);
  75. tr.test_eq(0., Dual<real>{3}.du);
  76. tr.test_eq(0., dual(3.).du);
  77. tr.section("tests with real");
  78. {
  79. std::vector<real> x(10);
  80. std::iota(x.begin(), x.end(), 1);
  81. for (real & xi: x) { xi *= .1; }
  82. test1<case0>(tr, "case0", x);
  83. test1<case1>(tr, "case1", x);
  84. test1<case2>(tr, "case2", x);
  85. test1<case3>(tr, "case3", x, 3e-14);
  86. test1<case4>(tr, "case4", x, 1e-15);
  87. test1<case5>(tr, "case5", x, 1e-15);
  88. test1<case6>(tr, "case6", x, 1e-15);
  89. test1<case7>(tr, "case7", x);
  90. test1<case8>(tr, "case8", x);
  91. }
  92. tr.section("demo with complex");
  93. {
  94. Dual<complex> x { complex(3, 1), 1. };
  95. cout << x << endl;
  96. cout << exp(x) << endl;
  97. cout << (x*x) << endl;
  98. }
  99. tr.section("real -> dual<complex> conversion");
  100. {
  101. Dual<complex> x { 1., 1. };
  102. x = 0.;
  103. tr.test_eq(0., x.re);
  104. tr.test_eq(0., x.du);
  105. }
  106. tr.section("tests with complex");
  107. {
  108. std::vector<complex> x(10);
  109. std::iota(x.begin(), x.end(), 1);
  110. for (complex & xi: x) { xi = xi*.1 + complex(0, 1); }
  111. test1<case0>(tr, "case0", x);
  112. test1<case1>(tr, "case1", x);
  113. test1<case2>(tr, "case2", x);
  114. test1<case3>(tr, "case3", x, 5e-14);
  115. test1<case4>(tr, "case4", x, 1e-15);
  116. test1<case5>(tr, "case5", x, 1e-15);
  117. test1<case6>(tr, "case6", x, 1.2e-15);
  118. test1<case7>(tr, "case7", x);
  119. test1<case8>(tr, "case8", x);
  120. }
  121. return tr.summary();
  122. }