| [221] | 1 | /***************************************************************************
 | 
|---|
 | 2 |  * blitz/matutri.h      Declarations for UpperTriangular matrices
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  * $Id: matutri.h,v 1.1.1.1 1999-04-09 17:59:00 ansari Exp $
 | 
|---|
 | 5 |  *
 | 
|---|
 | 6 |  * Copyright (C) 1997,1998 Todd Veldhuizen <tveldhui@seurat.uwaterloo.ca>
 | 
|---|
 | 7 |  *
 | 
|---|
 | 8 |  * This program is free software; you can redistribute it and/or
 | 
|---|
 | 9 |  * modify it under the terms of the GNU General Public License
 | 
|---|
 | 10 |  * as published by the Free Software Foundation; either version 2
 | 
|---|
 | 11 |  * of the License, or (at your option) any later version.
 | 
|---|
 | 12 |  *
 | 
|---|
 | 13 |  * This program is distributed in the hope that it will be useful,
 | 
|---|
 | 14 |  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
 | 15 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
 | 16 |  * GNU General Public License for more details.
 | 
|---|
 | 17 |  *
 | 
|---|
 | 18 |  * Suggestions:          blitz-suggest@cybervision.com
 | 
|---|
 | 19 |  * Bugs:                 blitz-bugs@cybervision.com
 | 
|---|
 | 20 |  *
 | 
|---|
 | 21 |  * For more information, please see the Blitz++ Home Page:
 | 
|---|
 | 22 |  *    http://seurat.uwaterloo.ca/blitz/
 | 
|---|
 | 23 |  *
 | 
|---|
 | 24 |  ***************************************************************************
 | 
|---|
 | 25 |  * $Log: not supported by cvs2svn $
 | 
|---|
 | 26 |  * Revision 1.3  1998/03/14 00:04:47  tveldhui
 | 
|---|
 | 27 |  * 0.2-alpha-05
 | 
|---|
 | 28 |  *
 | 
|---|
 | 29 |  * Revision 1.2  1997/01/24 14:42:00  tveldhui
 | 
|---|
 | 30 |  * Periodic RCS update
 | 
|---|
 | 31 |  *
 | 
|---|
 | 32 |  * Revision 1.1  1997/01/13 22:19:58  tveldhui
 | 
|---|
 | 33 |  * Periodic RCS update
 | 
|---|
 | 34 |  */
 | 
|---|
 | 35 | 
 | 
|---|
 | 36 | #ifndef BZ_MATUTRI_H
 | 
|---|
 | 37 | #define BZ_MATUTRI_H
 | 
|---|
 | 38 | 
 | 
|---|
 | 39 | #ifndef BZ_MSTRUCT_H
 | 
|---|
 | 40 |  #error <blitz/matutri.h> must be included via <blitz/mstruct.h>
 | 
|---|
 | 41 | #endif
 | 
|---|
 | 42 | 
 | 
|---|
 | 43 | BZ_NAMESPACE(blitz)
 | 
|---|
 | 44 | 
 | 
|---|
 | 45 | // Upper triangular, column major ordering
 | 
|---|
 | 46 | // [ 0 1 3 6 ]
 | 
|---|
 | 47 | // [ . 2 4 7 ]
 | 
|---|
 | 48 | // [ . . 5 8 ]
 | 
|---|
 | 49 | // [ . . . 9 ]
 | 
|---|
 | 50 | 
 | 
|---|
 | 51 | class UpperTriangularIterator {
 | 
|---|
 | 52 | public:
 | 
|---|
 | 53 |     UpperTriangularIterator(unsigned rows, unsigned cols)
 | 
|---|
 | 54 |     {
 | 
|---|
 | 55 |         BZPRECONDITION(rows == cols);
 | 
|---|
 | 56 |         size_ = rows;
 | 
|---|
 | 57 |         good_ = true;
 | 
|---|
 | 58 |         offset_ = 0;
 | 
|---|
 | 59 |         i_ = 0;
 | 
|---|
 | 60 |         j_ = 0;
 | 
|---|
 | 61 |     }
 | 
|---|
 | 62 |    
 | 
|---|
 | 63 |     operator _bz_bool() const
 | 
|---|
 | 64 |     { return good_; }
 | 
|---|
 | 65 | 
 | 
|---|
 | 66 |     void operator++()
 | 
|---|
 | 67 |     {
 | 
|---|
 | 68 |         BZPRECONDITION(good_);
 | 
|---|
 | 69 |         ++offset_;
 | 
|---|
 | 70 |         ++i_;
 | 
|---|
 | 71 |         if (i_ > j_)
 | 
|---|
 | 72 |         {
 | 
|---|
 | 73 |             i_ = 0;
 | 
|---|
 | 74 |             ++j_;
 | 
|---|
 | 75 |             if (j_ == size_)
 | 
|---|
 | 76 |                 good_ = false;
 | 
|---|
 | 77 |         }
 | 
|---|
 | 78 |     }
 | 
|---|
 | 79 | 
 | 
|---|
 | 80 |     unsigned row() const
 | 
|---|
 | 81 |     { return i_; }
 | 
|---|
 | 82 | 
 | 
|---|
 | 83 |     unsigned col() const
 | 
|---|
 | 84 |     { return j_; }
 | 
|---|
 | 85 | 
 | 
|---|
 | 86 |     unsigned offset() const
 | 
|---|
 | 87 |     { return offset_; }
 | 
|---|
 | 88 | 
 | 
|---|
 | 89 | protected:
 | 
|---|
 | 90 |     unsigned size_;
 | 
|---|
 | 91 |     _bz_bool good_;
 | 
|---|
 | 92 |     unsigned offset_;
 | 
|---|
 | 93 |     unsigned i_, j_;
 | 
|---|
 | 94 | };
 | 
