# source:stable/0.4/Couenne/src/expression/simplify/simplifiers.cpp@846

Last change on this file since 846 was 846, checked in by pbelotti, 22 months ago

revisited simplification

• Property svn:keywords set to `Author Date Id Revision`
File size: 2.5 KB
RevLine
[846]1/* \$Id\$
2 *
[1]3 * Name:    simplifiers.cpp
4 * Author:  Pietro Belotti
5 * Purpose: simplifiers for main operators (+,*,-)
6 *
7 * (C) Carnegie-Mellon University, 2006.
[1]9 */
10
[312]11#include "CouenneExprOp.hpp"
12#include "CouenneExprConst.hpp"
[1]13#include "CouennePrecisions.hpp"
14
[349]15using namespace Couenne;
[1]16
17//
18// shrink argument list
19//
20// used by + and * (for now), accepts a constant resulting from
21// applying an operator to the constants in the list of (pointers to)
22// function arguments contained in el. The constant is inserted in the
23// list if the result is not equal to null_element or if there are
24// other non-constant terms in the arglist.
25//
26// Example: f(x) + 3 + g(x) + 2 + 4
27//
28// el    = {pf, NULL, pg, NULL, NULL}
29// nargs = 5
30// c     = 3 + 2 + 4 = 9
31// null_element = 0 (for sums)
32//
33// where pf and pg are pointers to expression containing f and g,
34// resp.
35//
36// Result: el and nargs are changed to
37//
38// el    = {pf, p9, pg}
39// nargs = 3
40//
41// Another example: f(x) + 2 + g(x) + (-4) + 2
42// Result:
43// el    = {pf, pg}
44// nargs = 2
45//
46// Another example: f(x) * 3 * g(x) * 2
47//
48// el    = {pf, NULL, pg, NULL}
49// nargs = 4
50// c     = 3 * 2 = 6 != null_element = 1 (for multiplications)
51// Result:
52// el    = {pf, p6, pg}
53// nargs = 3
54//
55
56int exprOp::shrink_arglist (CouNumber c, CouNumber null_element) {
57
58  register int i=0, j=0;
59
60  bool one_fun = false;
61
62  // find first NULL spot (left by some constant)
[846]63  while ((i < nargs_) && (arglist_ [i]))
64    ++i;
[1]65
66  // no spots, leave
[846]67  if (i==nargs_)
[1]68    return 0;
69
70  // check if there is at least one non-constant expression
71  for (register int k=nargs_; k--;)
72    if (arglist_ [k]) {
73      one_fun = true;
74      break;
75    }
76
77  // add constant term if c is not null w.r.t. the operation or if it
78  // would be an empty operand list otherwise
79  if ((fabs (c - null_element) > COUENNE_EPS) || !one_fun)
80    arglist_ [i++] = new exprConst (c);
81
82  j = i;
83
84  // now shift back all operands to compress argument list
85  while (i < nargs_) {
86
87    while ((i < nargs_) && !(arglist_ [i]))
[846]88      ++i;
[1]89
90    if (i < nargs_)
91      one_fun = true;
92
[846]93    while ((i < nargs_) && (arglist_ [i])) {
94
95      //if (i != j) {
96      arglist_ [j++] = arglist_ [i++];
97        // arglist_ [i] = NULL; // useless
98        //}
99
100      //++i; ++j;
101    }
[1]102  }
103
[846]104  nargs_ = j; // doesn't change the size of the malloc'd array
[1]105
106  // only say shrinking simplified arg list if there is just the
107  // constant
108  return (nargs_ == 1);// && ((fabs (c - null_element) > COUENNE_EPS) || !one_fun));
109}
Note: See TracBrowser for help on using the repository browser.