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 { if (i<n_p-1) incRow(row,i); };
97 void decRow_pub(Double *&row, uInt i) const { if (i>0) decRow(row,i); };
98 // </group>
99 // Get diagonal element pointer <src>[i][i]</src>
100 Double *diag_pub(uInt i) const { return ((i<n_p) ? diag(i) : 0); };
101 // Get length of triangular array
102 uInt nelements_pub() const { return (len_p); };
103 // Get number of rows
104 uInt nrows_pub() const { return n_p; };
105 // Make diagonal element 1 if zero (Note that this is always called when
106 // <src>invert()</src> is called). Only n-length sub-matrix is done.
107 void doDiagonal_pub(uInt n) { if (n<n_p) doDiagonal(n); }
108 // Multiply n-length of diagonal with <src>1+fac</src>
109 void mulDiagonal_pub(uInt n, Double fac) { if (n<n_p) mulDiagonal(n,fac); };
110 // Add <src>fac</src> to n-length of diagonal
111 void addDiagonal_pub(uInt n, Double fac) { if (n<n_p) addDiagonal(n,fac); };
112 // Determine max of abs values of n-length of diagonal
113 Double maxDiagonal_pub(uInt n){ return ((n<n_p) ? maxDiagonal(n) : 0); };
114 // </group>
115
116 private:
117 //# Constructors
118 // Default constructor (empty, only usable after a <src>set(n)</src>)
120 // Construct an object with the number of rows and columns indicated.
121 // If a <src>Bool</src> argument is present, the number
122 // will be taken as double the number given (assumes complex).
123 // <group>
124 explicit LSQMatrix(uInt n);
126 // </group>
127 // Copy constructor (deep copy)
128 LSQMatrix(const LSQMatrix &other);
129 // Assignment (deep copy)
131
132 //# Destructor
134
135 //# Operators
136 // Index an element in the triangularised matrix
137 // <group>
138 Double &operator[](uInt index) { return (trian_p[index]); };
139 Double operator[](uInt index) const { return (trian_p[index]); };
140 // </group>
141
142 //# General Member Functions
143 // Reset all data to zero
144 void reset() { clear(); };
145 // Set new sizes (default is for Real, a Bool argument will make it complex)
146 // <group>
147 void set(uInt n);
148 void set(uInt n, Bool);
149 // </group>
150 // Get row pointer in normal equation (points to element <src>[i][0]</src>)
151 Double *row(uInt i) const { return &trian_p[((n2m1_p-i)*i)/2]; };
152 // Get next row or previous row pointer in normal equation if the pointer
153 // <src>row</src> is at row <src>i</src>.
154 // <group>
155 void incRow(Double *&row, uInt i) const { row += nm1_p-i; };
156 void decRow(Double *&row, uInt i) const { row -= n_p-i; };
157 // </group>
158 // Get diagonal element pointer <src>[i][i]</src>
159 Double *diag(uInt i) const { return &trian_p[((n2p1_p-i)*i)/2]; };
160 // Get length of triangular array
161 uInt nelements() const { return (len_p); };
162 // Get number of rows
163 uInt nrows() const { return n_p; };
164 // Copy data.
165 void copy(const LSQMatrix &other);
166 // Initialise matrix
167 void init();
168 // Clear matrix
169 void clear();
170 // De-initialise matrix
171 void deinit();
172 // Make diagonal element 1 if zero (Note that this is always called when
173 // <src>invert()</src> is called). Only n-length sub-matrix is done.
175 // Multiply n-length of diagonal with <src>1+fac</src>
176 void mulDiagonal(uInt n, Double fac);
177 // Add <src>fac</src> to n-length of diagonal
178 void addDiagonal(uInt n, Double fac);
179 // Determine max of abs values of n-length of diagonal
181 // Create a Matrix from a record. An error message is generated, and False
182 // returned if an invalid record is given. A valid record will return True.
183 // Error messages are postfixed to error.
184 // <group>
186 // </group>
187 // Create a record from an LSQMatrix. The return will be False and an error
188 // message generated only if the object does not contain a valid Matrix.
189 // Error messages are postfixed to error.
190 Bool toRecord(String &error, RecordInterface &out) const;
191 // Get identification of record
192 const String &ident() const;
193 // Convert a <src>carray</src> to/from a record. Field only written if
194 // non-zero length. No carray created if field does not exist on input.
195 // False returned if unexpectedly no data available for non-zero length
196 // (put), or a field has zero length vector(get).
197 // <group>
199 const String &fname,
200 uInt len, const Double * const in);
201 static Bool getCArray(String &error, const RecordInterface &in,
202 const String &fname,
203 uInt len, Double *&out);
205 const String &fname,
206 uInt len, const uInt * const in);
207 static Bool getCArray(String &error, const RecordInterface &in,
208 const String &fname,
209 uInt len, uInt *&out);
210 // </group>
211
212 // Save or restore using AipsIO.
213 void fromAipsIO (AipsIO& in);
214 void toAipsIO (AipsIO& out) const;
215 static void putCArray (AipsIO& out, uInt len, const Double* const in);
216 static void getCArray (AipsIO& in, uInt len, Double*& out);
217 static void putCArray (AipsIO& out, uInt len, const uInt* const in);
218 static void getCArray (AipsIO& in, uInt len, uInt*& out);
219
220 //# Data
221 // Matrix size (linear size)
223 // Derived sizes (all 0 if n_p equals 0)
224 // <group>
225 // Total size
227 // <src>n-1</src>
229 // <src>2n-1</src>
231 // <src>2n+1</src>
233 // </group>
234 // Matrix (triangular n_p * n_p)
236 // Record field names
237 static const String tmatsiz;
238 static const String tmatdat;
239 // <group>
240 // </group>
241 //
242};
243
244
245} //# NAMESPACE CASACORE - END
246
247#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:144
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:111
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:97
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:138
void clear()
Clear matrix.
const String & ident() const
Get identification of record.
static const String tmatsiz
Record field names.
Definition LSQMatrix.h:237
Double * row(uInt i) const
Get row pointer in normal equation (points to element [i][0])
Definition LSQMatrix.h:151
Double * diag(uInt i) const
Get diagonal element pointer [i][i]
Definition LSQMatrix.h:159
Double * trian_p
Matrix (triangular n_p * n_p)
Definition LSQMatrix.h:235
void set(uInt n, Bool)
Double operator[](uInt index) const
Definition LSQMatrix.h:139
static void getCArray(AipsIO &in, uInt len, Double *&out)
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:156
void deinit()
De-initialise matrix.
uInt nrows() const
Get number of rows.
Definition LSQMatrix.h:163
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:102
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:155
Double * diag_pub(uInt i) const
Get diagonal element pointer [i][i]
Definition LSQMatrix.h:100
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:113
uInt len_p
Derived sizes (all 0 if n_p equals 0)
Definition LSQMatrix.h:226
uInt n_p
Matrix size (linear size)
Definition LSQMatrix.h:222
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:109
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:107
static void putCArray(AipsIO &out, uInt len, const uInt *const in)
uInt nrows_pub() const
Get number of rows.
Definition LSQMatrix.h:104
uInt nelements() const
Get length of triangular array.
Definition LSQMatrix.h:161
static const String tmatdat
Definition LSQMatrix.h:238
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:223
this file contains all the compiler specific defines
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
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