ARTS  2.3.1285(git:92a29ea9-dirty)
matpackIV.h
Go to the documentation of this file.
1 /* Copyright (C) 2002-2012
2  Stefan Buehler <sbuehler@ltu.se>
3  Wolfram-Andre Haas <wolhaas@hermes.fho-emden.de>
4 
5  This program is free software; you can redistribute it and/or modify it
6  under the terms of the GNU General Public License as published by the
7  Free Software Foundation; either version 2, or (at your option) any
8  later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18  USA. */
19 
31 #ifndef matpackIV_h
32 #define matpackIV_h
33 
34 #include "matpackIII.h"
35 
38 class Iterator4D {
39  public:
40  // Constructors:
42  Iterator4D() = default;
43 
45  Iterator4D(const Tensor3View& x, Index stride)
46  : msv(x), mstride(stride) { /* Nothing to do here. */
47  }
48 
49  // Operators:
52  msv.mdata += mstride;
53  return *this;
54  }
55 
57  bool operator!=(const Iterator4D& other) const {
59  other.msv.mdata + other.msv.mpr.mstart + other.msv.mrr.mstart +
60  other.msv.mcr.mstart)
61  return true;
62  else
63  return false;
64  }
65 
68 
69  private:
74 };
75 
78  public:
79  // Constructors:
80  // Functions for ConstIterator4D
81  // -----------------------------
82 
84  ConstIterator4D() = default;
85 
88  : msv(x), mstride(stride) { /* Nothing to do here. */
89  }
90 
91  // Operators:
94  msv.mdata += mstride;
95  return *this;
96  }
97 
99  bool operator!=(const ConstIterator4D& other) const {
100  if (msv.mdata + msv.mpr.mstart + msv.mrr.mstart + msv.mcr.mstart !=
101  other.msv.mdata + other.msv.mpr.mstart + other.msv.mrr.mstart +
102  other.msv.mcr.mstart)
103  return true;
104  else
105  return false;
106  }
107 
108  const ConstTensor3View* operator->() const;
109  const ConstTensor3View& operator*() const;
110 
111  private:
116 };
117 
118 // Declare class Tensor4:
119 class Tensor4;
120 
134  public:
135  constexpr ConstTensor4View(const ConstTensor4View&) = default;
136  constexpr ConstTensor4View(ConstTensor4View&&) = default;
137  ConstTensor4View& operator=(const ConstTensor4View&) = default;
138  ConstTensor4View& operator=(ConstTensor4View&&) = default;
139 
140  // Member functions:
141 
142  bool empty() const;
143 
144  Index nbooks() const;
145  Index npages() const;
146  Index nrows() const;
147  Index ncols() const;
148 
149  // Const index operators:
150  ConstTensor4View operator()(const Range& b,
151  const Range& p,
152  const Range& r,
153  const Range& c) const;
154 
155  ConstTensor3View operator()(const Range& b,
156  const Range& p,
157  const Range& r,
158  Index c) const;
159  ConstTensor3View operator()(const Range& b,
160  const Range& p,
161  Index r,
162  const Range& c) const;
163  ConstTensor3View operator()(const Range& b,
164  Index p,
165  const Range& r,
166  const Range& c) const;
167  ConstTensor3View operator()(Index b,
168  const Range& p,
169  const Range& r,
170  const Range& c) const;
171 
172  ConstMatrixView operator()(const Range& b,
173  const Range& p,
174  Index r,
175  Index c) const;
176  ConstMatrixView operator()(const Range& b,
177  Index p,
178  const Range& r,
179  Index c) const;
180  ConstMatrixView operator()(const Range& b,
181  Index p,
182  Index r,
183  const Range& c) const;
184  ConstMatrixView operator()(Index b,
185  const Range& p,
186  Index r,
187  const Range& c) const;
188  ConstMatrixView operator()(Index b,
189  const Range& p,
190  const Range& r,
191  Index c) const;
192  ConstMatrixView operator()(Index b,
193  Index p,
194  const Range& r,
195  const Range& c) const;
196 
197  ConstVectorView operator()(const Range& b, Index p, Index r, Index c) const;
198  ConstVectorView operator()(Index b, const Range& p, Index r, Index c) const;
199  ConstVectorView operator()(Index b, Index p, const Range& r, Index c) const;
200  ConstVectorView operator()(Index b, Index p, Index r, const Range& c) const;
201 
204  Index p,
205  Index r,
206  Index c) const { // Check if indices are valid:
207  assert(0 <= b);
208  assert(0 <= p);
209  assert(0 <= r);
210  assert(0 <= c);
211  assert(b < mbr.mextent);
212  assert(p < mpr.mextent);
213  assert(r < mrr.mextent);
214  assert(c < mcr.mextent);
215 
216  return get(b, p, r, c);
217  }
218 
220  Numeric get(Index b, Index p, Index r, Index c) const {
221  return *(mdata + mbr.mstart + b * mbr.mstride + mpr.mstart +
222  p * mpr.mstride + mrr.mstart + r * mrr.mstride + mcr.mstart +
223  c * mcr.mstride);
224  }
225 
226  // Functions returning iterators:
227  ConstIterator4D begin() const;
228  ConstIterator4D end() const;
229 
231  virtual ~ConstTensor4View() = default;
232 
233  // Friends:
234  friend class Tensor4View;
235  friend class ConstIterator5D;
236  friend class ConstTensor5View;
237  friend class ConstTensor6View;
238  friend class ConstTensor7View;
239 
240  // Special constructor to make a Tensor4 view of a Tensor3.
242 
243  protected:
244  // Constructors:
245  ConstTensor4View() = default;
247  const Range& b,
248  const Range& p,
249  const Range& r,
250  const Range& c);
252  const Range& pb,
253  const Range& pp,
254  const Range& pr,
255  const Range& pc,
256  const Range& nb,
257  const Range& np,
258  const Range& nr,
259  const Range& nc);
260 
261  // Data members:
262  // -------------
264  Range mbr{0, 0, 1};
266  Range mpr{0, 0, 1};
268  Range mrr{0, 0, 1};
270  Range mcr{0, 0, 1};
272  Numeric* mdata{nullptr};
273 };
274 
285  public:
286  // Make const methods visible from base class
288  using ConstTensor4View::end;
289  using ConstTensor4View::operator();
290  using ConstTensor4View::get;
291 
292  constexpr Tensor4View(const Tensor4View&) = default;
293 
294  // Non-const index operators:
295 
296  Tensor4View operator()(const Range& b,
297  const Range& p,
298  const Range& r,
299  const Range& c);
300 
301  Tensor3View operator()(const Range& b,
302  const Range& p,
303  const Range& r,
304  Index c);
305  Tensor3View operator()(const Range& b,
306  const Range& p,
307  Index r,
308  const Range& c);
309  Tensor3View operator()(const Range& b,
310  Index p,
311  const Range& r,
312  const Range& c);
313  Tensor3View operator()(Index b,
314  const Range& p,
315  const Range& r,
316  const Range& c);
317 
318  MatrixView operator()(const Range& b, const Range& p, Index r, Index c);
319  MatrixView operator()(const Range& b, Index p, const Range& r, Index c);
320  MatrixView operator()(const Range& b, Index p, Index r, const Range& c);
321  MatrixView operator()(Index b, const Range& p, Index r, const Range& c);
322  MatrixView operator()(Index b, const Range& p, const Range& r, Index c);
323  MatrixView operator()(Index b, Index p, const Range& r, const Range& c);
324 
325  VectorView operator()(const Range& b, Index p, Index r, Index c);
326  VectorView operator()(Index b, const Range& p, Index r, Index c);
327  VectorView operator()(Index b, Index p, const Range& r, Index c);
328  VectorView operator()(Index b, Index p, Index r, const Range& c);
329 
332  Index p,
333  Index r,
334  Index c) { // Check if indices are valid:
335  assert(0 <= b);
336  assert(0 <= p);
337  assert(0 <= r);
338  assert(0 <= c);
339  assert(b < mbr.mextent);
340  assert(p < mpr.mextent);
341  assert(r < mrr.mextent);
342  assert(c < mcr.mextent);
343 
344  return get(b, p, r, c);
345  }
346 
348  Numeric& get(Index b, Index p, Index r, Index c) {
349  return *(mdata + mbr.mstart + b * mbr.mstride + mpr.mstart +
350  p * mpr.mstride + mrr.mstart + r * mrr.mstride + mcr.mstart +
351  c * mcr.mstride);
352  }
353 
354  // Conversion to a plain C-array
355  const Numeric* get_c_array() const;
356  Numeric* get_c_array();
357 
358  // Functions returning iterators:
359  Iterator4D begin();
360  Iterator4D end();
361 
362  // Assignment operators:
363  Tensor4View& operator=(const ConstTensor4View& v);
364  Tensor4View& operator=(const Tensor4View& v);
365  Tensor4View& operator=(const Tensor4& v);
366  Tensor4View& operator=(Numeric x);
367 
368  // Other operators:
369  Tensor4View& operator*=(Numeric x);
370  Tensor4View& operator/=(Numeric x);
372  Tensor4View& operator-=(Numeric x);
373 
374  Tensor4View& operator*=(const ConstTensor4View& x);
375  Tensor4View& operator/=(const ConstTensor4View& x);
377  Tensor4View& operator-=(const ConstTensor4View& x);
378 
380  virtual ~Tensor4View() = default;
381 
382  // Friends:
383  // friend class VectorView;
384  // friend ConstTensor4View transpose(ConstTensor4View m);
385  // friend Tensor4View transpose(Tensor4View m);
386  friend class Iterator5D;
387  friend class Tensor5View;
388  friend class Tensor6View;
389  friend class Tensor7View;
390 
391  // Special constructor to make a Tensor4 view of a Tensor3.
392  Tensor4View(const Tensor3View& a);
393 
394  protected:
395  // Constructors:
396  Tensor4View() = default;
398  const Range& b,
399  const Range& p,
400  const Range& r,
401  const Range& c);
403  const Range& pb,
404  const Range& pp,
405  const Range& pr,
406  const Range& pc,
407  const Range& nb,
408  const Range& np,
409  const Range& nr,
410  const Range& nc);
411 };
412 
421 class Tensor4 : public Tensor4View {
422  public:
423  // Constructors:
424  Tensor4() = default;
425  Tensor4(Index b, Index p, Index r, Index c);
426  Tensor4(Index b, Index p, Index r, Index c, Numeric fill);
427  Tensor4(const ConstTensor4View& v);
428  Tensor4(const Tensor4& v);
429  Tensor4(Tensor4&& v) noexcept : Tensor4View(std::forward<Tensor4View>(v)) {
430  v.mdata = nullptr;
431  }
432 
433  // Assignment operators:
434  Tensor4& operator=(const Tensor4& x);
435  Tensor4& operator=(Tensor4&& x) noexcept;
436  Tensor4& operator=(Numeric x);
437 
438  // Resize function:
439  void resize(Index b, Index p, Index r, Index c);
440 
441  // Swap function:
442  friend void swap(Tensor4& t1, Tensor4& t2);
443 
444  // Destructor:
445  virtual ~Tensor4();
446 };
447 
448 // Function declarations:
449 // ----------------------
450 
451 void copy(ConstIterator4D origin,
452  const ConstIterator4D& end,
453  Iterator4D target);
454 
455 void copy(Numeric x, Iterator4D target, const Iterator4D& end);
456 
457 void transform(Tensor4View y, double (&my_func)(double), ConstTensor4View x);
458 
459 Numeric max(const ConstTensor4View& x);
460 
461 Numeric min(const ConstTensor4View& x);
462 
463 std::ostream& operator<<(std::ostream& os, const ConstTensor4View& v);
464 
466 // Helper function for debugging
467 #ifndef NDEBUG
468 
470  Tensor4View& tv, Index b, Index p, Index r, Index c);
471 
472 #endif
473 
475 #endif // matpackIV_h
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
Iterator4D(const Tensor3View &x, Index stride)
Explicit constructor.
Definition: matpackIV.h:45
The VectorView class.
Definition: matpackI.h:610
std::ostream & operator<<(std::ostream &os, const ConstTensor4View &v)
Output operator.
Definition: matpackIV.cc:430
Index mstride
The stride.
Definition: matpackI.h:355
The Tensor4View class.
Definition: matpackIV.h:284
Iterator4D()=default
Default constructor.
Const version of Iterator4D.
Definition: matpackIV.h:77
A constant view of a Tensor7.
Definition: matpackVII.h:147
The Tensor7View class.
Definition: matpackVII.h:1286
Index mstart
The start index.
Definition: matpackI.h:346
void transform(Tensor4View y, double(&my_func)(double), ConstTensor4View x)
A generic transform function for tensors, which can be used to implement mathematical functions opera...
Definition: matpackIV.cc:1125
Tensor4(Tensor4 &&v) noexcept
Definition: matpackIV.h:429
ConstIterator4D(const ConstTensor3View &x, Index stride)
Explicit constructor.
Definition: matpackIV.h:87
Range mcr
The column range of mdata that is actually used.
Definition: matpackIII.h:225
The MatrixView class.
Definition: matpackI.h:1093
Implementation of Tensors of Rank 4.
Definition: matpackIV.h:38
A constant view of a Tensor6.
Definition: matpackVI.h:149
ConstIterator4D begin() const
Return const iterator to first book.
Definition: matpackIV.cc:371
The Tensor4 class.
Definition: matpackIV.h:421
The range class.
Definition: matpackI.h:160
ConstIterator4D & operator++()
Prefix increment operator.
Definition: matpackIV.h:93
The Tensor6View class.
Definition: matpackVI.h:621
Range mpr
The page range of mdata that is actually used.
Definition: matpackIII.h:221
Numeric min(const ConstTensor4View &x)
Min function, tensor version.
Definition: matpackIV.cc:1162
void copy(ConstIterator4D origin, const ConstIterator4D &end, Iterator4D target)
Copy data between begin and end to target.
Definition: matpackIV.cc:928
G0 G2 FVC Y DV Numeric Numeric Numeric Zeeman LowerQuantumNumbers void * data
A constant view of a Tensor4.
Definition: matpackIV.h:133
Tensor3View & operator*()
Dereferencing.
Definition: matpackIV.cc:35
Numeric & operator()(Index b, Index p, Index r, Index c)
Plain non-const index operator.
Definition: matpackIV.h:331
void swap(ComplexVector &v1, ComplexVector &v2)
Swaps two objects.
Definition: complex.cc:731
The Tensor3View class.
Definition: matpackIII.h:239
Range mrr
The row range of mdata that is actually used.
Definition: matpackVII.h:1270
Range mcr
The column range of mdata that is actually used.
Definition: matpackVII.h:1272
Index mstride
Stride.
Definition: matpackIV.h:73
A constant view of a Tensor5.
Definition: matpackV.h:143
bool operator!=(const ConstIterator4D &other) const
Not equal operator, needed for algorithms like copy.
Definition: matpackIV.h:99
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
Numeric operator()(Index b, Index p, Index r, Index c) const
Plain const index operator.
Definition: matpackIV.h:203
Const version of Iterator5D.
Definition: matpackV.h:85
ConstTensor3View msv
Current position.
Definition: matpackIV.h:113
bool operator!=(const Iterator4D &other) const
Not equal operator, needed for algorithms like copy.
Definition: matpackIV.h:57
The Tensor5View class.
Definition: matpackV.h:333
Numeric * mdata
Pointer to the plain C array that holds the data.
Definition: matpackIV.h:272
Implementation of Tensors of Rank 5.
Definition: matpackV.h:38
ConstIterator4D end() const
Return const iterator behind last book.
Definition: matpackIV.cc:377
Numeric * mdata
Pointer to the plain C array that holds the data.
Definition: matpackVII.h:1274
A constant view of a Tensor3.
Definition: matpackIII.h:132
A constant view of a Vector.
Definition: matpackI.h:476
Numeric * mdata
Pointer to the plain C array that holds the data.
Definition: matpackIII.h:227
A constant view of a Matrix.
Definition: matpackI.h:982
Tensor3View * operator->()
The -> operator is needed, so that we can write i->begin() to get the 3D iterators.
Definition: matpackIV.cc:32
Range mrr
The row range of mdata that is actually used.
Definition: matpackIII.h:223
Range mpr
The page range of mdata that is actually used.
Definition: matpackVII.h:1268
constexpr Rational end(Rational Ju, Rational Jl, Polarization type) noexcept
Gives the largest M for a polarization type of this transition.
Definition: zeemandata.h:108
Numeric max(const ConstTensor4View &x)
Max function, tensor version.
Definition: matpackIV.cc:1143
Iterator4D & operator++()
Prefix increment operator.
Definition: matpackIV.h:51
Numeric debug_tensor4view_get_elem(Tensor4View &tv, Index b, Index p, Index r, Index c)
Helper function to access tensor elements.
Definition: matpackIV.cc:1199
Range mbr
The book range of mdata that is actually used.
Definition: matpackVII.h:1266
MatrixView & operator+=(MatrixView &A, const Block &B)
Tensor3View msv
Current position.
Definition: matpackIV.h:71
Numeric get(Index b, Index p, Index r, Index c) const
Get element implementation without assertions.
Definition: matpackIV.h:220