In this tutorial we learn how the RVec class can be used to express easily mathematical operations involving arrays and scalars.
void vo002_VectorCalculations()
{
RVec<float> v1{1., 2., 3.};
RVec<float> v2{4., 5., 6.};
auto v_sum = v1 + v2;
auto v_mul = v1 * v2;
std::cout << "v1 = " << v1 << "\n"
<< "v2 = " << v2 << "\n"
<< "v1 + v2 = " << v_sum << "\n"
<< "v1 * v2 = " << v_mul << std::endl;
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;
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;
std::cout << "exp(" << v1 << ") = " << v_exp << "\n"
<< "log(" << v1 << ") = " << v_log << "\n"
<< "sin(" << v1 << ") = " << v_sin << std::endl;
#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;
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
}
Double_t Dot(const TGLVector3 &v1, const TGLVector3 &v2)
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.
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.
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.