00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00036
00037
00038
00039
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