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


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