unary_operator.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ACADO Toolkit.
3  *
4  * ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
5  * Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
6  * Milan Vukov, Rien Quirynen, KU Leuven.
7  * Developed within the Optimization in Engineering Center (OPTEC)
8  * under supervision of Moritz Diehl. All rights reserved.
9  *
10  * ACADO Toolkit is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 3 of the License, or (at your option) any later version.
14  *
15  * ACADO Toolkit is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with ACADO Toolkit; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  */
25 
26 
27 
37 
38 
40 
41 
43 {
44  fcn = 0;
45  dfcn = 0;
46  ddfcn = 0;
47  nCount = 0;
48 
49  derivative = 0;
50  derivative2 = 0;
51 }
52 
54 {
55  fcn = 0;
56  dfcn = 0;
57  ddfcn = 0;
58 
59  argument = _argument ;
60  dargument = NULL ;
61  argument_result = (double*)calloc(1,sizeof(double));
62  dargument_result = (double*)calloc(1,sizeof(double));
63  bufferSize = 1 ;
66 
67  nCount = 0;
68 
69  derivative = 0;
70  derivative2 = 0;
71 }
72 
73 
75 
76  int run1;
77 
78  fcn = 0;
79  dfcn = 0;
80  ddfcn = 0;
81 
82  bufferSize = arg.bufferSize;
83 
84  argument = arg.argument->clone();
85  derivative = 0;
86  derivative2 = 0;
87  if( arg.derivative != 0 ) derivative = arg.derivative->clone();
88  if( arg.derivative2 != 0 ) derivative2 = arg.derivative2->clone();
89 
90 // argument = arg.argument;
91 // argument->nCount++;
92 
93  if( arg.dargument == 0 ) dargument = 0;
94  else dargument = arg.dargument->clone();
95 
96  argument_result = (double*)calloc(bufferSize,sizeof(double));
97  dargument_result = (double*)calloc(bufferSize,sizeof(double));
98 
99  for( run1 = 0; run1 < bufferSize; run1++ ){
100 
101  argument_result[run1] = arg.argument_result[run1];
102  dargument_result[run1] = arg.dargument_result[run1];
103  }
104 
105  curvature = arg.curvature ;
107  cName = arg.cName ;
108 
109  nCount = 0;
110 }
111 
112 
114 
115 
116 // delete argument;
117  if( argument != 0 ){
118 
119  if( argument->nCount == 0 ){
120  delete argument;
121  argument = 0;
122  }
123  else{
124  argument->nCount--;
125  }
126  }
127  if( derivative != 0 ){
128 
129  if( derivative2 == derivative ) derivative2 = 0;
130  delete derivative;
131  derivative = 0;
132  }
133  if( derivative2 != 0 ){
134 
135  delete derivative2;
136  derivative2 = 0;
137  }
138  if( dargument != 0 ) delete dargument;
139 
140  free( argument_result );
141  free( dargument_result );
142 }
143 
144 
146 
147  if( this != &arg ){
148 
149 
150 
151 // delete argument;
152 
153  if( argument != 0 ){
154 
155  if( argument->nCount == 0 ){
156  delete argument;
157  argument = 0;
158  }
159  else{
160  argument->nCount--;
161  }
162  }
163  if( derivative != 0 ){
164 
165  if( derivative2 == derivative ) derivative2 = 0;
166  delete derivative;
167  derivative = 0;
168  }
169  if( derivative2 != 0 ){
170 
171  delete derivative2;
172  derivative2 = 0;
173  }
174  if( dargument != 0 ) delete dargument;
175 
176  free( argument_result );
177  free( dargument_result );
178 
179  argument = arg.argument->clone();
180  if( arg.derivative != 0 ) derivative = arg.derivative->clone();
181  if( arg.derivative2 != 0 ) derivative2 = arg.derivative2->clone();
182 
183 // argument = arg.argument;
184 // argument->nCount++;
185 
186  dargument = NULL ;
187  bufferSize = arg.bufferSize ;
188  argument_result = (double*)calloc(bufferSize,sizeof(double)) ;
189  dargument_result = (double*)calloc(bufferSize,sizeof(double)) ;
190 
191  curvature = arg.curvature ;
193  cName = arg.cName ;
194 
195  nCount = 0;
196  }
197  return *this;
198 }
199 
200 
201 returnValue UnaryOperator::evaluate( int number, double *x, double *result ){
202 
203  if( number >= bufferSize ){
204  bufferSize += number;
205  argument_result = (double*)realloc( argument_result,bufferSize*sizeof(double));
206  dargument_result = (double*)realloc(dargument_result,bufferSize*sizeof(double));
207  }
208  argument->evaluate( number, x , &argument_result[number] );
209  result[0] = (*fcn)( argument_result[number] );
210  return SUCCESSFUL_RETURN;
211 
212 }
213 
214 
216  VariableType *varType ,
217  int *component,
218  Operator **seed ,
219  int &nNewIS ,
220  TreeProjection ***newIS ){
221 
222  return ADforwardProtected( dim, varType, component, seed, nNewIS, newIS );
223 }
224 
225 
227  VariableType *varType ,
228  int *component,
229  Operator *seed ,
230  Operator **df ,
231  int &nNewIS ,
232  TreeProjection ***newIS ){
233 
234  return ADbackwardProtected( dim, varType, component, seed, df, nNewIS, newIS );
235 }
236 
237 
238 
240  VariableType *varType ,
241  int *component ,
242  Operator *l ,
243  Operator **S ,
244  int dimS ,
245  Operator **dfS ,
246  Operator **ldf ,
247  Operator **H ,
248  int &nNewLIS ,
249  TreeProjection ***newLIS ,
250  int &nNewSIS ,
251  TreeProjection ***newSIS ,
252  int &nNewHIS ,
253  TreeProjection ***newHIS ){
254 
255 return ADsymmetricProtected( dim, varType, component, l, S, dimS, dfS, ldf, H, nNewLIS, newLIS, nNewSIS, newSIS, nNewHIS, newHIS );
256 }
257 
258 
259 
260 
262 
263 
265 
266  return argument->isDependingOn(var);
267 }
268 
269 
271  VariableType *varType,
272  int *component,
273  BooleanType *implicit_dep ){
274 
275  return argument->isDependingOn( dim, varType, component, implicit_dep );
276 
277 }
278 
279 
281  VariableType *varType,
282  int *component,
283  BooleanType *implicit_dep ){
284 
285  if( argument->isDependingOn( dim, varType, component, implicit_dep ) == BT_TRUE ){
286  return BT_FALSE;
287  }
288 
289  return BT_TRUE;
290 }
291 
292 
294  VariableType *varType,
295  int *component,
296  BooleanType *implicit_dep ){
297 
298  if( argument->isDependingOn( dim, varType, component, implicit_dep ) == BT_TRUE ){
299  return BT_FALSE;
300  }
301 
302  return BT_TRUE;
303 }
304 
305 
307  VariableType *varType,
308  int *component,
309  BooleanType *implicit_dep ){
310 
311  if( argument->isDependingOn( dim, varType, component, implicit_dep ) == BT_TRUE ){
312  return BT_FALSE;
313  }
314 
315  return BT_TRUE;
316 }
317 
318 
320 
321  if( monotonicity != MT_UNKNOWN ) return monotonicity;
322  if( argument->getMonotonicity() == MT_CONSTANT ) return MT_CONSTANT ;
323 
324  return MT_NONMONOTONIC;
325 }
326 
327 
329 
330  if( curvature != CT_UNKNOWN ) return curvature ;
331  if( argument->getCurvature() == CT_CONSTANT ) return CT_CONSTANT;
332 
334 }
335 
336 
338 
339  monotonicity = monotonicity_;
340  return SUCCESSFUL_RETURN;
341 }
342 
343 
345 
346  curvature = curvature_;
347  return SUCCESSFUL_RETURN;
348 }
349 
350 
351 returnValue UnaryOperator::AD_forward( int number, double *x, double *seed,
352  double *f, double *df ){
353 
354  if( number >= bufferSize ){
355  bufferSize += number;
356  argument_result = (double*)realloc( argument_result,bufferSize*sizeof(double));
357  dargument_result = (double*)realloc(dargument_result,bufferSize*sizeof(double));
358  }
359  argument->AD_forward( number, x, seed, &argument_result[number],
360  &dargument_result[number] );
361 
362  f[0] = (*fcn)( argument_result[number] );
363  df[0] = (*dfcn)(argument_result[number])*dargument_result[number];
364 
365  return SUCCESSFUL_RETURN;
366 }
367 
368 
369 
370 returnValue UnaryOperator::AD_forward( int number, double *seed, double *df ){
371 
372 
373  argument->AD_forward( number, seed, &dargument_result[number] );
374 
375  df[0] = (*dfcn)(argument_result[number])*dargument_result[number];
376 
377  return SUCCESSFUL_RETURN;
378 }
379 
380 
381 returnValue UnaryOperator::AD_backward( int number, double seed, double *df ){
382  return argument->AD_backward( number, (*dfcn)(argument_result[number])*seed, df );
383 }
384 
385 
386 returnValue UnaryOperator::AD_forward2( int number, double *seed, double *dseed,
387  double *df, double *ddf ){
388 
389  double ddargument_result;
390  double dargument_result2;
391 
392  argument->AD_forward2( number, seed, dseed,
393  &dargument_result2, &ddargument_result);
394 
395  const double nn = (*dfcn)(argument_result[number]);
396 
397  df[0] = nn*dargument_result2;
398  ddf[0] = nn*ddargument_result
399  +(*ddfcn)( argument_result[number] )
400  *dargument_result2*dargument_result[number];
401 
402  return SUCCESSFUL_RETURN;
403 }
404 
405 
406 returnValue UnaryOperator::AD_backward2( int number, double seed1, double seed2,
407  double *df, double *ddf ){
408 
409  const double nn = (*dfcn)(argument_result[number]);
410 
411  argument->AD_backward2( number ,
412  seed1*nn ,
413  seed2*nn +
414  seed1*(*ddfcn)(argument_result[number])*dargument_result[number],
415  df, ddf );
416 
417  return SUCCESSFUL_RETURN;
418 }
419 
420 
421 std::ostream& UnaryOperator::print( std::ostream &stream ) const{
422 
423  return stream << "(" << cName << "(" << *argument << "))";
424 }
425 
426 
427 BooleanType UnaryOperator::isVariable( VariableType &varType, int &component ) const
428 {
429  return BT_FALSE;
430 }
431 
433 
434  if( bufferSize > 1 ){
435  bufferSize = 1;
436  argument_result = (double*)realloc( argument_result,bufferSize*sizeof(double));
437  dargument_result = (double*)realloc(dargument_result,bufferSize*sizeof(double));
438  }
439 
440  return SUCCESSFUL_RETURN;
441 }
442 
443 
444 
446 
447  return argument->enumerateVariables( indexList );
448 }
449 
450 
451 // //
452 // // PROTECTED MEMBER FUNCTIONS:
453 // // ---------------------------
454 
455 
457 
458  return operatorName;
459 }
460 
461 
463 
464  return argument->loadIndices( indexList );
465 }
466 
467 
469 
470  if( argument->isSymbolic() == BT_FALSE ) return BT_FALSE;
471  return BT_TRUE;
472 }
473 
474 
476  const std::vector< std::string >& _name
477  )
478 {
479  argument->setVariableExportName(_type, _name);
480 
481  return Operator::setVariableExportName(_type, _name);
482 }
483 
484 
486 
487  dargument = argument->differentiate( index );
488  return myProd( dargument, derivative );
489 }
490 
491 
493  VariableType *varType,
494  int *component,
495  Operator **seed,
496  int &nNewIS,
497  TreeProjection ***newIS ){
498 
499  if( dargument != 0 )
500  delete dargument;
501 
502  dargument = argument->AD_forward(dim,varType,component,seed,nNewIS,newIS);
503 
504  return myProd( dargument, derivative );
505 }
506 
507 
508 
510  VariableType *varType ,
511  int *component,
512  Operator *seed ,
513  Operator **df ,
514  int &nNewIS ,
515  TreeProjection ***newIS ){
516 
517  argument->AD_backward( dim,
518  varType,
519  component,
520  myProd( seed, derivative ),
521  df, nNewIS, newIS
522  );
523 
524  delete seed;
525  return SUCCESSFUL_RETURN;
526 }
527 
528 
530  VariableType *varType ,
531  int *component ,
532  Operator *l ,
533  Operator **S ,
534  int dimS ,
535  Operator **dfS ,
536  Operator **ldf ,
537  Operator **H ,
538  int &nNewLIS ,
539  TreeProjection ***newLIS ,
540  int &nNewSIS ,
541  TreeProjection ***newSIS ,
542  int &nNewHIS ,
543  TreeProjection ***newHIS ){
544 
545  TreeProjection dx, ddx;
546  dx = *derivative;
547  ddx = *derivative2;
548 
549  return ADsymCommon( argument, dx, ddx, dim, varType, component, l, S, dimS, dfS,
550  ldf, H, nNewLIS, newLIS, nNewSIS, newSIS, nNewHIS, newHIS );
551 }
552 
553 
554 
555 
557 
558 // end of file.
OperatorName
Definition: acado_types.hpp:72
Operator * derivative2
virtual returnValue AD_forward2(int number, double *seed1, double *seed2, double *df, double *ddf)=0
Operator * differentiate(int index)
virtual MonotonicityType getMonotonicity()=0
Abstract base class for all scalar-valued symbolic operators.
Definition: operator.hpp:60
Operator * dargument
virtual BooleanType isDependingOn(VariableType var) const =0
virtual BooleanType isPolynomialIn(int dim, VariableType *varType, int *component, BooleanType *implicit_dep)
double(* ddfcn)(double)
Operator * derivative
Abstract base class for all scalar-valued unary operators within the symbolic operators family...
virtual Operator * differentiate(int index)=0
Allows to pass back messages to the calling function.
virtual returnValue loadIndices(SymbolicIndexList *indexList)=0
virtual BooleanType isVariable(VariableType &varType, int &component) const
virtual Operator * clone() const =0
virtual BooleanType isSymbolic() const
virtual returnValue setVariableExportName(const VariableType &_type, const std::vector< std::string > &_name)
Definition: operator.cpp:223
virtual OperatorName getName()
#define CLOSE_NAMESPACE_ACADO
virtual BooleanType isSymbolic() const =0
Operator * argument
virtual returnValue AD_backward(int dim, VariableType *varType, int *component, Operator *seed, Operator **df, int &nNewIS, TreeProjection ***newIS)
Manages the indices of SymbolicVariables.
virtual NeutralElement isOneOrZero() const
virtual BooleanType isRationalIn(int dim, VariableType *varType, int *component, BooleanType *implicit_dep)
VariableType
Definition: acado_types.hpp:95
double(* dfcn)(double)
UnaryOperator & operator=(const UnaryOperator &arg)
int nCount
Definition: operator.hpp:556
virtual Operator * AD_forward(int dim, VariableType *varType, int *component, Operator **seed, int &nNewIS, TreeProjection ***newIS)=0
virtual returnValue loadIndices(SymbolicIndexList *indexList)
virtual returnValue enumerateVariables(SymbolicIndexList *indexList)=0
virtual returnValue AD_backward2(int number, double seed1, double seed2, double *df, double *ddf)
MonotonicityType monotonicity
virtual returnValue AD_backward2(int number, double seed1, double seed2, double *df, double *ddf)=0
virtual returnValue clearBuffer()
virtual BooleanType isLinearIn(int dim, VariableType *varType, int *component, BooleanType *implicit_dep)
virtual returnValue AD_symmetric(int dim, VariableType *varType, int *component, Operator *l, Operator **S, int dimS, Operator **dfS, Operator **ldf, Operator **H, int &nNewLIS, TreeProjection ***newLIS, int &nNewSIS, TreeProjection ***newSIS, int &nNewHIS, TreeProjection ***newHIS)
virtual returnValue setVariableExportName(const VariableType &_type, const std::vector< std::string > &_name)
virtual Operator * myProd(Operator *a, Operator *b)
Definition: operator.cpp:232
virtual returnValue AD_backward(int dim, VariableType *varType, int *component, Operator *seed, Operator **df, int &nNewIS, TreeProjection ***newIS)=0
returnValue ADsymmetricProtected(int dim, VariableType *varType, int *component, Operator *l, Operator **S, int dimS, Operator **dfS, Operator **ldf, Operator **H, int &nNewLIS, TreeProjection ***newLIS, int &nNewSIS, TreeProjection ***newSIS, int &nNewHIS, TreeProjection ***newHIS)
returnValue ADsymCommon(Operator *a, TreeProjection &da, TreeProjection &dda, int dim, VariableType *varType, int *component, Operator *l, Operator **S, int dimS, Operator **dfS, Operator **ldf, Operator **H, int &nNewLIS, TreeProjection ***newLIS, int &nNewSIS, TreeProjection ***newSIS, int &nNewHIS, TreeProjection ***newHIS)
Definition: operator.cpp:321
virtual std::ostream & print(std::ostream &stream) const
CurvatureType curvature
virtual MonotonicityType getMonotonicity()
virtual returnValue evaluate(int number, double *x, double *result)=0
virtual returnValue setCurvature(CurvatureType curvature_)
#define BT_TRUE
Definition: acado_types.hpp:47
virtual returnValue evaluate(int number, double *x, double *result)
NeutralElement
Definition: acado_types.hpp:64
std::string cName
double * argument_result
Abstract base class for all scalar-valued symbolic operators.
MonotonicityType
OperatorName operatorName
Implements the tree-projection operator within the family of SymbolicOperators.
CurvatureType
double * dargument_result
#define BEGIN_NAMESPACE_ACADO
Operator * ADforwardProtected(int dim, VariableType *varType, int *component, Operator **seed, int &nNewIS, TreeProjection ***newIS)
returnValue ADbackwardProtected(int dim, VariableType *varType, int *component, Operator *seed, Operator **df, int &nNewIS, TreeProjection ***newIS)
#define BT_FALSE
Definition: acado_types.hpp:49
virtual CurvatureType getCurvature()=0
virtual returnValue setMonotonicity(MonotonicityType monotonicity_)
virtual returnValue enumerateVariables(SymbolicIndexList *indexList)
virtual returnValue AD_forward2(int number, double *seed1, double *seed2, double *df, double *ddf)
double(* fcn)(double)
virtual CurvatureType getCurvature()
virtual Operator * AD_forward(int dim, VariableType *varType, int *component, Operator **seed, int &nNewIS, TreeProjection ***newIS)
virtual ~UnaryOperator()
virtual BooleanType isDependingOn(VariableType var) const


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:35:15