ocra-recipes
Doxygen documentation for the ocra-recipes repository
DiagonalLinearFunction.h
Go to the documentation of this file.
1 
13 #ifndef _OCRABASE_DIAGONAL_LINEAR_FUNCTION_H_
14 #define _OCRABASE_DIAGONAL_LINEAR_FUNCTION_H_
15 
16 #ifdef WIN32
17 # pragma once
18 #endif
19 
20 // includes
22 
28 namespace ocra
29 {
41  {
42  // ------------------------ structures --------------------------------------
43  public:
44  typedef LinearFunction functionType_t; //< alias on the type of the mother class. Needed to duplicate the function tree.
45 
46  // ------------------------ constructors ------------------------------------
47  private:
53 
54  public:
74  template<class VectorBase1, class VectorBase2>
75  DiagonalLinearFunction(Variable& x, const VectorBase1& d, const VectorBase2& b,
76  const bool useDefaultValue = false, const double defaultDiagValue = 1.,
77  const double defaultbValue = 0.);
78 
79  template<class VectorBase>
80  DiagonalLinearFunction(Variable& x, const double diagonalElementValue, const VectorBase& b,
81  const bool useDefaultValue = false, const double defaultbValue = 0.);
82 
83  template<class VectorBase>
84  DiagonalLinearFunction(Variable& x, const VectorBase& d, const double vectorElementValue,
85  const bool useDefaultValue = false, const double defaultDiagValue = 1.);
86 
87  DiagonalLinearFunction(Variable& x, const double diagonalElementValue, const double vectorElementValue,
88  const bool useDefaultValue = false);
90 
91  protected:
94 
95 
96  // ------------------------ public interface --------------------------------
97  public:
100  inline const VectorXd& getDiagonal() const;
101  inline double getDefaultDiagonalValue() const;
102  inline double getDefaultbValue() const;
103  inline bool isUsingDefaultValue() const;
105 
110  void changeDiagonal(const VectorXd& d);
111 
116  void changeDiagonal(const double diagonalElementValue, const bool changeDefault = true);
117 
119  void changeDefaultDiagonalValue(const double v);
120 
122  void changeDefaultbValue(const double v);
123 
124  // ------------------------ protected methods -------------------------------
125  protected:
126  virtual void updateValue() const;
127 
128  virtual void doUpdateInputSizeBegin();
129  virtual void doUpdateInputSizeEnd();
130 
131  virtual void doChangeDiagonal(const VectorXd& d);
132  virtual void doChangeDiagonal(const double diagonalElementValue, const bool changeDefault = true);
133  virtual void doChangeDefaultDiagonalValue(const double v);
134  virtual void doChangeDefaultbValue(const double v);
135 
136  virtual void doChangeA(const MatrixXd& A);
137 
138  int computeDimensionFromInputSize() const;
139  void doUpdateDimensionBegin(int newDimension);
140 
141  // ------------------------ private methods ---------------------------------
142  private:
144  void buildA();
145 
146  // ------------------------ protected members -------------------------------
147  protected:
148  VectorXd _d; //< diagonal value
149  double _defaultDiagonalValue; //< default diagonal value
150  double _defaultbValue; //< default value to fill b
151  bool _useDefaultValue; //< true if the default value can be used
152  };
153 
154 
155  template <class VectorBase1, class VectorBase2>
156  inline DiagonalLinearFunction::DiagonalLinearFunction(Variable& x, const VectorBase1& d, const VectorBase2& b,
157  const bool useDefaultValue, const double defaultDiagValue,
158  const double defaultbValue)
159  :NamedInstance("diagonal linear function")
161  , CoupledInputOutputSize(true)
162  , LinearFunction(x, x.getSize()), _useDefaultValue(useDefaultValue), _defaultDiagonalValue(defaultDiagValue)
163  , _defaultbValue(defaultbValue)
164  {
165  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
166  ocra_assert(d.rows()==1 || d.cols()==1);
167  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
168  ocra_assert(b.cols()==1);
169 
170  if (d.size() != _dim)
171  throw std::runtime_error("[ocra::DiagonalLinearFunction::DiagonalLinearFunction] Size of diagonal d doesn't match the variable size");
172 
173  if (b.size() != _dim)
174  throw std::runtime_error("[ocra::DiagonalLinearFunction::DiagonalLinearFunction] Size of b doesn't match the variable size");
175 
176  _d = d;
177  buildA();
178  changeb(b);
179  }
180 
181 
182  template <class VectorBase>
183  inline DiagonalLinearFunction::DiagonalLinearFunction(Variable& x, const double diagonalElementValue, const VectorBase& b,
184  const bool useDefaultValue, const double defaultbValue)
185  :NamedInstance("diagonal linear function")
187  , CoupledInputOutputSize(true)
188  , LinearFunction(x, x.getSize()), _useDefaultValue(useDefaultValue), _defaultDiagonalValue(diagonalElementValue)
189  , _defaultbValue(defaultbValue)
190  {
191  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
192  ocra_assert(b.cols()==1);
193 
194  if (b.size() != _dim)
195  throw std::runtime_error("[ocra::DiagonalLinearFunction::DiagonalLinearFunction] Size of b doesn't match the variable size");
196 
197  _d.resize(_dim);
198  _d.setConstant(diagonalElementValue);
199  buildA();
200  changeb(b);
201  }
202 
203 
204  template <class VectorBase>
205  inline DiagonalLinearFunction::DiagonalLinearFunction(Variable& x, const VectorBase& d, const double vectorElementValue,
206  const bool useDefaultValue, const double defaultDiagValue)
207  :NamedInstance("diagonal linear function")
209  , CoupledInputOutputSize(true)
210  , LinearFunction(x, x.getSize()), _useDefaultValue(useDefaultValue), _defaultDiagonalValue(defaultDiagValue), _d(d)
211  ,_defaultbValue(vectorElementValue)
212  {
213  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
214  ocra_assert(d.rows()==1 || d.cols()==1);
215 
216  if (d.size() != _dim)
217  throw std::runtime_error("[ocra::DiagonalLinearFunction::DiagonalLinearFunction] Size of diagonal d doesn't match the variable size");
218 
219  buildA();
220  _b.resize(_dim);
221  _b.setConstant(vectorElementValue);
222 
223  }
224 
225 
226 
227  inline const VectorXd& DiagonalLinearFunction::getDiagonal() const
228  {
229  return _d;
230  }
231 
232 
234  {
235  return _defaultDiagonalValue;
236  }
237 
238 
240  {
241  return _defaultbValue;
242  }
243 
244 
246  {
247  return _useDefaultValue;
248  }
249 
250 
252 }
253 
254 #endif //_OCRABASE_DIAGONAL_LINEAR_FUNCTION_H_
255 
256 // cmake:sourcegroup=Function
257 
virtual void doChangeDefaultbValue(const double v)
void doUpdateDimensionBegin(int newDimension)
Variable & x
Definition: Function.h:309
void changeDiagonal(const VectorXd &d)
void changeb(const VectorXd &b)
void testDiagonalLinearFunction()
LinearFunction class.
void changeDefaultDiagonalValue(const double v)
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
virtual void doChangeDefaultDiagonalValue(const double v)
Declaration file of the LinearFunction class.
This class represents a variable in a mathematical sense.
Definition: Variable.h:105
const VectorXd & getDiagonal() const
virtual void doChangeA(const MatrixXd &A)
virtual void doChangeDiagonal(const VectorXd &d)
#define ocra_assert(ocra_expression)
Definition: ocra_assert.h:45
void changeDefaultbValue(const double v)
#define OCRA_STATIC_ASSERT_VECTOR_OR_DYNAMIC_MATRIX(TYPE)
Definition: MathTypes.h:31
const int & _dim
Definition: Function.h:320
DiagonalLinearFunction class.