blitz  Version 0.9
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
matsymm.h
Go to the documentation of this file.
1 /***************************************************************************
2  * blitz/matsymm.h Declarations for Symmetric matrices
3  *
4  * $Id: matsymm.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_MATSYMM_H
27 #define BZ_MATSYMM_H
28 
29 #ifndef BZ_MSTRUCT_H
30  #error <blitz/matsymm.h> must be included via <blitz/mstruct.h>
31 #endif
32 
33 BZ_NAMESPACE(blitz)
34 
35 // Symmetric, lower triangular row major ordering
36 // [ 0 1 3 6 ]
37 // [ 1 2 4 7 ]
38 // [ 3 4 5 8 ]
39 // [ 6 7 8 9 ]
40 
42 public:
43  SymmetricIterator(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  ++j_;
60  if (j_ > i_)
61  {
62  j_ = 0;
63  ++i_;
64  if (i_ == 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 
78 protected:
79  unsigned size_;
80  bool good_;
81  unsigned offset_;
82  unsigned i_, j_;
83 };
84 
85 class Symmetric : public MatrixStructure {
86 
87 public:
89 
91  : size_(0)
92  { }
93 
94  Symmetric(unsigned size)
95  : size_(size)
96  { }
97 
98  Symmetric(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  if (i >= j)
111  return i*(i+1)/2 + j;
112  else
113  return j*(j+1)/2 + i;
114  }
115 
116  unsigned firstInRow(unsigned i) const
117  { return 0; }
118 
119  template<typename T_numtype>
120  T_numtype get(const T_numtype * restrict data,
121  unsigned i, unsigned j) const
122  {
123  BZPRECONDITION(inRange(i,j));
124  return data[coordToOffset(i,j)];
125  }
126 
127  template<typename T_numtype>
128  T_numtype& get(T_numtype * restrict data, unsigned i, unsigned j)
129  {
130  BZPRECONDITION(inRange(i,j));
131  return data[coordToOffset(i,j)];
132  }
133 
134  unsigned lastInRow(unsigned i) const
135  { return i; }
136 
137  unsigned firstInCol(unsigned j) const
138  { return j; }
139 
140  unsigned lastInCol(unsigned j) const
141  { return size_ - 1; }
142 
143  bool inRange(unsigned i, unsigned j) const {
144  return (i < size_) && (j < size_);
145  }
146 
147  unsigned numElements() const
148  { return size_ * (size_ + 1) / 2; }
149 
150  unsigned rows() const
151  { return size_; }
152 
153  void resize(unsigned size)
154  {
155  size_ = size;
156  }
157 
158  void resize(unsigned rows, unsigned cols)
159  {
160  BZPRECONDITION(rows == cols);
161  size_ = rows;
162  }
163 
164 private:
165  unsigned size_;
166 };
167 
169 
170 #endif // BZ_MATSYMM_H
171