ARTS  2.3.1285(git:92a29ea9-dirty)
matpackIII.h
Go to the documentation of this file.
1 /* Copyright (C) 2001-2012 Stefan Buehler <sbuehler@ltu.se>
2 
3  This program is free software; you can redistribute it and/or modify it
4  under the terms of the GNU General Public License as published by the
5  Free Software Foundation; either version 2, or (at your option) any
6  later version.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16  USA. */
17 
27 #ifndef matpackIII_h
28 #define matpackIII_h
29 
30 #include "matpackI.h"
31 
34 class Iterator3D {
35  public:
36  // Constructors:
38  Iterator3D() = default;
39 
41  Iterator3D(const MatrixView& x, Index stride)
42  : msv(x), mstride(stride) { /* Nothing to do here. */
43  }
44 
45  // Operators:
48  msv.mdata += mstride;
49  return *this;
50  }
51 
53  bool operator!=(const Iterator3D& other) const {
54  if (msv.mdata + msv.mrr.mstart + msv.mcr.mstart !=
55  other.msv.mdata + other.msv.mrr.mstart + other.msv.mcr.mstart)
56  return true;
57  else
58  return false;
59  }
60 
63  MatrixView* operator->() { return &msv; }
64 
66  MatrixView& operator*() { return msv; }
67 
68  private:
73 };
74 
77  public:
78  // Constructors:
80  ConstIterator3D() = default;
81 
84  : msv(x), mstride(stride) { /* Nothing to do here. */
85  }
86 
87  // Operators:
90  msv.mdata += mstride;
91  return *this;
92  }
93 
95  bool operator!=(const ConstIterator3D& other) const {
96  if (msv.mdata + msv.mrr.mstart + msv.mcr.mstart !=
97  other.msv.mdata + other.msv.mrr.mstart + other.msv.mcr.mstart)
98  return true;
99  else
100  return false;
101  }
102 
105  const ConstMatrixView* operator->() const { return &msv; }
106 
108  const ConstMatrixView& operator*() const { return msv; }
109 
110  private:
115 };
116 
117 // Declare class Tensor3:
118 class Tensor3;
119 
133  public:
134  constexpr ConstTensor3View(const ConstTensor3View&) = default;
135  constexpr ConstTensor3View(ConstTensor3View&&) = default;
136  ConstTensor3View& operator=(const ConstTensor3View&) = default;
137  ConstTensor3View& operator=(ConstTensor3View&&) = default;
138 
139  // Member functions:
140 
141  bool empty() const;
142 
144  Index npages() const { return mpr.mextent; }
145 
147  Index nrows() const { return mrr.mextent; }
148 
150  Index ncols() const { return mcr.mextent; }
151 
152  // Const index operators:
153  ConstTensor3View operator()(const Range& p,
154  const Range& r,
155  const Range& c) const;
156 
157  ConstMatrixView operator()(const Range& p, const Range& r, Index c) const;
158  ConstMatrixView operator()(const Range& p, Index r, const Range& c) const;
159  ConstMatrixView operator()(Index p, const Range& r, const Range& c) const;
160 
161  ConstVectorView operator()(Index p, Index r, const Range& c) const;
162  ConstVectorView operator()(Index p, const Range& r, Index c) const;
163  ConstVectorView operator()(const Range& p, Index r, Index c) const;
164 
167  Index r,
168  Index c) const { // Check if indices are valid:
169  assert(0 <= p);
170  assert(0 <= r);
171  assert(0 <= c);
172  assert(p < mpr.mextent);
173  assert(r < mrr.mextent);
174  assert(c < mcr.mextent);
175 
176  return get(p, r, c);
177  }
178 
180  Numeric get(Index p, Index r, Index c) const {
181  return *(mdata + mpr.mstart + p * mpr.mstride + mrr.mstart +
182  r * mrr.mstride + mcr.mstart + c * mcr.mstride);
183  }
184 
185  // Functions returning iterators:
186  ConstIterator3D begin() const;
187  ConstIterator3D end() const;
188 
190  virtual ~ConstTensor3View() = default;
191 
192  // Friends:
193  friend class Tensor3View;
194  friend class ConstIterator4D;
195  friend class ConstTensor4View;
196  friend class ConstTensor5View;
197  friend class ConstTensor6View;
198  friend class ConstTensor7View;
199 
200  // Special constructor to make a Tensor3 view of a matrix.
202 
203  protected:
204  // Constructors:
205  ConstTensor3View() = default;
207  const Range& p,
208  const Range& r,
209  const Range& c);
211  const Range& pp,
212  const Range& pr,
213  const Range& pc,
214  const Range& np,
215  const Range& nr,
216  const Range& nc);
217 
218  // Data members:
219  // -------------
221  Range mpr{0, 0, 1};
223  Range mrr{0, 0, 1};
225  Range mcr{0, 0, 1};
227  Numeric* mdata{nullptr};
228 };
229 
240  public:
241  // Make const methods visible from base class
243  using ConstTensor3View::end;
244  using ConstTensor3View::operator();
245  using ConstTensor3View::get;
246 
247  constexpr Tensor3View(const Tensor3View&) = default;
248 
249  // Non-const index operators:
250 
251  Tensor3View operator()(const Range& p, const Range& r, const Range& c);
252 
253  MatrixView operator()(const Range& p, const Range& r, Index c);
254  MatrixView operator()(const Range& p, Index r, const Range& c);
255  MatrixView operator()(Index p, const Range& r, const Range& c);
256 
257  VectorView operator()(Index p, Index r, const Range& c);
258  VectorView operator()(Index p, const Range& r, Index c);
259  VectorView operator()(const Range& p, Index r, Index c);
260 
263  // Check if indices are valid:
264  assert(0 <= p);
265  assert(0 <= r);
266  assert(0 <= c);
267  assert(p < mpr.mextent);
268  assert(r < mrr.mextent);
269  assert(c < mcr.mextent);
270 
271  return get(p, r, c);
272  }
273 
275  Numeric& get(Index p, Index r, Index c) {
276  return *(mdata + mpr.mstart + p * mpr.mstride + mrr.mstart +
277  r * mrr.mstride + mcr.mstart + c * mcr.mstride);
278  }
279 
280  // Conversion to a plain C-array
281  const Numeric* get_c_array() const;
282  Numeric* get_c_array();
283 
284  // Functions returning iterators:
285  Iterator3D begin();
286  Iterator3D end();
287 
288  // Assignment operators:
289  Tensor3View& operator=(const ConstTensor3View& v);
290  Tensor3View& operator=(const Tensor3View& v);
291  Tensor3View& operator=(const Tensor3& v);
292  Tensor3View& operator=(Numeric x);
293 
294  // Other operators:
295  Tensor3View& operator*=(Numeric x);
296  Tensor3View& operator/=(Numeric x);
298  Tensor3View& operator-=(Numeric x);
299 
300  Tensor3View& operator*=(const ConstTensor3View& x);
301  Tensor3View& operator/=(const ConstTensor3View& x);
303  Tensor3View& operator-=(const ConstTensor3View& x);
304 
306  virtual ~Tensor3View() = default;
307 
308  // Friends:
309  friend class Iterator4D;
310  friend class Tensor4View;
311  friend class Tensor5View;
312  friend class Tensor6View;
313  friend class Tensor7View;
314 
315  // Special constructor to make a Tensor3 view of a matrix.
316  Tensor3View(const MatrixView& a);
317 
318  protected:
319  // Constructors:
320  Tensor3View() = default;
321  Tensor3View(Numeric* data, const Range& p, const Range& r, const Range& c);
323  const Range& pp,
324  const Range& pr,
325  const Range& pc,
326  const Range& np,
327  const Range& nr,
328  const Range& nc);
329 };
330 
339 class Tensor3 : public Tensor3View {
340  public:
341  // Constructors:
342  Tensor3() = default;
343  Tensor3(Index p, Index r, Index c);
344  Tensor3(Index p, Index r, Index c, Numeric fill);
345  Tensor3(const ConstTensor3View& v);
346  Tensor3(const Tensor3& v);
347  Tensor3(Tensor3&& v) noexcept : Tensor3View(std::forward<Tensor3View>(v)) {
348  v.mdata = nullptr;
349  }
350 
351  // Assignment operators:
352  Tensor3& operator=(const Tensor3& x);
353  Tensor3& operator=(Tensor3&& x) noexcept;
354  Tensor3& operator=(Numeric x);
355 
356  // Resize function:
357  void resize(Index p, Index r, Index c);
358 
359  // Swap function:
360  friend void swap(Tensor3& t1, Tensor3& t2);
361 
362  // Destructor:
363  virtual ~Tensor3();
364 };
365 
366 // Function declarations:
367 // ----------------------
368 
369 void copy(ConstIterator3D origin,
370  const ConstIterator3D& end,
371  Iterator3D target);
372 
373 void copy(Numeric x, Iterator3D target, const Iterator3D& end);
374 
375 void transform(Tensor3View y, double (&my_func)(double), ConstTensor3View x);
376 
377 Numeric max(const ConstTensor3View& x);
378 
379 Numeric min(const ConstTensor3View& x);
380 
381 std::ostream& operator<<(std::ostream& os, const ConstTensor3View& v);
382 
384 // Helper function for debugging
385 #ifndef NDEBUG
386 
388 
389 #endif
390 
392 void mult(Tensor3View A, const ConstVectorView B, const ConstMatrixView C);
393 
394 #endif // matpackIII_h
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
const ConstMatrixView * operator->() const
The -> operator is needed, so that we can write i->begin() to get the 1D iterators.
Definition: matpackIII.h:105
The VectorView class.
Definition: matpackI.h:610
The Tensor4View class.
Definition: matpackIV.h:284
ConstMatrixView msv
Current position.
Definition: matpackIII.h:112
Numeric & operator()(Index p, Index r, Index c)
Plain non-const index operator.
Definition: matpackIII.h:262
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
MatrixView & operator*()
Dereferencing.
Definition: matpackIII.h:66
Numeric * mdata
Pointer to the plain C array that holds the data.
Definition: matpackI.h:1081
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
Numeric min(const ConstTensor3View &x)
Min function, tensor version.
Definition: matpackIII.cc:754
Iterator3D()=default
Default constructor.
The range class.
Definition: matpackI.h:160
Numeric debug_tensor3view_get_elem(Tensor3View &tv, Index p, Index r, Index c)
Helper function to access tensor elements.
Definition: matpackIII.cc:790
MatrixView * operator->()
The -> operator is needed, so that we can write i->begin() to get the 1D iterators.
Definition: matpackIII.h:63
Numeric max(const ConstTensor3View &x)
Max function, tensor version.
Definition: matpackIII.cc:735
The Tensor6View class.
Definition: matpackVI.h:621
Numeric operator()(Index p, Index r, Index c) const
Plain const index operator.
Definition: matpackIII.h:166
Index nrows() const
Returns the number of rows.
Definition: matpackIII.h:147
G0 G2 FVC Y DV Numeric Numeric Numeric Zeeman LowerQuantumNumbers void * data
A constant view of a Tensor4.
Definition: matpackIV.h:133
void copy(ConstIterator3D origin, const ConstIterator3D &end, Iterator3D target)
Copy data between begin and end to target.
Definition: matpackIII.cc:538
Iterator3D & operator++()
Prefix increment operator.
Definition: matpackIII.h:47
The Tensor3 class.
Definition: matpackIII.h:339
void swap(ComplexVector &v1, ComplexVector &v2)
Swaps two objects.
Definition: complex.cc:731
ConstIterator3D end() const
Return const iterator behind last page.
Definition: matpackIII.cc:145
Range mrr
The row range of mdata that is actually used.
Definition: matpackI.h:1077
Index ncols() const
Returns the number of columns.
Definition: matpackIII.h:150
MatrixView msv
Current position.
Definition: matpackIII.h:70
Index mstride
Stride.
Definition: matpackIII.h:72
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
bool operator!=(const ConstIterator3D &other) const
Not equal operator, needed for algorithms like copy.
Definition: matpackIII.h:95
void transform(Tensor3View y, double(&my_func)(double), ConstTensor3View x)
A generic transform function for tensors, which can be used to implement mathematical functions opera...
Definition: matpackIII.cc:718
A constant view of a Tensor5.
Definition: matpackV.h:143
const ConstMatrixView & operator*() const
Dereferencing.
Definition: matpackIII.h:108
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
Range mcr
The column range of mdata that is actually used.
Definition: matpackI.h:1079
Implementation of Matrix, Vector, and such stuff.
The Tensor5View class.
Definition: matpackV.h:333
Index npages() const
Returns the number of pages.
Definition: matpackIII.h:144
std::ostream & operator<<(std::ostream &os, const ConstTensor3View &v)
Output operator.
Definition: matpackIII.cc:194
Numeric * mdata
Pointer to the plain C array that holds the data.
Definition: matpackVII.h:1274
bool operator!=(const Iterator3D &other) const
Not equal operator, needed for algorithms like copy.
Definition: matpackIII.h:53
ConstIterator3D begin() const
Return const iterator to first page.
Definition: matpackIII.cc:139
A constant view of a Tensor3.
Definition: matpackIII.h:132
A constant view of a Vector.
Definition: matpackI.h:476
ConstIterator3D(const ConstMatrixView &x, Index stride)
Explicit constructor.
Definition: matpackIII.h:83
void mult(Tensor3View A, const ConstVectorView B, const ConstMatrixView C)
mult Tensor3
Definition: matpackIII.cc:813
Iterator3D(const MatrixView &x, Index stride)
Explicit constructor.
Definition: matpackIII.h:41
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
Implementation of Tensors of Rank 3.
Definition: matpackIII.h:34
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
ConstIterator3D & operator++()
Prefix increment operator.
Definition: matpackIII.h:89
Const version of Iterator3D.
Definition: matpackIII.h:76
MatrixView & operator+=(MatrixView &A, const Block &B)
Numeric get(Index p, Index r, Index c) const
Get element implementation without assertions.
Definition: matpackIII.h:180
Tensor3(Tensor3 &&v) noexcept
Definition: matpackIII.h:347