casacore
Loading...
Searching...
No Matches
LSQMatrix.h
Go to the documentation of this file.
1// # LSQMatrix.h: Support class for the LSQ package
2// # Copyright (C) 2004,2005,2006
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 SCIMATH_LSQMATRIX_H
27#define SCIMATH_LSQMATRIX_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <algorithm>
32#include <casacore/casa/Utilities/RecordTransformable.h>
33
34namespace casacore { // # NAMESPACE CASACORE - BEGIN
35
36// # Forward Declarations
37class AipsIO;
38
39// <summary> Support class for the LSQ package </summary>
40// <reviewed reviewer="Wim Brouw" date="2004/03/20" tests="tLSQFit"
41// demos="">
42// </reviewed>
43
44// <prerequisite>
45// <li> Some knowledge of Matrix operations
46// </prerequisite>
47//
48// <etymology>
49// From Least SQuares and Matrix
50// </etymology>
51//
52// <synopsis>
53// The LSQMatrix class contains the handling of the basic container used
54// in the <linkto class="LSQFit">LSQFit</linkto> class and its derivatives.
55// This basic container is a triangular matrix.
56//
57// The basic operations provided are referencing and indexing of cells,
58// rows, columns and diagonal of the triangular matrix.
59// The class is a private structure, with explicit friends.
60//
61// The class contains a number of public methods (with _pub in name) that
62// can be used anywhere, and which perform index range checking.
63//
64// The contents can be saved in a record (<src>toRecord</src>),
65// and an object can be created from a record (<src>fromRecord</src>).
66// The record identifier is 'tmat'.
67// </synopsis>
68//
69// <example>
70// See the <linkto class="LSQFit">LSQFit</linkto> class for its use.
71// </example>
72//
73// <motivation>
74// The class was written to isolate the handling of the normal equations
75// used in the <src>LSQ</src> classes.
76// </motivation>
77//
78// <todo asof="2004/03/20">
79// <li> Look in possibility of an STL iterator along row, column and
80// diagonal
81// </todo>
82
84 // # Friends
85 friend class LSQFit;
86
87 public:
88 // A set of public interface functions. Checks for index ranges are made,
89 // and zero or null returned if error.
90 // <group>
91 // Get row pointer in normal equation (points to element <src>[i][0]</src>)
92 Double *row_pub(uInt i) const { return (i < n_p) ? row(i) : 0; };
93 // Get next row or previous row pointer in normal equation if the pointer
94 // <src>row</src> is at row <src>i</src>.
95 // <group>
96 void incRow_pub(Double *&row, uInt i) const {
97 if (i < n_p - 1) incRow(row, i);
98 };
99 void decRow_pub(Double *&row, uInt i) const {
100 if (i > 0) decRow(row, i);
101 };
102 // </group>
103 // Get diagonal element pointer <src>[i][i]</src>
104 Double *diag_pub(uInt i) const { return ((i < n_p) ? diag(i) : 0); };
105 // Get length of triangular array
106 uInt nelements_pub() const { return (len_p); };
107 // Get number of rows
108 uInt nrows_pub() const { return n_p; };
109 // Make diagonal element 1 if zero (Note that this is always called when
110 // <src>invert()</src> is called). Only n-length sub-matrix is done.
112 if (n < n_p) doDiagonal(n);
113 }
114 // Multiply n-length of diagonal with <src>1+fac</src>
116 if (n < n_p) mulDiagonal(n, fac);
117 };
118 // Add <src>fac</src> to n-length of diagonal
120 if (n < n_p) addDiagonal(n, fac);
121 };
122 // Determine max of abs values of n-length of diagonal
123 Double maxDiagonal_pub(uInt n) { return ((n < n_p) ? maxDiagonal(n) : 0); };
124 // </group>
125
126 private:
127 // # Constructors
128 // Default constructor (empty, only usable after a <src>set(n)</src>)
130 // Construct an object with the number of rows and columns indicated.
131 // If a <src>Bool</src> argument is present, the number
132 // will be taken as double the number given (assumes complex).
133 // <group>
134 explicit LSQMatrix(uInt n);
136 // </group>
137 // Copy constructor (deep copy)
138 LSQMatrix(const LSQMatrix &other);
139 // Assignment (deep copy)
141
142 // # Destructor
144
145 // # Operators
146 // Index an element in the triangularised matrix
147 // <group>
148 Double &operator[](uInt index) { return (trian_p[index]); };
149 Double operator[](uInt index) const { return (trian_p[index]); };
150 // </group>
151
152 // # General Member Functions
153 // Reset all data to zero
154 void reset() { clear(); };
155 // Set new sizes (default is for Real, a Bool argument will make it complex)
156 // <group>
157 void set(uInt n);
158 void set(uInt n, Bool);
159 // </group>
160 // Get row pointer in normal equation (points to element <src>[i][0]</src>)
161 Double *row(uInt i) const { return &trian_p[((n2m1_p - i) * i) / 2]; };
162 // Get next row or previous row pointer in normal equation if the pointer
163 // <src>row</src> is at row <src>i</src>.
164 // <group>
165 void incRow(Double *&row, uInt i) const { row += nm1_p - i; };
166 void decRow(Double *&row, uInt i) const { row -= n_p - i; };
167 // </group>
168 // Get diagonal element pointer <src>[i][i]</src>
169 Double *diag(uInt i) const { return &trian_p[((n2p1_p - i) * i) / 2]; };
170 // Get length of triangular array
171 uInt nelements() const { return (len_p); };
172 // Get number of rows
173 uInt nrows() const { return n_p; };
174 // Copy data.
175 void copy(const LSQMatrix &other);
176 // Initialise matrix
177 void init();
178 // Clear matrix
179 void clear();
180 // De-initialise matrix
181 void deinit();
182 // Make diagonal element 1 if zero (Note that this is always called when
183 // <src>invert()</src> is called). Only n-length sub-matrix is done.
185 // Multiply n-length of diagonal with <src>1+fac</src>
186 void mulDiagonal(uInt n, Double fac);
187 // Add <src>fac</src> to n-length of diagonal
188 void addDiagonal(uInt n, Double fac);
189 // Determine max of abs values of n-length of diagonal
191 // Create a Matrix from a record. An error message is generated, and False
192 // returned if an invalid record is given. A valid record will return True.
193 // Error messages are postfixed to error.
194 // <group>
196 // </group>
197 // Create a record from an LSQMatrix. The return will be False and an error
198 // message generated only if the object does not contain a valid Matrix.
199 // Error messages are postfixed to error.
200 Bool toRecord(String &error, RecordInterface &out) const;
201 // Get identification of record
202 const String &ident() const;
203 // Convert a <src>carray</src> to/from a record. Field only written if
204 // non-zero length. No carray created if field does not exist on input.
205 // False returned if unexpectedly no data available for non-zero length
206 // (put), or a field has zero length vector(get).
207 // <group>
208 static Bool putCArray(String &error, RecordInterface &out, const String &fname, uInt len,
209 const Double *const in);
210 static Bool getCArray(String &error, const RecordInterface &in, const String &fname, uInt len,
211 Double *&out);
212 static Bool putCArray(String &error, RecordInterface &out, const String &fname, uInt len,
213 const uInt *const in);
214 static Bool getCArray(String &error, const RecordInterface &in, const String &fname, uInt len,
215 uInt *&out);
216 // </group>
217
218 // Save or restore using AipsIO.
220 void toAipsIO(AipsIO &out) const;
221 static void putCArray(AipsIO &out, uInt len, const Double *const in);
222 static void getCArray(AipsIO &in, uInt len, Double *&out);
223 static void putCArray(AipsIO &out, uInt len, const uInt *const in);
224 static void getCArray(AipsIO &in, uInt len, uInt *&out);
225
226 // # Data
227 // Matrix size (linear size)
229 // Derived sizes (all 0 if n_p equals 0)
230 // <group>
231 // Total size
233 // <src>n-1</src>
235 // <src>2n-1</src>
237 // <src>2n+1</src>
239 // </group>
240 // Matrix (triangular n_p * n_p)
242 // Record field names
243 static const String tmatsiz;
244 static const String tmatdat;
245 // <group>
246 // </group>
247 //
248};
249
250} // namespace casacore
251
252#endif
Double * row_pub(uInt i) const
A set of public interface functions.
Definition LSQMatrix.h:92
static Bool putCArray(String &error, RecordInterface &out, const String &fname, uInt len, const uInt *const in)
void reset()
Reset all data to zero.
Definition LSQMatrix.h:154
LSQMatrix()
Default constructor (empty, only usable after a set(n)).
void addDiagonal_pub(uInt n, Double fac)
Add fac to n-length of diagonal.
Definition LSQMatrix.h:119
LSQMatrix & operator=(const LSQMatrix &other)
Assignment (deep copy).
static Bool putCArray(String &error, RecordInterface &out, const String &fname, uInt len, const Double *const in)
Convert a carray to/from a record.
void decRow_pub(Double *&row, uInt i) const
Definition LSQMatrix.h:99
void addDiagonal(uInt n, Double fac)
Add fac to n-length of diagonal.
void toAipsIO(AipsIO &out) const
Double & operator[](uInt index)
Index an element in the triangularised matrix.
Definition LSQMatrix.h:148
void clear()
Clear matrix.
const String & ident() const
Get identification of record.
static const String tmatsiz
Record field names.
Definition LSQMatrix.h:243
Double * row(uInt i) const
Get row pointer in normal equation (points to element [i][0]).
Definition LSQMatrix.h:161
Double * diag(uInt i) const
Get diagonal element pointer [i][i].
Definition LSQMatrix.h:169
Double * trian_p
Matrix (triangular n_p * n_p).
Definition LSQMatrix.h:241
void set(uInt n, Bool)
Double operator[](uInt index) const
Definition LSQMatrix.h:149
static void getCArray(AipsIO &in, uInt len, Double *&out)
friend class LSQFit
Definition LSQMatrix.h:85
void incRow_pub(Double *&row, uInt i) const
Get next row or previous row pointer in normal equation if the pointer row is at row i.
Definition LSQMatrix.h:96
void decRow(Double *&row, uInt i) const
Definition LSQMatrix.h:166
void deinit()
De-initialise matrix.
uInt nrows() const
Get number of rows.
Definition LSQMatrix.h:173
LSQMatrix(uInt n)
Construct an object with the number of rows and columns indicated.
static void getCArray(AipsIO &in, uInt len, uInt *&out)
uInt nelements_pub() const
Get length of triangular array.
Definition LSQMatrix.h:106
Bool toRecord(String &error, RecordInterface &out) const
Create a record from an LSQMatrix.
static Bool getCArray(String &error, const RecordInterface &in, const String &fname, uInt len, Double *&out)
static void putCArray(AipsIO &out, uInt len, const Double *const in)
void set(uInt n)
Set new sizes (default is for Real, a Bool argument will make it complex).
void fromAipsIO(AipsIO &in)
Save or restore using AipsIO.
void incRow(Double *&row, uInt i) const
Get next row or previous row pointer in normal equation if the pointer row is at row i.
Definition LSQMatrix.h:165
Double * diag_pub(uInt i) const
Get diagonal element pointer [i][i].
Definition LSQMatrix.h:104
LSQMatrix(uInt n, Bool)
void doDiagonal(uInt n)
Make diagonal element 1 if zero (Note that this is always called when invert() is called).
Double maxDiagonal(uInt n)
Determine max of abs values of n-length of diagonal.
Double maxDiagonal_pub(uInt n)
Determine max of abs values of n-length of diagonal.
Definition LSQMatrix.h:123
uInt len_p
Derived sizes (all 0 if n_p equals 0).
Definition LSQMatrix.h:232
uInt n_p
Matrix size (linear size).
Definition LSQMatrix.h:228
static Bool getCArray(String &error, const RecordInterface &in, const String &fname, uInt len, uInt *&out)
void mulDiagonal(uInt n, Double fac)
Multiply n-length of diagonal with 1+fac.
void init()
Initialise matrix.
void mulDiagonal_pub(uInt n, Double fac)
Multiply n-length of diagonal with 1+fac.
Definition LSQMatrix.h:115
void copy(const LSQMatrix &other)
Copy data.
void doDiagonal_pub(uInt n)
Make diagonal element 1 if zero (Note that this is always called when invert() is called).
Definition LSQMatrix.h:111
static void putCArray(AipsIO &out, uInt len, const uInt *const in)
uInt nrows_pub() const
Get number of rows.
Definition LSQMatrix.h:108
uInt nelements() const
Get length of triangular array.
Definition LSQMatrix.h:171
static const String tmatdat
Definition LSQMatrix.h:244
Bool fromRecord(String &error, const RecordInterface &in)
Create a Matrix from a record.
LSQMatrix(const LSQMatrix &other)
Copy constructor (deep copy).
String: the storage and methods of handling collections of characters.
Definition String.h:355
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
RecordInterface()
The default constructor creates an empty record with a variable structure.
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
double Double
Definition aipstype.h:53