source: branches/Couenne/Couenne/src/simplify/simplifiers.cpp @ 531

Last change on this file since 531 was 357, checked in by pbelotti, 13 years ago

fixed some memory leaks

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