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