STK++ 1.0
STK_MatrixUpperTriangular.h
Go to the documentation of this file.
00001 /*--------------------------------------------------------------------*/
00002 /*     Copyright (C) 2004-2007  Serge Iovleff
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU Lesser General Public License as
00006     published by the Free Software Foundation; either version 2 of the
00007     License, or (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU Lesser General Public License for more details.
00013 
00014     You should have received a copy of the GNU Lesser General Public
00015     License along with this program; if not, write to the
00016     Free Software Foundation, Inc.,
00017     59 Temple Place,
00018     Suite 330,
00019     Boston, MA 02111-1307
00020     USA
00021 
00022     Contact : Serge.Iovleff@stkpp.org
00023 */
00024 
00025 /*
00026  * Project:  stkpp::Arrays
00027  * Purpose:  Define the Upper Triangular Matrix class.
00028  * Author:   Serge Iovleff, serge.iovleff@stkpp.org
00029  *
00030  **/
00031 
00036 #ifndef STK_MATRIXUPPERTRIANGULAR_H
00037 #define STK_MATRIXUPPERTRIANGULAR_H
00038 
00039 #include "STK_Vector.h"
00040 #include "STK_Point.h"
00041 
00042 #include "STK_ITArray2D.h"
00043 #include "STK_Display2D.h"
00044 
00045 #include "../../Sdk/include/STK_ContainerTraits.h"
00046 
00047 namespace STK
00048 {
00049 class MatrixUpperTriangular;
00054 template<>
00055 struct ContainerTraits<Real, MatrixUpperTriangular>
00056 {
00057    typedef Point TContainerHo;
00058    typedef Vector TContainerVe;
00059 };
00060 
00061 
00071 class MatrixUpperTriangular : public ITArray2D< Real, MatrixUpperTriangular>
00072 {
00073   private:
00075     const Real defaultConst_;
00076 
00078     Real default_;
00079 
00080   public:
00082     typedef Array1D<Real> Vector;
00083 
00085     typedef ArrayHo<Real> Point;
00086 
00088     typedef AllocatorBase<Real*> _AllocatorBaseType_;
00089 
00091     typedef ITArray2D< Real, MatrixUpperTriangular >
00092            _ITArray2DType;
00093 
00098     MatrixUpperTriangular( Range const& I = Range(), Range const& J = Range());
00099 
00106     MatrixUpperTriangular( Range const& I, Range const& J, Real const& v);
00107 
00112     MatrixUpperTriangular( const MatrixUpperTriangular& T
00113                          , bool ref=false
00114                          );
00115 
00121     MatrixUpperTriangular( const _ITArray2DType& T
00122                          , Range const& I
00123                          , Range const& J
00124                          );
00125 
00131     MatrixUpperTriangular( Real** q, Range const& I, Range const& J);
00132 
00134     virtual ~MatrixUpperTriangular();
00135 
00139     inline Integer compFirstHo( Integer const& irow) const
00140     {
00141       return max( this->firstCol() + irow - this->firstRow()
00142                 , this->firstCol()
00143                 );
00144     }
00145 
00149     inline Integer compLastHo( Integer const& irow) const
00150     {
00151       return this->lastCol();
00152     }
00153 
00157     inline Integer compSizeHo( Integer const& irow) const
00158     {
00159       return max(this->lastCol() - compFirstHo(irow) + 1, (Integer )0);
00160     }
00161 
00166     inline Range compRangeHo( Integer const& irow) const
00167     {
00168       return Range(compFirstHo(irow), this->lastCol());
00169     }
00170 
00174     inline Integer compFirstVe( Integer const& icol) const
00175     {
00176       return this->firstRow();
00177     }
00178 
00182     inline Integer compLastVe( Integer const& icol) const
00183     {
00184       return min( this->firstRow() + icol - this->firstCol()
00185                 , this->lastRow()
00186                 );
00187     }
00188 
00192     inline Integer compSizeVe( Integer const& icol) const
00193     {
00194       return max(compLastVe(icol) - this->firstRow() + 1, (Integer )0);
00195     }
00196 
00201     inline Range compRangeVe( Integer const& icol) const
00202     {
00203       return Range(this->firstRow(), this->compLastVe(icol));
00204     }
00205 
00209    inline bool isInside(Integer const& i, Integer const& j) const
00210    { return (i<=this->rangeCols_[j].last());}
00211 
00213     inline Real& elt(Integer i, Integer j)
00214     { return isInside(i,j) ? this->data(j)[i] : default_;}
00215 
00217     inline Real const& elt(Integer i, Integer j) const
00218     {
00219       if (isInside(i,j)) { return this->data(j)[i];}
00220       else               { return defaultConst_;}
00221     }
00223     inline MatrixUpperTriangular sub(Range const& I, Range const& J) const
00224     { return MatrixUpperTriangular(*this, I, J);}
00225 
00231     inline Vector col(Range const& I, Integer j) const
00232     {
00233       return Vector( this->data(j), Range::inf(I, compRangeVe(j)), j);
00234     }
00235 
00240     inline Vector col( Integer j) const
00241     {
00242       return Vector( this->data(j), compRangeVe(j), j);
00243     }
00244 
00250     inline Point row(Integer i, Range const& J) const
00251     {
00252       return Point(*this, Range::inf(J, compRangeHo(i)), i);
00253     }
00254 
00259     inline Point row( Integer i) const
00260     {
00261       return Point(*this, compRangeHo(i), i);
00262     }
00263 
00265     MatrixUpperTriangular& operator=(const MatrixUpperTriangular &T);
00266 
00269     inline MatrixUpperTriangular& operator=(Real const& v)
00270     {
00271       for (Integer j=this->firstCol(); j<=this->lastCol(); j++)
00272       {
00273         Real* p(this->data(j));
00274         Integer beg(this->rangeCols_[j].first());
00275         Integer end(this->rangeCols_[j].last());
00276 
00277         for (Integer i=beg; i<=end; i++) p[i]= v;
00278       }
00279       return *this;
00280     }
00281 };
00282 
00288 ostream& operator<<(ostream& s, MatrixUpperTriangular const& V);
00289 
00290 } // namespace STK
00291 
00292 #endif
00293 // STK_MATRIXUPPERTRIANGULAR_H