casacore
Loading...
Searching...
No Matches
LatticeExpr.h
Go to the documentation of this file.
1// # LatticeExpr.h: LatticeExpr.h
2// # Copyright (C) 1997,1998,1999,2000,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 LATTICES_LATTICEEXPR_H
27#define LATTICES_LATTICEEXPR_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/lattices/Lattices/MaskedLattice.h>
32#include <casacore/lattices/LEL/LatticeExprNode.h>
33#include <casacore/lattices/LRegions/LatticeRegion.h>
34#include <casacore/casa/Arrays/Slicer.h>
35#include <casacore/casa/Arrays/ArrayFwd.h>
36
37namespace casacore { // # NAMESPACE CASACORE - BEGIN
38
39// # Forward Declarations
40template <class T>
41class LELArray;
42
43// <summary> Class to allow C++ expressions involving lattices </summary>
44
45// <use visibility=export>
46
47// <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
48// </reviewed>
49
50// <prerequisite>
51// <li> <linkto class="Lattice"> Lattice</linkto>
52// <li> <linkto class="LatticeExprNode"> LatticeExprNode</linkto>
53//
54// </prerequisite>
55//
56// <etymology>
57// The name is derived from the fact that this class provides
58// an expression interface to the user which s/he may use to
59// write C++ expressions involving Lattices.
60// </etymology>
61//
62// <synopsis>
63// This class provides an interface which allows the C++ programmer
64// to enter expressions such as "sin(a)+b" where "a" and "b"
65// are Lattices.
66//
67// This class is termed an envelope class, and inside it are the
68// letter classes which do the real work. In reality, the letter
69// classes are actually accessed via a bridging class called
70// LatticeExprNode, which exists to handle type conversions.
71// The letter classes iterate through the Lattices and evaluate the
72// expression for each chunk of the iteration (usually a tile shape).
73//
74// It is in the LatticeExprNode class that all the available expression
75// operations are defined, so you should look there to see what
76// functionality is available.
77//
78// A description of the implementation details of these classes can
79// be found in
80// <a href="../notes/216.html">Note 216</a>
81// </synopsis>
82//
83// <example>
84// <srcblock>
85// ArrayLattice<Float> f1(IPosition (2,nx,ny));
86// ArrayLattice<Float> f2(IPosition (2,nx,ny));
87// f2.set(2.0);
88// f1.copyData(2*f2+f2);
89// </srcblock>
90//
91// In this example, the values of the pixels in Lattice f1 are set
92// to the values resulting from the expression "2*f2 + f2"
93// I.e. the expression is evaluated for each pixel in the Lattices
94//
95// Note that :
96// 1) the Lattice::copyData function is expecting a Lattice argument.
97// 2) LatticeExpr inherits from Lattice and therefore a LatticeExpr
98// object is a valid argument object type
99// 3) The expression in the copyData call is automatically converted to
100// a LatticeExprNode by the constructors and operators in LatticeExprNode
101// 4) The LatticeExprNode object so created is automatically converted
102// to a LatticeExpr by casting functions in LatticeExprNode.
103// </example>
104//
105// <example>
106// <srcblock>
107// ArrayLattice<Float> f1(IPosition (2,nx,ny));
108// ArrayLattice<Float> f2(IPosition (2,nx,ny));
109// ArrayLattice<Double> d(IPosition (2,nx,ny));
110// ArrayLattice<Complex> c(IPosition (2,nx,ny));
111// ArrayLattice<Bool> b(IPosition (2,nx,ny));
112//
113// f2.set(1.0); d.set(2.0); c.set(Complex(2.0,3.0)); b.set(True);
114// f1.copyData( (3.5*f2) + (cos(d)) - (10/min(d,f2)*(-abs(c))*ntrue(b)) - (C::pi) );
115// </srcblock>
116//
117// In this rather silly example, we fill Lattice "f1" with the result of the
118// expression. The expression shows the use of constants, unary operations,
119// binary operations, 1D and 2D functions. It also shows how mixed types can
120// be handled. The output Lattice is a Float, whereas mixed into the
121// expression are subexpressions involving Float, Double, Complex and Bool
122// Lattices.
123//
124// </example>
125//
126// <motivation>
127// The Lattice expression classes enable the C++ programmer much simpler
128// handling of mathematical expressions involving lattices. In addition,
129// these classes provide the infrastructure on top of which we can build
130// an image calculator for Glish users
131// </motivation>
132
133// <todo asof="1997/01/15">
134// <li> masks
135// <li> regions
136// </todo>
137
138template <class T>
139class LatticeExpr : public MaskedLattice<T> {
140 public:
141 // Default constructor
143
144 // Constructor from an arbitrary LatticeExprNode expression object.
145 // An exception is thrown if the expression data type cannot be
146 // converted to the template data type.
147 // The shape argument is mandatory if the expression has no shape.
148 // If the expression has a shape and if shape is given, it is checked
149 // if they are equal.
151 LatticeExpr(const LatticeExprNode& expr, const IPosition& latticeShape);
152
153 // Copy constructor (reference semantics)
155
156 // Destructor, does nothing
157 virtual ~LatticeExpr();
158
159 // Assignment (reference semantics)
161
162 // Make a copy of the derived object (reference semantics).
163 virtual MaskedLattice<T>* cloneML() const;
164
165 // Has the object really a mask?
166 virtual Bool isMasked() const;
167
168 // Get the region used (always returns 0).
169 virtual const LatticeRegion* getRegionPtr() const;
170
171 // Returns False, as the LatticeExpr lattice is not writable.
172 virtual Bool isWritable() const;
173
174 // Handle locking of the LatticeExpr which is delegated to all of its parts.
175 // <br>hasLock() is True if all parts of the expression return True.
176 // <br>It is strongly recommended to use class
177 // <linkto class=LatticeLocker>LatticeLocker</linkto> to
178 // handle lattice locking. It also contains a more detailed
179 // explanation of the locking process.
180 // <group>
181 virtual Bool lock(FileLocker::LockType, uInt nattempts);
182 virtual void unlock();
184 // </group>
185
186 // Resynchronize the Lattice object with the lattice file.
187 // This function is only useful if no read-locking is used, ie.
188 // if the table lock option is UserNoReadLocking or AutoNoReadLocking.
189 // In that cases the table system does not acquire a read-lock, thus
190 // does not synchronize itself automatically.
191 // <br>By default the function does not do anything at all.
192 virtual void resync();
193
194 // Returns the shape of the Lattice including all degenerate axes
195 // (i.e. axes with a length of one)
196 virtual IPosition shape() const;
197
198 // Return the best cursor shape.
199 virtual IPosition doNiceCursorShape(uInt maxPixels) const;
200
201 // Returns the coordinates of the lattice expression.
203
204 // Do the actual get of the data.
205 // The return value is always False, thus the buffer does not reference
206 // another array.
207 virtual Bool doGetSlice(Array<T>& buffer, const Slicer& section);
208
209 // Do the actual get of the mask data.
210 // The return value is always False, thus the buffer does not reference
211 // another array.
212 virtual Bool doGetMaskSlice(Array<Bool>& buffer, const Slicer& section);
213
214 // An expression is not writable so this functions throws an exception.
215 virtual void doPutSlice(const Array<T>& sourceBuffer, const IPosition& where,
216 const IPosition& stride);
217
218 // Copy the data from this lattice to the given lattice.
219 virtual void copyDataTo(Lattice<T>& to) const;
220
221 // Handle the Math operators (+=, -=, *=, /=).
222 // They work similarly to copyData(To).
223 // However, they are not defined for Bool types, thus specialized below.
224 virtual void handleMathTo(Lattice<T>& to, int oper) const;
225
226 private:
227 // Initialize the object from the expression.
228 void init(const LatticeExprNode& expr);
229
230 LatticeExprNode expr_p; // # its shape can be undefined
231 IPosition shape_p; // # this shape is always defined
234};
235
236template <>
239}
240
241} // namespace casacore
242
243#ifndef CASACORE_NO_AUTO_TEMPLATES
244#include <casacore/lattices/LEL/LatticeExpr.tcc>
245#endif // # CASACORE_NO_AUTO_TEMPLATES
246#endif
LockType
Define the possible lock types.
Definition FileLocker.h:89
void throwBoolMath() const
Throw an exception for arithmetic on a Bool Lattice.
virtual void resync()
Resynchronize the Lattice object with the lattice file.
virtual void doPutSlice(const Array< T > &sourceBuffer, const IPosition &where, const IPosition &stride)
An expression is not writable so this functions throws an exception.
virtual MaskedLattice< T > * cloneML() const
Make a copy of the derived object (reference semantics).
virtual IPosition doNiceCursorShape(uInt maxPixels) const
Return the best cursor shape.
virtual Bool doGetMaskSlice(Array< Bool > &buffer, const Slicer &section)
Do the actual get of the mask data.
virtual Bool doGetSlice(Array< T > &buffer, const Slicer &section)
Do the actual get of the data.
virtual void copyDataTo(Lattice< T > &to) const
Copy the data from this lattice to the given lattice.
LELArray< T > * lastChunkPtr_p
virtual ~LatticeExpr()
Destructor, does nothing.
virtual Bool isWritable() const
Returns False, as the LatticeExpr lattice is not writable.
LatticeExpr(const LatticeExpr< T > &other)
Copy constructor (reference semantics).
LatticeExpr(const LatticeExprNode &expr)
Constructor from an arbitrary LatticeExprNode expression object.
virtual LELCoordinates lelCoordinates() const
Returns the coordinates of the lattice expression.
LatticeExpr(const LatticeExprNode &expr, const IPosition &latticeShape)
LatticeExprNode expr_p
LatticeExpr< T > & operator=(const LatticeExpr< T > &other)
Assignment (reference semantics).
virtual Bool hasLock(FileLocker::LockType) const
virtual Bool isMasked() const
Has the object really a mask?
virtual IPosition shape() const
Returns the shape of the Lattice including all degenerate axes (i.e.
void init(const LatticeExprNode &expr)
Initialize the object from the expression.
virtual void handleMathTo(Lattice< T > &to, int oper) const
Handle the Math operators (+=, -=, *=, /=).
virtual Bool lock(FileLocker::LockType, uInt nattempts)
Handle locking of the LatticeExpr which is delegated to all of its parts.
LatticeExpr()
Default constructor.
virtual const LatticeRegion * getRegionPtr() const
Get the region used (always returns 0).
virtual void unlock()
MaskedLattice()
Default constructor.
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40