ARTS  2.4.0(git:4fb77825)
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;
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:
151  const Range& p,
152  const Range& r,
153  const Range& c) const;
154 
156  const Range& p,
157  const Range& r,
158  Index c) const;
160  const Range& p,
161  Index r,
162  const Range& c) const;
164  Index p,
165  const Range& r,
166  const Range& c) const;
168  const Range& p,
169  const Range& r,
170  const Range& c) const;
171 
173  const Range& p,
174  Index r,
175  Index c) const;
177  Index p,
178  const Range& r,
179  Index c) const;
181  Index p,
182  Index r,
183  const Range& c) const;
185  const Range& p,
186  Index r,
187  const Range& c) const;
189  const Range& p,
190  const Range& r,
191  Index c) const;
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);
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:
364  Tensor4View& operator=(const Tensor4View& v);
365  Tensor4View& operator=(const Tensor4& v);
367 
368  // Other operators:
373 
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;
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
Tensor7View
The Tensor7View class.
Definition: matpackVII.h:1286
Tensor4::resize
void resize(Index b, Index p, Index r, Index c)
Resize function.
Definition: matpackIV.cc:1064
ConstTensor4View::mpr
Range mpr
The page range of mdata that is actually used.
Definition: matpackIV.h:266
MatrixView
The MatrixView class.
Definition: matpackI.h:1093
ConstTensor4View::end
ConstIterator4D end() const
Return const iterator behind last book.
Definition: matpackIV.cc:377
ConstTensor4View::operator=
ConstTensor4View & operator=(const ConstTensor4View &)=default
ConstIterator4D::mstride
Index mstride
Stride.
Definition: matpackIV.h:115
ConstIterator4D::operator++
ConstIterator4D & operator++()
Prefix increment operator.
Definition: matpackIV.h:93
ConstTensor7View
A constant view of a Tensor7.
Definition: matpackVII.h:147
ConstTensor4View::ConstTensor4View
ConstTensor4View()=default
Iterator4D::operator++
Iterator4D & operator++()
Prefix increment operator.
Definition: matpackIV.h:51
Tensor4View::operator*=
Tensor4View & operator*=(Numeric x)
Multiplication by scalar.
Definition: matpackIV.cc:775
Tensor4View::Tensor4View
Tensor4View()=default
ARTS::Var::y
Vector y(Workspace &ws) noexcept
Definition: autoarts.h:7401
ConstTensor4View::mbr
Range mbr
The book range of mdata that is actually used.
Definition: matpackIV.h:264
ConstTensor4View::get
Numeric get(Index b, Index p, Index r, Index c) const
Get element implementation without assertions.
Definition: matpackIV.h:220
ConstTensor4View::operator()
ConstTensor4View operator()(const Range &b, const Range &p, const Range &r, const Range &c) const
Const index operator for subrange.
Definition: matpackIV.cc:71
ConstTensor4View::mcr
Range mcr
The column range of mdata that is actually used.
Definition: matpackIV.h:270
data
G0 G2 FVC Y DV Numeric Numeric Numeric Zeeman LowerQuantumNumbers void * data
Definition: arts_api_classes.cc:232
Tensor4::~Tensor4
virtual ~Tensor4()
Destructor for Tensor4.
Definition: matpackIV.cc:1104
ConstTensor4View::operator()
Numeric operator()(Index b, Index p, Index r, Index c) const
Plain const index operator.
Definition: matpackIV.h:203
Tensor4::swap
friend void swap(Tensor4 &t1, Tensor4 &t2)
Swaps two objects.
Definition: matpackIV.cc:1094
Tensor4::operator=
Tensor4 & operator=(const Tensor4 &x)
Assignment operator from another tensor.
Definition: matpackIV.cc:1026
Range::mstart
Index mstart
The start index.
Definition: matpackI.h:346
Iterator5D
Implementation of Tensors of Rank 5.
Definition: matpackV.h:38
Tensor4
The Tensor4 class.
Definition: matpackIV.h:421
debug_tensor4view_get_elem
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
Tensor4View::begin
Iterator4D begin()
Return iterator to first book.
Definition: matpackIV.cc:706
Iterator4D::operator->
Tensor3View * operator->()
The -> operator is needed, so that we can write i->begin() to get the 3D iterators.
Definition: matpackIV.cc:32
Iterator4D::Iterator4D
Iterator4D(const Tensor3View &x, Index stride)
Explicit constructor.
Definition: matpackIV.h:45
ConstIterator4D
Const version of Iterator4D.
Definition: matpackIV.h:77
ConstIterator4D::ConstIterator4D
ConstIterator4D()=default
Default constructor.
Tensor4View::~Tensor4View
virtual ~Tensor4View()=default
Destructor.
ConstTensor4View
A constant view of a Tensor4.
Definition: matpackIV.h:133
transform
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
Tensor3View
The Tensor3View class.
Definition: matpackIII.h:239
Tensor4View::operator+=
Tensor4View & operator+=(Numeric x)
Addition of scalar.
Definition: matpackIV.cc:793
ConstIterator4D::operator->
const ConstTensor3View * operator->() const
The -> operator is needed, so that we can write i->begin() to get the 3D iterators.
Definition: matpackIV.cc:39
Tensor4View::Tensor4View
constexpr Tensor4View(const Tensor4View &)=default
Tensor4::Tensor4
Tensor4()=default
ConstTensor3View::mrr
Range mrr
The row range of mdata that is actually used.
Definition: matpackIII.h:223
ConstTensor4View::empty
bool empty() const
Check if variable is empty.
Definition: matpackIV.cc:52
operator<<
std::ostream & operator<<(std::ostream &os, const ConstTensor4View &v)
Output operator.
Definition: matpackIV.cc:430
ConstTensor3View::mcr
Range mcr
The column range of mdata that is actually used.
Definition: matpackIII.h:225
ConstIterator4D::ConstIterator4D
ConstIterator4D(const ConstTensor3View &x, Index stride)
Explicit constructor.
Definition: matpackIV.h:87
VectorView
The VectorView class.
Definition: matpackI.h:610
max
Numeric max(const ConstTensor4View &x)
Max function, tensor version.
Definition: matpackIV.cc:1143
ConstTensor4View::~ConstTensor4View
virtual ~ConstTensor4View()=default
Destructor.
ConstIterator4D::operator!=
bool operator!=(const ConstIterator4D &other) const
Not equal operator, needed for algorithms like copy.
Definition: matpackIV.h:99
ConstTensor4View::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackIV.cc:66
ConstTensor4View::begin
ConstIterator4D begin() const
Return const iterator to first book.
Definition: matpackIV.cc:371
ConstTensor3View::mpr
Range mpr
The page range of mdata that is actually used.
Definition: matpackIII.h:221
ConstTensor6View
A constant view of a Tensor6.
Definition: matpackVI.h:149
Numeric
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
Tensor5View
The Tensor5View class.
Definition: matpackV.h:333
Tensor4View::operator()
Numeric & operator()(Index b, Index p, Index r, Index c)
Plain non-const index operator.
Definition: matpackIV.h:331
ConstTensor3View::mdata
Numeric * mdata
Pointer to the plain C array that holds the data.
Definition: matpackIII.h:227
ConstTensor4View::npages
Index npages() const
Returns the number of pages.
Definition: matpackIV.cc:60
matpackIII.h
ConstTensor4View::nbooks
Index nbooks() const
Returns the number of books.
Definition: matpackIV.cc:57
Tensor4View::operator=
Tensor4View & operator=(const ConstTensor4View &v)
Assignment operator.
Definition: matpackIV.cc:723
Tensor4View::get
Numeric & get(Index b, Index p, Index r, Index c)
Get element implementation without assertions.
Definition: matpackIV.h:348
Iterator4D::msv
Tensor3View msv
Current position.
Definition: matpackIV.h:71
Tensor4View::get
Numeric get(Index b, Index p, Index r, Index c) const
Get element implementation without assertions.
Definition: matpackIV.h:220
Tensor4View::operator/=
Tensor4View & operator/=(Numeric x)
Division by scalar.
Definition: matpackIV.cc:784
Tensor4View::end
Iterator4D end()
Return iterator behind last book.
Definition: matpackIV.cc:712
Zeeman::end
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
ConstMatrixView
A constant view of a Matrix.
Definition: matpackI.h:982
copy
void copy(ConstIterator4D origin, const ConstIterator4D &end, Iterator4D target)
Copy data between begin and end to target.
Definition: matpackIV.cc:928
ConstTensor4View::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackIV.cc:63
ConstIterator4D::operator*
const ConstTensor3View & operator*() const
Dereferencing.
Definition: matpackIV.cc:42
Range
The range class.
Definition: matpackI.h:160
min
Numeric min(const ConstTensor4View &x)
Min function, tensor version.
Definition: matpackIV.cc:1162
Range::mextent
Index mextent
The number of elements.
Definition: matpackI.h:353
Iterator4D::Iterator4D
Iterator4D()=default
Default constructor.
Iterator4D::operator*
Tensor3View & operator*()
Dereferencing.
Definition: matpackIV.cc:35
Iterator4D
Implementation of Tensors of Rank 4.
Definition: matpackIV.h:38
ConstTensor4View::mrr
Range mrr
The row range of mdata that is actually used.
Definition: matpackIV.h:268
ConstTensor3View
A constant view of a Tensor3.
Definition: matpackIII.h:132
Tensor4View
The Tensor4View class.
Definition: matpackIV.h:284
ConstIterator4D::msv
ConstTensor3View msv
Current position.
Definition: matpackIV.h:113
Iterator4D::mstride
Index mstride
Stride.
Definition: matpackIV.h:73
Tensor4::Tensor4
Tensor4(Tensor4 &&v) noexcept
Definition: matpackIV.h:429
Tensor4View::operator-=
Tensor4View & operator-=(Numeric x)
Subtraction of scalar.
Definition: matpackIV.cc:802
ConstTensor4View::operator=
ConstTensor4View & operator=(ConstTensor4View &&)=default
ARTS::Var::x
Vector x(Workspace &ws) noexcept
Definition: autoarts.h:7346
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
Tensor4View::operator()
Tensor4View operator()(const Range &b, const Range &p, const Range &r, const Range &c)
Index operator for subrange.
Definition: matpackIV.cc:454
ConstIterator5D
Const version of Iterator5D.
Definition: matpackV.h:85
ConstTensor4View::ConstTensor4View
constexpr ConstTensor4View(ConstTensor4View &&)=default
Iterator4D::operator!=
bool operator!=(const Iterator4D &other) const
Not equal operator, needed for algorithms like copy.
Definition: matpackIV.h:57
ConstTensor4View::ConstTensor4View
constexpr ConstTensor4View(const ConstTensor4View &)=default
Range::mstride
Index mstride
The stride.
Definition: matpackI.h:355
ConstVectorView
A constant view of a Vector.
Definition: matpackI.h:476
Tensor4View::get_c_array
const Numeric * get_c_array() const
Conversion to plain C-array.
Definition: matpackIV.cc:358
ConstTensor4View::mdata
Numeric * mdata
Pointer to the plain C array that holds the data.
Definition: matpackIV.h:272
ConstTensor5View
A constant view of a Tensor5.
Definition: matpackV.h:143
Tensor6View
The Tensor6View class.
Definition: matpackVI.h:621