source: XMLIO_V2/external/include/blitz/matltri.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/matltri.h      Declarations for LowerTriangular matrices
3 *
4 * $Id: matltri.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
27#ifndef BZ_MATLTRI_H
28#define BZ_MATLTRI_H
29
30#ifndef BZ_MSTRUCT_H
31 #error <blitz/matltri.h> must be included via <blitz/mstruct.h>
32#endif
33
34BZ_NAMESPACE(blitz)
35
36// Lower triangular, row major ordering
37// [ 0 . . . ]
38// [ 1 2 . . ]
39// [ 3 4 5 . ]
40// [ 6 7 8 9 ]
41
42class LowerTriangularIterator {
43public:
44    LowerTriangularIterator(unsigned rows, unsigned cols)
45    {
46        BZPRECONDITION(rows == cols);
47        size_ = rows;
48        good_ = true;
49        offset_ = 0;
50        i_ = 0;
51        j_ = 0;
52    }
53   
54    operator bool() const { return good_; }
55
56    void operator++()
57    {
58        BZPRECONDITION(good_);
59        ++offset_;
60        ++j_;
61        if (j_ > i_)
62        {
63            j_ = 0;
64            ++i_;
65            if (i_ == size_)
66                good_ = false;
67        }
68    }
69
70    unsigned row() const
71    { return i_; }
72
73    unsigned col() const
74    { return j_; }
75
76    unsigned offset() const
77    { return offset_; }
78
79protected:
80    unsigned size_;
81    bool good_;
82    unsigned offset_;
83    unsigned i_, j_;
84};
85
86class LowerTriangular : public MatrixStructure {
87
88public:
89    typedef LowerTriangularIterator T_iterator;
90
91    LowerTriangular()
92        : size_(0)
93    { }
94
95    LowerTriangular(unsigned size)
96        : size_(size)
97    { }
98
99    LowerTriangular(unsigned rows, unsigned cols)
100        : size_(rows)
101    {
102        BZPRECONDITION(rows == cols);
103    }
104
105    unsigned columns() const
106    { return size_; }
107
108    unsigned coordToOffset(unsigned i, unsigned j) const
109    {
110        BZPRECONDITION(inRange(i,j));
111        BZPRECONDITION(i >= j);
112        return i*(i+1)/2 + j;
113    }
114
115    unsigned firstInRow(unsigned i) const
116    { return 0; }
117
118    template<typename T_numtype>
119    T_numtype get(const T_numtype * restrict data,
120        unsigned i, unsigned j) const
121    {
122        BZPRECONDITION(inRange(i,j));
123        if (i >= j)
124            return data[coordToOffset(i,j)];
125        else
126            return ZeroElement<T_numtype>::zero();
127    }
128
129    template<typename T_numtype>
130    T_numtype& get(T_numtype * restrict data, unsigned i, unsigned j)
131    {
132        BZPRECONDITION(inRange(i,j));
133        if (i >= j)
134            return data[coordToOffset(i,j)];
135        else
136            return ZeroElement<T_numtype>::zero();
137    }
138
139    unsigned lastInRow(unsigned i) const
140    { return i; }
141
142    unsigned firstInCol(unsigned j) const
143    { return j; }
144
145    unsigned lastInCol(unsigned j) const
146    { return size_ - 1; }
147
148    bool inRange(unsigned i, unsigned j) const
149    {
150        return (i < size_) && (j < size_);
151    }
152
153    unsigned numElements() const
154    { return size_ * (size_ + 1) / 2; }
155
156    unsigned rows() const
157    { return size_; }
158
159    void resize(unsigned size)
160    {
161        size_ = size;
162    }
163
164    void resize(unsigned rows, unsigned cols)
165    {
166        BZPRECONDITION(rows == cols);
167        size_  = rows;
168    }
169
170private:
171    unsigned size_;
172};
173
174BZ_NAMESPACE_END
175
176#endif // BZ_MATLTRI_H
177
Note: See TracBrowser for help on using the repository browser.