blitz Version 0.9
|
00001 /*************************************************************************** 00002 * blitz/matdiag.h Declarations for Diagonal matrices 00003 * 00004 * $Id: matdiag.h,v 1.4 2003/12/11 03:44:22 julianc Exp $ 00005 * 00006 * Copyright (C) 1997-2001 Todd Veldhuizen <tveldhui@oonumerics.org> 00007 * 00008 * This program is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU General Public License 00010 * as published by the Free Software Foundation; either version 2 00011 * of the License, or (at your option) any later version. 00012 * 00013 * This program is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU General Public License for more details. 00017 * 00018 * Suggestions: blitz-dev@oonumerics.org 00019 * Bugs: blitz-bugs@oonumerics.org 00020 * 00021 * For more information, please see the Blitz++ Home Page: 00022 * http://oonumerics.org/blitz/ 00023 * 00024 ***************************************************************************/ 00025 00026 #ifndef BZ_MATDIAG_H 00027 #define BZ_MATDIAG_H 00028 00029 #ifndef BZ_MSTRUCT_H 00030 #error <blitz/matdiag.h> must be included via <blitz/mstruct.h> 00031 #endif 00032 00033 BZ_NAMESPACE(blitz) 00034 00035 // Diagonal matrix 00036 // [ 0 . . . ] 00037 // [ . 1 . . ] 00038 // [ . . 2 . ] 00039 // [ . . . 3 ] 00040 00041 class DiagonalIterator { 00042 public: 00043 DiagonalIterator(const unsigned rows,const unsigned cols) { 00044 BZPRECONDITION(rows==cols); 00045 size_ = rows; 00046 i_ = 0; 00047 } 00048 00049 operator bool() const { return i_ < size_; } 00050 00051 void operator++() { ++i_; } 00052 00053 unsigned row() const { return i_; } 00054 unsigned col() const { return i_; } 00055 unsigned offset() const { return i_; } 00056 00057 protected: 00058 unsigned i_, size_; 00059 }; 00060 00061 class Diagonal : public MatrixStructure { 00062 public: 00063 typedef DiagonalIterator T_iterator; 00064 00065 Diagonal(): size_(0) { } 00066 00067 Diagonal(const unsigned size): size_(size) { } 00068 00069 Diagonal(const unsigned rows,const unsigned cols): size_(rows) { 00070 BZPRECONDITION(rows == cols); 00071 } 00072 00073 unsigned columns() const { return size_; } 00074 00075 unsigned coordToOffset(const unsigned i,const unsigned j) const 00076 { 00077 BZPRECONDITION(inRange(i,j)); 00078 BZPRECONDITION(i == j); 00079 return i; 00080 } 00081 00082 unsigned firstInRow(const unsigned i) const { return i; } 00083 00084 template<typename T_numtype> 00085 T_numtype get(const T_numtype * restrict data,const unsigned i,const unsigned j) const 00086 { 00087 BZPRECONDITION(inRange(i,j)); 00088 return (i==j) ? data[coordToOffset(i,j)] : ZeroElement<T_numtype>::zero(); 00089 } 00090 00091 template<typename T_numtype> 00092 T_numtype& get(T_numtype * restrict data,const unsigned i,const unsigned j) { 00093 BZPRECONDITION(inRange(i,j)); 00094 return (i==j) ? data[coordToOffset(i,j)] : ZeroElement<T_numtype>::zero(); 00095 } 00096 00097 unsigned lastInRow(const unsigned i) const { return i; } 00098 unsigned firstInCol(const unsigned j) const { return j; } 00099 unsigned lastInCol(const unsigned j) const { return j; } 00100 00101 bool inRange(const unsigned i,const unsigned j) const { 00102 return (i < size_) && (j < size_); 00103 } 00104 00105 unsigned numElements() const { return size_; } 00106 unsigned rows() const { return size_; } 00107 00108 void resize(const unsigned size) { size_ = size; } 00109 00110 void resize(const unsigned rows,const unsigned cols) { 00111 BZPRECONDITION(rows == cols); 00112 size_ = rows; 00113 } 00114 00115 private: 00116 unsigned size_; 00117 }; 00118 00119 BZ_NAMESPACE_END 00120 00121 #endif // BZ_MATSYMM_H