powerint.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 
39 
41 
42 
43 
44 
46 {
47  nCount = 0;
48  derivative = 0;
49  derivative2 = 0;
50 }
51 
52 Power_Int::Power_Int( Operator *_argument, int _exponent ) : SmoothOperator( )
53 {
54  argument = _argument ;
55  exponent = _exponent ;
56  dargument = NULL ;
57  argument_result = (double*)calloc(1,sizeof(double));
58  dargument_result = (double*)calloc(1,sizeof(double));
59  bufferSize = 1 ;
62 
63  nCount = 0;
64  derivative = 0;
65  derivative2 = 0;
66 }
67 
68 
70 
71  int run1;
72 
73  bufferSize = arg.bufferSize;
74  exponent = arg.exponent;
75 
76  argument = arg.argument->clone();
77 
78 // argument = arg.argument;
79 // argument->nCount++;
80 
81  if( arg.dargument == NULL ){
82  dargument = NULL;
83  }
84  else{
85  dargument = arg.dargument->clone();
86  }
87 
88  argument_result = (double*)calloc(bufferSize,sizeof(double));
89  dargument_result = (double*)calloc(bufferSize,sizeof(double));
90 
91  for( run1 = 0; run1 < bufferSize; run1++ ){
92 
93  argument_result[run1] = arg.argument_result[run1];
94  dargument_result[run1] = arg.dargument_result[run1];
95 
96  }
97  curvature = arg.curvature ;
99 
100  nCount = 0;
101  derivative = 0;
102  derivative2 = 0;
103  if( arg.derivative != 0 ) {
104  derivative = arg.derivative->clone();
105  derivative2 = arg.derivative2->clone();
106  }
107 }
108 
109 
111 
112  delete argument;
113 // if( argument != 0 ){
114 //
115 // if( argument->nCount == 0 ){
116 // delete argument;
117 // argument = 0;
118 // }
119 // else{
120 // argument->nCount--;
121 // }
122 // }
123 
124  if( dargument != NULL ){
125  delete dargument;
126  }
127 
128  free( argument_result );
129  free( dargument_result );
130 
131  if( derivative != 0 ) {
132  delete derivative;
133  }
134  if( derivative2 != 0 ) {
135  delete derivative2;
136  }
137  derivative = 0;
138  derivative2 = 0;
139 }
140 
142 
143  if( this != &arg ){
144 
145 
146  delete argument;
147 // if( argument != 0 ){
148 //
149 // if( argument->nCount == 0 ){
150 // delete argument;
151 // argument = 0;
152 // }
153 // else{
154 // argument->nCount--;
155 // }
156 // }
157 
158  if( dargument != NULL ){
159  delete dargument;
160  }
161 
162  free( argument_result );
163  free( dargument_result );
164 
165  argument = arg.argument->clone();
166 
167 // argument = arg.argument;
168 // argument->nCount++;
169 
170  exponent = arg.exponent ;
171  dargument = NULL ;
172  bufferSize = arg.bufferSize ;
173  argument_result = (double*)calloc(bufferSize,sizeof(double)) ;
174  dargument_result = (double*)calloc(bufferSize,sizeof(double)) ;
175 
176  curvature = arg.curvature ;
178 
179  nCount = 0;
180  }
181 
182  return *this;
183 }
184 
185 
186 returnValue Power_Int::evaluate( int number, double *x, double *result ){
187 
188  if( number >= bufferSize ){
189  bufferSize += number;
190  argument_result = (double*)realloc( argument_result,bufferSize*sizeof(double));
191  dargument_result = (double*)realloc(dargument_result,bufferSize*sizeof(double));
192  }
193  argument->evaluate( number, x , &argument_result[number] );
194 
195  result[0] = pow( argument_result[number], exponent );
196 
197  return SUCCESSFUL_RETURN;
198 }
199 
200 
202 
204  return SUCCESSFUL_RETURN;
205 }
206 
207 
209 
210  if( exponent == 0 ){
211  return new DoubleConstant( 0.0 , NE_ZERO );
212  }
213 
214  dargument = argument->differentiate( index );
215 
216  Operator *result = myProd( derivative, dargument );
217 
218  return result;
219 }
220 
221 
223  VariableType *varType,
224  int *component,
225  Operator **seed,
226  int &nNewIS,
227  TreeProjection ***newIS ){
228 
229  if( dargument != 0 )
230  delete dargument;
231 
232  dargument = argument->AD_forward(dim,varType,component,seed,nNewIS,newIS);
233 
234  Operator *result = myProd( derivative, dargument );
235 
236  return result;
237 }
238 
239 
240 
242  VariableType *varType ,
243  int *component,
244  Operator *seed ,
245  Operator **df ,
246  int &nNewIS ,
247  TreeProjection ***newIS ){
248 
249  Operator *result = myProd( derivative, seed );
250 
251  argument->AD_backward( dim, varType, component, result, df, nNewIS, newIS );
252 
253  delete seed;
254  return SUCCESSFUL_RETURN;
255 }
256 
257 
258 
260  VariableType *varType ,
261  int *component ,
262  Operator *l ,
263  Operator **S ,
264  int dimS ,
265  Operator **dfS ,
266  Operator **ldf ,
267  Operator **H ,
268  int &nNewLIS ,
269  TreeProjection ***newLIS ,
270  int &nNewSIS ,
271  TreeProjection ***newSIS ,
272  int &nNewHIS ,
273  TreeProjection ***newHIS ){
274 
275  TreeProjection tmp( *derivative );
276  TreeProjection tmp2( *derivative2 );
277 
278  return ADsymCommon( argument, tmp, tmp2, dim, varType, component, l, S, dimS, dfS,
279  ldf, H, nNewLIS, newLIS, nNewSIS, newSIS, nNewHIS, newHIS );
280 }
281 
282 
284 
285  if( initialized ) return SUCCESSFUL_RETURN;
287 
288  Operator *powerTmp = myPowerInt( argument, exponent-1 );
289  Operator *expTmp = new DoubleConstant( (double) exponent, NE_NEITHER_ONE_NOR_ZERO );
290 
291  derivative = convert2TreeProjection(myProd( expTmp, powerTmp ));
292 
293  Operator *powerTmp2 = myPowerInt( argument, exponent-2 );
294  Operator *expTmp2 = new DoubleConstant( (double) exponent-1, NE_NEITHER_ONE_NOR_ZERO );
295  Operator *prodTmp = myProd( expTmp, expTmp2 );
296 
297  derivative2 = convert2TreeProjection(myProd( prodTmp, powerTmp2 ));
298 
299  delete powerTmp;
300  delete expTmp;
301  delete powerTmp2;
302  delete expTmp2;
303  delete prodTmp;
304 
305  return argument->initDerivative();
306 }
307 
308 
309 
310 Operator* Power_Int::substitute( int index, const Operator *sub ){
311 
312  return new Power_Int( argument->substitute( index , sub ), exponent );
313 
314 }
315 
316 
318 
319 
321 
322  return argument->isDependingOn(var);
323 }
324 
325 
327  VariableType *varType,
328  int *component,
329  BooleanType *implicit_dep ){
330 
331  if( exponent == 0 ){
332  return BT_FALSE;
333  }
334 
335  return argument->isDependingOn( dim, varType, component, implicit_dep );
336 }
337 
338 
340  VariableType *varType,
341  int *component,
342  BooleanType *implicit_dep ){
343 
344  if( exponent == 0 ){
345  return BT_TRUE;
346  }
347 
348  if( exponent == 1 && argument->isLinearIn( dim, varType, component, implicit_dep ) == BT_TRUE ){
349  return BT_TRUE;
350  }
351 
352  return BT_FALSE;
353 }
354 
355 
357  VariableType *varType,
358  int *component,
359  BooleanType *implicit_dep ){
360 
361  if( argument->isPolynomialIn( dim, varType, component, implicit_dep ) == BT_TRUE &&
362  exponent >= 0 ){
363  return BT_TRUE;
364  }
365 
366  return BT_FALSE;
367 }
368 
369 
371  VariableType *varType,
372  int *component,
373  BooleanType *implicit_dep ){
374 
375  if( argument->isRationalIn( dim, varType, component, implicit_dep ) == BT_TRUE ){
376  return BT_TRUE;
377  }
378 
379  return BT_FALSE;
380 }
381 
382 
384 
385  if( monotonicity != MT_UNKNOWN ) return monotonicity;
386 
388 
389  if( m == MT_CONSTANT ) return MT_CONSTANT;
390 
391  // if exponent is even:
392  if( fabs( ceil( ((double) exponent)/2.0 - EPS ) - ((double) exponent)/2.0 ) < 10.0*EPS ){
393 
394  if( exponent == 0 ) return MT_CONSTANT;
395 
396  return MT_NONMONOTONIC;
397  }
398  else{
399 
400  if( exponent > 0 ) return m;
401  return MT_NONMONOTONIC;
402  }
403 
404  return MT_NONMONOTONIC;
405 }
406 
407 
409 
410  if( curvature != CT_UNKNOWN ) return curvature;
411 
412  const CurvatureType cc = argument->getCurvature();
413 
414  if( cc == CT_CONSTANT ) return CT_CONSTANT;
415 
416  // if exponent is even:
417  if( fabs( ceil( ((double) exponent)/2.0 - EPS ) - ((double) exponent)/2.0 ) < 10.0*EPS ){
418 
419  if( exponent < 0 ) return CT_NEITHER_CONVEX_NOR_CONCAVE;
420  if( exponent == 0 ) return CT_CONSTANT;
421  if( cc == CT_AFFINE ) return CT_CONVEX ;
422 
424  }
425  else{
426 
427  if( exponent == 1 ) return cc;
429  }
430 
432 }
433 
434 
436 
437  monotonicity = monotonicity_;
438  return SUCCESSFUL_RETURN;
439 }
440 
441 
443 
444  curvature = curvature_;
445  return SUCCESSFUL_RETURN;
446 }
447 
448 
449 returnValue Power_Int::AD_forward( int number, double *x, double *seed,
450  double *f, double *df ){
451 
452  if( number >= bufferSize ){
453  bufferSize += number;
454  argument_result = (double*)realloc( argument_result,bufferSize*sizeof(double));
455  dargument_result = (double*)realloc(dargument_result,bufferSize*sizeof(double));
456  }
457  argument->AD_forward( number, x, seed, &argument_result[number],
458  &dargument_result[number] );
459 
460  f[0] = pow( argument_result[number],exponent );
461  df[0] = exponent*pow( argument_result[number],exponent-1 )*dargument_result[number];
462 
463  return SUCCESSFUL_RETURN;
464 }
465 
466 
467 
468 returnValue Power_Int::AD_forward( int number, double *seed, double *df ){
469 
470 
471  argument->AD_forward( number, seed, &dargument_result[number] );
472 
473  df[0] = exponent*pow( argument_result[number],exponent-1 )*dargument_result[number];
474 
475  return SUCCESSFUL_RETURN;
476 }
477 
478 
479 returnValue Power_Int::AD_backward( int number, double seed, double *df ){
480  return argument->AD_backward( number, exponent*pow( argument_result[number],
481  exponent-1 )*seed, df );
482 }
483 
484 
485 returnValue Power_Int::AD_forward2( int number, double *seed, double *dseed,
486  double *df, double *ddf ){
487 
488  double ddargument_result;
489  double dargument_result2;
490 
491  argument->AD_forward2( number, seed, dseed, &dargument_result2, &ddargument_result);
492 
493  const double nn = exponent*pow( argument_result[number],exponent-1 );
494  df[0] = nn*dargument_result2;
495  ddf[0] = nn*ddargument_result + exponent*(exponent-1)*dargument_result[number]
496  * dargument_result2*
497  pow( argument_result[number],exponent-2 ) ;
498 
499  return SUCCESSFUL_RETURN;
500 }
501 
502 
503 returnValue Power_Int::AD_backward2( int number, double seed1, double seed2,
504  double *df, double *ddf ){
505 
506  const double nn = exponent*pow(argument_result[number],exponent-1);
507 
508  argument->AD_backward2( number,
509  seed1*nn,
510  seed2*nn +
511  seed1*exponent*(exponent-1)
512  *pow(argument_result[number],exponent-2)
513  *dargument_result[number],
514  df, ddf );
515 
516  return SUCCESSFUL_RETURN;
517 }
518 
519 
520 std::ostream& Power_Int::print( std::ostream &stream ) const{
521 
522  if ( argument->getName() == ON_POWER )
523  {
524  Power* dummy = (Power*)argument;
525 
526  if ( acadoIsEqual( dummy->argument2->getValue(),1.0/((double)exponent) ) == BT_TRUE )
527  {
528  return stream << "(" << *(dummy->argument1) << ")";
529  }
530  }
531 
532  if ( exponent == 1 )
533  {
534  return stream << "(" << *argument << ")";
535  }
536  else
537  {
538  VariableType dummy1;
539  int dummy2;
540 
541  if ( ( exponent == 2 ) && ( argument->isVariable( dummy1,dummy2 ) == BT_TRUE ) )
542  {
543  return stream << "((" << *argument << ")*(" << *argument << "))";
544  }
545  else
546  {
547  return stream << "(pow(" << *argument << "," << exponent << "))";
548  }
549  }
550 }
551 
552 
553 
555 
556  return new Power_Int(*this);
557 }
558 
559 
561 
562  if( bufferSize > 1 ){
563  bufferSize = 1;
564  argument_result = (double*)realloc( argument_result,bufferSize*sizeof(double));
565  dargument_result = (double*)realloc(dargument_result,bufferSize*sizeof(double));
566  }
567 
568  return SUCCESSFUL_RETURN;
569 }
570 
571 
573 
574  return argument->enumerateVariables( indexList );
575 }
576 
577 
578 //
579 // PROTECTED MEMBER FUNCTIONS:
580 // ---------------------------
581 
582 
584 
585  return ON_POWER_INT;
586 }
587 
588 BooleanType Power_Int::isVariable( VariableType &varType, int &component ) const
589 {
590  return BT_FALSE;
591 }
592 
594 
595  return argument->loadIndices( indexList );
596 }
597 
598 
600 
601  if( argument->isSymbolic() == BT_FALSE ) return BT_FALSE;
602  return BT_TRUE;
603 }
604 
606  const std::vector< std::string >& _name
607  )
608 {
609  argument->setVariableExportName(_type, _name);
610 
611  return Operator::setVariableExportName(_type, _name);
612 }
613 
615 
616 // end of file.
OperatorName
Definition: acado_types.hpp:72
Operator * dargument
Definition: powerint.hpp:464
int exponent
Definition: powerint.hpp:461
virtual returnValue AD_forward2(int number, double *seed1, double *seed2, double *df, double *ddf)=0
virtual returnValue setMonotonicity(MonotonicityType monotonicity_)
Definition: powerint.cpp:435
TreeProjection * derivative
Definition: powerint.hpp:455
virtual MonotonicityType getMonotonicity()=0
double * dargument_result
Definition: powerint.hpp:470
Abstract base class for all scalar-valued symbolic operators.
Definition: operator.hpp:60
virtual BooleanType isDependingOn(VariableType var) const =0
BooleanType acadoIsEqual(double x, double y, double TOL)
Definition: acado_utils.cpp:88
virtual returnValue evaluate(int number, double *x, double *result)
Definition: powerint.cpp:186
BEGIN_NAMESPACE_ACADO const double EPS
virtual Operator * myPowerInt(Operator *a, int b)
Definition: operator.cpp:293
virtual Operator * differentiate(int index)=0
int bufferSize
Definition: powerint.hpp:474
Allows to pass back messages to the calling function.
virtual returnValue AD_forward2(int number, double *seed1, double *seed2, double *df, double *ddf)
Definition: powerint.cpp:485
virtual returnValue loadIndices(SymbolicIndexList *indexList)=0
Implements the scalar power operator within the symbolic operators family.
Definition: power.hpp:56
virtual Operator * clone() const =0
IntermediateState pow(const Expression &arg1, const Expression &arg2)
virtual BooleanType isSymbolic() const
Definition: powerint.cpp:599
Operator * argument
Definition: powerint.hpp:458
virtual Operator * substitute(int index, const Operator *sub)=0
virtual returnValue clearBuffer()
Definition: powerint.cpp:560
TreeProjection * derivative2
Definition: powerint.hpp:456
virtual returnValue setVariableExportName(const VariableType &_type, const std::vector< std::string > &_name)
Definition: operator.cpp:223
Operator * argument2
virtual double getValue() const
Definition: operator.cpp:211
#define CLOSE_NAMESPACE_ACADO
virtual BooleanType isSymbolic() const =0
Manages the indices of SymbolicVariables.
virtual BooleanType isDependingOn(VariableType var) const
Definition: powerint.cpp:320
VariableType
Definition: acado_types.hpp:95
virtual OperatorName getName()=0
BooleanType initialized
Definition: operator.hpp:642
virtual returnValue AD_backward2(int number, double seed1, double seed2, double *df, double *ddf)
Definition: powerint.cpp:503
virtual returnValue enumerateVariables(SymbolicIndexList *indexList)
Definition: powerint.cpp:572
virtual void powerInt(Operator &arg1, int &arg2)=0
virtual Operator * AD_forward(int dim, VariableType *varType, int *component, Operator **seed, int &nNewIS, TreeProjection ***newIS)=0
virtual returnValue enumerateVariables(SymbolicIndexList *indexList)=0
Operator * argument1
virtual returnValue loadIndices(SymbolicIndexList *indexList)
Definition: powerint.cpp:593
virtual returnValue AD_backward2(int number, double seed1, double seed2, double *df, double *ddf)=0
virtual MonotonicityType getMonotonicity()
Definition: powerint.cpp:383
CurvatureType curvature
Definition: powerint.hpp:476
Power_Int & operator=(const Power_Int &arg)
Definition: powerint.cpp:141
virtual CurvatureType getCurvature()
Definition: powerint.cpp:408
Implements the scalar power operator with integer exponent within the symbolic operators family...
Definition: powerint.hpp:55
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
virtual returnValue initDerivative()
Definition: operator.cpp:519
virtual OperatorName getName()
Definition: powerint.cpp:583
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 Operator * AD_forward(int dim, VariableType *varType, int *component, Operator **seed, int &nNewIS, TreeProjection ***newIS)
Definition: powerint.cpp:222
virtual NeutralElement isOneOrZero() const
Definition: powerint.cpp:317
virtual BooleanType isVariable(VariableType &varType, int &component) const
Definition: powerint.cpp:588
virtual BooleanType isLinearIn(int dim, VariableType *varType, int *component, BooleanType *implicit_dep)=0
virtual returnValue evaluate(int number, double *x, double *result)=0
virtual Operator * differentiate(int index)
Definition: powerint.cpp:208
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)
Definition: powerint.cpp:259
#define BT_TRUE
Definition: acado_types.hpp:47
NeutralElement
Definition: acado_types.hpp:64
virtual returnValue initDerivative()
Definition: powerint.cpp:283
virtual BooleanType isVariable(VariableType &varType, int &component) const =0
virtual BooleanType isPolynomialIn(int dim, VariableType *varType, int *component, BooleanType *implicit_dep)=0
Abstract base class for all scalar-valued symbolic operators.
MonotonicityType
virtual std::ostream & print(std::ostream &stream) const
Definition: powerint.cpp:520
Implements the tree-projection operator within the family of SymbolicOperators.
CurvatureType
virtual TreeProjection * convert2TreeProjection(Operator *a) const
Definition: operator.cpp:312
virtual BooleanType isPolynomialIn(int dim, VariableType *varType, int *component, BooleanType *implicit_dep)
Definition: powerint.cpp:356
#define BEGIN_NAMESPACE_ACADO
Abstract base class for templated evaluation of operators.
#define BT_FALSE
Definition: acado_types.hpp:49
virtual CurvatureType getCurvature()=0
virtual BooleanType isRationalIn(int dim, VariableType *varType, int *component, BooleanType *implicit_dep)=0
MonotonicityType monotonicity
Definition: powerint.hpp:477
virtual TreeProjection * clone() const
virtual returnValue AD_backward(int dim, VariableType *varType, int *component, Operator *seed, Operator **df, int &nNewIS, TreeProjection ***newIS)
Definition: powerint.cpp:241
double * argument_result
Definition: powerint.hpp:467
virtual Operator * clone() const
Definition: powerint.cpp:554
virtual Operator * substitute(int index, const Operator *sub)
Definition: powerint.cpp:310
virtual returnValue setCurvature(CurvatureType curvature_)
Definition: powerint.cpp:442
virtual returnValue setVariableExportName(const VariableType &_type, const std::vector< std::string > &_name)
Definition: powerint.cpp:605
Implements a scalar constant within the symbolic operators family.
virtual BooleanType isLinearIn(int dim, VariableType *varType, int *component, BooleanType *implicit_dep)
Definition: powerint.cpp:339
virtual BooleanType isRationalIn(int dim, VariableType *varType, int *component, BooleanType *implicit_dep)
Definition: powerint.cpp:370


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