blitz Version 0.10
|
00001 // -*- C++ -*- 00002 /*************************************************************************** 00003 * blitz/matutri.h Declarations for UpperTriangular matrices 00004 * 00005 * $Id: matutri.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_MATUTRI_H 00033 #define BZ_MATUTRI_H 00034 00035 #ifndef BZ_MSTRUCT_H 00036 #error <blitz/matutri.h> must be included via <blitz/mstruct.h> 00037 #endif 00038 00039 BZ_NAMESPACE(blitz) 00040 00041 // Upper triangular, column major ordering 00042 // [ 0 1 3 6 ] 00043 // [ . 2 4 7 ] 00044 // [ . . 5 8 ] 00045 // [ . . . 9 ] 00046 00047 class UpperTriangularIterator { 00048 public: 00049 UpperTriangularIterator(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 ++i_; 00066 if (i_ > j_) 00067 { 00068 i_ = 0; 00069 ++j_; 00070 if (j_ == 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 UpperTriangular : public MatrixStructure { 00092 00093 public: 00094 typedef UpperTriangularIterator T_iterator; 00095 00096 UpperTriangular() 00097 : size_(0) 00098 { } 00099 00100 UpperTriangular(unsigned size) 00101 : size_(size) 00102 { } 00103 00104 UpperTriangular(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 BZPRECONDITION(j >= i); 00117 return j*(j+1)/2 + i; 00118 } 00119 00120 unsigned firstInRow(unsigned i) const 00121 { return 0; } 00122 00123 template<typename T_numtype> 00124 T_numtype get(const T_numtype * restrict data, 00125 unsigned i, unsigned j) const 00126 { 00127 BZPRECONDITION(inRange(i,j)); 00128 if (j >= i) 00129 return data[coordToOffset(i,j)]; 00130 else 00131 return ZeroElement<T_numtype>::zero(); 00132 } 00133 00134 template<typename T_numtype> 00135 T_numtype& get(T_numtype * restrict data, unsigned i, unsigned j) 00136 { 00137 BZPRECONDITION(inRange(i,j)); 00138 if (j >= i) 00139 return data[coordToOffset(i,j)]; 00140 else 00141 return ZeroElement<T_numtype>::zero(); 00142 } 00143 00144 unsigned lastInRow(unsigned i) const 00145 { return size_ - 1; } 00146 00147 unsigned firstInCol(unsigned j) const 00148 { return 0; } 00149 00150 unsigned lastInCol(unsigned j) const 00151 { return j; } 00152 00153 bool inRange(const unsigned i,const unsigned j) const { return (i<size_) && (j<size_); } 00154 00155 unsigned numElements() const { return size_ * (size_ + 1) / 2; } 00156 00157 unsigned rows() const { return size_; } 00158 00159 void resize(const unsigned size) { size_ = size; } 00160 00161 void resize(const unsigned rows,const unsigned cols) { 00162 BZPRECONDITION(rows == cols); 00163 size_ = rows; 00164 } 00165 00166 private: 00167 unsigned size_; 00168 }; 00169 00170 BZ_NAMESPACE_END 00171 00172 #endif // BZ_MATUTRI_H 00173