ocra-recipes
Doxygen documentation for the ocra-recipes repository
DoubleDiagonalLinearFunction.h
Go to the documentation of this file.
1 
13 #ifndef _OCRABASE_DOUBLE_DIAGONAL_LINEAR_FUNCTION_H_
14 #define _OCRABASE_DOUBLE_DIAGONAL_LINEAR_FUNCTION_H_
15 
16 // includes
18 
24 namespace ocra
25 {
33  {
34  // ------------------------ structures --------------------------------------
35  public:
36  typedef LinearFunction functionType_t; //< alias on the type of the mother class. Needed to duplicate the function tree.
37 
38  // ------------------------ constructors ------------------------------------
39  private:
42 
43  public:
44  template<class VectorBase1, class VectorBase2, class VectorBase3>
45  DoubleDiagonalLinearFunction(Variable& x, const VectorBase1& d1, const VectorBase2& d2, const VectorBase3& b,
46  const bool useDefaultValue = false, const double defaultDiagValue1 = 1., const double defaultDiagValue2 = 1.,
47  const double defaultbValue = 0.);
48 
49  template<class VectorBase>
50  DoubleDiagonalLinearFunction(Variable& x, const double diagonalElementValue1, const double diagonalElementValue2, const VectorBase& b,
51  const bool useDefaultValue = false, const double defaultbValue = 0.);
52 
53  template<class VectorBase1, class VectorBase2>
54  DoubleDiagonalLinearFunction(Variable& x, const VectorBase1& d1 , const VectorBase2& d2, const double vectorElementValue,
55  const bool useDefaultValue = false, const double defaultValue1 = 1., const double defaultValue2 = 1.);
56 
57  DoubleDiagonalLinearFunction(Variable& x, const double diagonalElementValue1, const double diagonalElementValue2, const double vectorElementValue,
58  const bool useDefaultValue = false);
59 
60 
61  // ------------------------ public interface --------------------------------
62  public:
63  inline const VectorXd& getDiagonal1() const;
64  inline const VectorXd& getDiagonal2() const;
65  inline double getDefaultDiagonalValue1() const;
66  inline double getDefaultDiagonalValue2() const;
67  inline double getDefaultbValue() const;
68  inline bool isUsingDefaultValue() const;
69  virtual void changeDiagonal1(const VectorXd& d);
70  virtual void changeDiagonal2(const VectorXd& d);
71  virtual void changeDiagonal1(const double diagonalElementValue, const bool changeDefault = true);
72  virtual void changeDiagonal2(const double diagonalElementValue, const bool changeDefault = true);
73  virtual void changeDefaultDiagonalValue1(const double v);
74  virtual void changeDefaultDiagonalValue2(const double v);
75  virtual void changeDefaultbValue(const double v);
76 
77 
78  // ------------------------ protected methods -------------------------------
79  protected:
80  virtual void updateValue() const;
81 
82  virtual void doUpdateInputSizeBegin();
83  virtual void doUpdateInputSizeEnd();
84 
85  virtual void doChangeA(const MatrixXd& A);
86 
88  void doUpdateDimensionBegin(int newDimension);
89 
90  // ------------------------ private methods ---------------------------------
91  private:
92  void buildA();
93 
94  // ------------------------ protected members -------------------------------
95  protected:
96  VectorXd _d1;
97  VectorXd _d2;
100  double _defaultbValue; //default value to fill b
102  };
103 
104 
105 
106  template<class VectorBase1, class VectorBase2, class VectorBase3>
107  inline DoubleDiagonalLinearFunction::DoubleDiagonalLinearFunction(Variable& x,
108  const VectorBase1& d1, const VectorBase2& d2, const VectorBase3& b,
109  const bool useDefaultValue, const double defaultDiagValue1, const double defaultDiagValue2,
110  const double defaultbValue)
111  :NamedInstance("double diagonal linear function")
113  , CoupledInputOutputSize(true)
114  , LinearFunction(x, x.getSize()/2)
115  , _useDefaultValue(useDefaultValue)
116  , _defaultDiagonalValue1(defaultDiagValue1)
117  , _defaultDiagonalValue2(defaultDiagValue2)
118  , _defaultbValue(defaultbValue)
119  {
120  OCRA_STATIC_ASSERT_VECTOR_OR_DYNAMIC_MATRIX(VectorBase1); //Did you put an int instead of a double ? That would explain you arrived in the wrong ctor
121  ocra_assert(d1.rows()==1 || d1.cols()==1);
122  OCRA_STATIC_ASSERT_VECTOR_OR_DYNAMIC_MATRIX(VectorBase2); //Did you put an int instead of a double ? That would explain you arrived in the wrong ctor
123  ocra_assert(d2.rows()==1 || d2.cols()==1);
124  OCRA_STATIC_ASSERT_VECTOR_OR_DYNAMIC_MATRIX(VectorBase3); //Did you put an int instead of a double ? That would explain you arrived in the wrong ctor
125  ocra_assert(b.cols()==1);
126 
127  if (d1.size() != _dim)
128  throw std::runtime_error("[ocra::DoubleDiagonalLinearFunction::DoubleDiagonalLinearFunction] Size of diagonal d1 doesn't match the function dimension");
129 
130  if (d2.size() != _dim)
131  throw std::runtime_error("[ocra::DoubleDiagonalLinearFunction::DoubleDiagonalLinearFunction] Size of diagonal d2 doesn't match the function dimension");
132 
133  if (b.size() != _dim)
134  throw std::runtime_error("[ocra::DoubleDiagonalLinearFunction::DoubleDiagonalLinearFunction] Size of b doesn't match the function dimension");
135 
136  _d1 = d1;
137  _d2 = d2;
138  buildA();
139  changeb(b);
140  }
141 
142 
143  template<class VectorBase>
144  inline DoubleDiagonalLinearFunction::DoubleDiagonalLinearFunction(Variable& x,
145  const double diagonalElementValue1, const double diagonalElementValue2, const VectorBase& b,
146  const bool useDefaultValue, const double defaultbValue)
147  :NamedInstance("double diagonal linear function")
149  , CoupledInputOutputSize(true)
150  , LinearFunction(x, x.getSize()/2)
151  , _useDefaultValue(useDefaultValue)
152  , _defaultDiagonalValue1(diagonalElementValue1)
153  , _defaultDiagonalValue2(diagonalElementValue2)
154  , _defaultbValue(defaultbValue)
155  {
156  OCRA_STATIC_ASSERT_VECTOR_OR_DYNAMIC_MATRIX(VectorBase); //Did you put an int instead of a double ? That would explain you arrived in the wrong ctor
157  ocra_assert(b.cols()==1);
158 
159  if (b.size() != _dim)
160  throw std::runtime_error("[ocra::DiagonalLinearFunction::DiagonalLinearFunction] Size of b doesn't match the function dimension");
161 
162  _d1.resize(_dim);
163  _d1.setConstant(diagonalElementValue1);
164  _d2.resize(_dim);
165  _d2.setConstant(diagonalElementValue2);
166  buildA();
167  changeb(b);
168  }
169 
170 
171  template<class VectorBase1, class VectorBase2>
172  inline DoubleDiagonalLinearFunction::DoubleDiagonalLinearFunction(Variable& x,
173  const VectorBase1& d1 , const VectorBase2& d2, const double vectorElementValue,
174  const bool useDefaultValue, const double defaultValue1, const double defaultValue2)
175  :NamedInstance("double diagonal linear function")
177  , CoupledInputOutputSize(true)
178  , LinearFunction(x, x.getSize()/2)
179  , _useDefaultValue(useDefaultValue)
180  , _defaultDiagonalValue1(defaultValue1)
181  , _defaultDiagonalValue2(defaultValue2)
182  ,_defaultbValue(vectorElementValue)
183  {
184  OCRA_STATIC_ASSERT_VECTOR_OR_DYNAMIC_MATRIX(VectorBase1); //Did you put an int instead of a double ? That would explain you arrived in the wrong ctor
185  ocra_assert(d1.rows()==1 || d1.cols()==1);
186  OCRA_STATIC_ASSERT_VECTOR_OR_DYNAMIC_MATRIX(VectorBase2); //Did you put an int instead of a double ? That would explain you arrived in the wrong ctor
187  ocra_assert(d2.rows()==1 || d2.cols()==1);
188 
189  if (d1.size() != _dim)
190  throw std::runtime_error("[ocra::DoubleDiagonalLinearFunction::DoubleDiagonalLinearFunction] Size of diagonal d1 doesn't match the function dimension");
191 
192  if (d2.size() != _dim)
193  throw std::runtime_error("[ocra::DoubleDiagonalLinearFunction::DoubleDiagonalLinearFunction] Size of diagonal d2 doesn't match the function dimension");
194 
195  _d1 = d1;
196  _d2 = d2;
197  buildA();
198  _b.resize(_dim);
199  _b.setConstant(vectorElementValue);
200  }
201 
202  inline const VectorXd& DoubleDiagonalLinearFunction::getDiagonal1() const
203  {
204  return _d1;
205  }
206 
207  inline const VectorXd& DoubleDiagonalLinearFunction::getDiagonal2() const
208  {
209  return _d2;
210  }
211 
213  {
214  return _defaultDiagonalValue1;
215  }
216 
218  {
219  return _defaultDiagonalValue2;
220  }
221 
223  {
224  return _defaultbValue;
225  }
226 
228  {
229  return _useDefaultValue;
230  }
231 }
232 
233 #endif //_OCRABASE_DOUBLE_DIAGONAL_LINEAR_FUNCTION_H_
234 
235 // cmake:sourcegroup=Function
236 
Variable & x
Definition: Function.h:309
DoubleDiagonalLinearFunction class.
void changeb(const VectorXd &b)
LinearFunction class.
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
virtual void changeDefaultDiagonalValue1(const double v)
Declaration file of the LinearFunction class.
virtual void changeDiagonal1(const VectorXd &d)
This class represents a variable in a mathematical sense.
Definition: Variable.h:105
virtual void changeDefaultDiagonalValue2(const double v)
virtual void changeDiagonal2(const VectorXd &d)
#define ocra_assert(ocra_expression)
Definition: ocra_assert.h:45
#define OCRA_STATIC_ASSERT_VECTOR_OR_DYNAMIC_MATRIX(TYPE)
Definition: MathTypes.h:31
const int & _dim
Definition: Function.h:320
virtual void changeDefaultbValue(const double v)