/home/coin/SVN-release/OS-2.1.0/Couenne/src/standardize/elementBreak.cpp

Go to the documentation of this file.
00001 /* $Id: elementBreak.cpp 154 2009-06-16 18:52:53Z pbelotti $ */
00002 /*
00003  * Name:    elementBreak.cpp
00004  * Author:  Pietro Belotti
00005  * Purpose: decompose element of sum if it is of the form cx or x
00006  *
00007  * (C) Carnegie-Mellon University, 2007. 
00008  * This file is licensed under the Common Public License (CPL)
00009  */
00010 
00011 #include "expression.hpp"
00012 
00016 
00017 void elementBreak (expression *arg, int &index, CouNumber &coeff) {
00018 
00019   CouNumber oppMulCoe = 1.;
00020   bool isMul = false;
00021   index = -1;
00022 
00023   if (arg -> Linearity () <= LINEAR) { 
00024 
00025     // check if it's w, c*w, or w/c. If more complicated (sum or
00026     // subtraction) nevermind
00027 
00028     switch (arg -> code ()) { // check element of sum
00029 
00030     case COU_EXPRCONST: // it is a constant, nevermind
00031       break; 
00032 
00033     case COU_EXPRVAR: // it is a simple variable, w
00034       index = arg -> Index ();
00035       coeff = 1.;
00036       break;
00037 
00038     case COU_EXPROPP: { // it is the opposite of a simple variable or of
00039                         // another linear term
00040 
00041       index = arg -> Argument () -> Index (); 
00042       coeff = -1.;
00043 
00044       oppMulCoe = -1; // to be used below
00045       arg = arg -> Argument (); // transfer analysis to argument of exprOpp
00046 
00047       int argcode = arg -> code ();
00048 
00049       if      (argcode == COU_EXPRMUL) isMul = true;
00050       else if (argcode != COU_EXPRDIV) break;
00051     } 
00052       // no break. Bail out of switch only if this was a -w rather
00053       // than a -c*w or a -w/c
00054 
00055     case COU_EXPRMUL: 
00056       if (isMul) { 
00057 
00058       // it is c*w or w*c. Forget the case with more than two
00059       // non-constant arguments.
00060 
00061       expression **factors = arg -> ArgList ();
00062 
00063       int pos = 0;
00064 
00065       index = (*factors) -> Index ();
00066 
00067       if (index < 0) 
00068         index = factors [pos = 1] -> Index ();
00069 
00070       if ((index >= 0) && 
00071           (fabs (coeff = oppMulCoe * factors [1 - pos] -> Value ()) < COUENNE_EPS))
00072         index = -1;
00073 
00074       break;
00075     } 
00076       // no break outside, there could be some residue from case
00077       // COU_EXPROPP
00078 
00079     case COU_EXPRDIV: { // if linear, it must be of the form w/c
00080 
00081       expression **factors = arg -> ArgList ();
00082 
00083       coeff = factors [1] -> Value ();
00084       index = (*factors) -> Index ();
00085 
00086       if (fabs (coeff) < COUENNE_EPS)
00087         index = -1;
00088       else coeff = 1. / coeff;
00089 
00090     } break;
00091 
00092     default: break;
00093     }
00094   }
00095 }

Generated on Tue Mar 30 03:04:38 2010 by  doxygen 1.4.7