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