Belle II Software  release-08-01-10
test_dual_numbers.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #include <generators/utilities/beamHelpers.h>
10 #include <gtest/gtest.h>
11 #include <cmath>
12 
13 using namespace std;
14 using namespace Belle2;
15 
16 namespace {
17 
19  class DualNumbersTests : public ::testing::Test {
20  protected:
21 
23  virtual void SetUp() { }
24 
26  virtual void TearDown() { }
27 
28  };
29 
31  TEST_F(DualNumbersTests, TestDerivatives)
32  {
33  DualNumber x0(0, 1);
34 
35  EXPECT_NEAR(tan(x0).x, 0, 1e-15); // value of tan(x) for x = 0
36  EXPECT_NEAR(tan(x0).dx, 1, 1e-15); // derivative of tan(x) for x = 0
37 
38  EXPECT_NEAR(atan(x0).x, 0, 1e-15); // value of atan(x) for x = 0
39  EXPECT_NEAR(atan(x0).dx, 1., 1e-15); // derivative of atan(x) for x = 0
40 
41  DualNumber x4(4, 1);
42 
43  EXPECT_NEAR(sqrt(x4).x, 2, 1e-15); // value of sqrt(x) for x = 4
44  EXPECT_NEAR(sqrt(x4).dx, 1. / 4, 1e-15); // derivative of sqrt(x) for x = 4
45 
46 
47  //derivative of product
48  EXPECT_NEAR((tan(x0) * x0).x, 0, 1e-15); // value of x tan(x) for x = 0
49  EXPECT_NEAR((tan(x0) * x0).dx, 0, 1e-15); // derivative of x tan(x) for x = 0
50 
51  //derivative of ratio
52  DualNumber xPi(M_PI, 1);
53 
54  EXPECT_NEAR((tan(xPi) / xPi).x, 0, 1e-15); // value of tan(x)/x for x = pi
55  EXPECT_NEAR((tan(xPi) / xPi).dx, 1. / M_PI, 1e-15); // derivative of tan(x)/x for x = pi
56  }
57 
58 
60  TEST_F(DualNumbersTests, TestVectorDerivatives)
61  {
63 
64  EXPECT_NEAR((-1.0 / sqrt(r.norm2())).x, -1. / sqrt(3), 1e-15); // -1/r for r=(1,1,1)
65  EXPECT_NEAR((-1.0 / sqrt(r.norm2())).dx, 1. / (3 * sqrt(3)), 1e-15); // grad -1/r for r=(1,1,1), x-component
66 
67  EXPECT_NEAR(dot(r, r).x, r.norm2().x, 1e-15); // dot(r,r) == |r|^2
68  EXPECT_NEAR(dot(r, r).dx, 2., 1e-15); // grad dot(r,r) == 2 for r =(1,1,1), x-component
69  }
70 
71 }
TEST_F(GlobalLabelTest, LargeNumberOfTimeDependentParameters)
Test large number of time-dep params for registration and retrieval.
Definition: globalLabel.cc:72
double sqrt(double a)
sqrt for double
Definition: beamHelpers.h:28
double atan(double a)
atan for double
Definition: beamHelpers.h:34
T dot(GeneralVector< T > a, GeneralVector< T > b)
dot product of two general vectors
Definition: beamHelpers.h:163
double tan(double a)
tan for double
Definition: beamHelpers.h:31
Abstract base class for different kinds of events.
Simple structure implementing dual numbers which are used for exact evaluation of the derivatives,...
Definition: beamHelpers.h:38
3-vector with members of arbitrary type, especially members can be dual numbers
Definition: beamHelpers.h:136