PSCF v1.5.0
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, David Morse (morse012@um.edu)
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 and destructor.
37
38 // Private copy constructor, to prohibit copy construction.
39
40 // Prohibit copy construction.
41 Matrix(Matrix<Data> const & other) = delete;
42
43 // Prohibit assignment.
44 Matrix<Data>& operator = (Matrix<Data> const & other) = delete;
45
49 int capacity1() const;
50
54 int capacity2() const;
55
62 Data const & operator() (int i, int j) const;
63
70 Data& operator() (int i, int j);
71
75 Data* cArray();
76
80 Data const * cArray() const;
81
82 protected:
83
85 Data* data_;
86
89
92
98 Matrix();
99
106
107 };
108
109 // Member function definitions
110
114 template <typename Data>
116 : data_(nullptr),
117 capacity1_(0),
118 capacity2_(0)
119 {}
120
121 /*
122 * Destructor (protected).
123 */
124 template <typename Data>
127
128 /*
129 * Get the number of rows.
130 */
131 template <typename Data>
132 inline int Matrix<Data>::capacity1() const
133 { return capacity1_; }
134
135 /*
136 * Get the number of columns.
137 */
138 template <typename Data>
139 inline int Matrix<Data>::capacity2() const
140 { return capacity2_; }
141
142 /*
143 * Return element (i,j) of matrix by const reference.
144 */
145 template <typename Data>
146 inline Data const & Matrix<Data>::operator() (int i, int j) const
147 {
148 assert(data_);
149 assert(i >= 0);
150 assert(i < capacity1_);
151 assert(j >= 0);
152 assert(j < capacity2_);
153 return *(data_ + i*capacity2_ + j);
154 }
155
156 /*
157 * Return element (i,j) of matrix by reference.
158 */
159 template <typename Data>
160 inline Data& Matrix<Data>::operator() (int i, int j)
161 {
162 assert(data_);
163 assert(i >= 0);
164 assert(i < capacity1_);
165 assert(j >= 0);
166 assert(j < capacity2_);
167 return *(data_ + i*capacity2_ + j);
168 }
169
170 /*
171 * Get a pointer to the underlying one-dimensional C array.
172 */
173 template <typename Data>
174 inline Data* Matrix<Data>::cArray()
175 { return data_; }
176
177 /*
178 * Get a pointer to const to the underlying one-dimensional C array.
179 */
180 template <typename Data>
181 inline Data const * Matrix<Data>::cArray() const
182 { return data_; }
183
184}
185#endif
Data * cArray()
Return pointer to underlying one-dimensional C array.
Definition Matrix.h:174
int capacity2() const
Get number of columns (range of the second array index).
Definition Matrix.h:139
Data * data_
Pointer to 1D C array of all elements.
Definition Matrix.h:85
int capacity2_
Number of columns (range of first index).
Definition Matrix.h:91
~Matrix()
Destructor.
Definition Matrix.h:125
int capacity1_
Number of rows (range of first index).
Definition Matrix.h:88
Data const & operator()(int i, int j) const
Return element (i,j) of matrix by const reference.
Definition Matrix.h:146
Matrix()
Default constructor.
Definition Matrix.h:115
int capacity1() const
Get number of rows (range of the first array index).
Definition Matrix.h:132
File containing preprocessor macros for error handling.
Utility classes for scientific computation.