blitz/matutri.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  * blitz/matutri.h      Declarations for UpperTriangular matrices
00003  *
00004  * $Id: matutri.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 #ifndef BZ_MATUTRI_H
00027 #define BZ_MATUTRI_H
00028 
00029 #ifndef BZ_MSTRUCT_H
00030  #error <blitz/matutri.h> must be included via <blitz/mstruct.h>
00031 #endif
00032 
00033 BZ_NAMESPACE(blitz)
00034 
00035 // Upper triangular, column major ordering
00036 // [ 0 1 3 6 ]
00037 // [ . 2 4 7 ]
00038 // [ . . 5 8 ]
00039 // [ . . . 9 ]
00040 
00041 class UpperTriangularIterator {
00042 public:
00043     UpperTriangularIterator(unsigned rows, unsigned cols)
00044     {
00045         BZPRECONDITION(rows == cols);
00046         size_ = rows;
00047         good_ = true;
00048         offset_ = 0;
00049         i_ = 0;
00050         j_ = 0;
00051     }
00052    
00053     operator bool() const { return good_; }
00054 
00055     void operator++()
00056     {
00057         BZPRECONDITION(good_);
00058         ++offset_;
00059         ++i_;
00060         if (i_ > j_)
00061         {
00062             i_ = 0;
00063             ++j_;
00064             if (j_ == size_)
00065                 good_ = false;
00066         }
00067     }
00068 
00069     unsigned row() const
00070     { return i_; }
00071 
00072     unsigned col() const
00073     { return j_; }
00074 
00075     unsigned offset() const
00076     { return offset_; }
00077 
00078 protected:
00079     unsigned size_;
00080     bool     good_;
00081     unsigned offset_;
00082     unsigned i_, j_;
00083 };
00084 
00085 class UpperTriangular : public MatrixStructure {
00086 
00087 public:
00088     typedef UpperTriangularIterator T_iterator;
00089 
00090     UpperTriangular()
00091         : size_(0)
00092     { }
00093 
00094     UpperTriangular(unsigned size)
00095         : size_(size)
00096     { }
00097 
00098     UpperTriangular(unsigned rows, unsigned cols)
00099         : size_(rows)
00100     {
00101         BZPRECONDITION(rows == cols);
00102     }
00103 
00104     unsigned columns() const
00105     { return size_; }
00106 
00107     unsigned coordToOffset(unsigned i, unsigned j) const
00108     {
00109         BZPRECONDITION(inRange(i,j));
00110         BZPRECONDITION(j >= i);
00111         return j*(j+1)/2 + i;
00112     }
00113 
00114     unsigned firstInRow(unsigned i) const
00115     { return 0; }
00116 
00117     template<typename T_numtype>
00118     T_numtype get(const T_numtype * restrict data,
00119         unsigned i, unsigned j) const
00120     {
00121         BZPRECONDITION(inRange(i,j));
00122         if (j >= i)
00123             return data[coordToOffset(i,j)];
00124         else
00125             return ZeroElement<T_numtype>::zero();
00126     }
00127 
00128     template<typename T_numtype>
00129     T_numtype& get(T_numtype * restrict data, unsigned i, unsigned j)
00130     {
00131         BZPRECONDITION(inRange(i,j));
00132         if (j >= i)
00133             return data[coordToOffset(i,j)];
00134         else
00135             return ZeroElement<T_numtype>::zero();
00136     }
00137 
00138     unsigned lastInRow(unsigned i) const
00139     { return size_ - 1; }
00140 
00141     unsigned firstInCol(unsigned j) const
00142     { return 0; }
00143 
00144     unsigned lastInCol(unsigned j) const
00145     { return j; }
00146 
00147     bool inRange(const unsigned i,const unsigned j) const { return (i<size_) && (j<size_); }
00148 
00149     unsigned numElements() const { return size_ * (size_ + 1) / 2; }
00150 
00151     unsigned rows() const { return size_; }
00152 
00153     void resize(const unsigned size) { size_ = size; }
00154 
00155     void resize(const unsigned rows,const unsigned cols) {
00156         BZPRECONDITION(rows == cols);
00157         size_  = rows;
00158     }
00159 
00160 private:
00161     unsigned size_;
00162 };
00163 
00164 BZ_NAMESPACE_END
00165 
00166 #endif // BZ_MATUTRI_H
00167 

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