blitz Version 0.10
|
00001 // -*- C++ -*- 00002 /*************************************************************************** 00003 * blitz/matsymm.h Declarations for Symmetric matrices 00004 * 00005 * $Id: matsymm.h,v 1.5 2011/03/25 22:41:16 julianc Exp $ 00006 * 00007 * Copyright (C) 1997-2011 Todd Veldhuizen <tveldhui@acm.org> 00008 * 00009 * This file is a part of Blitz. 00010 * 00011 * Blitz is free software: you can redistribute it and/or modify 00012 * it under the terms of the GNU Lesser General Public License 00013 * as published by the Free Software Foundation, either version 3 00014 * of the License, or (at your option) any later version. 00015 * 00016 * Blitz is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Lesser General Public License for more details. 00020 * 00021 * You should have received a copy of the GNU Lesser General Public 00022 * License along with Blitz. If not, see <http://www.gnu.org/licenses/>. 00023 * 00024 * Suggestions: blitz-devel@lists.sourceforge.net 00025 * Bugs: blitz-support@lists.sourceforge.net 00026 * 00027 * For more information, please see the Blitz++ Home Page: 00028 * https://sourceforge.net/projects/blitz/ 00029 * 00030 ***************************************************************************/ 00031 00032 #ifndef BZ_MATSYMM_H 00033 #define BZ_MATSYMM_H 00034 00035 #ifndef BZ_MSTRUCT_H 00036 #error <blitz/matsymm.h> must be included via <blitz/mstruct.h> 00037 #endif 00038 00039 BZ_NAMESPACE(blitz) 00040 00041 // Symmetric, lower triangular row major ordering 00042 // [ 0 1 3 6 ] 00043 // [ 1 2 4 7 ] 00044 // [ 3 4 5 8 ] 00045 // [ 6 7 8 9 ] 00046 00047 class SymmetricIterator { 00048 public: 00049 SymmetricIterator(unsigned rows, unsigned cols) 00050 { 00051 BZPRECONDITION(rows == cols); 00052 size_ = rows; 00053 good_ = true; 00054 offset_ = 0; 00055 i_ = 0; 00056 j_ = 0; 00057 } 00058 00059 operator bool() const { return good_; } 00060 00061 void operator++() 00062 { 00063 BZPRECONDITION(good_); 00064 ++offset_; 00065 ++j_; 00066 if (j_ > i_) 00067 { 00068 j_ = 0; 00069 ++i_; 00070 if (i_ == size_) 00071 good_ = false; 00072 } 00073 } 00074 00075 unsigned row() const 00076 { return i_; } 00077 00078 unsigned col() const 00079 { return j_; } 00080 00081 unsigned offset() const 00082 { return offset_; } 00083 00084 protected: 00085 unsigned size_; 00086 bool good_; 00087 unsigned offset_; 00088 unsigned i_, j_; 00089 }; 00090 00091 class Symmetric : public MatrixStructure { 00092 00093 public: 00094 typedef SymmetricIterator T_iterator; 00095 00096 Symmetric() 00097 : size_(0) 00098 { } 00099 00100 Symmetric(unsigned size) 00101 : size_(size) 00102 { } 00103 00104 Symmetric(unsigned rows, unsigned cols) 00105 : size_(rows) 00106 { 00107 BZPRECONDITION(rows == cols); 00108 } 00109 00110 unsigned columns() const 00111 { return size_; } 00112 00113 unsigned coordToOffset(unsigned i, unsigned j) const 00114 { 00115 BZPRECONDITION(inRange(i,j)); 00116 if (i >= j) 00117 return i*(i+1)/2 + j; 00118 else 00119 return j*(j+1)/2 + i; 00120 } 00121 00122 unsigned firstInRow(unsigned i) const 00123 { return 0; } 00124 00125 template<typename T_numtype> 00126 T_numtype get(const T_numtype * restrict data, 00127 unsigned i, unsigned j) const 00128 { 00129 BZPRECONDITION(inRange(i,j)); 00130 return data[coordToOffset(i,j)]; 00131 } 00132 00133 template<typename T_numtype> 00134 T_numtype& get(T_numtype * restrict data, unsigned i, unsigned j) 00135 { 00136 BZPRECONDITION(inRange(i,j)); 00137 return data[coordToOffset(i,j)]; 00138 } 00139 00140 unsigned lastInRow(unsigned i) const 00141 { return i; } 00142 00143 unsigned firstInCol(unsigned j) const 00144 { return j; } 00145 00146 unsigned lastInCol(unsigned j) const 00147 { return size_ - 1; } 00148 00149 bool inRange(unsigned i, unsigned j) const { 00150 return (i < size_) && (j < size_); 00151 } 00152 00153 unsigned numElements() const 00154 { return size_ * (size_ + 1) / 2; } 00155 00156 unsigned rows() const 00157 { return size_; } 00158 00159 void resize(unsigned size) 00160 { 00161 size_ = size; 00162 } 00163 00164 void resize(unsigned rows, unsigned cols) 00165 { 00166 BZPRECONDITION(rows == cols); 00167 size_ = rows; 00168 } 00169 00170 private: 00171 unsigned size_; 00172 }; 00173 00174 BZ_NAMESPACE_END 00175 00176 #endif // BZ_MATSYMM_H 00177