Helios++
Helios software for LiDAR simulations
DiffDesignMatrix.h
1 #pragma once
2 
3 #include <fluxionum/FluxionumException.h>
4 #include <fluxionum/AbstractDesignMatrix.h>
5 #include <fluxionum/TemporalDesignMatrix.h>
6 #include <fluxionum/FluxionumTypes.h>
7 
8 #include <armadillo>
9 
10 #include <vector>
11 #include <string>
12 #include <sstream>
13 #include <algorithm>
14 
15 namespace fluxionum {
16 
17 using std::vector;
18 using std::string;
19 
168 template <typename TimeType, typename VarType>
169 class DiffDesignMatrix : public AbstractDesignMatrix<VarType>{
170 public:
171  // *** USING *** //
172  // *************** //
174 
175 protected:
176  // *** ATTRIBUTES *** //
177  // ******************** //
182  enum DiffDesignMatrixType diffType;
186  TimeType ta;
190  TimeType tb;
215  arma::Col<TimeType> t;
220  arma::Mat<VarType> A;
226  string timeName;
227 
228 public:
229  // *** CONSTRUCTION / DESTRUCTION *** //
230  // ************************************ //
237  vector<string> const &columnNames=vector<string>(0),
238  string const timeName="time",
239  DiffDesignMatrixType diffType=DiffDesignMatrixType::UNKNOWN
240  ) :
244  {}
255  arma::Col<TimeType> const &t,
256  arma::Mat<VarType> const &A,
257  string const &timeName="time",
258  vector<string> const &columnNames=vector<string>(0),
259  DiffDesignMatrixType diffType=DiffDesignMatrixType::UNKNOWN
260  ) :
263  ta(arma::min(t)),
264  tb(arma::max(t)),
265  t(t),
266  A(A),
268  {}
277  string const &path,
278  string const &timeName="time",
279  DiffDesignMatrixType diffType=DiffDesignMatrixType::UNKNOWN
280  ){
282  std::unordered_map<string, string> kv;
283  DesignMatrix<VarType> const dm = reader.read(&kv);
284  size_t const tCol = (size_t) std::strtoul(
285  kv.at("TIME_COLUMN").c_str(), nullptr, 10
286  );
287  if(kv.find("DIFF_TYPE") != kv.end()){
288  string diffTypeStr = kv.at("DIFF_TYPE");
289  string::iterator begin = diffTypeStr.begin();
290  string::iterator end = diffTypeStr.end();
291  std::function<bool(char const)> filter = [&] (
292  char const c
293  ) -> bool{
294  return c==' ' || c=='\t';
295  };
296  end = std::remove_if(
297  diffTypeStr.begin(), diffTypeStr.end(), filter
298  );
299  diffTypeStr = string(begin, end);
300  if(diffTypeStr == "FORWARD_FINITE_DIFFERENCES"){
301  diffType = DiffDesignMatrixType::FORWARD_FINITE_DIFFERENCES;
302  }
303  else if(diffTypeStr == "CENTRAL_FINITE_DIFFERENCES"){
304  diffType = DiffDesignMatrixType::CENTRAL_FINITE_DIFFERENCES;
305  }
306  }
309  dm.getX(), tCol
310  ),
312  dm.getX(), tCol
313  ),
314  dm.hasColumnNames() ? dm.getColumnName(tCol) : timeName,
316  dm.getColumnNames(), tCol
317  ),
318  diffType
319  );
320  }
335  DiffDesignMatrixType const diffType,
336  bool const sort=true
337  ) :
338  AbstractDesignMatrix<VarType>(tdm.getColumnNames())
339  {
340  switch (diffType) {
341  case DiffDesignMatrixType::FORWARD_FINITE_DIFFERENCES:{
342  arma::uvec const tSort = (sort) ?
343  arma::sort_index(tdm.getTimeVector()) : arma::uvec();
344  arma::Col<TimeType> const t = (sort) ?
345  tdm.getTimeVector()(tSort) : tdm.getTimeVector();
346  arma::Mat<VarType> const X = (sort) ?
347  tdm.getX().rows(tSort) : tdm.getX();
348  arma::Mat<VarType> DXDT = arma::diff(X, 1, 0);
349  DXDT.each_col() /= arma::diff(t, 1);
350  *this = DiffDesignMatrix(
351  t.subvec(0, t.n_elem-2),
352  DXDT,
353  tdm.getTimeName(),
354  getColumnNames(),
355  diffType
356  );
357  break;
358  }
359  case DiffDesignMatrixType::CENTRAL_FINITE_DIFFERENCES:{
360  arma::uvec const tSort = (sort) ?
361  arma::sort_index(tdm.getTimeVector()) : arma::uvec();
362  arma::Col<TimeType> const t = (sort) ?
363  tdm.getTimeVector()(tSort) : tdm.getTimeVector();
364  arma::Mat<VarType> const X = (sort) ?
365  tdm.getX().rows(tSort) : tdm.getX();
366  arma::Col<TimeType> T(
367  (t.subvec(0, t.n_elem-2) + t.subvec(1, t.n_elem-1))
368  );
369  arma::Mat<VarType> DXDT(
370  (X.rows(2, X.n_rows-1) - X.rows(0, X.n_rows-3))
371  );
372  DXDT.each_col() /= arma::diff(T, 1);
373  T = T.subvec(0, T.n_elem-2) / 2.0;
374  *this = DiffDesignMatrix(
375  T,
376  DXDT,
377  tdm.getTimeName(),
378  getColumnNames(),
379  diffType
380  );
381  break;
382  }
383  default:{
384  std::stringstream ss;
385  ss << "DiffDesignMatrix::DiffDesignMatrix("
386  "TemporalDesignMatrix const &, DiffType const) failed."
387  "\n\tUnexpected differential type";
388  throw FluxionumException(ss.str());
389  }
390  }
391  }
392 
393  // *** OPERATORS *** //
394  // ******************* //
405  inline VarType& operator() (size_t const i, size_t const j) override
406  {return A.at(i, j);}
415  inline TimeType& operator[] (size_t const i) {return t.at(i);}
416 
417  // *** METHODS *** //
418  // ***************** //
430  virtual void shiftTime(TimeType const x){
431  t += x;
432  }
433 
434 
435  // *** GETTERs and SETTERs *** //
436  // ***************************** //
443  inline size_t getNumRows() const override {return A.n_rows;}
450  inline size_t getNumColumns() const override {return A.n_cols;}
457  inline size_t getNumElements() const override {return A.n_elem;}
463  inline DiffDesignMatrixType getDiffType() const {return diffType;}
469  inline string const & getTimeName() const {return timeName;}
475  inline arma::Col<TimeType> const & getTimeVector() const {return t;}
481  inline arma::Mat<VarType> const & getA() const {return A;}
482 };
483 
484 }
The abstract class which represents the fundamentals of any design matrix.
Definition: AbstractDesignMatrix.h:26
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
arma::Mat< T > 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
string timeName
The name of the time field.
Definition: DiffDesignMatrix.h:226
DiffDesignMatrix(arma::Col< TimeType > const &t, arma::Mat< VarType > const &A, string const &timeName="time", vector< string > const &columnNames=vector< string >(0), DiffDesignMatrixType diffType=DiffDesignMatrixType::UNKNOWN)
Build a DiffDesignMatrix from given armadillo column vector of time values and given armadillo matrix...
Definition: DiffDesignMatrix.h:254
TimeType & operator[](size_t const i)
Access to the component of the sorted time vector.
Definition: DiffDesignMatrix.h:415
TimeType tb
The end value for the differential time interval, .
Definition: DiffDesignMatrix.h:190
size_t getNumRows() const override
Obtain the number of rows of the DiffDesignMatrix .
Definition: DiffDesignMatrix.h:443
DiffDesignMatrix(vector< string > const &columnNames=vector< string >(0), string const timeName="time", DiffDesignMatrixType diffType=DiffDesignMatrixType::UNKNOWN)
Build a DiffDesignMatrix with no data.
Definition: DiffDesignMatrix.h:236
virtual void shiftTime(TimeType const x)
Add given time to all time values in vector .
Definition: DiffDesignMatrix.h:430
string const & getTimeName() const
Obtain the name of the time attribute.
Definition: DiffDesignMatrix.h:469
size_t getNumColumns() const override
Obtain the number of columns of the DiffDesignMatrix .
Definition: DiffDesignMatrix.h:450
DiffDesignMatrix(string const &path, string const &timeName="time", DiffDesignMatrixType diffType=DiffDesignMatrixType::UNKNOWN)
Build a DiffDesignMatrix from data in file at given path.
Definition: DiffDesignMatrix.h:276
arma::Mat< VarType > const & getA() const
Obtain a constant/read reference to the matrix.
Definition: DiffDesignMatrix.h:481
arma::Col< TimeType > t
The sorted series of time values so is strictly satisfied and the -th time value corresponds to the ...
Definition: DiffDesignMatrix.h:215
enum DiffDesignMatrixType diffType
Specify the type of differential of the DiffDesignMatrix.
Definition: DiffDesignMatrix.h:182
arma::Col< TimeType > const & getTimeVector() const
Obtain the sorted series of time values DiffDesignMatrix::t.
Definition: DiffDesignMatrix.h:475
DiffDesignMatrix(TemporalDesignMatrix< TimeType, VarType > const &tdm, DiffDesignMatrixType const diffType, bool const sort=true)
Build a DiffDesignMatrix from given TemporalDesignMatrix.
Definition: DiffDesignMatrix.h:333
arma::Mat< VarType > A
The matrix, whether it comes from forward finite differences or from central finite differences.
Definition: DiffDesignMatrix.h:220
size_t getNumElements() const override
Obtain the number of elements of the DiffDesignMatrix .
Definition: DiffDesignMatrix.h:457
VarType & operator()(size_t const i, size_t const j) override
Access to the component of the design matrix.
Definition: DiffDesignMatrix.h:405
TimeType ta
The starting value for differential time interval, .
Definition: DiffDesignMatrix.h:186
DiffDesignMatrixType getDiffType() const
Obtain the type of differential of the DiffDesignMatrix.
Definition: DiffDesignMatrix.h:463
Base class for fluxionum exceptions.
Definition: FluxionumException.h:16
This class represents a DesignMatrix where each row satisfy that its elements can be placed into a co...
Definition: TemporalDesignMatrix.h:74
string const & getTimeName() const
Obtain the name of the time attribute.
Definition: TemporalDesignMatrix.h:424
arma::Col< TimeType > const & getTimeVector() const
Obtain a constant/read reference to the time vector .
Definition: TemporalDesignMatrix.h:418
Class to read design matrices.
Definition: DesignMatrixReader.h:44
virtual fluxionum::DesignMatrix< VarType > read(unordered_map< string, string > *keyval=nullptr)
Read the design matrix.