Logo ROOT   6.14/05
Reference Guide
vo002_VectorCalculations.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_vecops
3 /// \notebook -nodraw
4 /// In this tutorial we learn how the RVec class can be used to
5 /// express easily mathematical operations involving arrays and scalars.
6 ///
7 /// \macro_code
8 ///
9 /// \date May 2018
10 /// \author Danilo Piparo
11 
12 using namespace ROOT::VecOps;
13 
14 void vo002_VectorCalculations()
15 {
16 
17  // Operations on RVec instances are made to be *fast* (vectorisation is exploited)
18  // and easy to use.
19  RVec<float> v1{1., 2., 3.};
20  RVec<float> v2{4., 5., 6.};
21 
22  // Arithmetic operations are to be intended on pairs of elements with identical index
23  auto v_sum = v1 + v2;
24  auto v_mul = v1 * v2;
25 
26  // Easy to inspect:
27  std::cout << "v1 = " << v1 << "\n"
28  << "v2 = " << v2 << "\n"
29  << "v1 + v2 = " << v_sum << "\n"
30  << "v1 * v2 = " << v_mul << std::endl;
31 
32  // It's also possible to mix scalars and RVecs
33  auto v_diff_s_0 = v1 - 2;
34  auto v_diff_s_1 = 2 - v1;
35  auto v_div_s_0 = v1 / 2.;
36  auto v_div_s_1 = 2. / v1;
37 
38  std::cout << v1 << " - 2 = " << v_diff_s_0 << "\n"
39  << "2 - " << v1 << " = " << v_diff_s_1 << "\n"
40  << v1 << " / 2 = " << v_div_s_0 << "\n"
41  << "2 / " << v1 << " = " << v_div_s_1 << std::endl;
42 
43  // Dot product and the extraction of quantities such as Mean, Min and Max
44  // are also easy to express (see here for the full list:
45  // https://root.cern.ch/doc/master/namespaceROOT_1_1VecOps.html)
46  auto v1_mean = Mean(v1);
47  auto v1_dot_v2 = Dot(v1, v2);
48 
49  std::cout << "Mean of " << v1 << " is " << v1_mean << "\n"
50  << "Dot product of " << v1 << " and " << v2 << " is " << v1_dot_v2 << std::endl;
51 
52  // Most used mathematical functions are supported
53  auto v_exp = exp(v1);
54  auto v_log = log(v1);
55  auto v_sin = sin(v1);
56 
57  std::cout << "exp(" << v1 << ") = " << v_exp << "\n"
58  << "log(" << v1 << ") = " << v_log << "\n"
59  << "sin(" << v1 << ") = " << v_sin << std::endl;
60 
61  // Even an optimised version of the functions is available
62  // provided that VDT is not disabled during the configuration
63 #ifdef R__HAS_VDT
64  auto v_fast_exp = fast_exp(v1);
65  auto v_fast_log = fast_log(v1);
66  auto v_fast_sin = fast_sin(v1);
67 
68  std::cout << "fast_exp(" << v1 << ") = " << v_fast_exp << "\n"
69  << "fast_log(" << v1 << ") = " << v_fast_log << "\n"
70  << "fast_sin(" << v1 << ") = " << v_fast_sin << std::endl;
71 
72  // It may happen that a custom operation needs to be applied to the RVec.
73  // In this case, the Map utitlity can be used:
74  auto v_transf = Map(v1, [](double x) { return x * 2 / 3; });
75 
76  std::cout << "Applying [](double x){return x * 2 / 3;} to " << v1 << " leads to " << v_transf << "\n";
77 #endif
78 }
auto Map(const RVec< T > &v, F &&f) -> RVec< decltype(f(v[0]))>
Create new collection applying a callable to the elements of the input collection.
Definition: RVec.hxx:627
Double_t x[n]
Definition: legend1.C:17
double sin(double)
A "std::vector"-like collection of values implementing handy operation to analyse them...
Definition: RVec.hxx:146
double Mean(const RVec< T > &v)
Get Mean.
Definition: RVec.hxx:598
auto Dot(const RVec< T > &v0, const RVec< V > &v1) -> decltype(v0[0] *v1[0])
Inner product.
Definition: RVec.hxx:582
double exp(double)
double log(double)