Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RFlat2DMatrixOperators.hxx
Go to the documentation of this file.
1// Author: Martin Føll, University of Oslo (UiO) & CERN 1/2026
2
3/*************************************************************************
4 * Copyright (C) 1995-2026, Rene Brun and Fons Rademakers. *
5 * All rights reserved. *
6 * *
7 * For the licensing terms see $ROOTSYS/LICENSE. *
8 * For the list of contributors see $ROOTSYS/README/CREDITS. *
9 *************************************************************************/
10
11#ifndef ROOT_INTERNAL_ML_RFLAT2DMATRIXOPERATORS
12#define ROOT_INTERNAL_ML_RFLAT2DMATRIXOPERATORS
13
14#include <random>
15#include <algorithm>
16
18
20/**
21\class ROOT::Experimental::Internal::ML::RFlat2DMatrixOperators
22
23\brief Collection of operations applied to one or multiple flat 2D matrices.
24*/
25
27private:
29 std::size_t fSetSeed;
30
31public:
32 RFlat2DMatrixOperators(bool shuffle = true, const std::size_t setSeed = 0) : fShuffle(shuffle), fSetSeed(setSeed) {}
33
35 {
36 if (fShuffle) {
37 std::random_device rd;
38 std::mt19937 g;
39
40 if (fSetSeed == 0) {
41 g.seed(rd());
42 } else {
43 g.seed(fSetSeed);
44 }
45
46 std::size_t rows = Tensor.GetRows();
47 std::size_t cols = Tensor.GetCols();
48 ShuffledTensor.Resize(rows, cols);
49
50 // make an identity permutation map
51 std::vector<Long_t> indices(rows);
52 std::iota(indices.begin(), indices.end(), 0);
53
54 // shuffle the identity permutation to create a new permutation
55 std::shuffle(indices.begin(), indices.end(), g);
56
57 // shuffle data in the tensor with the permutation map defined above
58 for (std::size_t i = 0; i < rows; i++) {
59 std::copy(Tensor.GetData() + indices[i] * cols, Tensor.GetData() + (indices[i] + 1) * cols,
60 ShuffledTensor.GetData() + i * cols);
61 }
62 } else {
64 }
65 }
66
67 void
68 SliceTensor(RFlat2DMatrix &SlicedTensor, RFlat2DMatrix &Tensor, const std::vector<std::vector<std::size_t>> &slice)
69 {
70 const auto &rowSlice = slice[0];
71 const auto &colSlice = slice[1];
72
73 std::size_t rowStart = rowSlice[0];
74 std::size_t rowEnd = rowSlice[1];
75 std::size_t colStart = colSlice[0];
76 std::size_t colEnd = colSlice[1];
77
78 std::size_t rows = rowEnd - rowStart;
79 std::size_t cols = colEnd - colStart;
80
81 SlicedTensor.Resize(rows, cols);
82 std::copy(Tensor.GetData() + rowStart * cols, Tensor.GetData() + rowStart * cols + rows * cols,
83 SlicedTensor.GetData());
84 }
85
86 void ConcatenateTensors(RFlat2DMatrix &ConcatTensor, const std::vector<RFlat2DMatrix> &Tensors)
87 {
88 std::size_t cols = Tensors[0].GetCols();
89 std::size_t rows = 0;
90
91 for (const auto &t : Tensors) {
92 rows += t.GetRows();
93 }
94
95 ConcatTensor.Resize(rows, cols);
96
97 std::size_t index = 0;
98 for (std::size_t i = 0; i < Tensors.size(); i++) {
99 std::size_t tensorRows = Tensors[i].GetRows();
100 std::copy(Tensors[i].GetData(), Tensors[i].GetData() + tensorRows * cols,
101 ConcatTensor.GetData() + index * cols);
102 index += tensorRows;
103 }
104 }
105};
106
107} // namespace ROOT::Experimental::Internal::ML
108#endif // ROOT_INTERNAL_ML_RFLAT2DMATRIXOPERATORS
#define g(i)
Definition RSha256.hxx:105
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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
Collection of operations applied to one or multiple flat 2D matrices.
void SliceTensor(RFlat2DMatrix &SlicedTensor, RFlat2DMatrix &Tensor, const std::vector< std::vector< std::size_t > > &slice)
RFlat2DMatrixOperators(bool shuffle=true, const std::size_t setSeed=0)
void ConcatenateTensors(RFlat2DMatrix &ConcatTensor, const std::vector< RFlat2DMatrix > &Tensors)
void ShuffleTensor(RFlat2DMatrix &ShuffledTensor, RFlat2DMatrix &Tensor)
Wrapper around ROOT::RVec<float> representing a 2D matrix.