Helios++
Helios software for LiDAR simulations
IndexedDesignMatrix.h
1 #ifndef _FLUXIONUM_INDEXED_DESIGN_MATRIX_H_
2 
3 #include <fluxionum/DesignMatrix.h>
4 #include <fluxionum/TemporalDesignMatrix.h>
5 #include <fluxionum/FluxionumTypes.h>
6 #include <filems/read/core/DesignMatrixReader.h>
7 
8 #include <vector>
9 #include <memory>
10 
11 namespace fluxionum{
12 
13 using std::vector;
14 using std::shared_ptr;
15 using std::make_shared;
16 
42 template <typename IndexType, typename VarType>
43 class IndexedDesignMatrix : public DesignMatrix<VarType> {
44 protected:
45  // *** USING *** //
46  // *************** //
48 
49  // *** ATTRIBUTES *** //
50  // ******************** //
69  vector<IndexType> indices;
75  string indexName;
76 
77 
78 public:
79  // *** STATIC METHODS *** //
80  // ************************ //
89  static inline vector<IndexType> extractIndices(
90  arma::Mat<VarType> const &X, size_t const indicesColumnIndex
91  ){
92  arma::Col<VarType> inds = X.col(indicesColumnIndex);
93  size_t const m = inds.n_rows;
94  vector<IndexType> indices(inds.n_rows);
95  for(size_t i = 0 ; i < m ; ++i) indices[i] = (IndexType) inds[i];
96  return indices;
97  }
98 
99 
100  // *** CONSTRUCTION / DESTRUCTION *** //
101  // ************************************ //
112  DesignMatrix<VarType> const &designMatrix,
113  size_t const indicesColumnIndex,
114  string const indexName="index",
115  vector<string> const &columnNames=vector<string>(0)
116  ) :
117  DesignMatrix<VarType>(
118  TemporalDesignMatrix<double, VarType>::extractNonTimeMatrix(
119  designMatrix.getX(), indicesColumnIndex
120  ),
121  designMatrix.hasColumnNames() ?
122  TemporalDesignMatrix<double, VarType>::extractNonTimeNames(
123  designMatrix.getColumnNames(),
124  indicesColumnIndex
125  ) : columnNames
126  ),
127  indices(extractIndices(designMatrix.getX(), indicesColumnIndex)),
128  indexName(
129  designMatrix.hasColumnNames() ?
130  designMatrix.getColumnName(indicesColumnIndex) :
131  indexName
132  )
133  {}
142  DesignMatrix<VarType> const &designMatrix,
143  vector<IndexType> const &indices,
144  string const indexName="index",
145  vector<string> const &columnNames=vector<string>(0)
146  ) :
147  DesignMatrix<VarType>(designMatrix),
148  indices(indices),
150  {}
159  arma::Mat<VarType> const &X,
160  size_t const indicesColumnIndex,
161  string const indexName="index",
162  vector<string> const &columnNames=vector<string>(0)
163  ) :
164  DesignMatrix<VarType>(
165  TemporalDesignMatrix<double, VarType>::extractNonTimeMatrix(
166  X, indicesColumnIndex
167  ),
169  ),
170  indices(extractIndices(X, indicesColumnIndex)),
172  {}
181  arma::Mat<VarType> const &X,
182  vector<IndexType> const &indices,
183  string const indexName="index",
184  vector<string> const &columnNames=vector<string>(0)
185  ) :
186  DesignMatrix<VarType>(X, columnNames),
187  indices(indices),
189  {}
199  string const &path,
200  string const indexName="index"
201  ){
203  std::unordered_map<string, string> kv;
204  DesignMatrix<VarType> const dm = reader.read(&kv);
205  size_t const idxCol = (size_t) std::strtoul(
206  kv.at("INDEX_COLUMN").c_str(), nullptr, 10
207  );
209  dm,
210  idxCol,
211  dm.hasColumnNames() ? dm.getColumnName(idxCol) : indexName
212  );
213  }
214  virtual ~IndexedDesignMatrix() = default;
215 
216 
217  // *** OPERATORS *** //
218  // ******************* //
224  inline IndexType& operator[] (size_t const i) {return indices[i];}
225 
226 
227  // *** METHODS *** //
228  // ***************** //
243  double const ta=0.0,
244  double const tb=1.0,
245  DiffDesignMatrixType diffType = \
246  DiffDesignMatrixType::FORWARD_FINITE_DIFFERENCES
247  ) const;
255  shared_ptr<DiffDesignMatrix<double, VarType>>
257  double const ta=0.0,
258  double const tb=1.0,
259  DiffDesignMatrixType diffType = \
260  DiffDesignMatrixType::FORWARD_FINITE_DIFFERENCES
261  ) const;
262 
263 
269  void mergeInPlace(DesignMatrix<VarType> const &dm) override{
271  IndexedDesignMatrix const &idm = static_cast<
273  >(dm);
274  indices.insert(
275  indices.end(), idm.getIndices().begin(), idm.getIndices().end()
276  );
277  }
278 
279 
280  // *** GETTERs and SETTERs *** //
281  // ***************************** //
287  inline vector<IndexType> const & getIndices() const {return indices;}
293  inline string const getIndexName() const {return indexName;}
299  inline void setIndexName(string const &indexName)
300  {this->indexName = indexName;}
301 };
302 
303 
304 }
305 
306 #define _FLUXIONUM_INDEXED_DESIGN_MATRIX_H_
307 #include <fluxionum/IndexedDesignMatrix.tpp>
308 #endif
string const & getColumnName(size_t const j) const
Obtain the name of the -th column.
Definition: AbstractDesignMatrix.h:111
vector< string > columnNames
The column names for the DesignMatrix. It can be either an empty vector when no column names are spec...
Definition: AbstractDesignMatrix.h:35
bool hasColumnNames() const
Check whether there are available column names for the AbstractDesignMatrix (true) or not (false)
Definition: AbstractDesignMatrix.h:104
vector< string > const & getColumnNames() const
Obtain a constant/read reference to the column names.
Definition: AbstractDesignMatrix.h:126
This class represents a DesignMatrix .
Definition: DesignMatrix.h:41
virtual void mergeInPlace(DesignMatrix const &dm)
Merge given DesignMatrix into this DesignMatrix.
Definition: DesignMatrix.h:119
arma::Mat< VarType > X
The design matrix .
Definition: DesignMatrix.h:52
arma::Mat< VarType > const & getX() const
Obtain a constant/read reference to the matrix.
Definition: DesignMatrix.h:219
The heart of a differential design matrix is the idea that the columns are defining the values of var...
Definition: DiffDesignMatrix.h:169
Definition: IndexedDesignMatrix.h:43
IndexedDesignMatrix(arma::Mat< VarType > const &X, vector< IndexType > const &indices, string const indexName="index", vector< string > const &columnNames=vector< string >(0))
Build an IndexedDesignMatrix from given DesignMatrix and indices.
Definition: IndexedDesignMatrix.h:180
string indexName
The name of the index field in the original DesignMatrix.
Definition: IndexedDesignMatrix.h:75
IndexedDesignMatrix(DesignMatrix< VarType > const &designMatrix, vector< IndexType > const &indices, string const indexName="index", vector< string > const &columnNames=vector< string >(0))
Build an IndexedDesignMatrix from given DesignMatrix and indices vector.
Definition: IndexedDesignMatrix.h:141
void setIndexName(string const &indexName)
Set the name of the index attribute.
Definition: IndexedDesignMatrix.h:299
IndexType & operator[](size_t const i)
Access to the -th index.
Definition: IndexedDesignMatrix.h:224
void mergeInPlace(DesignMatrix< VarType > const &dm) override
Extend DesignMatrix::mergeInPlace method so the indices are also merged.
Definition: IndexedDesignMatrix.h:269
IndexedDesignMatrix(DesignMatrix< VarType > const &designMatrix, size_t const indicesColumnIndex, string const indexName="index", vector< string > const &columnNames=vector< string >(0))
Build an IndexedDesignMatrix from given DesignMatrix and specified indices column.
Definition: IndexedDesignMatrix.h:111
vector< IndexType > const & getIndices() const
Obtain a constant/read reference to the vector of indices.
Definition: IndexedDesignMatrix.h:287
vector< IndexType > indices
The indices vector .
Definition: IndexedDesignMatrix.h:69
IndexedDesignMatrix(arma::Mat< VarType > const &X, size_t const indicesColumnIndex, string const indexName="index", vector< string > const &columnNames=vector< string >(0))
Build an IndexedDesignMatrix from given matrix and specified indices column.
Definition: IndexedDesignMatrix.h:158
static vector< IndexType > extractIndices(arma::Mat< VarType > const &X, size_t const indicesColumnIndex)
Do a copy of the indices column from given DesignMatrix .
Definition: IndexedDesignMatrix.h:89
IndexedDesignMatrix(string const &path, string const indexName="index")
Build an IndexedDesignMatrix from data in file at given path and specified index column.
Definition: IndexedDesignMatrix.h:198
DiffDesignMatrix< double, VarType > toLinearTimeDiffDesignMatrix(double const ta=0.0, double const tb=1.0, DiffDesignMatrixType diffType=DiffDesignMatrixType::FORWARD_FINITE_DIFFERENCES) const
Build a DiffDesignMatrix from the IndexedDesignMatrix assuming that the index 0 lies at while the ma...
string const getIndexName() const
Obtain the name of the index attribute.
Definition: IndexedDesignMatrix.h:293
shared_ptr< DiffDesignMatrix< double, VarType > > toLinearTimeDiffDesignMatrixPointer(double const ta=0.0, double const tb=1.0, DiffDesignMatrixType diffType=DiffDesignMatrixType::FORWARD_FINITE_DIFFERENCES) const
This class represents a DesignMatrix where each row satisfy that its elements can be placed into a co...
Definition: TemporalDesignMatrix.h:74
Class to read design matrices.
Definition: DesignMatrixReader.h:44
virtual fluxionum::DesignMatrix< VarType > read(unordered_map< string, string > *keyval=nullptr)
Read the design matrix.