ocra-recipes
Doxygen documentation for the ocra-recipes repository
DotProductFunction.cpp
Go to the documentation of this file.
1 #if 0
2 
4 #include "ocra/optim/FunctionProperties.h"
5 
6 #include <stdexcept>
7 
8 using namespace xde;
9 
10 namespace ocra
11 {
12  //TODO [todo]
13 /* DotProductFunction::DotProductFunction(Function *f1, Function *f2)
14  :Function(*FunctionProperties::getConcatenatedVariable(*f1, *f2), 1,
15  L_UNDEFINED, C_UNDEFINED,
16  FunctionProperties::getAddContinuityProperty(*f1,*f2),
17  FunctionProperties::computeHessian(*f1,*f2), FunctionProperties::computeGradient(*f1,*f2)),
18  _f1(f1), _f2(f2), _twoFunctions(true), _tmp(f->getVariable().getSize())
19  {
20  //TODO [todo] : check size
21  _f1->attach(*this);
22  _f2->attach(*this);
23  }*/
24 
25  DotProductFunction::DotProductFunction(Function* f, const Vector& v)
26  :Function(f->getVariable(), 1, L_UNDEFINED, C_UNDEFINED,
27  f->getContinuityProperty(), f->canComputeHessian(), f->canComputeGradient())
28  , _f1(f), _v(v), _f2(NULL), _twoFunctions(false), _tmp(f->getVariable().getSize())
29  {
30  assert(_f1->getDimension()==_v.getSize());
31  _f1->attach(*this);
32  }
33 
34 
35  void DotProductFunction::changeV(const Vector& v)
36  {
37  if (_twoFunctions)
38  throw std::runtime_error("[ocra::SubtractionFunction::changeV]: no vector involved in this DotProductFunction");
39  else
40  _v.copyValuesFrom(v);
41  invalidate();
42  }
43 
44 
45  void DotProductFunction::computeValue(void) const
46  {
47  if (_twoFunctions)
48  _value[0] = CML_dot(_f1->getValues(), _f2->getValues());
49  else
50  _value[0] = CML_dot(_f1->getValues(), _v);
51  }
52 
53 
55  {
56  //_tmp.setToZero();
57  if (_twoFunctions)
58  {
59  CML_gemv<'t'>(1., _f1->getGradients(), _f2->getValues(), 0., _tmp);
60  CML_gemv<'t'>(1., _f2->getGradients(), _f1->getValues(), 1., _tmp);
61  }
62  else
63  CML_gemv<'t'>(1., _f1->getGradients(), _v, 0., _tmp);
64 
65  for (cfl_size_t i=0; i<_tmp.getSize(); ++i)
66  _gradient(0,i) = _tmp[i];
67  }
68 
69  void DotProductFunction::computeHessian(void) const
70  {
71  //TODO [mineur] : might be optimizable
72  if (_twoFunctions)
73  {
74  //TODO [todo]
75  }
76  else
77  {
78  (const_cast<Matrix*>(_hessians[0]))->copyValuesFrom(*_f1->getHessian(0));
79  (const_cast<Matrix*>(_hessians[0]))->scalarMultInPlace(_v[0]);
80  for (cfl_size_t i=1; i<_f1->getDimension(); ++i)
81  {
82  for (cfl_size_t j=0; j<_x->getSize(); ++j)
83  {
84  VectorWrap hessianColumn = ( (const_cast<Matrix*>(_hessians[0])) )->getColumnAsVector(j);
85  CML_axpy(_v[i], _f1->getHessian(i)->getColumnAsVector(j), hessianColumn);
86  }
87  }
88  }
89  }
90 
91  void DotProductFunction::computeJdot(void) const
92  {
93  if (_twoFunctions)
94  {
95  //TODO [todo]
96  }
97  else
98  CML_gemv<'t'>(1., _f1->getJdot(), _v, 0., _tmp);
99 
100  for (cfl_size_t i=0; i<_tmp.getSize(); ++i)
101  _Jdot(0,i) = _tmp[i];
102  }
103 
104  void DotProductFunction::computeJdotXdot(void) const
105  {
106  //this can be optimized with eigen : we're doing a dot product but getJdot gives a 1*n matrix
107  _JdotXdot[0] = 0;
108  for (cfl_size_t i=0; i<_tmp.getSize(); ++i)
109  _JdotXdot[0] += getJdot()(0,i)*(*_x->getTimeDerivative())[i];
110  }
111 
112 
114  {
115  }
116 
117  void DotProductFunction::initHessian(void)
118  {
119  _hessians[0] = new Matrix(_x->getSize(), _x->getSize());
120  }
121 }
122 
123 #endif
124 
125 // cmake:sourcegroup=toBeUpdated
126 
void invalidate()
Definition: Function.h:325
VectorXd & _value
Definition: Function.h:312
virtual void computeJdot(void) const
void changeV(const Vector &v)
virtual void computeHessian(void) const
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
virtual void computeGradient(void) const
Declaration file of the DotProductFunction class.
virtual void computeValue(void) const
virtual void doUpdateSize(void)
virtual void computeJdotXdot(void) const