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