| [221] | 1 | /*************************************************************************** | 
|---|
|  | 2 | * blitz/matltri.h      Declarations for LowerTriangular matrices | 
|---|
|  | 3 | * | 
|---|
|  | 4 | * $Id: matltri.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 | */ | 
|---|
|  | 37 |  | 
|---|
|  | 38 |  | 
|---|
|  | 39 | #ifndef BZ_MATLTRI_H | 
|---|
|  | 40 | #define BZ_MATLTRI_H | 
|---|
|  | 41 |  | 
|---|
|  | 42 | #ifndef BZ_MSTRUCT_H | 
|---|
|  | 43 | #error <blitz/matltri.h> must be included via <blitz/mstruct.h> | 
|---|
|  | 44 | #endif | 
|---|
|  | 45 |  | 
|---|
|  | 46 | BZ_NAMESPACE(blitz) | 
|---|
|  | 47 |  | 
|---|
|  | 48 | // Lower triangular, row major ordering | 
|---|
|  | 49 | // [ 0 . . . ] | 
|---|
|  | 50 | // [ 1 2 . . ] | 
|---|
|  | 51 | // [ 3 4 5 . ] | 
|---|
|  | 52 | // [ 6 7 8 9 ] | 
|---|
|  | 53 |  | 
|---|
|  | 54 | class LowerTriangularIterator { | 
|---|
|  | 55 | public: | 
|---|
|  | 56 | LowerTriangularIterator(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 | ++j_; | 
|---|
|  | 74 | if (j_ > i_) | 
|---|
|  | 75 | { | 
|---|
|  | 76 | j_ = 0; | 
|---|
|  | 77 | ++i_; | 
|---|
|  | 78 | if (i_ == 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 LowerTriangular : public MatrixStructure { | 
|---|
|  | 100 |  | 
|---|
|  | 101 | public: | 
|---|
|  | 102 | typedef LowerTriangularIterator T_iterator; | 
|---|
|  | 103 |  | 
|---|
|  | 104 | LowerTriangular() | 
|---|
|  | 105 | : size_(0) | 
|---|
|  | 106 | { } | 
|---|
|  | 107 |  | 
|---|
|  | 108 | LowerTriangular(unsigned size) | 
|---|
|  | 109 | : size_(size) | 
|---|
|  | 110 | { } | 
|---|
|  | 111 |  | 
|---|
|  | 112 | LowerTriangular(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(i >= j); | 
|---|
|  | 125 | return i*(i+1)/2 + j; | 
|---|
|  | 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 (i >= j) | 
|---|
|  | 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 (i >= j) | 
|---|
|  | 147 | return data[coordToOffset(i,j)]; | 
|---|
|  | 148 | else | 
|---|
|  | 149 | return ZeroElement<T_numtype>::zero(); | 
|---|
|  | 150 | } | 
|---|
|  | 151 |  | 
|---|
|  | 152 | unsigned lastInRow(unsigned i) const | 
|---|
|  | 153 | { return i; } | 
|---|
|  | 154 |  | 
|---|
|  | 155 | unsigned firstInCol(unsigned j) const | 
|---|
|  | 156 | { return j; } | 
|---|
|  | 157 |  | 
|---|
|  | 158 | unsigned lastInCol(unsigned j) const | 
|---|
|  | 159 | { return size_ - 1; } | 
|---|
|  | 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_MATLTRI_H | 
|---|
|  | 190 |  | 
|---|