LOOS  v2.3.2
MatrixOrder.hpp
1 /*
2  MatrixOrder.hpp
3 
4  Matrix ordering policies...
5 */
6 
7 /*
8  This file is part of LOOS.
9 
10  LOOS (Lightweight Object-Oriented Structure library)
11  Copyright (c) 2008, Tod D. Romo, Alan Grossfield
12  Department of Biochemistry and Biophysics
13  School of Medicine & Dentistry, University of Rochester
14 
15  This package (LOOS) is free software: you can redistribute it and/or modify
16  it under the terms of the GNU General Public License as published by
17  the Free Software Foundation under version 3 of the License.
18 
19  This package is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  GNU General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program. If not, see <http://www.gnu.org/licenses/>.
26 */
27 
28 
29 #if !defined(LOOS_MATRIX_ORDER_HPP)
30 #define LOOS_MATRIX_ORDER_HPP
31 
32 #include <string>
33 #include <cassert>
34 #include <stdexcept>
35 
36 #include <loos_defs.hpp>
37 
38 namespace loos {
39 
40  namespace Math {
41 
42  // These are the policy classes for the Matrix class. They define
43  // how the data is actually stored internally, i.e. lower
44  // triangular, column major, or row major order...
45  //
46  // Basically, they store the physical size of the matrix and convert
47  // 2D matrix coords to a single index.
48 
49 
51 
54  class Triangular {
55  public:
56  explicit Triangular() : m(0), n(0), s(0) { }
57  explicit Triangular(const uint y, const uint x) : m(y), n(x), s( (static_cast<ulong>(y)*(y+1))/2 ) {
58  assert(y == x && "Cannot have a non-square triangular matrix... (you know what I mean!)");
59  }
60 
61  ulong size(void) const { return(s); }
62 
64  ulong index(const uint y, const uint x) const {
65  ulong b = y;
66  ulong a = x;
67 
68  if (x > y) {
69  b = x;
70  a = y;
71  }
72 
73  return( (b * (b + 1)) / 2 + a );
74  }
75 
76  protected:
77 
79 
80  void setSize(const uint y, const uint x) {
81  assert(y == x && "Cannot have a non-square triangular matrix... (you know what I mean!)");
82  m=y; n=x;
83  s = (static_cast<ulong>(y)*(y+1))/2;
84  }
85 
86 
87  protected:
88  uint m, n;
89  ulong s;
90  };
91 
93  class ColMajor {
94  public:
95  explicit ColMajor() : m(0), n(0), s(0) { }
96  explicit ColMajor(const uint y, const uint x) : m(y), n(x), s(static_cast<ulong>(y)*x) { }
97 
98  ulong size(void) const { return(s); }
99 
101  ulong index(const uint y, const uint x) const {
102  return(static_cast<ulong>(x)*m + y);
103  }
104 
105  protected:
107 
108  void setSize(const uint y, const uint x) { m=y; n=x; s = static_cast<ulong>(y)*x; }
109 
110  protected:
111  uint m, n;
112  ulong s;
113  };
114 
115 
117  class RowMajor {
118  public:
119  explicit RowMajor() : m(0), n(0), s(0) { }
120  explicit RowMajor(const uint y, const uint x) : m(y), n(x), s(static_cast<ulong>(y)*x) { }
121 
122  ulong size(void) const { return(s); }
123 
125  ulong index(const uint y, const uint x) const {
126  return(static_cast<ulong>(y)*n + x);
127  }
128 
129  protected:
131 
132  void setSize(const uint y, const uint x) { m=y; n=x; s=static_cast<ulong>(y)*x; }
133 
134  protected:
135  uint m, n;
136  ulong s;
137  };
138 
139  }
140 
141 }
142 
143 #endif
Class for storing a symmetric triangular matrix.
Definition: MatrixOrder.hpp:54
void setSize(const uint y, const uint x)
Reset the [virtual] size of the matrix.
void setSize(const uint y, const uint x)
Reset the [virtual] size of the matrix.
Class for storing a matrix in row-major order...
ulong index(const uint y, const uint x) const
Get the index into the linear array of data.
ulong index(const uint y, const uint x) const
Get the index into the linear array of data.
Definition: MatrixOrder.hpp:64
Class for storing a matrix in column-major order.
Definition: MatrixOrder.hpp:93
Namespace for most things not already encapsulated within a class.
ulong index(const uint y, const uint x) const
Get the index into the linear array of data.
void setSize(const uint y, const uint x)
Reset the [virtual] size of the matrix.
Definition: MatrixOrder.hpp:80