Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RFlat2DMatrixOperators.cxx
Go to the documentation of this file.
2
3#include <algorithm>
4#include <numeric>
5#include <random>
6
8#include "Rtypes.h"
9
11
13
15{
16 // nothing to permute, no copy
17 if (!fShuffle) {
18 return Tensor;
19 }
20
21 std::random_device rd;
22 std::mt19937 g;
23
24 if (fSetSeed == 0) {
25 g.seed(rd());
26 } else {
27 g.seed(fSetSeed);
28 }
29
30 std::size_t rows = Tensor.GetRows();
31 std::size_t cols = Tensor.GetCols();
32 ShuffledTensor.Resize(rows, cols);
33
34 // make an identity permutation map
35 std::vector<Long_t> indices(rows);
36 std::iota(indices.begin(), indices.end(), 0);
37
38 // shuffle the identity permutation to create a new permutation
39 std::shuffle(indices.begin(), indices.end(), g);
40
41 // shuffle data in the tensor with the permutation map defined above
42 for (std::size_t i = 0; i < rows; i++) {
43 std::copy(Tensor.GetData() + indices[i] * cols, Tensor.GetData() + (indices[i] + 1) * cols,
44 ShuffledTensor.GetData() + i * cols);
45 }
46
47 return ShuffledTensor;
48}
49
51 const std::vector<std::vector<std::size_t>> &slice)
52{
53 const auto &rowSlice = slice[0];
54 const auto &colSlice = slice[1];
55
56 std::size_t rowStart = rowSlice[0];
57 std::size_t rowEnd = rowSlice[1];
58 std::size_t colStart = colSlice[0];
59 std::size_t colEnd = colSlice[1];
60
61 std::size_t rows = rowEnd - rowStart;
62 std::size_t cols = colEnd - colStart;
63
64 SlicedTensor.Resize(rows, cols);
65 std::copy(Tensor.GetData() + rowStart * cols, Tensor.GetData() + rowStart * cols + rows * cols,
66 SlicedTensor.GetData());
67}
68
69void RFlat2DMatrixOperators::ConcatenateTensors(RFlat2DMatrix &ConcatTensor, const std::vector<RFlat2DMatrix> &Tensors)
70{
71 std::size_t cols = Tensors[0].GetCols();
72 std::size_t rows = 0;
73
74 for (const auto &t : Tensors) {
75 rows += t.GetRows();
76 }
77
78 ConcatTensor.Resize(rows, cols);
79
80 std::size_t index = 0;
81 for (std::size_t i = 0; i < Tensors.size(); i++) {
82 std::size_t tensorRows = Tensors[i].GetRows();
83 std::copy(Tensors[i].GetData(), Tensors[i].GetData() + tensorRows * cols, ConcatTensor.GetData() + index * cols);
84 index += tensorRows;
85 }
86}
87} // namespace ROOT::Experimental::Internal::ML
#define g(i)
Definition RSha256.hxx:105
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
void SliceTensor(RFlat2DMatrix &SlicedTensor, RFlat2DMatrix &Tensor, const std::vector< std::vector< std::size_t > > &slice)
void ConcatenateTensors(RFlat2DMatrix &ConcatTensor, const std::vector< RFlat2DMatrix > &Tensors)
RFlat2DMatrix & ShuffleTensor(RFlat2DMatrix &ShuffledTensor, RFlat2DMatrix &Tensor)
Wrapper around ROOT::RVec<float> representing a 2D matrix.
void Resize(std::size_t rows, std::size_t cols)