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