ARTS 2.5.4 (git: 31ce4f0e)
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 return false;
63 }
64
67
68 private:
73};
74
77 public:
78 // Constructors:
79 // Functions for ConstIterator4D
80 // -----------------------------
81
83 ConstIterator4D() = default;
84
87 : msv(x), mstride(stride) { /* Nothing to do here. */
88 }
89
90 // Operators:
93 msv.mdata += mstride;
94 return *this;
95 }
96
98 bool operator!=(const ConstIterator4D& other) const {
100 other.msv.mdata + other.msv.mpr.mstart + other.msv.mrr.mstart +
101 other.msv.mcr.mstart)
102 return true;
103 return false;
104 }
105
106 const ConstTensor3View* operator->() const;
107 const ConstTensor3View& operator*() const;
108
109 private:
114};
115
116// Declare class Tensor4:
117class Tensor4;
118
132 public:
133 constexpr ConstTensor4View(const ConstTensor4View&) = default;
134 constexpr ConstTensor4View(ConstTensor4View&&) = default;
137
138 // Member functions:
139 [[nodiscard]] Index nbooks() const noexcept { return mbr.mextent; }
140 [[nodiscard]] Index npages() const noexcept { return mpr.mextent; }
141 [[nodiscard]] Index nrows() const noexcept { return mrr.mextent; }
142 [[nodiscard]] Index ncols() const noexcept { return mcr.mextent; }
143
144 // Total size
145 [[nodiscard]] Index size() const noexcept {
146 return nbooks() * npages() * nrows() * ncols();
147 }
148 [[nodiscard]] bool empty() const noexcept { return size() == 0; }
149
151 [[nodiscard]] Shape<4> shape() const {
152 return {nbooks(), npages(), nrows(), ncols()};
153 }
154
155 // Const index operators:
157 const Range& p,
158 const Range& r,
159 const Range& c) const;
160
162 const Range& p,
163 const Range& r,
164 Index c) const;
166 const Range& p,
167 Index r,
168 const Range& c) const;
170 Index p,
171 const Range& r,
172 const Range& c) const;
174 const Range& p,
175 const Range& r,
176 const Range& c) const;
177
179 const Range& p,
180 Index r,
181 Index c) const;
183 Index p,
184 const Range& r,
185 Index c) const;
187 Index p,
188 Index r,
189 const Range& c) const;
191 const Range& p,
192 Index r,
193 const Range& c) const;
195 const Range& p,
196 const Range& r,
197 Index c) const;
199 Index p,
200 const Range& r,
201 const Range& c) const;
202
203 ConstVectorView operator()(const Range& b, Index p, Index r, Index c) const;
204 ConstVectorView operator()(Index b, const Range& p, Index r, Index c) const;
205 ConstVectorView operator()(Index b, Index p, const Range& r, Index c) const;
206 ConstVectorView operator()(Index b, Index p, Index r, const Range& c) const;
207
210 Index p,
211 Index r,
212 Index c) const { // Check if indices are valid:
213 ARTS_ASSERT(0 <= b);
214 ARTS_ASSERT(0 <= p);
215 ARTS_ASSERT(0 <= r);
216 ARTS_ASSERT(0 <= c);
221
222 return get(b, p, r, c);
223 }
224
226 [[nodiscard]] Numeric get(Index b, Index p, Index r, Index c) const {
227 return *(mdata + mbr.mstart + b * mbr.mstride + mpr.mstart +
228 p * mpr.mstride + mrr.mstart + r * mrr.mstride + mcr.mstart +
229 c * mcr.mstride);
230 }
231
232 // Functions returning iterators:
233 [[nodiscard]] ConstIterator4D begin() const;
234 [[nodiscard]] ConstIterator4D end() const;
235
237 virtual ~ConstTensor4View() = default;
238
239 // Friends:
240 friend class Tensor4View;
241 friend class ConstIterator5D;
242 friend class ConstTensor5View;
243 friend class ConstTensor6View;
244 friend class ConstTensor7View;
245
246 friend std::ostream& operator<<(std::ostream& os, const ConstTensor4View& v);
247
248 // Special constructor to make a Tensor4 view of a Tensor3.
250
251 protected:
252 // Constructors:
253 ConstTensor4View() = default;
255 const Range& b,
256 const Range& p,
257 const Range& r,
258 const Range& c);
260 const Range& pb,
261 const Range& pp,
262 const Range& pr,
263 const Range& pc,
264 const Range& nb,
265 const Range& np,
266 const Range& nr,
267 const Range& nc);
268
269 // Data members:
270 // -------------
272 Range mbr{0, 0, 1};
274 Range mpr{0, 0, 1};
276 Range mrr{0, 0, 1};
278 Range mcr{0, 0, 1};
280 Numeric* mdata{nullptr};
281};
282
293 public:
294 // Make const methods visible from base class
297 using ConstTensor4View::operator();
299
300 constexpr Tensor4View(const Tensor4View&) = default;
301
302 // Non-const index operators:
303
305 const Range& p,
306 const Range& r,
307 const Range& c);
308
310 const Range& p,
311 const Range& r,
312 Index c);
314 const Range& p,
315 Index r,
316 const Range& c);
318 Index p,
319 const Range& r,
320 const Range& c);
322 const Range& p,
323 const Range& r,
324 const Range& c);
325
326 MatrixView operator()(const Range& b, const Range& p, Index r, Index c);
327 MatrixView operator()(const Range& b, Index p, const Range& r, Index c);
328 MatrixView operator()(const Range& b, Index p, Index r, const Range& c);
329 MatrixView operator()(Index b, const Range& p, Index r, const Range& c);
330 MatrixView operator()(Index b, const Range& p, const Range& r, Index c);
331 MatrixView operator()(Index b, Index p, const Range& r, const Range& c);
332
333 VectorView operator()(const Range& b, Index p, Index r, Index c);
334 VectorView operator()(Index b, const Range& p, Index r, Index c);
335 VectorView operator()(Index b, Index p, const Range& r, Index c);
336 VectorView operator()(Index b, Index p, Index r, const Range& c);
337
340 Index p,
341 Index r,
342 Index c) { // Check if indices are valid:
343 ARTS_ASSERT(0 <= b);
344 ARTS_ASSERT(0 <= p);
345 ARTS_ASSERT(0 <= r);
346 ARTS_ASSERT(0 <= c);
351
352 return get(b, p, r, c);
353 }
354
357 return *(mdata + mbr.mstart + b * mbr.mstride + mpr.mstart +
358 p * mpr.mstride + mrr.mstart + r * mrr.mstride + mcr.mstart +
359 c * mcr.mstride);
360 }
361
362 // Conversion to a plain C-array
363 [[nodiscard]] const Numeric* get_c_array() const ARTS_NOEXCEPT;
365
366 // Functions returning iterators:
368 Iterator4D end();
369
370 // Assignment operators:
371 Tensor4View& operator=(const ConstTensor4View& v);
372 Tensor4View& operator=(const Tensor4View& v);
373 Tensor4View& operator=(const Tensor4& v);
374 Tensor4View& operator=(Numeric x);
375
376 // Other operators:
377 Tensor4View& operator*=(Numeric x);
378 Tensor4View& operator/=(Numeric x);
379 Tensor4View& operator+=(Numeric x);
380 Tensor4View& operator-=(Numeric x);
381
382 Tensor4View& operator*=(const ConstTensor4View& x);
383 Tensor4View& operator/=(const ConstTensor4View& x);
384 Tensor4View& operator+=(const ConstTensor4View& x);
385 Tensor4View& operator-=(const ConstTensor4View& x);
386
388 virtual ~Tensor4View() = default;
389
390 // Friends:
391 // friend class VectorView;
392 // friend ConstTensor4View transpose(ConstTensor4View m);
393 // friend Tensor4View transpose(Tensor4View m);
394 friend class Iterator5D;
395 friend class Tensor5View;
396 friend class Tensor6View;
397 friend class Tensor7View;
398
399 // Special constructor to make a Tensor4 view of a Tensor3.
400 Tensor4View(const Tensor3View& a);
401
402 protected:
403 // Constructors:
404 Tensor4View() = default;
405 Tensor4View(Numeric* data,
406 const Range& b,
407 const Range& p,
408 const Range& r,
409 const Range& c);
410 Tensor4View(Numeric* data,
411 const Range& pb,
412 const Range& pp,
413 const Range& pr,
414 const Range& pc,
415 const Range& nb,
416 const Range& np,
417 const Range& nr,
418 const Range& nc);
419};
420
429class Tensor4 : public Tensor4View {
430 public:
431 // Constructors:
432 Tensor4() = default;
433 Tensor4(Index b, Index p, Index r, Index c);
434 Tensor4(Index b, Index p, Index r, Index c, Numeric fill);
435 Tensor4(const ConstTensor4View& v);
436 Tensor4(const Tensor4& v);
437 Tensor4(Tensor4&& v) noexcept : Tensor4View(std::forward<Tensor4View>(v)) {
438 v.mdata = nullptr;
439 }
440
453 const Range& r0,
454 const Range& r1,
455 const Range& r2,
456 const Range& r3) ARTS_NOEXCEPT : Tensor4View(d, r0, r1, r2, r3) {
457 ARTS_ASSERT(not(r0.get_extent() < 0), "Must have size");
458 ARTS_ASSERT(not(r1.get_extent() < 0), "Must have size");
459 ARTS_ASSERT(not(r2.get_extent() < 0), "Must have size");
460 ARTS_ASSERT(not(r3.get_extent() < 0), "Must have size");
461 }
462
463 // Assignment operators:
464 Tensor4& operator=(const Tensor4& x);
465 Tensor4& operator=(Tensor4&& x) noexcept;
467
468 // Resize function:
469 void resize(Index b, Index p, Index r, Index c);
470
471 // Swap function:
472 friend void swap(Tensor4& t1, Tensor4& t2);
473
474 // Destructor:
475 virtual ~Tensor4();
476
477 // Returns data as a Vector
478 Vector flatten() && ARTS_NOEXCEPT;
479
481 template <std::size_t dim0>
483 static_assert(dim0 < 4, "Bad Dimension, Out-of-Bounds");
484
485 Range r0(0,
486 dim0 == 0 ? nbooks()
487 : dim0 == 1 ? npages()
488 : dim0 == 2 ? nrows()
489 : ncols());
490
491 Vector out(mdata, r0);
492 ARTS_ASSERT(size() == out.size(),
493 "Can only reduce size on same size input");
494 mdata = nullptr;
495 return out;
496 }
497
499 template <std::size_t dim0, std::size_t dim1>
501 static_assert(dim1 < 4, "Bad Dimension, Out-of-Bounds");
502 static_assert(dim0 < dim1, "Bad Dimensions, dim1 must be larger than dim0");
503
504 const Range r1(0, dim1 == 1 ? npages() : dim1 == 2 ? nrows() : ncols());
505 const Range r0(0,
506 dim0 == 0 ? nbooks()
507 : dim0 == 1 ? npages()
508 : nrows(),
509 r1.get_extent());
510
511 Matrix out(mdata, r0, r1);
512 ARTS_ASSERT(size() == out.size(),
513 "Can only reduce size on same size input");
514 mdata = nullptr;
515 return out;
516 }
517
519 template <std::size_t dim0, std::size_t dim1, std::size_t dim2>
521 static_assert(dim2 < 4, "Bad Dimension, Out-of-Bounds");
522 static_assert(dim0 < dim1, "Bad Dimensions, dim1 must be larger than dim0");
523 static_assert(dim1 < dim2, "Bad Dimensions, dim2 must be larger than dim1");
524
525 const Range r2(0, dim2 == 2 ? nrows() : ncols());
526 const Range r1(0, dim1 == 1 ? npages() : nrows(), r2.get_extent());
527 const Range r0(
528 0, dim0 == 0 ? nbooks() : npages(), r1.get_extent() * r2.get_extent());
529
530 Tensor3 out(mdata, r0, r1, r2);
531 ARTS_ASSERT(size() == out.size(),
532 "Can only reduce size on same size input");
533 mdata = nullptr;
534 return out;
535 }
536
537 template <class F>
538 void transform_elementwise(F&& func) {
539 std::transform(mdata, mdata + size(), mdata, func);
540 }
541};
542
543// Function declarations:
544// ----------------------
545
546void copy(ConstIterator4D origin,
547 const ConstIterator4D& end,
548 Iterator4D target);
549
550void copy(Numeric x, Iterator4D target, const Iterator4D& end);
551
552void transform(Tensor4View y, double (&my_func)(double), ConstTensor4View x);
553
555
557
559// Helper function for debugging
560#ifndef NDEBUG
561
563 Tensor4View& tv, Index b, Index p, Index r, Index c);
564
565#endif
567
570
572
573#endif // matpackIV_h
This can be used to make arrays out of anything.
Definition: array.h:48
Const version of Iterator4D.
Definition: matpackIV.h:76
Index mstride
Stride.
Definition: matpackIV.h:113
ConstIterator4D & operator++()
Prefix increment operator.
Definition: matpackIV.h:92
const ConstTensor3View & operator*() const
Dereferencing.
Definition: matpackIV.cc:41
const ConstTensor3View * operator->() const
The -> operator is needed, so that we can write i->begin() to get the 3D iterators.
Definition: matpackIV.cc:38
ConstTensor3View msv
Current position.
Definition: matpackIV.h:111
ConstIterator4D()=default
Default constructor.
ConstIterator4D(const ConstTensor3View &x, Index stride)
Explicit constructor.
Definition: matpackIV.h:86
bool operator!=(const ConstIterator4D &other) const
Not equal operator, needed for algorithms like copy.
Definition: matpackIV.h:98
Const version of Iterator5D.
Definition: matpackV.h:84
A constant view of a Matrix.
Definition: matpackI.h:1043
Index size() const noexcept
Definition: matpackI.h:1061
A constant view of a Tensor3.
Definition: matpackIII.h:130
Numeric * mdata
Pointer to the plain C array that holds the data.
Definition: matpackIII.h:234
Range mrr
The row range of mdata that is actually used.
Definition: matpackIII.h:230
Range mcr
The column range of mdata that is actually used.
Definition: matpackIII.h:232
Index size() const noexcept
Definition: matpackIII.h:149
Range mpr
The page range of mdata that is actually used.
Definition: matpackIII.h:228
A constant view of a Tensor4.
Definition: matpackIV.h:131
Index ncols() const noexcept
Definition: matpackIV.h:142
Range mrr
The row range of mdata that is actually used.
Definition: matpackIV.h:276
ConstTensor4View()=default
ConstTensor4View & operator=(const ConstTensor4View &)=default
Range mbr
The book range of mdata that is actually used.
Definition: matpackIV.h:272
ConstTensor4View operator()(const Range &b, const Range &p, const Range &r, const Range &c) const
Const index operator for subrange.
Definition: matpackIV.cc:49
virtual ~ConstTensor4View()=default
Destructor.
Index size() const noexcept
Definition: matpackIV.h:145
bool empty() const noexcept
Definition: matpackIV.h:148
constexpr ConstTensor4View(const ConstTensor4View &)=default
Range mcr
The column range of mdata that is actually used.
Definition: matpackIV.h:278
Numeric * mdata
Pointer to the plain C array that holds the data.
Definition: matpackIV.h:280
constexpr ConstTensor4View(ConstTensor4View &&)=default
Range mpr
The page range of mdata that is actually used.
Definition: matpackIV.h:274
friend std::ostream & operator<<(std::ostream &os, const ConstTensor4View &v)
Output operator.
Definition: matpackIV.cc:420
ConstTensor4View & operator=(ConstTensor4View &&)=default
Numeric get(Index b, Index p, Index r, Index c) const
Get element implementation without assertions.
Definition: matpackIV.h:226
ConstIterator4D end() const
Return const iterator behind last book.
Definition: matpackIV.cc:367
ConstIterator4D begin() const
Return const iterator to first book.
Definition: matpackIV.cc:361
Index nrows() const noexcept
Definition: matpackIV.h:141
Shape< 4 > shape() const
Definition: matpackIV.h:151
Index nbooks() const noexcept
Definition: matpackIV.h:139
Numeric operator()(Index b, Index p, Index r, Index c) const
Plain const index operator.
Definition: matpackIV.h:209
Index npages() const noexcept
Definition: matpackIV.h:140
A constant view of a Tensor5.
Definition: matpackV.h:141
A constant view of a Tensor6.
Definition: matpackVI.h:147
A constant view of a Tensor7.
Definition: matpackVII.h:145
A constant view of a Vector.
Definition: matpackI.h:512
Index size() const noexcept
Definition: matpackI.h:539
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:72
Tensor3View * operator->()
The -> operator is needed, so that we can write i->begin() to get the 3D iterators.
Definition: matpackIV.cc:31
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:34
Tensor3View msv
Current position.
Definition: matpackIV.h:70
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:1164
The Matrix class.
Definition: matpackI.h:1261
The range class.
Definition: matpackI.h:159
Index mstart
The start index.
Definition: matpackI.h:367
constexpr Index get_extent() const noexcept
Returns the extent of the range.
Definition: matpackI.h:342
Index mstride
The stride.
Definition: matpackI.h:372
Index mextent
The number of elements.
Definition: matpackI.h:370
The Tensor3View class.
Definition: matpackIII.h:246
The Tensor3 class.
Definition: matpackIII.h:346
The Tensor4View class.
Definition: matpackIV.h:292
constexpr Tensor4View(const Tensor4View &)=default
Iterator4D begin()
Return iterator to first book.
Definition: matpackIV.cc:696
const Numeric * get_c_array() const ARTS_NOEXCEPT
Conversion to plain C-array.
Definition: matpackIV.cc:342
Numeric & get(Index b, Index p, Index r, Index c)
Get element implementation without assertions.
Definition: matpackIV.h:356
Tensor4View operator()(const Range &b, const Range &p, const Range &r, const Range &c)
Index operator for subrange.
Definition: matpackIV.cc:444
Tensor4View & operator=(const ConstTensor4View &v)
Assignment operator.
Definition: matpackIV.cc:713
Iterator4D end()
Return iterator behind last book.
Definition: matpackIV.cc:702
Tensor4View()=default
Numeric & operator()(Index b, Index p, Index r, Index c)
Plain non-const index operator.
Definition: matpackIV.h:339
The Tensor4 class.
Definition: matpackIV.h:429
Matrix reduce_rank() &&ARTS_NOEXCEPT
Definition: matpackIV.h:500
Tensor4(Tensor4 &&v) noexcept
Definition: matpackIV.h:437
Tensor4(Numeric *d, const Range &r0, const Range &r1, const Range &r2, const Range &r3) ARTS_NOEXCEPT
Definition: matpackIV.h:452
Tensor3 reduce_rank() &&ARTS_NOEXCEPT
Definition: matpackIV.h:520
void transform_elementwise(F &&func)
Definition: matpackIV.h:538
Vector reduce_rank() &&ARTS_NOEXCEPT
Definition: matpackIV.h:482
Tensor4()=default
The Tensor5View class.
Definition: matpackV.h:343
The Tensor6View class.
Definition: matpackVI.h:632
The Tensor7View class.
Definition: matpackVII.h:1303
The VectorView class.
Definition: matpackI.h:663
The Vector class.
Definition: matpackI.h:899
#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:918
Numeric debug_tensor4view_get_elem(Tensor4View &tv, Index b, Index p, Index r, Index c)
Helper function to access tensor elements.
Definition: matpackIV.cc:1189
Numeric max(const ConstTensor4View &x)
Max function, tensor version.
Definition: matpackIV.cc:1133
Numeric min(const ConstTensor4View &x)
Min function, tensor version.
Definition: matpackIV.cc:1152
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:1115
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
void swap(ComplexVector &v1, ComplexVector &v2)
Swaps two objects.
constexpr Numeric r0
The reference radius in IGRF13.
Definition: igrf13.cc:203
Helper shape class.
Definition: matpackI.h:377
#define d
#define v
#define a
#define c
#define b