PSCF v1.1
Matrix.h
1#ifndef UTIL_MATRIX_H
2#define UTIL_MATRIX_H
3
4/*
5* Util Package - C++ Utilities for Scientific Computation
6*
7* Copyright 2010 - 2017, The Regents of the University of Minnesota
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <util/global.h>
12
13namespace Util
14{
15
30 template <typename Data>
31 class Matrix
32 {
33
34 public:
35
36 // Protected default constructor, to prohibit direct instantiation.
37
38 // Private copy constructor, to prohibit copy construction.
39
43 virtual ~Matrix();
44
48 int capacity1() const;
49
53 int capacity2() const;
54
61 Data const & operator() (int i, int j) const;
62
69 Data& operator() (int i, int j);
70
74 Data* cArray();
75
79 Data const * cArray() const;
80
81 protected:
82
84 Data* data_;
85
88
91
98
99 private:
100
104 Matrix(Matrix<Data> const & other);
105
109 Matrix<Data>& operator = (Matrix<Data> const & other);
110
111 };
112
113 // Method definitions
114
118 template <typename Data>
120 : data_(0),
121 capacity1_(0),
122 capacity2_(0)
123 {}
124
125 /*
126 * Destructor.
127 */
128 template <typename Data>
130 {}
131
132 /*
133 * Get the number of rows.
134 */
135 template <typename Data>
136 inline int Matrix<Data>::capacity1() const
137 { return capacity1_; }
138
139 /*
140 * Get the number of columns.
141 */
142 template <typename Data>
143 inline int Matrix<Data>::capacity2() const
144 { return capacity2_; }
145
146 /*
147 * Return element (i,j) of matrix by const reference.
148 */
149 template <typename Data>
150 inline Data const & Matrix<Data>::operator() (int i, int j) const
151 {
152 assert(data_ != 0);
153 assert(i >= 0);
154 assert(i < capacity1_);
155 assert(j >= 0);
156 assert(j < capacity2_);
157 return *(data_ + i*capacity2_ + j);
158 }
159
160 /*
161 * Return element (i,j) of matrix by reference.
162 */
163 template <typename Data>
164 inline Data& Matrix<Data>::operator() (int i, int j)
165 {
166 assert(data_ != 0);
167 assert(i >= 0);
168 assert(i < capacity1_);
169 assert(j >= 0);
170 assert(j < capacity2_);
171 return *(data_ + i*capacity2_ + j);
172 }
173
174 /*
175 * Get a pointer to the underlying one-dimensional C array.
176 */
177 template <typename Data>
178 inline Data* Matrix<Data>::cArray()
179 { return data_; }
180
181 /*
182 * Get a pointer to const to the underlying one-dimensional C array.
183 */
184 template <typename Data>
185 inline Data const * Matrix<Data>::cArray() const
186 { return data_; }
187
188}
189#endif
Two-dimensional array container template (abstract).
Definition: Matrix.h:32
Data * cArray()
Return pointer to underlying one-dimensional C array.
Definition: Matrix.h:178
int capacity2() const
Get number of columns (range of the second array index).
Definition: Matrix.h:143
Data * data_
Pointer to 1D C array of all elements.
Definition: Matrix.h:84
int capacity2_
Number of columns (range of first index).
Definition: Matrix.h:90
virtual ~Matrix()
Destructor.
Definition: Matrix.h:129
Data const * cArray() const
Return pointer to const to underlying one-dimensional C array.
Definition: Matrix.h:185
int capacity1_
Number of rows (range of first index).
Definition: Matrix.h:87
Data const & operator()(int i, int j) const
Return element (i,j) of matrix by const reference.
Definition: Matrix.h:150
Matrix()
Default constructor.
Definition: Matrix.h:119
int capacity1() const
Get number of rows (range of the first array index).
Definition: Matrix.h:136
File containing preprocessor macros for error handling.
Utility classes for scientific computation.
Definition: accumulators.mod:1