|---|
 | 95 | 
 | 
|---|
 | 96 | class UpperTriangular : public MatrixStructure {
 | 
|---|
 | 97 | 
 | 
|---|
 | 98 | public:
 | 
|---|
 | 99 |     typedef UpperTriangularIterator T_iterator;
 | 
|---|
 | 100 | 
 | 
|---|
 | 101 |     UpperTriangular()
 | 
|---|
 | 102 |         : size_(0)
 | 
|---|
 | 103 |     { }
 | 
|---|
 | 104 | 
 | 
|---|
 | 105 |     UpperTriangular(unsigned size)
 | 
|---|
 | 106 |         : size_(size)
 | 
|---|
 | 107 |     { }
 | 
|---|
 | 108 | 
 | 
|---|
 | 109 |     UpperTriangular(unsigned rows, unsigned cols)
 | 
|---|
 | 110 |         : size_(rows)
 | 
|---|
 | 111 |     {
 | 
|---|
 | 112 |         BZPRECONDITION(rows == cols);
 | 
|---|
 | 113 |     }
 | 
|---|
 | 114 | 
 | 
|---|
 | 115 |     unsigned columns() const
 | 
|---|
 | 116 |     { return size_; }
 | 
|---|
 | 117 | 
 | 
|---|
 | 118 |     unsigned coordToOffset(unsigned i, unsigned j) const
 | 
|---|
 | 119 |     {
 | 
|---|
 | 120 |         BZPRECONDITION(inRange(i,j));
 | 
|---|
 | 121 |         BZPRECONDITION(j >= i);
 | 
|---|
 | 122 |         return j*(j+1)/2 + i;
 | 
|---|
 | 123 |     }
 | 
|---|
 | 124 | 
 | 
|---|
 | 125 |     unsigned firstInRow(unsigned i) const
 | 
|---|
 | 126 |     { return 0; }
 | 
|---|
 | 127 | 
 | 
|---|
 | 128 |     template<class T_numtype>
 | 
|---|
 | 129 |     T_numtype get(const T_numtype * _bz_restrict data,
 | 
|---|
 | 130 |         unsigned i, unsigned j) const
 | 
|---|
 | 131 |     {
 | 
|---|
 | 132 |         BZPRECONDITION(inRange(i,j));
 | 
|---|
 | 133 |         if (j >= i)
 | 
|---|
 | 134 |             return data[coordToOffset(i,j)];
 | 
|---|
 | 135 |         else
 | 
|---|
 | 136 |             return ZeroElement<T_numtype>::zero();
 | 
|---|
 | 137 |     }
 | 
|---|
 | 138 | 
 | 
|---|
 | 139 |     template<class T_numtype>
 | 
|---|
 | 140 |     T_numtype& get(T_numtype * _bz_restrict data, unsigned i, unsigned j)
 | 
|---|
 | 141 |     {
 | 
|---|
 | 142 |         BZPRECONDITION(inRange(i,j));
 | 
|---|
 | 143 |         if (j >= i)
 | 
|---|
 | 144 |             return data[coordToOffset(i,j)];
 | 
|---|
 | 145 |         else
 | 
|---|
 | 146 |             return ZeroElement<T_numtype>::zero();
 | 
|---|
 | 147 |     }
 | 
|---|
 | 148 | 
 | 
|---|
 | 149 |     unsigned lastInRow(unsigned i) const
 | 
|---|
 | 150 |     { return size_ - 1; }
 | 
|---|
 | 151 | 
 | 
|---|
 | 152 |     unsigned firstInCol(unsigned j) const
 | 
|---|
 | 153 |     { return 0; }
 | 
|---|
 | 154 | 
 | 
|---|
 | 155 |     unsigned lastInCol(unsigned j) const
 | 
|---|
 | 156 |     { return j; }
 | 
|---|
 | 157 | 
 | 
|---|
 | 158 |     _bz_bool inRange(unsigned i, unsigned j) const
 | 
|---|
 | 159 |     {
 | 
|---|
 | 160 |         return (i < size_) && (j < size_);
 | 
|---|
 | 161 |     }
 | 
|---|
 | 162 | 
 | 
|---|
 | 163 |     unsigned numElements() const
 | 
|---|
 | 164 |     { return size_ * (size_ + 1) / 2; }
 | 
|---|
 | 165 | 
 | 
|---|
 | 166 |     unsigned rows() const
 | 
|---|
 | 167 |     { return size_; }
 | 
|---|
 | 168 | 
 | 
|---|
 | 169 |     void resize(unsigned size)
 | 
|---|
 | 170 |     {
 | 
|---|
 | 171 |         size_ = size;
 | 
|---|
 | 172 |     }
 | 
|---|
 | 173 | 
 | 
|---|
 | 174 |     void resize(unsigned rows, unsigned cols)
 | 
|---|
 | 175 |     {
 | 
|---|
 | 176 |         BZPRECONDITION(rows == cols);
 | 
|---|
 | 177 |         size_  = rows;
 | 
|---|
 | 178 |     }
 | 
|---|
 | 179 | 
 | 
|---|
 | 180 | private:
 | 
|---|
 | 181 |     unsigned size_;
 | 
|---|
 | 182 | };
 | 
|---|
 | 183 | 
 | 
|---|
 | 184 | BZ_NAMESPACE_END
 | 
|---|
 | 185 | 
 | 
|---|
 | 186 | #endif // BZ_MATUTRI_H
 | 
|---|
 | 187 | 
 | 
|---|