blitz/matltri.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  * blitz/matltri.h      Declarations for LowerTriangular matrices
00003  *
00004  * $Id: matltri.h,v 1.4 2003/12/11 03:44:22 julianc Exp $
00005  *
00006  * Copyright (C) 1997-2001 Todd Veldhuizen <tveldhui@oonumerics.org>
00007  *
00008  * This program is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU General Public License
00010  * as published by the Free Software Foundation; either version 2
00011  * of the License, or (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * Suggestions:          blitz-dev@oonumerics.org
00019  * Bugs:                 blitz-bugs@oonumerics.org
00020  *
00021  * For more information, please see the Blitz++ Home Page:
00022  *    http://oonumerics.org/blitz/
00023  *
00024  ***************************************************************************/
00025 
00026 
00027 #ifndef BZ_MATLTRI_H
00028 #define BZ_MATLTRI_H
00029 
00030 #ifndef BZ_MSTRUCT_H
00031  #error <blitz/matltri.h> must be included via <blitz/mstruct.h>
00032 #endif
00033 
00034 BZ_NAMESPACE(blitz)
00035 
00036 // Lower triangular, row major ordering
00037 // [ 0 . . . ]
00038 // [ 1 2 . . ]
00039 // [ 3 4 5 . ]
00040 // [ 6 7 8 9 ]
00041 
00042 class LowerTriangularIterator {
00043 public:
00044     LowerTriangularIterator(unsigned rows, unsigned cols)
00045     {
00046         BZPRECONDITION(rows == cols);
00047         size_ = rows;
00048         good_ = true;
00049         offset_ = 0;
00050         i_ = 0;
00051         j_ = 0;
00052     }
00053    
00054     operator bool() const { return good_; }
00055 
00056     void operator++()
00057     {
00058         BZPRECONDITION(good_);
00059         ++offset_;
00060         ++j_;
00061         if (j_ > i_)
00062         {
00063             j_ = 0;
00064             ++i_;
00065             if (i_ == size_)
00066                 good_ = false;
00067         }
00068     }
00069 
00070     unsigned row() const
00071     { return i_; }
00072 
00073     unsigned col() const
00074     { return j_; }
00075 
00076     unsigned offset() const
00077     { return offset_; }
00078 
00079 protected:
00080     unsigned size_;
00081     bool good_;
00082     unsigned offset_;
00083     unsigned i_, j_;
00084 };
00085 
00086 class LowerTriangular : public MatrixStructure {
00087 
00088 public:
00089     typedef LowerTriangularIterator T_iterator;
00090 
00091     LowerTriangular()
00092         : size_(0)
00093     { }
00094 
00095     LowerTriangular(unsigned size)
00096         : size_(size)
00097     { }
00098 
00099     LowerTriangular(unsigned rows, unsigned cols)
00100         : size_(rows)
00101     {
00102         BZPRECONDITION(rows == cols);
00103     }
00104 
00105     unsigned columns() const
00106     { return size_; }
00107 
00108     unsigned coordToOffset(unsigned i, unsigned j) const
00109     {
00110         BZPRECONDITION(inRange(i,j));
00111         BZPRECONDITION(i >= j);
00112         return i*(i+1)/2 + j;
00113     }
00114 
00115     unsigned firstInRow(unsigned i) const
00116     { return 0; }
00117 
00118     template<typename T_numtype>
00119     T_numtype get(const T_numtype * restrict data,
00120         unsigned i, unsigned j) const
00121     {
00122         BZPRECONDITION(inRange(i,j));
00123         if (i >= j)
00124             return data[coordToOffset(i,j)];
00125         else
00126             return ZeroElement<T_numtype>::zero();
00127     }
00128 
00129     template<typename T_numtype>
00130     T_numtype& get(T_numtype * restrict data, unsigned i, unsigned j)
00131     {
00132         BZPRECONDITION(inRange(i,j));
00133         if (i >= j)
00134             return data[coordToOffset(i,j)];
00135         else
00136             return ZeroElement<T_numtype>::zero();
00137     }
00138 
00139     unsigned lastInRow(unsigned i) const
00140     { return i; }
00141 
00142     unsigned firstInCol(unsigned j) const
00143     { return j; }
00144 
00145     unsigned lastInCol(unsigned j) const
00146     { return size_ - 1; }
00147 
00148     bool inRange(unsigned i, unsigned j) const
00149     {
00150         return (i < size_) && (j < size_);
00151     }
00152 
00153     unsigned numElements() const
00154     { return size_ * (size_ + 1) / 2; }
00155 
00156     unsigned rows() const
00157     { return size_; }
00158 
00159     void resize(unsigned size)
00160     {
00161         size_ = size;
00162     }
00163 
00164     void resize(unsigned rows, unsigned cols)
00165     {
00166         BZPRECONDITION(rows == cols);
00167         size_  = rows;
00168     }
00169 
00170 private:
00171     unsigned size_;
00172 };
00173 
00174 BZ_NAMESPACE_END
00175 
00176 #endif // BZ_MATLTRI_H
00177 

Generated on Mon Dec 3 09:21:54 2007 for blitz by  doxygen 1.5.1