source: XMLIO_V2/external/include/blitz/matutri.h @ 73

Last change on this file since 73 was 73, checked in by ymipsl, 14 years ago
  • Property svn:eol-style set to native
File size: 3.9 KB
Line 
1/***************************************************************************
2 * blitz/matutri.h      Declarations for UpperTriangular matrices
3 *
4 * $Id: matutri.h,v 1.4 2003/12/11 03:44:22 julianc Exp $
5 *
6 * Copyright (C) 1997-2001 Todd Veldhuizen <tveldhui@oonumerics.org>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * Suggestions:          blitz-dev@oonumerics.org
19 * Bugs:                 blitz-bugs@oonumerics.org
20 *
21 * For more information, please see the Blitz++ Home Page:
22 *    http://oonumerics.org/blitz/
23 *
24 ***************************************************************************/
25
26#ifndef BZ_MATUTRI_H
27#define BZ_MATUTRI_H
28
29#ifndef BZ_MSTRUCT_H
30 #error <blitz/matutri.h> must be included via <blitz/mstruct.h>
31#endif
32
33BZ_NAMESPACE(blitz)
34
35// Upper triangular, column major ordering
36// [ 0 1 3 6 ]
37// [ . 2 4 7 ]
38// [ . . 5 8 ]
39// [ . . . 9 ]
40
41class UpperTriangularIterator {
42public:
43    UpperTriangularIterator(unsigned rows, unsigned cols)
44    {
45        BZPRECONDITION(rows == cols);
46        size_ = rows;
47        good_ = true;
48        offset_ = 0;
49        i_ = 0;
50        j_ = 0;
51    }
52   
53    operator bool() const { return good_; }
54
55    void operator++()
56    {
57        BZPRECONDITION(good_);
58        ++offset_;
59        ++i_;
60        if (i_ > j_)
61        {
62            i_ = 0;
63            ++j_;
64            if (j_ == size_)
65                good_ = false;
66        }
67    }
68
69    unsigned row() const
70    { return i_; }
71
72    unsigned col() const
73    { return j_; }
74
75    unsigned offset() const
76    { return offset_; }
77
78protected:
79    unsigned size_;
80    bool     good_;
81    unsigned offset_;
82    unsigned i_, j_;
83};
84
85class UpperTriangular : public MatrixStructure {
86
87public:
88    typedef UpperTriangularIterator T_iterator;
89
90    UpperTriangular()
91        : size_(0)
92    { }
93
94    UpperTriangular(unsigned size)
95        : size_(size)
96    { }
97
98    UpperTriangular(unsigned rows, unsigned cols)
99        : size_(rows)
100    {
101        BZPRECONDITION(rows == cols);
102    }
103
104    unsigned columns() const
105    { return size_; }
106
107    unsigned coordToOffset(unsigned i, unsigned j) const
108    {
109        BZPRECONDITION(inRange(i,j));
110        BZPRECONDITION(j >= i);
111        return j*(j+1)/2 + i;
112    }
113
114    unsigned firstInRow(unsigned i) const
115    { return 0; }
116
117    template<typename T_numtype>
118    T_numtype get(const T_numtype * restrict data,
119        unsigned i, unsigned j) const
120    {
121        BZPRECONDITION(inRange(i,j));
122        if (j >= i)
123            return data[coordToOffset(i,j)];
124        else
125            return ZeroElement<T_numtype>::zero();
126    }
127
128    template<typename T_numtype>
129    T_numtype& get(T_numtype * restrict data, unsigned i, unsigned j)
130    {
131        BZPRECONDITION(inRange(i,j));
132        if (j >= i)
133            return data[coordToOffset(i,j)];
134        else
135            return ZeroElement<T_numtype>::zero();
136    }
137
138    unsigned lastInRow(unsigned i) const
139    { return size_ - 1; }
140
141    unsigned firstInCol(unsigned j) const
142    { return 0; }
143
144    unsigned lastInCol(unsigned j) const
145    { return j; }
146
147    bool inRange(const unsigned i,const unsigned j) const { return (i<size_) && (j<size_); }
148
149    unsigned numElements() const { return size_ * (size_ + 1) / 2; }
150
151    unsigned rows() const { return size_; }
152
153    void resize(const unsigned size) { size_ = size; }
154
155    void resize(const unsigned rows,const unsigned cols) {
156        BZPRECONDITION(rows == cols);
157        size_  = rows;
158    }
159
160private:
161    unsigned size_;
162};
163
164BZ_NAMESPACE_END
165
166#endif // BZ_MATUTRI_H
167
Note: See TracBrowser for help on using the repository browser.