| 1 | /***************************************************************************
 | 
|---|
| 2 |  * blitz/matutri.h      Declarations for UpperTriangular matrices
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  * $Id: matutri.h,v 1.1.1.1 1999-11-26 16:37:04 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.1.1.1  1999/04/09  17:59:00  ansari
 | 
|---|
| 27 |  * Creation module DPC/Blitz (blitz 0.4) Reza 09/04/99
 | 
|---|
| 28 |  *
 | 
|---|
| 29 |  * Revision 1.3  1998/03/14 00:04:47  tveldhui
 | 
|---|
| 30 |  * 0.2-alpha-05
 | 
|---|
| 31 |  *
 | 
|---|
| 32 |  * Revision 1.2  1997/01/24 14:42:00  tveldhui
 | 
|---|
| 33 |  * Periodic RCS update
 | 
|---|
| 34 |  *
 | 
|---|
| 35 |  * Revision 1.1  1997/01/13 22:19:58  tveldhui
 | 
|---|
| 36 |  * Periodic RCS update
 | 
|---|
| 37 |  */
 | 
|---|
| 38 | 
 | 
|---|
| 39 | #ifndef BZ_MATUTRI_H
 | 
|---|
| 40 | #define BZ_MATUTRI_H
 | 
|---|
| 41 | 
 | 
|---|
| 42 | #ifndef BZ_MSTRUCT_H
 | 
|---|
| 43 |  #error <blitz/matutri.h> must be included via <blitz/mstruct.h>
 | 
|---|
| 44 | #endif
 | 
|---|
| 45 | 
 | 
|---|
| 46 | BZ_NAMESPACE(blitz)
 | 
|---|
| 47 | 
 | 
|---|
| 48 | // Upper triangular, column major ordering
 | 
|---|
| 49 | // [ 0 1 3 6 ]
 | 
|---|
| 50 | // [ . 2 4 7 ]
 | 
|---|
| 51 | // [ . . 5 8 ]
 | 
|---|
| 52 | // [ . . . 9 ]
 | 
|---|
| 53 | 
 | 
|---|
| 54 | class UpperTriangularIterator {
 | 
|---|
| 55 | public:
 | 
|---|
| 56 |     UpperTriangularIterator(unsigned rows, unsigned cols)
 | 
|---|
| 57 |     {
 | 
|---|
| 58 |         BZPRECONDITION(rows == cols);
 | 
|---|
| 59 |         size_ = rows;
 | 
|---|
| 60 |         good_ = true;
 | 
|---|
| 61 |         offset_ = 0;
 | 
|---|
| 62 |         i_ = 0;
 | 
|---|
| 63 |         j_ = 0;
 | 
|---|
| 64 |     }
 | 
|---|
| 65 |    
 | 
|---|
| 66 |     operator _bz_bool() const
 | 
|---|
| 67 |     { return good_; }
 | 
|---|
| 68 | 
 | 
|---|
| 69 |     void operator++()
 | 
|---|
| 70 |     {
 | 
|---|
| 71 |         BZPRECONDITION(good_);
 | 
|---|
| 72 |         ++offset_;
 | 
|---|
| 73 |         ++i_;
 | 
|---|
| 74 |         if (i_ > j_)
 | 
|---|
| 75 |         {
 | 
|---|
| 76 |             i_ = 0;
 | 
|---|
| 77 |             ++j_;
 | 
|---|
| 78 |             if (j_ == size_)
 | 
|---|
| 79 |                 good_ = false;
 | 
|---|
| 80 |         }
 | 
|---|
| 81 |     }
 | 
|---|
| 82 | 
 | 
|---|
| 83 |     unsigned row() const
 | 
|---|
| 84 |     { return i_; }
 | 
|---|
| 85 | 
 | 
|---|
| 86 |     unsigned col() const
 | 
|---|
| 87 |     { return j_; }
 | 
|---|
| 88 | 
 | 
|---|
| 89 |     unsigned offset() const
 | 
|---|
| 90 |     { return offset_; }
 | 
|---|
| 91 | 
 | 
|---|
| 92 | protected:
 | 
|---|
| 93 |     unsigned size_;
 | 
|---|
| 94 |     _bz_bool good_;
 | 
|---|
| 95 |     unsigned offset_;
 | 
|---|
| 96 |     unsigned i_, j_;
 | 
|---|
| 97 | };
 | 
|---|
| 98 | 
 | 
|---|
| 99 | class UpperTriangular : public MatrixStructure {
 | 
|---|
| 100 | 
 | 
|---|
| 101 | public:
 | 
|---|
| 102 |     typedef UpperTriangularIterator T_iterator;
 | 
|---|
| 103 | 
 | 
|---|
| 104 |     UpperTriangular()
 | 
|---|
| 105 |         : size_(0)
 | 
|---|
| 106 |     { }
 | 
|---|
| 107 | 
 | 
|---|
| 108 |     UpperTriangular(unsigned size)
 | 
|---|
| 109 |         : size_(size)
 | 
|---|
| 110 |     { }
 | 
|---|
| 111 | 
 | 
|---|
| 112 |     UpperTriangular(unsigned rows, unsigned cols)
 | 
|---|
| 113 |         : size_(rows)
 | 
|---|
| 114 |     {
 | 
|---|
| 115 |         BZPRECONDITION(rows == cols);
 | 
|---|
| 116 |     }
 | 
|---|
| 117 | 
 | 
|---|
| 118 |     unsigned columns() const
 | 
|---|
| 119 |     { return size_; }
 | 
|---|
| 120 | 
 | 
|---|
| 121 |     unsigned coordToOffset(unsigned i, unsigned j) const
 | 
|---|
| 122 |     {
 | 
|---|
| 123 |         BZPRECONDITION(inRange(i,j));
 | 
|---|
| 124 |         BZPRECONDITION(j >= i);
 | 
|---|
| 125 |         return j*(j+1)/2 + i;
 | 
|---|
| 126 |     }
 | 
|---|
| 127 | 
 | 
|---|
| 128 |     unsigned firstInRow(unsigned i) const
 | 
|---|
| 129 |     { return 0; }
 | 
|---|
| 130 | 
 | 
|---|
| 131 |     template<class T_numtype>
 | 
|---|
| 132 |     T_numtype get(const T_numtype * _bz_restrict data,
 | 
|---|
| 133 |         unsigned i, unsigned j) const
 | 
|---|
| 134 |     {
 | 
|---|
| 135 |         BZPRECONDITION(inRange(i,j));
 | 
|---|
| 136 |         if (j >= i)
 | 
|---|
| 137 |             return data[coordToOffset(i,j)];
 | 
|---|
| 138 |         else
 | 
|---|
| 139 |             return ZeroElement<T_numtype>::zero();
 | 
|---|
| 140 |     }
 | 
|---|
| 141 | 
 | 
|---|
| 142 |     template<class T_numtype>
 | 
|---|
| 143 |     T_numtype& get(T_numtype * _bz_restrict data, unsigned i, unsigned j)
 | 
|---|
| 144 |     {
 | 
|---|
| 145 |         BZPRECONDITION(inRange(i,j));
 | 
|---|
| 146 |         if (j >= i)
 | 
|---|
| 147 |             return data[coordToOffset(i,j)];
 | 
|---|
| 148 |         else
 | 
|---|
| 149 |             return ZeroElement<T_numtype>::zero();
 | 
|---|
| 150 |     }
 | 
|---|
| 151 | 
 | 
|---|
| 152 |     unsigned lastInRow(unsigned i) const
 | 
|---|
| 153 |     { return size_ - 1; }
 | 
|---|
| 154 | 
 | 
|---|
| 155 |     unsigned firstInCol(unsigned j) const
 | 
|---|
| 156 |     { return 0; }
 | 
|---|
| 157 | 
 | 
|---|
| 158 |     unsigned lastInCol(unsigned j) const
 | 
|---|
| 159 |     { return j; }
 | 
|---|
| 160 | 
 | 
|---|
| 161 |     _bz_bool inRange(unsigned i, unsigned j) const
 | 
|---|
| 162 |     {
 | 
|---|
| 163 |         return (i < size_) && (j < size_);
 | 
|---|
| 164 |     }
 | 
|---|
| 165 | 
 | 
|---|
| 166 |     unsigned numElements() const
 | 
|---|
| 167 |     { return size_ * (size_ + 1) / 2; }
 | 
|---|
| 168 | 
 | 
|---|
| 169 |     unsigned rows() const
 | 
|---|
| 170 |     { return size_; }
 | 
|---|
| 171 | 
 | 
|---|
| 172 |     void resize(unsigned size)
 | 
|---|
| 173 |     {
 | 
|---|
| 174 |         size_ = size;
 | 
|---|
| 175 |     }
 | 
|---|
| 176 | 
 | 
|---|
| 177 |     void resize(unsigned rows, unsigned cols)
 | 
|---|
| 178 |     {
 | 
|---|
| 179 |         BZPRECONDITION(rows == cols);
 | 
|---|
| 180 |         size_  = rows;
 | 
|---|
| 181 |     }
 | 
|---|
| 182 | 
 | 
|---|
| 183 | private:
 | 
|---|
| 184 |     unsigned size_;
 | 
|---|
| 185 | };
 | 
|---|
| 186 | 
 | 
|---|
| 187 | BZ_NAMESPACE_END
 | 
|---|
| 188 | 
 | 
|---|
| 189 | #endif // BZ_MATUTRI_H
 | 
|---|
| 190 | 
 | 
|---|