blitz Version 0.10
blitz/matdiag.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /***************************************************************************
00003  * blitz/matdiag.h      Declarations for Diagonal matrices 
00004  *
00005  * $Id: matdiag.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_MATDIAG_H
00033 #define BZ_MATDIAG_H
00034 
00035 #ifndef BZ_MSTRUCT_H
00036  #error <blitz/matdiag.h> must be included via <blitz/mstruct.h>
00037 #endif
00038 
00039 BZ_NAMESPACE(blitz)
00040 
00041 // Diagonal matrix
00042 // [ 0 . . . ]
00043 // [ . 1 . . ]
00044 // [ . . 2 . ]
00045 // [ . . . 3 ]
00046 
00047 class DiagonalIterator {
00048 public:
00049     DiagonalIterator(const unsigned rows,const unsigned cols) {
00050         BZPRECONDITION(rows==cols);
00051         size_ = rows;
00052         i_ = 0;
00053     }
00054 
00055     operator bool() const { return i_ < size_; }
00056 
00057     void operator++() { ++i_; }
00058 
00059     unsigned row()    const { return i_; }
00060     unsigned col()    const { return i_; }
00061     unsigned offset() const { return i_; }
00062 
00063 protected:
00064     unsigned i_, size_;
00065 };
00066 
00067 class Diagonal : public MatrixStructure {
00068 public:
00069     typedef DiagonalIterator T_iterator;
00070 
00071     Diagonal(): size_(0) { }
00072 
00073     Diagonal(const unsigned size): size_(size) { }
00074 
00075     Diagonal(const unsigned rows,const unsigned cols): size_(rows) {
00076         BZPRECONDITION(rows == cols);
00077     }
00078 
00079     unsigned columns() const { return size_; }
00080 
00081     unsigned coordToOffset(const unsigned i,const unsigned j) const
00082     {
00083         BZPRECONDITION(inRange(i,j));
00084         BZPRECONDITION(i == j);
00085         return i;
00086     }
00087 
00088     unsigned firstInRow(const unsigned i) const { return i; }
00089 
00090     template<typename T_numtype>
00091     T_numtype get(const T_numtype * restrict data,const unsigned i,const unsigned j) const
00092     {
00093         BZPRECONDITION(inRange(i,j));
00094         return (i==j) ? data[coordToOffset(i,j)] : ZeroElement<T_numtype>::zero();
00095     }
00096 
00097     template<typename T_numtype>
00098     T_numtype& get(T_numtype * restrict data,const unsigned i,const unsigned j) {
00099         BZPRECONDITION(inRange(i,j));
00100         return (i==j) ? data[coordToOffset(i,j)] : ZeroElement<T_numtype>::zero();
00101     }
00102 
00103     unsigned lastInRow(const unsigned i)  const { return i; }
00104     unsigned firstInCol(const unsigned j) const { return j; }
00105     unsigned lastInCol(const unsigned j)  const { return j; }
00106 
00107     bool inRange(const unsigned i,const unsigned j) const {
00108         return (i < size_) && (j < size_);
00109     }
00110 
00111     unsigned numElements() const { return size_; }
00112     unsigned rows()        const { return size_; }
00113 
00114     void resize(const unsigned size) { size_ = size; }
00115 
00116     void resize(const unsigned rows,const unsigned cols) {
00117         BZPRECONDITION(rows == cols);
00118         size_  = rows;
00119     }
00120 
00121 private:
00122     unsigned size_;
00123 };
00124 
00125 BZ_NAMESPACE_END
00126 
00127 #endif // BZ_MATSYMM_H
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines