libcmaes
A C++11 library for stochastic optimization with CMA-ES
 All Classes Namespaces Functions Variables Typedefs
eo_matrix.h
1 
22 #ifndef EO_MATRIX_H
23 #define EO_MATRIX_H
24 
25 #include <algorithm>
26 #include <Eigen/Dense>
27 #include <stdlib.h>
28 
29 typedef Eigen::MatrixXd dMat; // declares a column-major non-sparse matrix type of double
30 typedef Eigen::VectorXd dVec; // declares a vector of double.
31 
32 #include <unsupported/Eigen/MatrixFunctions>
33 
34 inline void removeRow(dMat& matrix, unsigned int rowToRemove)
35 {
36  unsigned int numRows = matrix.rows()-1;
37  unsigned int numCols = matrix.cols();
38 
39  if( rowToRemove < numRows )
40  matrix.block(rowToRemove,0,numRows-rowToRemove,numCols) = matrix.block(rowToRemove+1,0,numRows-rowToRemove,numCols);
41 
42  matrix.conservativeResize(numRows,numCols);
43 }
44 
45 inline void removeColumn(dMat& matrix, unsigned int colToRemove)
46 {
47  unsigned int numRows = matrix.rows();
48  unsigned int numCols = matrix.cols()-1;
49 
50  if( colToRemove < numCols )
51  matrix.block(0,colToRemove,numRows,numCols-colToRemove) = matrix.block(0,colToRemove+1,numRows,numCols-colToRemove);
52 
53  matrix.conservativeResize(numRows,numCols);
54 }
55 
56 inline void removeElement(dVec &vec, unsigned int k)
57 {
58  if (k >= vec.size())
59  return;
60  std::copy(vec.data()+k+1,vec.data()+vec.size(),vec.data()+k);
61  vec.conservativeResize(vec.size()-1);
62 }
63 
64 inline void addElement(dVec &vec, unsigned int k, const double &xk)
65 {
66  if (k >= vec.size()+1)
67  return;
68  vec.conservativeResize(vec.size()+1);
69  std::copy(vec.data()+k,vec.data()+vec.size()-1,vec.data()+k+1);
70  vec[k] = xk;
71 }
72 
73 #endif