ocra-recipes
Doxygen documentation for the ocra-recipes repository
SolverUtilities.cpp
Go to the documentation of this file.
2 
3 
4 namespace ocra
5 {
6  namespace utils
7  {
8  using namespace details;
9 
35  };
36 
37 
38 
39  void printSolution(const VectorXd& result, const Variable& var, int space, int precision)
40  {
41  double eps = 1.e-8;
42  double zero = 1.e-15;
43  if (var.getNumberOfChildren() >0)
44  {
45  const CompositeVariable& cv = reinterpret_cast<const CompositeVariable&>(var);
46  int n=0;
47  int l;
48  for (size_t i=0; i<cv.getNumberOfChildren(); ++i)
49  {
50  l = cv(i).getSize();
51  printSolution(result.segment(n,l), cv(i), space, precision);
52  n+=l;
53  }
54  }
55  else
56  {
57  std::cout << var.getName() << ": ";
58  for (int i=0; i< result.size(); ++i)
59  {
60  if (fabs(result[i]) < eps)
61  {
62  if (result[i] > zero)
63  std::cout << std::setw(space) << "+eps" << ",";
64  else if (result[i] < -zero)
65  std::cout << std::setw(space) << "-eps" << ",";
66  else
67  std::cout << std::setw(space) << "0" << ",";
68  }
69  else
70  std::cout << std::setw(space) << std::setprecision(precision) << result[i] << ",";
71  }
72  }
73  std::cout << std::endl;
74  }
75  }
76 
78  {
79  BaseVariable w("t",2);
80  BaseVariable x("x",3);
81  BaseVariable y("y",3);
82  BaseVariable z("z",2);
83  CompositeVariable X("X");
84  CompositeVariable V("V");
85  X.add(w).add(x).add(y).add(z);
86  V.add(w).add(y);
87 
88  MatrixXd m;
89  MatrixXd A = MatrixXd::Zero(12,10);
90  MatrixXd Q = MatrixXd::Zero(10,10);
91  std::vector<int> mapping;
92 
93  m = MatrixXd::Random(4,5);
94  utils::uncompressByCol(X, V, m, A.block(3,0,4,10), mapping);
95  std::cout << m << std::endl << std::endl;
96  std::cout << A << std::endl << std::endl;
97 
98  m = MatrixXd::Random(5,5);
99  utils::addCompressed2d(m, Q, mapping);
100  std::cout << m << std::endl << std::endl;
101  std::cout << Q << std::endl << std::endl;
102 
103  int ret = system("pause");
104  }
105 }
106 
107 
108 // cmake:sourcegroup=Solvers
virtual const std::string & getName() const
Definition: Variable.cpp:164
const details::eConvertCase conversion_cases[7][7]
void addCompressed2d(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > const &_out, const std::vector< int > &mapping, double scale, bool reverseMapping)
size_t getNumberOfChildren() const
Returns 0 in BaseVariable and the number of childhoods otherwise.
Definition: Variable.cpp:223
void uncompressByCol(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > const &_out, const std::vector< int > &mapping, double scale, bool reverseMapping)
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
int getSize() const
Definition: Variable.cpp:81
This class represents a variable in a mathematical sense.
Definition: Variable.h:105
void testUtilities()
CompositeVariable & add(Variable &child)
Attach/detach the child to/from this node.
Definition: Variable.cpp:580
A concatenation of base variables and other composite variables.
Definition: Variable.h:357
Implements a basic variable.
Definition: Variable.h:304
void printSolution(const VectorXd &result, const Variable &var, int space=9, int precision=3)