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


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