blitz Version 0.10
|
00001 // -*- C++ -*- 00002 /*************************************************************************** 00003 * blitz/matltri.h Declarations for LowerTriangular matrices 00004 * 00005 * $Id: matltri.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 00033 #ifndef BZ_MATLTRI_H 00034 #define BZ_MATLTRI_H 00035 00036 #ifndef BZ_MSTRUCT_H 00037 #error <blitz/matltri.h> must be included via <blitz/mstruct.h> 00038 #endif 00039 00040 BZ_NAMESPACE(blitz) 00041 00042 // Lower triangular, row major ordering 00043 // [ 0 . . . ] 00044 // [ 1 2 . . ] 00045 // [ 3 4 5 . ] 00046 // [ 6 7 8 9 ] 00047 00048 class LowerTriangularIterator { 00049 public: 00050 LowerTriangularIterator(unsigned rows, unsigned cols) 00051 { 00052 BZPRECONDITION(rows == cols); 00053 size_ = rows; 00054 good_ = true; 00055 offset_ = 0; 00056 i_ = 0; 00057 j_ = 0; 00058 } 00059 00060 operator bool() const { return good_; } 00061 00062 void operator++() 00063 { 00064 BZPRECONDITION(good_); 00065 ++offset_; 00066 ++j_; 00067 if (j_ > i_) 00068 { 00069 j_ = 0; 00070 ++i_; 00071 if (i_ == size_) 00072 good_ = false; 00073 } 00074 } 00075 00076 unsigned row() const 00077 { return i_; } 00078 00079 unsigned col() const 00080 { return j_; } 00081 00082 unsigned offset() const 00083 { return offset_; } 00084 00085 protected: 00086 unsigned size_; 00087 bool good_; 00088 unsigned offset_; 00089 unsigned i_, j_; 00090 }; 00091 00092 class LowerTriangular : public MatrixStructure { 00093 00094 public: 00095 typedef LowerTriangularIterator T_iterator; 00096 00097 LowerTriangular() 00098 : size_(0) 00099 { } 00100 00101 LowerTriangular(unsigned size) 00102 : size_(size) 00103 { } 00104 00105 LowerTriangular(unsigned rows, unsigned cols) 00106 : size_(rows) 00107 { 00108 BZPRECONDITION(rows == cols); 00109 } 00110 00111 unsigned columns() const 00112 { return size_; } 00113 00114 unsigned coordToOffset(unsigned i, unsigned j) const 00115 { 00116 BZPRECONDITION(inRange(i,j)); 00117 BZPRECONDITION(i >= j); 00118 return i*(i+1)/2 + j; 00119 } 00120 00121 unsigned firstInRow(unsigned i) const 00122 { return 0; } 00123 00124 template<typename T_numtype> 00125 T_numtype get(const T_numtype * restrict data, 00126 unsigned i, unsigned j) const 00127 { 00128 BZPRECONDITION(inRange(i,j)); 00129 if (i >= j) 00130 return data[coordToOffset(i,j)]; 00131 else 00132 return ZeroElement<T_numtype>::zero(); 00133 } 00134 00135 template<typename T_numtype> 00136 T_numtype& get(T_numtype * restrict data, unsigned i, unsigned j) 00137 { 00138 BZPRECONDITION(inRange(i,j)); 00139 if (i >= j) 00140 return data[coordToOffset(i,j)]; 00141 else 00142 return ZeroElement<T_numtype>::zero(); 00143 } 00144 00145 unsigned lastInRow(unsigned i) const 00146 { return i; } 00147 00148 unsigned firstInCol(unsigned j) const 00149 { return j; } 00150 00151 unsigned lastInCol(unsigned j) const 00152 { return size_ - 1; } 00153 00154 bool inRange(unsigned i, unsigned j) const 00155 { 00156 return (i < size_) && (j < size_); 00157 } 00158 00159 unsigned numElements() const 00160 { return size_ * (size_ + 1) / 2; } 00161 00162 unsigned rows() const 00163 { return size_; } 00164 00165 void resize(unsigned size) 00166 { 00167 size_ = size; 00168 } 00169 00170 void resize(unsigned rows, unsigned cols) 00171 { 00172 BZPRECONDITION(rows == cols); 00173 size_ = rows; 00174 } 00175 00176 private: 00177 unsigned size_; 00178 }; 00179 00180 BZ_NAMESPACE_END 00181 00182 #endif // BZ_MATLTRI_H 00183