exprSum.cpp
Go to the documentation of this file.
1 /* $Id: exprSum.cpp 1147 2015-05-04 14:01:51Z stefan $
2  *
3  * Name: exprSum.cpp
4  * Author: Pietro Belotti
5  * Purpose: definition of sum expressions
6  *
7  * (C) Carnegie-Mellon University, 2006-07.
8  * This file is licensed under the Eclipse Public License (EPL)
9  */
10 
11 #include <stdlib.h>
12 
13 #include "CouenneExprSum.hpp"
14 #include "CouenneExprCopy.hpp"
15 #include "CouenneExprConst.hpp"
16 
17 using namespace Couenne;
18 
21  exprOp (al, n) { //< non-leaf expression, with argument list
22 
23  if (al==NULL) {
24  arglist_ = new expression * [1];
25  *arglist_ = new exprConst (0);
26  nargs_ = 1;
27  }
28 
29  // commutative operator, sort elements
30  if (nargs_ > 1)
31  qsort (arglist_, nargs_, sizeof (expression*), compareExpr);
32 }
33 
34 
37  exprOp (arg0, arg1) {
38 
39  // qsort (arglist_, nargs_, sizeof (expression*), compareExpr); // useless...
40 
41  if (arg0 -> compare (*arg1) > 0) { // swap elements
42  expression *swap = arglist_ [0];
43  arglist_ [0] = arglist_ [1];
44  arglist_ [1] = swap;
45  }
46 }
47 
48 
50 
52 
54 
55  if (nargs_ == 1) {
56 
57  expression *ret = arglist_ [0];
58  arglist_ [0] = NULL;
59  return ret;
60  }
61 
62  // from here on we assume the operands have been simplified
63 
64  CouNumber total = 0;
65  bool found_one = false;
66 
67  for (register int i=0; i<nargs_; i++) {
68 
69  // check for constant operands in multiplications
70 
71  if (arglist_ [i] -> Type () == CONST) {
72 
73  total += arglist_ [i] -> Value ();
74  found_one = true;
75  delete arglist_ [i];
76  arglist_ [i] = NULL;
77  }
78  }
79 
80  /*
81  if (found_one && shrink_arglist (total, 0))
82  return new exprConst (arglist_ [0] -> Value ());
83  else return NULL;
84  */
85 
86  if (found_one && shrink_arglist (total, 0.)) {
87  expression *ret = arglist_ [0];
88  arglist_ [0] = NULL;
89  return ret;
90  }
91  else return NULL;
92 }
93 
94 
96 
98 
99  // exprOp::differentiate (index); // FIX IT: why is it called?
100 
101  expression **arglist = new expression * [nargs_];
102 
103  register int nonconst = 0;
104 
105  for (int i = 0; i < nargs_; i++)
106  if (arglist_ [i] -> dependsOn (index))
107  arglist [nonconst++] = arglist_ [i] -> differentiate (index);
108 
109  if (!nonconst) {
110  delete [] arglist;
111  return new exprConst (0.);
112  }
113  else return new exprSum (arglist, nonconst);
114 }
115 
116 
119 
120  expression **all = new expression * [nargs_];
121  expression **alu = new expression * [nargs_];
122 
123  for (int i=0; i<nargs_; i++)
124  arglist_ [i] -> getBounds (all [i], alu [i]);
125 
126  lb = new exprSum (all, nargs_);
127  ub = new exprSum (alu, nargs_);
128 }
129 
130 
133 
134  CouNumber tlb, tub;
135 
136  lb = ub = 0;
137 
138  for (int i=0; i<nargs_; i++) {
139  arglist_ [i] -> getBounds (tlb, tub);
140  lb += tlb;
141  ub += tub;
142  }
143 }
144 
145 
148 
149  int linmax = arglist_ [0] -> Linearity ();
150 
151  for (register int i=1; i<nargs_; i++) {
152  register int lin = arglist_ [i] -> Linearity ();
153  if (lin > linmax) linmax = lin;
154  }
155  return linmax;
156 }
virtual int dependsOn(int *ind, int n, enum dig_type type=STOP_AT_AUX)
dependence on variable set: return cardinality of subset of the set of indices in first argument whic...
Definition: expression.cpp:172
int compareExpr(const void *e0, const void *e1)
independent comparison
int shrink_arglist(CouNumber, CouNumber)
compress argument list
Definition: exprOp.cpp:300
exprSum(expression **=NULL, int=0)
Constructors, destructor.
Definition: exprSum.cpp:20
virtual expression * differentiate(int index)
Differentiation.
Definition: exprSum.cpp:97
constant-type operator
virtual enum nodeType Type() const
Node type.
virtual void getBounds(expression *&, expression *&)
Get lower and upper bound of an expression (if any)
Definition: exprSum.cpp:118
virtual expression * simplify()
simplification
Definition: exprOp.cpp:243
expression ** arglist_
argument list is an array of pointers to other expressions
double CouNumber
main number type in Couenne
int nargs_
number of arguments (cardinality of arglist)
general n-ary operator-type expression: requires argument list.
Expression base class.
virtual expression * simplify()
Simplification.
Definition: exprSum.cpp:51
void fint * n
virtual CouNumber Value() const
value (empty)
virtual int compare(exprOp &)
compare with other generic exprOp
Definition: exprOp.cpp:71
virtual int Linearity()
Get a measure of &quot;how linear&quot; the expression is:
Definition: exprSum.cpp:147