# CouenneMatrix.hpp

Go to the documentation of this file.

    00001 /* $Id: CouenneMatrix.hpp 1094 2015-01-10 16:59:20Z pbelotti $
    00002  *
    00003  * Name:    CouenneMatrix.hpp
    00004  * Author:  Pietro Belotti
    00005  * Purpose: define the class of expression matrices
    00006  *
    00007  * This file is licensed under the Eclipse Public License (EPL)
    00008  */
    00009
    00010 #ifndef CouenneMatrix_hpp
    00011 #define CouenneMatrix_hpp
    00012
    00013 #include <set>
    00014 #include <vector>
    00015
    00016 #include "CouenneExprClone.hpp"
    00017
    00018 namespace Couenne {
    00019
    00020   class expression;
    00021   class CouenneExprMatrix;
    00022
    00023   // Base class for elements of our sparse structures ////////////////////////////
    00024
    00025   class CouenneScalar {
    00026
    00027   protected:
    00028
    00029     int         index_;
    00030     expression *elem_;
    00031
    00032   public:
    00033
    00034     CouenneScalar (int index, expression *elem):
    00035       index_  (index),
    00036       elem_   (elem -> code () == COU_EXPRCONST ? elem : new exprClone (elem)) {}
    00037
    00038     ~CouenneScalar ();
    00039
    00040     CouenneScalar (const CouenneScalar &rhs):
    00041       index_  (rhs.index_),
    00042       elem_   (new exprClone (rhs.elem_)) {}
    00043
    00044     CouenneScalar &operator= (const CouenneScalar &rhs) {
    00045       index_  = rhs.index_;
    00046       elem_   = new exprClone (rhs.elem_);
    00047       return *this;
    00048     }
    00049
    00050     inline CouenneScalar *clone () {return new CouenneScalar (*this);}
    00051
    00052     inline int         getIndex () const {return index_;}
    00053     inline expression *getElem  () const {return elem_;}
    00054
    00055     inline bool operator< (const CouenneScalar &rhs) const {return (index_ < rhs.index_);}
    00056
    00057     friend bool operator< (const CouenneScalar &first, const CouenneScalar &second);
    00058
    00059     void print () const;
    00060   };
    00061
    00062   inline bool operator< (const CouenneScalar &first, const CouenneScalar &second) {return (first.index_ < second.index_);}
    00063
    00064   // Sparse vector of expressions /////////////////////////////////////////////////
    00065
    00066   class CouenneSparseVector {
    00067
    00068   public:
    00069
    00070     struct compare_scalars {
    00071       inline bool operator() (register CouenneScalar * const &a,
    00072                               register CouenneScalar * const &b)
    00073       {return a -> getIndex () < b -> getIndex ();}
    00074     };
    00075
    00076   protected:
    00077
    00078     std::set <CouenneScalar *, compare_scalars> elem_;
    00079
    00080   public:
    00081
    00082     CouenneSparseVector () {}
    00083    ~CouenneSparseVector ();
    00084
    00085     CouenneSparseVector                (const CouenneSparseVector &rhs);
    00086     CouenneSparseVector &operator=     (const CouenneSparseVector &rhs);
    00087     CouenneSparseVector *clone () {return new CouenneSparseVector (*this);}
    00088
    00089     void add_element (int index, expression *elem);
    00090
    00091     void print () const;
    00092
    00093     const std::set <CouenneScalar *, compare_scalars> &getElements () {return elem_;}
    00094
    00095     double               operator *     (const CouenneSparseVector &factor)           const;
    00096     CouenneSparseVector &operator *     (const CouenneExprMatrix   &post)             const;
    00097
    00098     double               multiply_thres (const CouenneSparseVector &v2, double thres) const;
    00099   };
    00100
    00101
    00102   // Sparse matrix of expressions ///////////////////////////////////////////////////
    00103
    00104   class CouenneExprMatrix {
    00105
    00106   public:
    00107
    00108     struct compare_pair_ind {
    00109       inline bool operator() (register const std::pair <int, CouenneSparseVector *> &a,
    00110                               register const std::pair <int, CouenneSparseVector *> &b) const
    00111       {return a. first < b. first;}
    00112     };
    00113
    00114   protected:
    00115
    00116     std::set <std::pair <int, CouenneSparseVector *>, compare_pair_ind> row_;
    00117     std::set <std::pair <int, CouenneSparseVector *>, compare_pair_ind> col_;
    00118
    00119     std::vector <expression *> varIndices_;
    00120
    00121   public:
    00122
    00123     CouenneExprMatrix () {}
    00124    ~CouenneExprMatrix ();
    00125
    00126     CouenneExprMatrix            (const CouenneExprMatrix &rhs);
    00127     CouenneExprMatrix &operator= (const CouenneExprMatrix &rhs);
    00128
    00129     CouenneExprMatrix *clone () {return new CouenneExprMatrix (*this);}
    00130
    00131     const std::set <std::pair <int, CouenneSparseVector *>, compare_pair_ind> &getRows () const {return row_;}
    00132     const std::set <std::pair <int, CouenneSparseVector *>, compare_pair_ind> &getCols () const {return col_;}
    00133
    00134     std::vector <expression *> &varIndices () {return varIndices_;}
    00135
    00136     void add_element (int row, int column, expression *elem);
    00137     void print () const;
    00138     long unsigned int size ();
    00139
    00140     CouenneSparseVector &operator * (const CouenneSparseVector &factor) const;
    00141     CouenneExprMatrix   &operator * (const CouenneExprMatrix   &post)   const;
    00142   };
    00143 }
    00144
    00145 #endif

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines