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