Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
vo002_VectorCalculations.C File Reference

Detailed Description

View in nbviewer Open in SWAN
In this tutorial we learn how the RVec class can be used to express easily mathematical operations involving arrays and scalars.

void vo002_VectorCalculations()
{
// Operations on RVec instances are made to be *fast* (vectorisation is exploited)
// and easy to use. RVecF, RVecD, RVecI, ..., are handy aliases for RVec<float>, RVec<double>, RVec<int>, ...
ROOT::RVecF v1{1., 2., 3.};
ROOT::RVecF v2{4., 5., 6.};
// Arithmetic operations are to be intended on pairs of elements with identical index
auto v_sum = v1 + v2;
auto v_mul = v1 * v2;
// Easy to inspect:
std::cout << "v1 = " << v1 << "\n"
<< "v2 = " << v2 << "\n"
<< "v1 + v2 = " << v_sum << "\n"
<< "v1 * v2 = " << v_mul << std::endl;
// It's also possible to mix scalars and RVecs
auto v_diff_s_0 = v1 - 2;
auto v_diff_s_1 = 2 - v1;
auto v_div_s_0 = v1 / 2.;
auto v_div_s_1 = 2. / v1;
std::cout << v1 << " - 2 = " << v_diff_s_0 << "\n"
<< "2 - " << v1 << " = " << v_diff_s_1 << "\n"
<< v1 << " / 2 = " << v_div_s_0 << "\n"
<< "2 / " << v1 << " = " << v_div_s_1 << std::endl;
// Dot product and the extraction of quantities such as Mean, Min and Max
// are also easy to express (see here for the full list:
// https://root.cern/doc/master/namespaceROOT_1_1VecOps.html)
auto v1_mean = Mean(v1);
auto v1_dot_v2 = Dot(v1, v2);
std::cout << "Mean of " << v1 << " is " << v1_mean << "\n"
<< "Dot product of " << v1 << " and " << v2 << " is " << v1_dot_v2 << std::endl;
// Most used mathematical functions are supported
auto v_exp = exp(v1);
auto v_log = log(v1);
auto v_sin = sin(v1);
std::cout << "exp(" << v1 << ") = " << v_exp << "\n"
<< "log(" << v1 << ") = " << v_log << "\n"
<< "sin(" << v1 << ") = " << v_sin << std::endl;
// Even an optimised version of the functions is available
// provided that VDT is not disabled during the configuration
#ifdef R__HAS_VDT
auto v_fast_exp = fast_exp(v1);
auto v_fast_log = fast_log(v1);
auto v_fast_sin = fast_sin(v1);
std::cout << "fast_exp(" << v1 << ") = " << v_fast_exp << "\n"
<< "fast_log(" << v1 << ") = " << v_fast_log << "\n"
<< "fast_sin(" << v1 << ") = " << v_fast_sin << std::endl;
// It may happen that a custom operation needs to be applied to the RVec.
// In this case, the Map utility can be used:
auto v_transf = Map(v1, [](double x) { return x * 2 / 3; });
std::cout << "Applying [](double x){return x * 2 / 3;} to " << v1 << " leads to " << v_transf << "\n";
#endif
}
RVec< PromoteType< T > > log(const RVec< T > &v)
Definition RVec.hxx:1841
auto Map(Args &&... args)
Create new collection applying a callable to the elements of the input collection.
Definition RVec.hxx:2150
RVec< PromoteType< T > > exp(const RVec< T > &v)
Definition RVec.hxx:1837
RVec< PromoteType< T > > sin(const RVec< T > &v)
Definition RVec.hxx:1851
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
v1 = { 1, 2, 3 }
v2 = { 4, 5, 6 }
v1 + v2 = { 5, 7, 9 }
v1 * v2 = { 4, 10, 18 }
{ 1, 2, 3 } - 2 = { -1, 0, 1 }
2 - { 1, 2, 3 } = { 1, 0, -1 }
{ 1, 2, 3 } / 2 = { 0.5, 1, 1.5 }
2 / { 1, 2, 3 } = { 2, 1, 0.666667 }
Mean of { 1, 2, 3 } is 2
Dot product of { 1, 2, 3 } and { 4, 5, 6 } is 32
exp({ 1, 2, 3 }) = { 2.71828, 7.38906, 20.0855 }
log({ 1, 2, 3 }) = { 0, 0.693147, 1.09861 }
sin({ 1, 2, 3 }) = { 0.841471, 0.909297, 0.14112 }
fast_exp({ 1, 2, 3 }) = { 2.71828, 7.38906, 20.0855 }
fast_log({ 1, 2, 3 }) = { 0, 0.693147, 1.09861 }
fast_sin({ 1, 2, 3 }) = { 0.841471, 0.909297, 0.14112 }
Applying [](double x){return x * 2 / 3;} to { 1, 2, 3 } leads to { 0.666667, 1.33333, 2 }
Date
May 2018
Author
Danilo Piparo

Definition in file vo002_VectorCalculations.C.