1 /* $Id$
2  *
3  * Name:    exprUnary.hpp
4  * Author:  Pietro Belotti
5  * Purpose: definition of the class for univariate functions
6  *
7  * (C) Carnegie-Mellon University, 2006-08.
8  * This file is licensed under the Eclipse Public License (EPL)
9  */
10 
11 #ifndef COUENNE_EXPRUNARY_HPP
12 #define COUENNE_EXPRUNARY_HPP
13 
14 #include <iostream>
15 
16 #include "CouenneExpression.hpp"
17 #include "CouenneTypes.hpp"
18 
19 namespace Couenne {
20 
21 /// zero function (used by default by exprUnary)
zero_fun(CouNumber x)22 inline CouNumber zero_fun (CouNumber x)
23 {return 0.;}
24 
25 
26 /// expression class for unary functions (sin, log, etc.)
27 ///
28 /// univariate operator-type expression: requires single argument. All
29 /// unary functions are derived from this base class, which has a lot
30 /// of common methods that need not be re-implemented by any
31 /// univariate class.
32 
33 class exprUnary: public expression {
34 
35  protected:
36 
37   /// single argument taken by this expression
38   expression *argument_;
39 
40  public:
41 
42   /// node type
Type() const43   virtual inline enum nodeType Type () const
44   {return UNARY;}
45 
46   /// Constructor
exprUnary(expression * argument)47   exprUnary  (expression *argument):
48     argument_ (argument)        //< non-leaf expression, with argument list
49   {}
50 
51   /// the operator itself (e.g. sin, log...)
F()52   virtual inline unary_function F ()
53   {return zero_fun;}
54 
55   /// Destructor
~exprUnary()56   virtual ~exprUnary ()
57   {if (argument_) delete argument_;}
58 
59   /// return number of arguments
nArgs() const60   inline int nArgs () const
61   {return 1;}
62 
63   /// return argument
Argument() const64   virtual inline expression *Argument () const
65   {return argument_;}
66 
67   /// return pointer to argument
ArgPtr()68   virtual inline expression **ArgPtr ()
69   {return &argument_;}
70 
71   /// print this expression to iostream
72   virtual void print (std::ostream &out = std::cout, bool = false) const;
73 
74   /// print position (PRE, INSIDE, POST)
printPos() const75   virtual enum pos printPos () const
76   {return PRE;}
77 
78   /// print operator
printOp() const79   virtual std::string printOp () const
80   {return "?";}
81 
82   /// compute value of unary operator
operator ()()83   virtual inline CouNumber operator () ()
84   {return (F ()) ((*argument_) ());}
85 
86   /// fill in the set with all indices of variables appearing in the
87   /// expression
DepList(std::set<int> & deplist,enum dig_type type=ORIG_ONLY)88   virtual inline int DepList (std::set <int> &deplist, enum dig_type type = ORIG_ONLY)
89   {return argument_ -> DepList (deplist, type);}
90 
91   /// simplification
92   expression *simplify ();
93 
94   /// get a measure of "how linear" the expression is (see CouenneTypes.h)
95   /// for general univariate functions, return nonlinear.
Linearity()96   virtual inline int Linearity ()
97   {return NONLINEAR;}
98 
99   /// reduce expression in standard form, creating additional aux
100   /// variables (and constraints)
101   virtual exprAux *standardize (CouenneProblem *, bool addAux = true);
102 
103   /// type of operator
code()104   virtual inline enum expr_type code ()
105   {return COU_EXPRUNARY;}
106 
107   /// is this expression integer?
108   virtual bool isInteger ();
109 
110   /// compare two unary functions
111   virtual int compare (exprUnary &);
112 
113   /// used in rank-based branching variable choice
rank()114   virtual inline int rank ()
115   {return (argument_ -> rank ());}
116 
117   /// fill in dependence structure
fillDepSet(std::set<DepNode *,compNode> * dep,DepGraph * g)118   virtual inline void fillDepSet (std::set <DepNode *, compNode> *dep, DepGraph *g)
119   {argument_ -> fillDepSet (dep, g);}
120 
121   /// replace variable with other
122   virtual void replace (exprVar *, exprVar *);
123 
124   /// empty function to redirect variables to proper variable vector
realign(const CouenneProblem * p)125   virtual inline void realign (const CouenneProblem *p)
126   {argument_ -> realign (p);}
127 };
128 
129 }
130 
131 #endif
132