addition.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  :BinaryOperator( ){}
47 
48 
49 Addition::Addition( Operator *_argument1, Operator *_argument2 )
50  :BinaryOperator( _argument1, _argument2 ){}
51 
52 
54  :BinaryOperator( arg ){}
55 
56 
58 
59 
61 
62  if( this != &arg ){
63 
65  }
66  return *this;
67 }
68 
69 
70 returnValue Addition::evaluate( int number, double *x, double *result ){
71 
72  if( number >= bufferSize ){
73  bufferSize += number;
74  argument1_result = (double*)realloc( argument1_result,bufferSize*sizeof(double));
75  argument2_result = (double*)realloc( argument2_result,bufferSize*sizeof(double));
76  dargument1_result = (double*)realloc(dargument1_result,bufferSize*sizeof(double));
77  dargument2_result = (double*)realloc(dargument2_result,bufferSize*sizeof(double));
78  }
79 
80  argument1->evaluate( number, x , &argument1_result[number] );
81  argument2->evaluate( number, x , &argument2_result[number] );
82 
83  result[0] = argument1_result[number] + argument2_result[number];
84 
85  return SUCCESSFUL_RETURN;
86 }
87 
88 
90 
92  return SUCCESSFUL_RETURN;
93 }
94 
95 
97 
100 
101  return myAdd( dargument1, dargument2 );
102 
103 }
104 
105 
106 
108  VariableType *varType,
109  int *component,
110  Operator **seed,
111  int &nNewIS,
112  TreeProjection ***newIS ){
113 
114  if( dargument1 != 0 )
115  delete dargument1;
116 
117  if( dargument2 != 0 )
118  delete dargument2;
119 
120  dargument1 = argument1->AD_forward(dim,varType,component,seed,nNewIS,newIS);
121  dargument2 = argument2->AD_forward(dim,varType,component,seed,nNewIS,newIS);
122 
123  return myAdd( dargument1, dargument2 );
124 }
125 
126 
128  VariableType *varType ,
129  int *component,
130  Operator *seed ,
131  Operator **df ,
132  int &nNewIS ,
133  TreeProjection ***newIS ){
134 
135  TreeProjection tmp;
136  tmp = *seed;
137 
138  argument1->AD_backward( dim, varType, component, tmp.clone(), df, nNewIS, newIS );
139  argument2->AD_backward( dim, varType, component, tmp.clone(), df, nNewIS, newIS );
140 
141  delete seed;
142 
143  return SUCCESSFUL_RETURN;
144 }
145 
146 
147 
149  VariableType *varType ,
150  int *component ,
151  Operator *l ,
152  Operator **S ,
153  int dimS ,
154  Operator **dfS ,
155  Operator **ldf ,
156  Operator **H ,
157  int &nNewLIS ,
158  TreeProjection ***newLIS ,
159  int &nNewSIS ,
160  TreeProjection ***newSIS ,
161  int &nNewHIS ,
162  TreeProjection ***newHIS ){
163 
164  TreeProjection dx,dy,dxx,dxy,dyy;
165 
166  dx = DoubleConstant(1.0,NE_ONE );
167  dy = DoubleConstant(1.0,NE_ONE );
168  dxx = DoubleConstant(0.0,NE_ZERO);
169  dxy = DoubleConstant(0.0,NE_ZERO);
170  dyy = DoubleConstant(0.0,NE_ZERO);
171 
172  return ADsymCommon2( argument1,argument2,dx,dy,dxx,dxy,dyy, dim, varType, component, l, S, dimS, dfS,
173  ldf, H, nNewLIS, newLIS, nNewSIS, newSIS, nNewHIS, newHIS );
174 }
175 
176 
177 
178 Operator* Addition::substitute( int index, const Operator *sub ){
179 
180  return new Addition( argument1->substitute( index , sub ),
181  argument2->substitute( index , sub ) );
182 
183 }
184 
185 
187  VariableType *varType,
188  int *component,
189  BooleanType *implicit_dep ){
190 
191  if( argument1->isLinearIn( dim, varType, component, implicit_dep ) == BT_TRUE &&
192  argument2->isLinearIn( dim, varType, component, implicit_dep ) == BT_TRUE ){
193  return BT_TRUE;
194  }
195 
196  return BT_FALSE;
197 }
198 
199 
201  VariableType *varType,
202  int *component,
203  BooleanType *implicit_dep ){
204 
205  if( argument1->isPolynomialIn( dim, varType, component, implicit_dep ) == BT_TRUE &&
206  argument2->isPolynomialIn( dim, varType, component, implicit_dep ) == BT_TRUE ){
207  return BT_TRUE;
208  }
209 
210  return BT_FALSE;
211 }
212 
213 
215  VariableType *varType,
216  int *component,
217  BooleanType *implicit_dep ){
218 
219  if( argument1->isRationalIn( dim, varType, component, implicit_dep ) == BT_TRUE &&
220  argument2->isRationalIn( dim, varType, component, implicit_dep ) == BT_TRUE ){
221  return BT_TRUE;
222  }
223 
224  return BT_FALSE;
225 }
226 
227 
229 
230  if( monotonicity != MT_UNKNOWN ) return monotonicity;
231 
232  MonotonicityType m1, m2;
233 
234  m1 = argument1->getMonotonicity();
235  m2 = argument2->getMonotonicity();
236 
237  if( m1 == MT_CONSTANT ){
238 
239  if( m2 == MT_CONSTANT ) return MT_CONSTANT ;
240  if( m2 == MT_NONDECREASING ) return MT_NONDECREASING;
241  if( m2 == MT_NONINCREASING ) return MT_NONINCREASING;
242 
243  return MT_NONMONOTONIC;
244  }
245 
246  if( m1 == MT_NONDECREASING ){
247 
248  if( m2 == MT_CONSTANT ) return MT_NONDECREASING;
249  if( m2 == MT_NONDECREASING ) return MT_NONDECREASING;
250 
251  return MT_NONMONOTONIC;
252  }
253 
254  if( m1 == MT_NONINCREASING ){
255 
256  if( m2 == MT_CONSTANT ) return MT_NONINCREASING;
257  if( m2 == MT_NONINCREASING ) return MT_NONINCREASING;
258 
259  return MT_NONMONOTONIC;
260  }
261 
262  return MT_NONMONOTONIC;
263 }
264 
265 
267 
268  if( curvature != CT_UNKNOWN ) return curvature;
269 
270  CurvatureType c1, c2;
271 
272  c1 = argument1->getCurvature();
273  c2 = argument2->getCurvature();
274 
275  if( c1 == CT_CONSTANT ) return c2;
276 
277  if( c1 == CT_AFFINE ){
278 
279  if( c2 == CT_CONSTANT ) return CT_AFFINE ;
280  if( c2 == CT_AFFINE ) return CT_AFFINE ;
281  if( c2 == CT_CONVEX ) return CT_CONVEX ;
282  if( c2 == CT_CONCAVE ) return CT_CONCAVE ;
283  }
284 
285  if( c1 == CT_CONVEX ){
286 
287  if( c2 == CT_CONSTANT ) return CT_CONVEX ;
288  if( c2 == CT_AFFINE ) return CT_CONVEX ;
289  if( c2 == CT_CONVEX ) return CT_CONVEX ;
290 
292  }
293 
294  if( c1 == CT_CONCAVE ){
295 
296  if( c2 == CT_CONSTANT ) return CT_CONCAVE ;
297  if( c2 == CT_AFFINE ) return CT_CONCAVE ;
298  if( c2 == CT_CONCAVE ) return CT_CONCAVE ;
299 
301  }
302 
304 }
305 
306 
307 double Addition::getValue() const
308 {
309  if ( ( argument1 == 0 ) || ( argument2 == 0 ) )
310  return INFTY;
311 
312  if ( ( acadoIsEqual( argument1->getValue(),INFTY ) == BT_TRUE ) ||
314  return INFTY;
315 
316  return (argument1->getValue() + argument2->getValue());
317 }
318 
319 
320 returnValue Addition::AD_forward( int number, double *x, double *seed,
321  double *f, double *df ){
322 
323  if( number >= bufferSize ){
324  bufferSize += number;
325  argument1_result = (double*)realloc( argument1_result,bufferSize*sizeof(double));
326  argument2_result = (double*)realloc( argument2_result,bufferSize*sizeof(double));
327  dargument1_result = (double*)realloc(dargument1_result,bufferSize*sizeof(double));
328  dargument2_result = (double*)realloc(dargument2_result,bufferSize*sizeof(double));
329  }
330 
331  argument1->AD_forward( number, x, seed, &argument1_result[number],
332  &dargument1_result[number] );
333  argument2->AD_forward( number, x, seed, &argument2_result[number],
334  &dargument2_result[number] );
335 
336  f[0] = argument1_result[number] + argument2_result[number];
337  df[0] = dargument1_result[number] + dargument2_result[number];
338 
339  return SUCCESSFUL_RETURN;
340 }
341 
342 
343 
344 returnValue Addition::AD_forward( int number, double *seed, double *df ){
345 
346  argument1->AD_forward( number, seed, &dargument1_result[number] );
347  argument2->AD_forward( number, seed, &dargument2_result[number] );
348 
349  df[0] = dargument1_result[number] + dargument2_result[number];
350 
351  return SUCCESSFUL_RETURN;
352 }
353 
354 
355 returnValue Addition::AD_backward( int number, double seed, double *df ){
356 
357  argument1->AD_backward( number, seed, df );
358  argument2->AD_backward( number, seed, df );
359 
360  return SUCCESSFUL_RETURN;
361 }
362 
363 
364 returnValue Addition::AD_forward2( int number, double *seed, double *dseed,
365  double *df, double *ddf ){
366 
367  double ddargument1_result;
368  double ddargument2_result;
369  double dargument_result1;
370  double dargument_result2;
371 
372  argument1->AD_forward2( number, seed, dseed,
373  &dargument_result1, &ddargument1_result);
374  argument2->AD_forward2( number, seed, dseed,
375  &dargument_result2, &ddargument2_result);
376 
377  df[0] = dargument_result1 + dargument_result2 ;
378  ddf[0] = ddargument1_result + ddargument2_result ;
379 
380  return SUCCESSFUL_RETURN;
381 }
382 
383 
384 returnValue Addition::AD_backward2( int number, double seed1, double seed2,
385  double *df, double *ddf ){
386 
387  argument1->AD_backward2( number, seed1, seed2, df, ddf );
388  argument2->AD_backward2( number, seed1, seed2, df, ddf );
389 
390  return SUCCESSFUL_RETURN;
391 }
392 
393 
394 std::ostream& Addition::print( std::ostream &stream ) const{
395 
396  if ( ( acadoIsFinite( argument1->getValue() ) == BT_FALSE ) ||
398  {
399  return stream << "(" << *argument1 << "+" << *argument2 << ")";
400  }
401  else
402  {
403  return stream << "((real_t)(" << (argument1->getValue() + argument2->getValue()) << "))";
404  }
405 }
406 
407 
409 
410  if( argument1 != 0 && argument2 != 0 ){
411  if( argument1->isOneOrZero() == NE_ZERO ) return argument2->clone();
412  if( argument2->isOneOrZero() == NE_ZERO ) return argument1->clone();
413  }
414 
415  return new Addition(*this);
416 }
417 
418 
420 
421  return ON_ADDITION;
422 
423 }
424 
425 
426 
428 
429 // end of file.
OperatorName
Definition: acado_types.hpp:72
virtual returnValue AD_forward2(int number, double *seed1, double *seed2, double *df, double *ddf)
Definition: addition.cpp:364
Operator * dargument1
virtual returnValue AD_forward2(int number, double *seed1, double *seed2, double *df, double *ddf)=0
virtual MonotonicityType getMonotonicity()=0
Operator * dargument2
Abstract base class for all scalar-valued binary operators within the symbolic operators family...
Abstract base class for all scalar-valued symbolic operators.
Definition: operator.hpp:60
BooleanType acadoIsEqual(double x, double y, double TOL)
Definition: acado_utils.cpp:88
const double INFTY
virtual CurvatureType getCurvature()
Definition: addition.cpp:266
virtual NeutralElement isOneOrZero() const =0
CurvatureType curvature
double * argument1_result
virtual Operator * differentiate(int index)=0
virtual BooleanType isRationalIn(int dim, VariableType *varType, int *component, BooleanType *implicit_dep)
Definition: addition.cpp:214
Allows to pass back messages to the calling function.
virtual Operator * clone() const =0
virtual Operator * substitute(int index, const Operator *sub)=0
Operator * argument2
virtual double getValue() const
Definition: operator.cpp:211
virtual returnValue AD_backward2(int number, double seed1, double seed2, double *df, double *ddf)
Definition: addition.cpp:384
#define CLOSE_NAMESPACE_ACADO
VariableType
Definition: acado_types.hpp:95
double * dargument2_result
virtual Operator * AD_forward(int dim, VariableType *varType, int *component, Operator **seed, int &nNewIS, TreeProjection ***newIS)=0
returnValue ADsymCommon2(Operator *a, Operator *b, TreeProjection &dx, TreeProjection &dy, TreeProjection &dxx, TreeProjection &dxy, TreeProjection &dyy, 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:387
Operator * argument1
virtual std::ostream & print(std::ostream &stream) const
Definition: addition.cpp:394
virtual returnValue AD_backward2(int number, double seed1, double seed2, double *df, double *ddf)=0
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: addition.cpp:148
MonotonicityType monotonicity
virtual returnValue evaluate(int number, double *x, double *result)
Definition: addition.cpp:70
virtual BooleanType isPolynomialIn(int dim, VariableType *varType, int *component, BooleanType *implicit_dep)
Definition: addition.cpp:200
double * argument2_result
virtual Operator * differentiate(int index)
Definition: addition.cpp:96
virtual returnValue AD_backward(int dim, VariableType *varType, int *component, Operator *seed, Operator **df, int &nNewIS, TreeProjection ***newIS)=0
virtual BooleanType isLinearIn(int dim, VariableType *varType, int *component, BooleanType *implicit_dep)
Definition: addition.cpp:186
double * dargument1_result
Implements the scalar addition operator within the symbolic expressions family.
Definition: addition.hpp:54
virtual Operator * myAdd(Operator *a, Operator *b)
Definition: operator.cpp:246
virtual BooleanType isLinearIn(int dim, VariableType *varType, int *component, BooleanType *implicit_dep)=0
virtual returnValue evaluate(int number, double *x, double *result)=0
virtual OperatorName getName()
Definition: addition.cpp:419
#define BT_TRUE
Definition: acado_types.hpp:47
virtual BooleanType isPolynomialIn(int dim, VariableType *varType, int *component, BooleanType *implicit_dep)=0
virtual Operator * AD_forward(int dim, VariableType *varType, int *component, Operator **seed, int &nNewIS, TreeProjection ***newIS)
Definition: addition.cpp:107
BooleanType acadoIsFinite(double x, double TOL)
MonotonicityType
virtual returnValue AD_backward(int dim, VariableType *varType, int *component, Operator *seed, Operator **df, int &nNewIS, TreeProjection ***newIS)
Definition: addition.cpp:127
virtual Operator * substitute(int index, const Operator *sub)
Definition: addition.cpp:178
Implements the tree-projection operator within the family of SymbolicOperators.
CurvatureType
virtual Operator * clone() const
Definition: addition.cpp:408
BinaryOperator & operator=(const BinaryOperator &arg)
#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
virtual TreeProjection * clone() const
~Addition()
Definition: addition.cpp:57
Addition()
Definition: addition.cpp:45
virtual void addition(Operator &arg1, Operator &arg2)=0
Addition & operator=(const Addition &arg)
Definition: addition.cpp:60
virtual MonotonicityType getMonotonicity()
Definition: addition.cpp:228
Implements a scalar constant within the symbolic operators family.
virtual double getValue() const
Definition: addition.cpp:307


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