casacore
Loading...
Searching...
No Matches
Matrix.h
Go to the documentation of this file.
1//# Matrix.h: A 2-D Specialization of the Array Class
2//# Copyright (C) 1993,1994,1995,1996,1999,2000,2001,2003
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26#ifndef CASA_MATRIX_2_H
27#define CASA_MATRIX_2_H
28
29//# Includes
30#include "Array.h"
31
32namespace casacore { //#Begin casa namespace
33
34// <summary> A 2-D Specialization of the Array class </summary>
35// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
36// </reviewed>
37//
38// Matrix objects are two-dimensional specializations (e.g., more convenient
39// and efficient indexing) of the general Array class. You might also want
40// to look at the Array documentation to see inherited functionality. A
41// tutorial on using the array classes in general is available in the
42// "AIPS++ Programming Manual".
43//
44// Generally the member functions of Array are also available in
45// Matrix versions which take a pair of integers where the array
46// needs an IPosition. Since the Matrix
47// is two-dimensional, the IPositions are overkill, although you may
48// use those versions if you want to.
49// <srcblock>
50// Matrix<int> mi(100,100); // Shape is 100x100
51// mi.resize(50,50); // Shape now 50x50
52// </srcblock>
53//
54// Slices may be taken with the Slice class. To take a slice, one "indexes"
55// with one Slice(start, length, inc) for each axis,
56// where end and inc are optional.
57// Additionally, there are row(), column() and diagonal()
58// member functions which return Vector's which refer to the storage back
59// in the Matrix:
60// <srcblock>
61// Matrix<float> mf(100, 100);
62// mf.diagonal() = 1;
63// </srcblock>
64//
65// Correct indexing order of a matrix is:
66// <srcblock>
67// Matrix<int> mi(n1,n2) // [nrow, ncolumn]
68// for (size_t j=0; j<mi.ncolumn(); j++) {
69// for (size_t i=0; i<mi.nrow(); i++) {
70// mi(i,j) = i*j;
71// }
72// }
73// </srcblock>
74//
75//
76// Element-by-element arithmetic and logical operations are available (in
77// aips/ArrayMath.h and aips/ArrayLogical.h). Other Matrix operations (e.g.
78// LU decomposition) are available, and more appear periodically.
79//
80// As with the Arrays, if the preprocessor symbol AIPS_DEBUG is
81// defined at compile time invariants will be checked on entry to most
82// member functions. Additionally, if AIPS_ARRAY_INDEX_CHECK is defined
83// index operations will be bounds-checked. Neither of these should
84// be defined for production code.
85
86template<typename T> class Matrix : public Array<T>
87{
88public:
89 // A Matrix of length zero in each dimension; zero origin.
91
92 // A Matrix with "l1" rows and "l2" columns.
93 // Fill it with the initial value.
94 Matrix(size_t l1, size_t l2, const T &initialValue = T());
95
96 // An uninitialized Matrix with "l1" rows and "l2" columns.
97 Matrix(size_t l1, size_t l2, typename Array<T>::uninitializedType);
98
99 // A matrix of shape with shape "len".
100 // Fill it with the initial value.
101 Matrix(const IPosition &len, const T &initialValue = T());
102
103 // An uninitialized matrix of shape with shape "len".
105
106 // The copy/move constructor uses reference semantics.
107 Matrix(const Matrix<T>& source);
108 Matrix(Matrix<T>&& source);
109
110 // Construct a Matrix by reference from "source". "source must have
111 // ndim() of 2 or less.
112 Matrix(const Array<T>& source);
113 Matrix(Array<T>&& source);
114
115 // Create an Matrix of a given shape from a pointer.
116 Matrix(const IPosition &shape, T *storage, StorageInitPolicy policy = COPY);
117 // Create an Matrix of a given shape from a pointer. Because the pointer
118 // is const, a copy is always made.
119 Matrix(const IPosition &shape, const T *storage);
120
121 // Create an identity matrix of side length n. (Could not do this as a constructor
122 // because of ambiguities with other constructors).
123 static Matrix<T> identity (size_t n);
124
125 // Resize to the given shape
126 // <group>
127 using Array<T>::resize;
128 void resize(size_t nx, size_t ny, bool copyValues=false);
129 // </group>
130
132 { return assign_conforming(source); }
134 { return assign_conforming(std::move(source)); }
136 { return assign_conforming(source); }
138 { return assign_conforming(std::move(source)); }
139
140 // Copy the values from other to this Matrix. If this matrix has zero
141 // elements then it will resize to be the same shape as other; otherwise
142 // other must conform to this.
143 // Note that the assign function can be used to assign a
144 // non-conforming matrix.
145 // <group>
147 { Array<T>::assign_conforming(source); return *this; }
149 { Array<T>::assign_conforming(std::move(source)); return *this; }
150
152 {
153 // TODO Should be supported by the Array class,
154 // see Cube::operator=(const Array&)
155
156 if (source.ndim() == 2) {
158 } else {
159 // This might work if a.ndim == 1 or 2
160 (*this) = Matrix<T>(source);
161 }
162 return *this;
163 }
164
166 {
167 if (source.ndim() == 2) {
168 Array<T>::assign_conforming(std::move(source));
169 } else {
170 (*this) = Matrix<T>(std::move(source));
171 }
172 return *this;
173 }
174 // </group>
175
176 // Copy val into every element of this Matrix; i.e. behaves as if
177 // val were a constant conformant matrix.
178 Array<T>& operator=(const T &val)
179 { return Array<T>::operator=(val); }
180
181 // Copy to this those values in marray whose corresponding elements
182 // in marray's mask are true.
185
186 // Single-pixel addressing. If AIPS_ARRAY_INDEX_CHECK is defined,
187 // bounds checking is performed.
188 // <group>
190 { return Array<T>::operator()(i); }
191 const T &operator()(const IPosition &i) const
192 { return Array<T>::operator()(i); }
193 T &operator()(size_t i1, size_t i2)
194 {
195 return this->contiguous_p ? this->begin_p[i1 + i2*yinc()] :
196 this->begin_p[i1*xinc() + i2*yinc()];
197 }
198
199 const T &operator()(size_t i1, size_t i2) const
200 {
201 return this->contiguous_p ? this->begin_p[i1 + i2*yinc()] :
202 this->begin_p[i1*xinc() + i2*yinc()];
203 }
204 // </group>
205
206
207 // The array is masked by the input LogicalArray.
208 // This mask must conform to the array.
209 // <group>
210
211 // Return a MaskedArray.
214
215 // Return a MaskedArray.
218
219 // </group>
220
221
222 // The array is masked by the input MaskedLogicalArray.
223 // The mask is effectively the AND of the internal LogicalArray
224 // and the internal mask of the MaskedLogicalArray.
225 // The MaskedLogicalArray must conform to the array.
226 // <group>
227
228 // Return a MaskedArray.
231
232 // Return a MaskedArray.
235
236 // </group>
237
238
239 // Returns a reference to the i'th row.
240 // <group>
241 Vector<T> row(size_t i);
242 const Vector<T> row(size_t i) const;
243 // </group>
244
245 // Returns a reference to the j'th column
246 // <group>
247 Vector<T> column(size_t j);
248 const Vector<T> column(size_t j) const;
249 // </group>
250
251 // Returns a diagonal from the Matrix. The Matrix must be square.
252 // n==0 is the main diagonal. n>0 is above the main diagonal, n<0
253 // is below it.
254 // <group>
255 Vector<T> diagonal(long long n=0);
256 const Vector<T> diagonal(long long n=0) const;
257 // </group>
258
259 // Take a slice of this matrix. Slices are always indexed starting
260 // at zero. This uses reference semantics, i.e. changing a value
261 // in the slice changes the original.
262 // <srcblock>
263 // Matrix<double> vd(100,100);
264 // //...
265 // vd(Slice(0,10),Slice(10,10)) = -1.0; // 10x10 sub-matrix set to -1.0
266 // </srcblock>
267 // <group>
268 Matrix<T> operator()(const Slice &sliceX, const Slice &sliceY);
269 const Matrix<T> operator()(const Slice &sliceX, const Slice &sliceY) const;
270 // </group>
271
272 // Slice using IPositions. Required to be defined, otherwise the base
273 // class versions are hidden.
274 // <group>
275 Array<T> operator()(const IPosition &blc, const IPosition &trc,
276 const IPosition &incr)
277 { return Array<T>::operator()(blc,trc,incr); }
278 const Array<T> operator()(const IPosition &blc, const IPosition &trc,
279 const IPosition &incr) const
280 { return Array<T>::operator()(blc,trc,incr); }
281 Array<T> operator()(const IPosition &blc, const IPosition &trc)
282 { return Array<T>::operator()(blc,trc); }
283 const Array<T> operator()(const IPosition &blc, const IPosition &trc) const
284 { return Array<T>::operator()(blc,trc); }
286 { return Array<T>::operator()(slicer); }
287 const Array<T> operator()(const Slicer& slicer) const
288 { return Array<T>::operator()(slicer); }
289 // </group>
290
291 // The length of each axis of the Matrix.
292 const IPosition &shape() const
293 { return this->length_p; }
294 void shape(int &s1, int &s2) const
295 { s1 = this->length_p(0); s2=this->length_p(1); }
296
297 // The number of rows in the Matrix, i.e. the length of the first axis.
298 size_t nrow() const
299 { return this->length_p(0); }
300
301 // The number of columns in the Matrix, i.e. the length of the 2nd axis.
302 size_t ncolumn() const
303 { return this->length_p(1); }
304
305 // Checks that the Matrix is consistent (invariants check out).
306 virtual bool ok() const override;
307
308protected:
309 virtual void preTakeStorage(const IPosition &shape) override;
310 // Remove the degenerate axes from other and store result in this matrix.
311 // An exception is thrown if removing degenerate axes does not result
312 // in a matrix.
313 virtual void doNonDegenerate(const Array<T> &other,
314 const IPosition &ignoreAxes) override;
315
316 virtual size_t fixedDimensionality() const override { return 2; }
317
318private:
319 // Cached constants to improve indexing.
320 // size_t xinc_p, yinc_p;
321
322 size_t xinc() const { return this->inc_p(0); }
323 size_t yinc() const { return this->inc_p(1)*this->originalLength_p(0); }
324};
325
326//# Declare extern templates for often used types.
327 extern template class Matrix<bool>;
328 extern template class Matrix<float>;
329 extern template class Matrix<double>;
330
331} //#End casa namespace
332
333#include "Matrix.tcc"
334
335#endif
size_t ndim() const
The dimensionality of this array.
Definition ArrayBase.h:96
bool contiguous_p
Are the data contiguous?
Definition ArrayBase.h:271
IPosition originalLength_p
Definition ArrayBase.h:274
IPosition length_p
Used to hold the shape, increment into the underlying storage and originalLength of the array.
Definition ArrayBase.h:274
Array< T > & operator=(const Array< T > &other)
TODO we should change the semantics.
Definition Array.h:291
T * begin_p
This pointer is adjusted to point to the first element of the array.
Definition Array.h:968
T & operator()(const IPosition &)
Access a single element of the array.
Array< T > & assign_conforming(const Array< T > &other)
Copy the values in other to this.
Definition Array.h:277
void resize()
Make this array a different shape.
size_t nrow() const
The number of rows in the Matrix, i.e.
Definition Matrix.h:298
Matrix< T > operator()(const Slice &sliceX, const Slice &sliceY)
Take a slice of this matrix.
const Vector< T > column(size_t j) const
const Array< T > operator()(const IPosition &blc, const IPosition &trc) const
Definition Matrix.h:283
Matrix(const Array< T > &source)
Construct a Matrix by reference from "source".
const T & operator()(const IPosition &i) const
Definition Matrix.h:191
Vector< T > diagonal(long long n=0)
Returns a diagonal from the Matrix.
size_t ncolumn() const
The number of columns in the Matrix, i.e.
Definition Matrix.h:302
Array< T > operator()(const IPosition &blc, const IPosition &trc, const IPosition &incr)
Slice using IPositions.
Definition Matrix.h:275
Matrix(Matrix< T > &&source)
const Vector< T > diagonal(long long n=0) const
Matrix< T > & operator=(const Matrix< T > &source)
Definition Matrix.h:131
const Vector< T > row(size_t i) const
Matrix< T > & assign_conforming(Array< T > &&source)
Definition Matrix.h:165
Matrix()
A Matrix of length zero in each dimension; zero origin.
Matrix< T > & operator=(const Array< T > &source)
Definition Matrix.h:135
Array< T > operator()(const Slicer &slicer)
Definition Matrix.h:285
Matrix< T > & operator=(Matrix< T > &&source)
Definition Matrix.h:133
Vector< T > row(size_t i)
Returns a reference to the i'th row.
Matrix(const IPosition &len, typename Array< T >::uninitializedType)
An uninitialized matrix of shape with shape "len".
void shape(int &s1, int &s2) const
Definition Matrix.h:294
Matrix(const IPosition &shape, const T *storage)
Create an Matrix of a given shape from a pointer.
T & operator()(const IPosition &i)
Single-pixel addressing.
Definition Matrix.h:189
const Array< T > operator()(const IPosition &blc, const IPosition &trc, const IPosition &incr) const
Definition Matrix.h:278
Matrix< T > & operator=(Array< T > &&source)
Definition Matrix.h:137
Matrix(size_t l1, size_t l2, typename Array< T >::uninitializedType)
An uninitialized Matrix with "l1" rows and "l2" columns.
size_t xinc() const
Cached constants to improve indexing.
Definition Matrix.h:322
void resize(size_t nx, size_t ny, bool copyValues=false)
Matrix(const IPosition &len, const T &initialValue=T())
A matrix of shape with shape "len".
Vector< T > column(size_t j)
Returns a reference to the j'th column.
Array< T > & operator=(const T &val)
Copy val into every element of this Matrix; i.e.
Definition Matrix.h:178
T & operator()(size_t i1, size_t i2)
Definition Matrix.h:193
const Array< T > operator()(const Slicer &slicer) const
Definition Matrix.h:287
Matrix(Array< T > &&source)
virtual void preTakeStorage(const IPosition &shape) override
pre/post processing hook of takeStorage() for subclasses.
static Matrix< T > identity(size_t n)
Create an identity matrix of side length n.
virtual bool ok() const override
Checks that the Matrix is consistent (invariants check out).
Matrix< T > & assign_conforming(const MaskedArray< T > &marray)
Copy to this those values in marray whose corresponding elements in marray's mask are true.
Definition Matrix.h:183
Array< T > operator()(const IPosition &blc, const IPosition &trc)
Definition Matrix.h:281
const IPosition & shape() const
The length of each axis of the Matrix.
Definition Matrix.h:292
Matrix< T > & assign_conforming(const Array< T > &source)
Definition Matrix.h:151
Matrix(const Matrix< T > &source)
The copy/move constructor uses reference semantics.
virtual void doNonDegenerate(const Array< T > &other, const IPosition &ignoreAxes) override
Remove the degenerate axes from other and store result in this matrix.
Matrix< T > & assign_conforming(Matrix< T > &&source)
Definition Matrix.h:148
Matrix< T > & assign_conforming(const Matrix< T > &source)
Copy the values from other to this Matrix.
Definition Matrix.h:146
Matrix(size_t l1, size_t l2, const T &initialValue=T())
A Matrix with "l1" rows and "l2" columns.
Matrix(const IPosition &shape, T *storage, StorageInitPolicy policy=COPY)
Create an Matrix of a given shape from a pointer.
size_t yinc() const
Definition Matrix.h:323
const T & operator()(size_t i1, size_t i2) const
Definition Matrix.h:199
virtual size_t fixedDimensionality() const override
Subclasses can return their dimensionality.
Definition Matrix.h:316
const Matrix< T > operator()(const Slice &sliceX, const Slice &sliceY) const
StorageInitPolicy
Definition ArrayBase.h:49
@ COPY
COPY is used when an internal copy of the storage is to be made.
Definition ArrayBase.h:52
this file contains all the compiler specific defines
Definition mainpage.dox:28
LatticeExprNode mask(const LatticeExprNode &expr)
This function returns the mask of the given expression.
TableExprNode marray(const TableExprNode &array, const TableExprNode &mask)
Form a masked array.
Definition ExprNode.h:1939
This is a tag for the constructor that may be used to construct an uninitialized Array.
Definition Array.h:179