casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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: aips2-request@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 //# $Id$
27 
28 #ifndef CASA_MATRIX_H
29 #define CASA_MATRIX_H
30 
31 
32 //# Includes
33 #include <casacore/casa/aips.h>
35 
36 namespace casacore { //#Begin casa namespace
37 
38 //# Forward Declarations
39 template<class T> class Vector;
40 
41 
42 // <summary> A 2-D Specialization of the Array class </summary>
43 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
44 // </reviewed>
45 //
46 // Matrix objects are two-dimensional specializations (e.g., more convenient
47 // and efficient indexing) of the general Array class. You might also want
48 // to look at the Array documentation to see inherited functionality. A
49 // tutorial on using the array classes in general is available in the
50 // "AIPS++ Programming Manual".
51 //
52 // Generally the member functions of Array are also available in
53 // Matrix versions which take a pair of integers where the array
54 // needs an IPosition. Since the Matrix
55 // is two-dimensional, the IPositions are overkill, although you may
56 // use those versions if you want to.
57 // <srcblock>
58 // Matrix<Int> mi(100,100); // Shape is 100x100
59 // mi.resize(50,50); // Shape now 50x50
60 // </srcblock>
61 //
62 // Slices may be taken with the Slice class. To take a slice, one "indexes"
63 // with one Slice(start, length, inc) for each axis,
64 // where end and inc are optional.
65 // Additionally, there are row(), column() and diagonal()
66 // member functions which return Vector's which refer to the storage back
67 // in the Matrix:
68 // <srcblock>
69 // Matrix<Float> mf(100, 100);
70 // mf.diagonal() = 1;
71 // </srcblock>
72 //
73 // Correct indexing order of a matrix is:
74 // <srcblock>
75 // Matrix<Int> mi(n1,n2) // [nrow, ncolumn]
76 // for (uInt j=0; j<mi.ncolumn(); j++) {
77 // for (uInt i=0; i<mi.nrow(); i++) {
78 // mi(i,j) = i*j;
79 // }
80 // }
81 // </srcblock>
82 //
83 //
84 // Element-by-element arithmetic and logical operations are available (in
85 // aips/ArrayMath.h and aips/ArrayLogical.h). Other Matrix operations (e.g.
86 // LU decomposition) are available, and more appear periodically.
87 //
88 // As with the Arrays, if the preprocessor symbol AIPS_DEBUG is
89 // defined at compile time invariants will be checked on entry to most
90 // member functions. Additionally, if AIPS_ARRAY_INDEX_CHECK is defined
91 // index operations will be bounds-checked. Neither of these should
92 // be defined for production code.
93 
94 template<class T> class Matrix : public Array<T>
95 {
96 public:
97  // A Matrix of length zero in each dimension; zero origin.
98  Matrix();
99 
100  // A Matrix with "l1" rows and "l2" columns.
101  Matrix(size_t l1, size_t l2);
102 
103  // A Matrix with "l1" rows and "l2" columns.
104  Matrix(size_t l1, size_t l2, ArrayInitPolicy initPolicy);
105 
106  // A Matrix with "l1" rows and "l2" columns.
107  // Fill it with the initial value.
108  Matrix(size_t l1, size_t l2, const T &initialValue);
109 
110  // A matrix of shape with shape "len".
111  Matrix(const IPosition &len);
112 
113  // A matrix of shape with shape "len".
114  Matrix(const IPosition &len, ArrayInitPolicy initPolicy);
115 
116  // A matrix of shape with shape "len".
117  // Fill it with the initial value.
118  Matrix(const IPosition &len, const T &initialValue);
119 
120  // The copy constructor uses reference semantics.
121  Matrix(const Matrix<T> &other);
122 
123  // Construct a Matrix by reference from "other". "other must have
124  // ndim() of 2 or less.
125  Matrix(const Array<T> &other);
126 
127  // Create an Matrix of a given shape from a pointer.
128  Matrix(const IPosition &shape, T *storage, StorageInitPolicy policy = COPY);
129  // Create an Matrix of a given shape from a pointer.
130  Matrix(const IPosition &shape, T *storage, StorageInitPolicy policy, AbstractAllocator<T> const &allocator);
131  // Create an Matrix of a given shape from a pointer. Because the pointer
132  // is const, a copy is always made.
133  Matrix(const IPosition &shape, const T *storage);
134 
135  // Define a destructor, otherwise the (SUN) compiler makes a static one.
136  virtual ~Matrix();
137 
138  // Create an identity matrix of side length n. (Could not do this as a constructor
139  // because of ambiguities with other constructors).
140  static Matrix<T> identity (size_t n);
141 
142  // Assign the other array (which must be dimension 2) to this matrix.
143  // If the shapes mismatch, this array is resized.
144  virtual void assign (const Array<T>& other);
145 
146  // Make this matrix a reference to other. Other must be of dimensionality
147  // 2 or less.
148  virtual void reference(const Array<T> &other);
149 
150  // Resize to the given shape (must be 2-dimensional).
151  // Resize without argument is equal to resize(0,0).
152  // <group>
153  using Array<T>::resize;
154  void resize(size_t nx, size_t ny, Bool copyValues=False) {
156  }
157  void resize(size_t nx, size_t ny, Bool copyValues, ArrayInitPolicy policy);
158  virtual void resize();
159  virtual void resize(const IPosition &newShape, Bool copyValues, ArrayInitPolicy policy);
160  // </group>
161 
162  // Copy the values from other to this Matrix. If this matrix has zero
163  // elements then it will resize to be the same shape as other; otherwise
164  // other must conform to this.
165  // Note that the assign function can be used to assign a
166  // non-conforming matrix.
167  // <group>
168  Matrix<T> &operator=(const Matrix<T> &other);
169  virtual Array<T> &operator=(const Array<T> &other);
170  // </group>
171 
172  // Copy val into every element of this Matrix; i.e. behaves as if
173  // val were a constant conformant matrix.
174  Array<T> &operator=(const T &val)
175  { return Array<T>::operator=(val); }
176 
177  // Copy to this those values in marray whose corresponding elements
178  // in marray's mask are True.
180  { Array<T> (*this) = marray; return *this; }
181 
182 
183  // Single-pixel addressing. If AIPS_ARRAY_INDEX_CHECK is defined,
184  // bounds checking is performed.
185  // <group>
186  T &operator()(const IPosition &i)
187  { return Array<T>::operator()(i); }
188  const T &operator()(const IPosition &i) const
189  { return Array<T>::operator()(i); }
190  T &operator()(size_t i1, size_t i2)
191  {
192 #if defined(AIPS_ARRAY_INDEX_CHECK)
193  this->validateIndex(i1, i2); // Throws an exception on failure
194 #endif
195  return this->contiguous_p ? this->begin_p[i1 + i2*yinc_p] :
196  this->begin_p[i1*xinc_p + i2*yinc_p];
197  }
198 
199  const T &operator()(size_t i1, size_t i2) const
200  {
201 #if defined(AIPS_ARRAY_INDEX_CHECK)
202  this->validateIndex(i1, i2); // Throws an exception on failure
203 #endif
204  return this->contiguous_p ? this->begin_p[i1 + i2*yinc_p] :
205  this->begin_p[i1*xinc_p + i2*yinc_p];
206  }
207  // </group>
208 
209 
210  // The array is masked by the input LogicalArray.
211  // This mask must conform to the array.
212  // <group>
213 
214  // Return a MaskedArray.
215  MaskedArray<T> operator() (const LogicalArray &mask) const
216  { return Array<T>::operator() (mask); }
217 
218  // Return a MaskedArray.
219  MaskedArray<T> operator() (const LogicalArray &mask)
220  { return Array<T>::operator() (mask); }
221 
222  // </group>
223 
224 
225  // The array is masked by the input MaskedLogicalArray.
226  // The mask is effectively the AND of the internal LogicalArray
227  // and the internal mask of the MaskedLogicalArray.
228  // The MaskedLogicalArray must conform to the array.
229  // <group>
230 
231  // Return a MaskedArray.
232  MaskedArray<T> operator() (const MaskedLogicalArray &mask) const
233  { return Array<T>::operator() (mask); }
234 
235  // Return a MaskedArray.
236  MaskedArray<T> operator() (const MaskedLogicalArray &mask)
237  { return Array<T>::operator() (mask); }
238 
239  // </group>
240 
241 
242  // Returns a reference to the i'th row.
243  // <group>
244  Vector<T> row(size_t i);
245  const Vector<T> row(size_t i) const;
246  // </group>
247 
248  // Returns a reference to the j'th column
249  // <group>
250  Vector<T> column(size_t j);
251  const Vector<T> column(size_t j) const;
252  // </group>
253 
254  // Returns a diagonal from the Matrix. The Matrix must be square.
255  // n==0 is the main diagonal. n>0 is above the main diagonal, n<0
256  // is below it.
257  // <group>
258  Vector<T> diagonal(Int64 n=0);
259  const Vector<T> diagonal(Int64 n=0) const;
260  // </group>
261 
262  // Take a slice of this matrix. Slices are always indexed starting
263  // at zero. This uses reference semantics, i.e. changing a value
264  // in the slice changes the original.
265  // <srcblock>
266  // Matrix<Double> vd(100,100);
267  // //...
268  // vd(Slice(0,10),Slice(10,10)) = -1.0; // 10x10 sub-matrix set to -1.0
269  // </srcblock>
270  // <group>
271  Matrix<T> operator()(const Slice &sliceX, const Slice &sliceY);
272  const Matrix<T> operator()(const Slice &sliceX, const Slice &sliceY) const;
273  // </group>
274 
275  // Slice using IPositions. Required to be defined, otherwise the base
276  // class versions are hidden.
277  // <group>
278  Array<T> operator()(const IPosition &blc, const IPosition &trc,
279  const IPosition &incr)
280  { return Array<T>::operator()(blc,trc,incr); }
281  const Array<T> operator()(const IPosition &blc, const IPosition &trc,
282  const IPosition &incr) const
283  { return Array<T>::operator()(blc,trc,incr); }
284  Array<T> operator()(const IPosition &blc, const IPosition &trc)
285  { return Array<T>::operator()(blc,trc); }
286  const Array<T> operator()(const IPosition &blc, const IPosition &trc) const
287  { return Array<T>::operator()(blc,trc); }
288  Array<T> operator()(const Slicer& slicer)
289  { return Array<T>::operator()(slicer); }
290  const Array<T> operator()(const Slicer& slicer) const
291  { return Array<T>::operator()(slicer); }
292  // </group>
293 
294  // The length of each axis of the Matrix.
295  const IPosition &shape() const
296  { return this->length_p; }
297  void shape(Int &s1, Int &s2) const
298  { s1 = this->length_p(0); s2=this->length_p(1); }
299 
300  // The number of rows in the Matrix, i.e. the length of the first axis.
301  size_t nrow() const
302  { return this->length_p(0); }
303 
304  // The number of columns in the Matrix, i.e. the length of the 2nd axis.
305  size_t ncolumn() const
306  { return this->length_p(1); }
307 
308  // Checks that the Matrix is consistent (invariants check out).
309  virtual Bool ok() const;
310 
311 protected:
312  virtual void preTakeStorage(const IPosition &shape);
313  virtual void postTakeStorage();
314  // Remove the degenerate axes from other and store result in this matrix.
315  // An exception is thrown if removing degenerate axes does not result
316  // in a matrix.
317  virtual void doNonDegenerate(const Array<T> &other,
318  const IPosition &ignoreAxes);
319 
320 private:
321  // Cached constants to improve indexing.
322  size_t xinc_p, yinc_p;
323 
324  // Helper fn to calculate the indexing constants.
325  void makeIndexingConstants();
326 };
327 
328 //# Declare extern templates for often used types.
329  extern template class Matrix<Bool>;
330  extern template class Matrix<Float>;
331  extern template class Matrix<Double>;
332  extern template class Matrix<Complex>;
333  extern template class Matrix<DComplex>;
334 
335 } //#End casa namespace
336 #ifndef CASACORE_NO_AUTO_TEMPLATES
337 #include <casacore/casa/Arrays/Matrix.tcc>
338 #endif //# CASACORE_NO_AUTO_TEMPLATES
339 #endif
virtual void reference(const Array< T > &other)
Make this matrix a reference to other.
Bool contiguous_p
Are the data contiguous?
Definition: ArrayBase.h:268
A Vector of integers, for indexing into Array&lt;T&gt; objects.
Definition: IPosition.h:119
A 1-D Specialization of the Array class.
Array< T > operator()(const Slicer &slicer)
Definition: Matrix.h:288
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
Definition: aipsxtype.h:38
int Int
Definition: aipstype.h:50
std::vector< double > Vector
Definition: ds9context.h:24
Vector< T > column(size_t j)
Returns a reference to the j&#39;th column.
size_t nrow() const
The number of rows in the Matrix, i.e.
Definition: Matrix.h:301
size_t yinc_p
Definition: Matrix.h:322
const IPosition & shape() const
The length of each axis of the Matrix.
Definition: Matrix.h:295
LatticeExprNode mask(const LatticeExprNode &expr)
This function returns the mask of the given expression.
virtual ~Matrix()
Define a destructor, otherwise the (SUN) compiler makes a static one.
A 2-D Specialization of the Array class.
TableExprNode marray(const TableExprNode &array, const TableExprNode &mask)
Form a masked array.
Definition: ExprNode.h:1892
Vector< T > row(size_t i)
Returns a reference to the i&#39;th row.
virtual void assign(const Array< T > &other)
Assign the other array (which must be dimension 2) to this matrix.
A global enum used by some Array/Block constructors.
Definition: Allocator.h:54
IPosition length_p
Used to hold the shape, increment into the underlying storage and originalLength of the array...
Definition: ArrayBase.h:271
Class for masking an Array for operations on that Array.
void validateIndex(const IPosition &) const
T & operator()(size_t i1, size_t i2)
Definition: Matrix.h:190
virtual void resize()
Make this array a different shape.
void shape(Int &s1, Int &s2) const
Definition: Matrix.h:297
define a (start,length,increment) along an axis
Definition: Slice.h:93
const Array< T > operator()(const IPosition &blc, const IPosition &trc, const IPosition &incr) const
Definition: Matrix.h:281
virtual Array< T > & operator=(const Array< T > &other)
Copy the values in other to this.
const Array< T > operator()(const IPosition &blc, const IPosition &trc) const
Definition: Matrix.h:286
Array< T > & operator=(const T &val)
Copy val into every element of this Matrix; i.e.
Definition: Matrix.h:174
friend class Matrix< T >
Needed to be a friend for Matrix&lt;T&gt;::reference()
Definition: Array.h:684
T & operator()(const IPosition &)
Access a single element of the array.
virtual void preTakeStorage(const IPosition &shape)
pre/post processing hook of takeStorage() for subclasses.
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
Matrix< T > & operator=(const Matrix< T > &other)
Copy the values from other to this Matrix.
Array< T > operator()(const IPosition &blc, const IPosition &trc)
Definition: Matrix.h:284
size_t ncolumn() const
The number of columns in the Matrix, i.e.
Definition: Matrix.h:305
const T & operator()(const IPosition &i) const
Definition: Matrix.h:188
Vector< T > diagonal(Int64 n=0)
Returns a diagonal from the Matrix.
T * begin_p
This pointer is adjusted to point to the first element of the array.
Definition: Array.h:913
const Bool False
Definition: aipstype.h:44
template &lt;class T, class U&gt; class vector;
Definition: MSFlagger.h:37
Array< T > operator()(const IPosition &blc, const IPosition &trc, const IPosition &incr)
Slice using IPositions.
Definition: Matrix.h:278
Specify which elements to extract from an n-dimensional array.
Definition: Slicer.h:289
virtual void doNonDegenerate(const Array< T > &other, const IPosition &ignoreAxes)
Remove the degenerate axes from other and store result in this matrix.
static Matrix< T > identity(size_t n)
Create an identity matrix of side length n.
void makeIndexingConstants()
Helper fn to calculate the indexing constants.
void resize(size_t nx, size_t ny, Bool copyValues=False)
Definition: Matrix.h:154
const Array< T > operator()(const Slicer &slicer) const
Definition: Matrix.h:290
COPY is used when an internal copy of the storage is to be made.
Definition: ArrayBase.h:56
StorageInitPolicy
A global enum used by some Array constructors.
Definition: ArrayBase.h:53
Matrix()
A Matrix of length zero in each dimension; zero origin.
const T & operator()(size_t i1, size_t i2) const
Definition: Matrix.h:199
T & operator()(const IPosition &i)
Single-pixel addressing.
Definition: Matrix.h:186
virtual void postTakeStorage()
size_t xinc_p
Cached constants to improve indexing.
Definition: Matrix.h:322
virtual Bool ok() const
Checks that the Matrix is consistent (invariants check out).
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42