Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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/// \macro_output
9///
10/// \date May 2018
11/// \author Danilo Piparo
12
13void vo002_VectorCalculations()
14{
15
16 // Operations on RVec instances are made to be *fast* (vectorisation is exploited)
17 // and easy to use. RVecF, RVecD, RVecI, ..., are handy aliases for RVec<float>, RVec<double>, RVec<int>, ...
18 ROOT::RVecF v1{1., 2., 3.};
19 ROOT::RVecF v2{4., 5., 6.};
20
21 // Arithmetic operations are to be intended on pairs of elements with identical index
22 auto v_sum = v1 + v2;
23 auto v_mul = v1 * v2;
24
25 // Easy to inspect:
26 std::cout << "v1 = " << v1 << "\n"
27 << "v2 = " << v2 << "\n"
28 << "v1 + v2 = " << v_sum << "\n"
29 << "v1 * v2 = " << v_mul << std::endl;
30
31 // It's also possible to mix scalars and RVecs
32 auto v_diff_s_0 = v1 - 2;
33 auto v_diff_s_1 = 2 - v1;
34 auto v_div_s_0 = v1 / 2.;
35 auto v_div_s_1 = 2. / v1;
36
37 std::cout << v1 << " - 2 = " << v_diff_s_0 << "\n"
38 << "2 - " << v1 << " = " << v_diff_s_1 << "\n"
39 << v1 << " / 2 = " << v_div_s_0 << "\n"
40 << "2 / " << v1 << " = " << v_div_s_1 << std::endl;
41
42 // Dot product and the extraction of quantities such as Mean, Min and Max
43 // are also easy to express (see here for the full list:
44 // https://root.cern/doc/master/namespaceROOT_1_1VecOps.html)
45 auto v1_mean = Mean(v1);
46 auto v1_dot_v2 = Dot(v1, v2);
47
48 std::cout << "Mean of " << v1 << " is " << v1_mean << "\n"
49 << "Dot product of " << v1 << " and " << v2 << " is " << v1_dot_v2 << std::endl;
50
51 // Most used mathematical functions are supported
52 auto v_exp = exp(v1);
53 auto v_log = log(v1);
54 auto v_sin = sin(v1);
55
56 std::cout << "exp(" << v1 << ") = " << v_exp << "\n"
57 << "log(" << v1 << ") = " << v_log << "\n"
58 << "sin(" << v1 << ") = " << v_sin << std::endl;
59
60 // Even an optimised version of the functions is available
61 // provided that VDT is not disabled during the configuration
62#ifdef R__HAS_VDT
63 auto v_fast_exp = fast_exp(v1);
64 auto v_fast_log = fast_log(v1);
65 auto v_fast_sin = fast_sin(v1);
66
67 std::cout << "fast_exp(" << v1 << ") = " << v_fast_exp << "\n"
68 << "fast_log(" << v1 << ") = " << v_fast_log << "\n"
69 << "fast_sin(" << v1 << ") = " << v_fast_sin << std::endl;
70
71 // It may happen that a custom operation needs to be applied to the RVec.
72 // In this case, the Map utility can be used:
73 auto v_transf = Map(v1, [](double x) { return x * 2 / 3; });
74
75 std::cout << "Applying [](double x){return x * 2 / 3;} to " << v1 << " leads to " << v_transf << "\n";
76#endif
77}
RVec< PromoteType< T > > log(const RVec< T > &v)
Definition RVec.hxx:1804
auto Map(Args &&... args)
Create new collection applying a callable to the elements of the input collection.
Definition RVec.hxx:2113
RVec< PromoteType< T > > exp(const RVec< T > &v)
Definition RVec.hxx:1800
RVec< PromoteType< T > > sin(const RVec< T > &v)
Definition RVec.hxx:1814
Double_t x[n]
Definition legend1.C:17
__roodevice__ double fast_exp(double x)
__roodevice__ double fast_sin(double x)
__roodevice__ double fast_log(double x)
Double_t Mean(Long64_t n, const T *a, const Double_t *w=nullptr)
Returns the weighted mean of an array a with length n.
Definition TMath.h:1089
#define Dot(u, v)
Definition normal.c:49