ARTS 2.5.0 (git: 9ee3ac6c)
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
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 {
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:
119class 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 ARTS_ASSERT(0 <= b);
208 ARTS_ASSERT(0 <= p);
209 ARTS_ASSERT(0 <= r);
210 ARTS_ASSERT(0 <= c);
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
289 using ConstTensor4View::operator();
291
292 constexpr Tensor4View(const Tensor4View&) = default;
293
294 // Non-const index operators:
295
297 const Range& p,
298 const Range& r,
299 const Range& c);
300
302 const Range& p,
303 const Range& r,
304 Index c);
306 const Range& p,
307 Index r,
308 const Range& c);
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 ARTS_ASSERT(0 <= b);
336 ARTS_ASSERT(0 <= p);
337 ARTS_ASSERT(0 <= r);
338 ARTS_ASSERT(0 <= c);
343
344 return get(b, p, r, c);
345 }
346
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 ARTS_NOEXCEPT;
357
358 // Functions returning iterators:
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);
371 Tensor4View& operator+=(Numeric x);
372 Tensor4View& operator-=(Numeric x);
373
374 Tensor4View& operator*=(const ConstTensor4View& x);
375 Tensor4View& operator/=(const ConstTensor4View& x);
376 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
421class 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
444 Tensor4(Numeric* d, const Range& r0, const Range& r1, const Range& r2, const Range& r3) ARTS_NOEXCEPT
445 : Tensor4View(d, r0, r1, r2, r3) {
446 ARTS_ASSERT (not (r0.get_extent() < 0), "Must have size");
447 ARTS_ASSERT (not (r1.get_extent() < 0), "Must have size");
448 ARTS_ASSERT (not (r2.get_extent() < 0), "Must have size");
449 ARTS_ASSERT (not (r3.get_extent() < 0), "Must have size");
450 }
451
452 // Assignment operators:
453 Tensor4& operator=(const Tensor4& x);
454 Tensor4& operator=(Tensor4&& x) noexcept;
456
457 // Resize function:
458 void resize(Index b, Index p, Index r, Index c);
459
460 // Swap function:
461 friend void swap(Tensor4& t1, Tensor4& t2);
462
463 // Destructor:
464 virtual ~Tensor4();
465
466 // Total size
467 Index size() const noexcept {return nbooks() * npages() * nrows() * ncols();}
468
470 template <std::size_t dim0>
472 static_assert(dim0 < 4, "Bad Dimension, Out-of-Bounds");
473
474 Range r0(0, dim0 == 0 ? nbooks() : dim0 == 1 ? npages() : dim0 == 2 ? nrows() : ncols());
475
476 Vector out(mdata, r0);
477 ARTS_ASSERT (size() == out.size(), "Can only reduce size on same size input");
478 mdata = nullptr;
479 return out;
480 }
481
483 template <std::size_t dim0, std::size_t dim1>
485 static_assert(dim1 < 4, "Bad Dimension, Out-of-Bounds");
486 static_assert(dim0 < dim1, "Bad Dimensions, dim1 must be larger than dim0");
487
488 const Range r1(0, dim1 == 1 ? npages() : dim1 == 2 ? nrows() : ncols());
489 const Range r0(0, dim0 == 0 ? nbooks() : dim0 == 1 ? npages() : nrows(), r1.get_extent());
490
491 Matrix out(mdata, r0, r1);
492 ARTS_ASSERT (size() == out.size(), "Can only reduce size on same size input");
493 mdata = nullptr;
494 return out;
495 }
496
498 template <std::size_t dim0, std::size_t dim1, std::size_t dim2>
500 static_assert(dim2 < 4, "Bad Dimension, Out-of-Bounds");
501 static_assert(dim0 < dim1, "Bad Dimensions, dim1 must be larger than dim0");
502 static_assert(dim1 < dim2, "Bad Dimensions, dim2 must be larger than dim1");
503
504 const Range r2(0, dim2 == 2 ? nrows() : ncols());
505 const Range r1(0, dim1 == 1 ? npages() : nrows(), r2.get_extent());
506 const Range r0(0, dim0 == 0 ? nbooks() : npages(), r1.get_extent() * r2.get_extent());
507
508 Tensor3 out(mdata, r0, r1, r2);
509 ARTS_ASSERT (size() == out.size(), "Can only reduce size on same size input");
510 mdata = nullptr;
511 return out;
512 }
513};
514
515// Function declarations:
516// ----------------------
517
518void copy(ConstIterator4D origin,
519 const ConstIterator4D& end,
520 Iterator4D target);
521
522void copy(Numeric x, Iterator4D target, const Iterator4D& end);
523
524void transform(Tensor4View y, double (&my_func)(double), ConstTensor4View x);
525
527
529
530std::ostream& operator<<(std::ostream& os, const ConstTensor4View& v);
531
533// Helper function for debugging
534#ifndef NDEBUG
535
537 Tensor4View& tv, Index b, Index p, Index r, Index c);
538
539#endif
541
542#endif // matpackIV_h
void * data
Const version of Iterator4D.
Definition: matpackIV.h:77
Index mstride
Stride.
Definition: matpackIV.h:115
ConstIterator4D & operator++()
Prefix increment operator.
Definition: matpackIV.h:93
const ConstTensor3View & operator*() const
Dereferencing.
Definition: matpackIV.cc:40
const ConstTensor3View * operator->() const
The -> operator is needed, so that we can write i->begin() to get the 3D iterators.
Definition: matpackIV.cc:37
ConstTensor3View msv
Current position.
Definition: matpackIV.h:113
ConstIterator4D()=default
Default constructor.
ConstIterator4D(const ConstTensor3View &x, Index stride)
Explicit constructor.
Definition: matpackIV.h:87
bool operator!=(const ConstIterator4D &other) const
Not equal operator, needed for algorithms like copy.
Definition: matpackIV.h:99
Const version of Iterator5D.
Definition: matpackV.h:85
A constant view of a Matrix.
Definition: matpackI.h:1014
A constant view of a Tensor3.
Definition: matpackIII.h:132
Numeric * mdata
Pointer to the plain C array that holds the data.
Definition: matpackIII.h:227
Range mrr
The row range of mdata that is actually used.
Definition: matpackIII.h:223
Range mcr
The column range of mdata that is actually used.
Definition: matpackIII.h:225
Range mpr
The page range of mdata that is actually used.
Definition: matpackIII.h:221
A constant view of a Tensor4.
Definition: matpackIV.h:133
Range mrr
The row range of mdata that is actually used.
Definition: matpackIV.h:268
ConstTensor4View()=default
ConstTensor4View & operator=(const ConstTensor4View &)=default
Range mbr
The book range of mdata that is actually used.
Definition: matpackIV.h:264
ConstTensor4View operator()(const Range &b, const Range &p, const Range &r, const Range &c) const
Const index operator for subrange.
Definition: matpackIV.cc:69
virtual ~ConstTensor4View()=default
Destructor.
Index npages() const
Returns the number of pages.
Definition: matpackIV.cc:58
constexpr ConstTensor4View(const ConstTensor4View &)=default
Range mcr
The column range of mdata that is actually used.
Definition: matpackIV.h:270
Numeric * mdata
Pointer to the plain C array that holds the data.
Definition: matpackIV.h:272
Index nbooks() const
Returns the number of books.
Definition: matpackIV.cc:55
Index ncols() const
Returns the number of columns.
Definition: matpackIV.cc:64
constexpr ConstTensor4View(ConstTensor4View &&)=default
Range mpr
The page range of mdata that is actually used.
Definition: matpackIV.h:266
ConstTensor4View & operator=(ConstTensor4View &&)=default
Numeric get(Index b, Index p, Index r, Index c) const
Get element implementation without assertions.
Definition: matpackIV.h:220
ConstIterator4D end() const
Return const iterator behind last book.
Definition: matpackIV.cc:371
ConstIterator4D begin() const
Return const iterator to first book.
Definition: matpackIV.cc:365
bool empty() const
Check if variable is empty.
Definition: matpackIV.cc:50
Index nrows() const
Returns the number of rows.
Definition: matpackIV.cc:61
Numeric operator()(Index b, Index p, Index r, Index c) const
Plain const index operator.
Definition: matpackIV.h:203
A constant view of a Tensor5.
Definition: matpackV.h:143
A constant view of a Tensor6.
Definition: matpackVI.h:149
A constant view of a Tensor7.
Definition: matpackVII.h:147
A constant view of a Vector.
Definition: matpackI.h:489
Index size() const ARTS_NOEXCEPT
Definition: matpackI.cc:50
Implementation of Tensors of Rank 4.
Definition: matpackIV.h:38
Iterator4D & operator++()
Prefix increment operator.
Definition: matpackIV.h:51
Index mstride
Stride.
Definition: matpackIV.h:73
Tensor3View * operator->()
The -> operator is needed, so that we can write i->begin() to get the 3D iterators.
Definition: matpackIV.cc:30
Iterator4D()=default
Default constructor.
bool operator!=(const Iterator4D &other) const
Not equal operator, needed for algorithms like copy.
Definition: matpackIV.h:57
Tensor3View & operator*()
Dereferencing.
Definition: matpackIV.cc:33
Tensor3View msv
Current position.
Definition: matpackIV.h:71
Iterator4D(const Tensor3View &x, Index stride)
Explicit constructor.
Definition: matpackIV.h:45
Implementation of Tensors of Rank 5.
Definition: matpackV.h:38
The MatrixView class.
Definition: matpackI.h:1125
The Matrix class.
Definition: matpackI.h:1225
Index size() const noexcept
Definition: matpackI.h:1268
The range class.
Definition: matpackI.h:165
Index mstart
The start index.
Definition: matpackI.h:351
Index mstride
The stride.
Definition: matpackI.h:360
constexpr Index get_extent() const ARTS_NOEXCEPT
Returns the extent of the range.
Definition: matpackI.h:334
Index mextent
The number of elements.
Definition: matpackI.h:358
The Tensor3View class.
Definition: matpackIII.h:239
The Tensor3 class.
Definition: matpackIII.h:339
Index size() const noexcept
Definition: matpackIII.h:383
The Tensor4View class.
Definition: matpackIV.h:284
constexpr Tensor4View(const Tensor4View &)=default
Iterator4D begin()
Return iterator to first book.
Definition: matpackIV.cc:700
const Numeric * get_c_array() const ARTS_NOEXCEPT
Conversion to plain C-array.
Definition: matpackIV.cc:354
Numeric & get(Index b, Index p, Index r, Index c)
Get element implementation without assertions.
Definition: matpackIV.h:348
Tensor4View operator()(const Range &b, const Range &p, const Range &r, const Range &c)
Index operator for subrange.
Definition: matpackIV.cc:448
Tensor4View & operator=(const ConstTensor4View &v)
Assignment operator.
Definition: matpackIV.cc:717
Iterator4D end()
Return iterator behind last book.
Definition: matpackIV.cc:706
Tensor4View()=default
Numeric & operator()(Index b, Index p, Index r, Index c)
Plain non-const index operator.
Definition: matpackIV.h:331
The Tensor4 class.
Definition: matpackIV.h:421
Matrix reduce_rank() &&ARTS_NOEXCEPT
Definition: matpackIV.h:484
Tensor4(Tensor4 &&v) noexcept
Definition: matpackIV.h:429
Tensor4(Numeric *d, const Range &r0, const Range &r1, const Range &r2, const Range &r3) ARTS_NOEXCEPT
Definition: matpackIV.h:444
Tensor3 reduce_rank() &&ARTS_NOEXCEPT
Definition: matpackIV.h:499
Vector reduce_rank() &&ARTS_NOEXCEPT
Definition: matpackIV.h:471
Tensor4()=default
Index size() const noexcept
Definition: matpackIV.h:467
The Tensor5View class.
Definition: matpackV.h:333
The Tensor6View class.
Definition: matpackVI.h:621
The Tensor7View class.
Definition: matpackVII.h:1286
The VectorView class.
Definition: matpackI.h:626
The Vector class.
Definition: matpackI.h:876
#define ARTS_NOEXCEPT
Definition: debug.h:80
#define ARTS_ASSERT(condition,...)
Definition: debug.h:83
void copy(ConstIterator4D origin, const ConstIterator4D &end, Iterator4D target)
Copy data between begin and end to target.
Definition: matpackIV.cc:922
Numeric debug_tensor4view_get_elem(Tensor4View &tv, Index b, Index p, Index r, Index c)
Helper function to access tensor elements.
Definition: matpackIV.cc:1193
Numeric max(const ConstTensor4View &x)
Max function, tensor version.
Definition: matpackIV.cc:1137
Numeric min(const ConstTensor4View &x)
Min function, tensor version.
Definition: matpackIV.cc:1156
std::ostream & operator<<(std::ostream &os, const ConstTensor4View &v)
Output operator.
Definition: matpackIV.cc:424
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:1119
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
void swap(ComplexVector &v1, ComplexVector &v2)
Swaps two objects.
constexpr Numeric r0
The reference radius in IGRF13.
Definition: igrf13.cc:203
#define d
#define v
#define a
#define c
#define b