STK++ 1.0
STK_MatrixUpperTriangular.cpp
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:  Algebra
00027  * Purpose:  Define the MatrixUpperTriangular class.
00028  * Author:   Serge Iovleff, serge.iovleff@stkpp.org
00029  *
00030  **/
00031 
00036 #include "../include/STK_MatrixUpperTriangular.h"
00037 
00038 namespace STK
00039 {
00040 /* Default constructor
00041  * @param I range of the Rows
00042  * @param J range of the Cols
00043  **/
00044 MatrixUpperTriangular::MatrixUpperTriangular( Range const& I, Range const& J)
00045                                             : _ITArray2DType(I, J)
00046                                             , defaultConst_(Real())
00047 {
00048   // initialize vertically the container
00049   this->initializeCols(J);
00050 }
00051 
00052 /* constructor with rbeg, rend, cbeg and cend specified,
00053  *  initialization with a constant.
00054  *  @param I range of the Rows
00055  *  @param J range of the Cols
00056  *  @param v initial value of the container
00057  **/
00058 MatrixUpperTriangular::MatrixUpperTriangular( Range const& I
00059                                             , Range const& J
00060                                             , Real const& v
00061                                             )
00062                                             : _ITArray2DType(I, J)
00063                                             , defaultConst_(Real())
00064 {
00065   // initialize vertically the container
00066   this->initializeCols(J);
00067   // initialize with v
00068   for (Integer j = J.first(); j <= J.last(); j++)
00069   {
00070     Real* p(this->data(j));
00071     Integer beg(this->rangeCols_[j].first());
00072     Integer end(this->rangeCols_[j].last());
00073 
00074     for (Integer i = beg; i <= end; i++) p[i] = v;
00075   }
00076 }
00077 
00078 /* Copy constructor
00079  *  @param T the container to copy
00080  *  @param ref true if T is wrapped
00081  **/
00082 MatrixUpperTriangular::MatrixUpperTriangular( const MatrixUpperTriangular &T
00083                                             , bool ref
00084                                             )
00085                                             : _ITArray2DType(T, ref)
00086                                             , defaultConst_(Real())
00087 {
00088   if (!ref)
00089   {
00090     // initialize vertically the container
00091     this->initializeCols(T.rangeHo());
00092     // initialize with T
00093     for (Integer j=T.firstCol(); j<=T.lastCol(); j++)
00094     {
00095       Real* p(this->data(j));
00096       const Real* pt= T.data(j);
00097 
00098       for (Integer i=T.firstRow(); i<=T.lastRow(); i++) p[i]= pt[i];
00099     }
00100   }
00101 }
00102 
00103 /* constructor by reference, ref_=1.
00104  *  @param T the container to wrap
00105  *  @param I range of the Rows to wrap
00106  *  @param J range of the Cols to wrap
00107  **/
00108 MatrixUpperTriangular::MatrixUpperTriangular( const _ITArray2DType& T
00109                                             , Range const& I
00110                                             , Range const& J
00111                                             )
00112                                             : _ITArray2DType(T, I, J)
00113                                             , defaultConst_(Real())
00114 { ;}
00115 
00116 /* Wrapper constructor Contruct a reference container.
00117  *  @param q pointer on the data
00118  *  @param I range of the  Rows to wrap
00119  *  @param J range of the Cols to wrap
00120  **/
00121 MatrixUpperTriangular::MatrixUpperTriangular( Real** q
00122                                             , Range const& I
00123                                             , Range const& J
00124                                             )
00125                                             : _ITArray2DType(q, I, J)
00126                                             , defaultConst_(Real())
00127 { ;}
00128 
00129 /* virtual destructor : use destructor of Array2D.                            */
00130 MatrixUpperTriangular::~MatrixUpperTriangular()
00131 { ;}
00132 
00133 /* operator = : overwrite the MatrixUpperTriangular with T.
00134  *  We resize the object if this and T does not have the same size
00135  *  but if they have the same size, we don't modify the range
00136  *  of the object.
00137  *  @param T the container to copy
00138  **/
00139 MatrixUpperTriangular& MatrixUpperTriangular::operator=(const MatrixUpperTriangular &T)
00140 {
00141   // Resize if necessary.
00142   if ( (this->sizeVe() != T.sizeVe())||(this->sizeHo() != T.sizeHo()))
00143   {
00144     this->initialize(T.rangeVe(), T.rangeHo());
00145   }
00146   // coopy without overlapping
00147   if (T.firstCol()>=this->firstCol())
00148   {
00149     for ( Integer jt=T.firstCol(), j=this->firstCol()
00150         ; jt<=T.lastCol()
00151         ; j++, jt++)
00152     {
00153       Real* p(this->data(j));
00154       const Real* pt= T.data(jt);
00155       Integer beg(this->rangeCols_[j].first());
00156       Integer end(this->rangeCols_[j].last());
00157       Integer tbeg(T.rangeCols_[j].first());
00158 
00159       for (Integer i=beg, it=tbeg; i<=end; i++, it++)
00160         p[i]= pt[it];
00161     }
00162     return *this;
00163   }
00164   // T.firstCol()<this->firstCol()
00165   for ( Integer jt=T.lastCol(), j=this->lastCol()
00166       ; jt>=T.firstCol()
00167       ; j--, jt--)
00168   {
00169     Real* p(this->data(j));
00170     const Real* pt= T.data(jt);
00171     Integer beg(this->rangeCols_[j].first());
00172     Integer end(this->rangeCols_[j].last());
00173     Integer tbeg(T.rangeCols_[j].first());
00174 
00175     for (Integer i=beg, it=tbeg; i<=end; i++, it++) p[i]= pt[it];
00176   }
00177   return *this;
00178 }
00179 
00185 ostream& operator<<(ostream& s, MatrixUpperTriangular const& V)
00186 { return out2D(s,V);}
00187 
00188 } // namespace STK