ARTS 2.5.4 (git: 4c0d3b4d)
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 // Special constructor to make a Tensor4 view of a Tensor3.
248
249 protected:
250 // Constructors:
251 ConstTensor4View() = default;
253 const Range& b,
254 const Range& p,
255 const Range& r,
256 const Range& c);
258 const Range& pb,
259 const Range& pp,
260 const Range& pr,
261 const Range& pc,
262 const Range& nb,
263 const Range& np,
264 const Range& nr,
265 const Range& nc);
266
267 // Data members:
268 // -------------
270 Range mbr{0, 0, 1};
272 Range mpr{0, 0, 1};
274 Range mrr{0, 0, 1};
276 Range mcr{0, 0, 1};
278 Numeric* mdata{nullptr};
279};
280
291 public:
292 // Make const methods visible from base class
295 using ConstTensor4View::operator();
297
298 constexpr Tensor4View(const Tensor4View&) = default;
299
300 // Non-const index operators:
301
303 const Range& p,
304 const Range& r,
305 const Range& c);
306
308 const Range& p,
309 const Range& r,
310 Index c);
312 const Range& p,
313 Index r,
314 const Range& c);
316 Index p,
317 const Range& r,
318 const Range& c);
320 const Range& p,
321 const Range& r,
322 const Range& c);
323
324 MatrixView operator()(const Range& b, const Range& p, Index r, Index c);
325 MatrixView operator()(const Range& b, Index p, const Range& r, Index c);
326 MatrixView operator()(const Range& b, Index p, Index r, const Range& c);
327 MatrixView operator()(Index b, const Range& p, Index r, const Range& c);
328 MatrixView operator()(Index b, const Range& p, const Range& r, Index c);
329 MatrixView operator()(Index b, Index p, const Range& r, const Range& c);
330
331 VectorView operator()(const Range& b, Index p, Index r, Index c);
332 VectorView operator()(Index b, const Range& p, Index r, Index c);
333 VectorView operator()(Index b, Index p, const Range& r, Index c);
334 VectorView operator()(Index b, Index p, Index r, const Range& c);
335
338 Index p,
339 Index r,
340 Index c) { // Check if indices are valid:
341 ARTS_ASSERT(0 <= b);
342 ARTS_ASSERT(0 <= p);
343 ARTS_ASSERT(0 <= r);
344 ARTS_ASSERT(0 <= c);
349
350 return get(b, p, r, c);
351 }
352
355 return *(mdata + mbr.mstart + b * mbr.mstride + mpr.mstart +
356 p * mpr.mstride + mrr.mstart + r * mrr.mstride + mcr.mstart +
357 c * mcr.mstride);
358 }
359
360 // Conversion to a plain C-array
361 [[nodiscard]] const Numeric* get_c_array() const ARTS_NOEXCEPT;
363
364 // Functions returning iterators:
366 Iterator4D end();
367
368 // Assignment operators:
369 Tensor4View& operator=(const ConstTensor4View& v);
370 Tensor4View& operator=(const Tensor4View& v);
371 Tensor4View& operator=(const Tensor4& v);
372 Tensor4View& operator=(Numeric x);
373
374 // Other operators:
375 Tensor4View& operator*=(Numeric x);
376 Tensor4View& operator/=(Numeric x);
377 Tensor4View& operator+=(Numeric x);
378 Tensor4View& operator-=(Numeric x);
379
380 Tensor4View& operator*=(const ConstTensor4View& x);
381 Tensor4View& operator/=(const ConstTensor4View& x);
382 Tensor4View& operator+=(const ConstTensor4View& x);
383 Tensor4View& operator-=(const ConstTensor4View& x);
384
386 virtual ~Tensor4View() = default;
387
388 // Friends:
389 // friend class VectorView;
390 // friend ConstTensor4View transpose(ConstTensor4View m);
391 // friend Tensor4View transpose(Tensor4View m);
392 friend class Iterator5D;
393 friend class Tensor5View;
394 friend class Tensor6View;
395 friend class Tensor7View;
396
397 // Special constructor to make a Tensor4 view of a Tensor3.
398 Tensor4View(const Tensor3View& a);
399
400 protected:
401 // Constructors:
402 Tensor4View() = default;
403 Tensor4View(Numeric* data,
404 const Range& b,
405 const Range& p,
406 const Range& r,
407 const Range& c);
408 Tensor4View(Numeric* data,
409 const Range& pb,
410 const Range& pp,
411 const Range& pr,
412 const Range& pc,
413 const Range& nb,
414 const Range& np,
415 const Range& nr,
416 const Range& nc);
417};
418
427class Tensor4 : public Tensor4View {
428 public:
429 // Constructors:
430 Tensor4() = default;
431 Tensor4(Index b, Index p, Index r, Index c);
432 Tensor4(Index b, Index p, Index r, Index c, Numeric fill);
433 Tensor4(const ConstTensor4View& v);
434 Tensor4(const Tensor4& v);
435 Tensor4(Tensor4&& v) noexcept : Tensor4View(std::forward<Tensor4View>(v)) {
436 v.mdata = nullptr;
437 }
438
451 const Range& r0,
452 const Range& r1,
453 const Range& r2,
454 const Range& r3) ARTS_NOEXCEPT : Tensor4View(d, r0, r1, r2, r3) {
455 ARTS_ASSERT(not(r0.get_extent() < 0), "Must have size");
456 ARTS_ASSERT(not(r1.get_extent() < 0), "Must have size");
457 ARTS_ASSERT(not(r2.get_extent() < 0), "Must have size");
458 ARTS_ASSERT(not(r3.get_extent() < 0), "Must have size");
459 }
460
461 // Assignment operators:
462 Tensor4& operator=(const Tensor4& x);
463 Tensor4& operator=(Tensor4&& x) noexcept;
465
466 // Resize function:
467 void resize(Index b, Index p, Index r, Index c);
468
469 // Swap function:
470 friend void swap(Tensor4& t1, Tensor4& t2);
471
472 // Destructor:
473 virtual ~Tensor4();
474
476 template <std::size_t dim0>
478 static_assert(dim0 < 4, "Bad Dimension, Out-of-Bounds");
479
480 Range r0(0,
481 dim0 == 0 ? nbooks()
482 : dim0 == 1 ? npages()
483 : dim0 == 2 ? nrows()
484 : ncols());
485
486 Vector out(mdata, r0);
487 ARTS_ASSERT(size() == out.size(),
488 "Can only reduce size on same size input");
489 mdata = nullptr;
490 return out;
491 }
492
494 template <std::size_t dim0, std::size_t dim1>
496 static_assert(dim1 < 4, "Bad Dimension, Out-of-Bounds");
497 static_assert(dim0 < dim1, "Bad Dimensions, dim1 must be larger than dim0");
498
499 const Range r1(0, dim1 == 1 ? npages() : dim1 == 2 ? nrows() : ncols());
500 const Range r0(0,
501 dim0 == 0 ? nbooks()
502 : dim0 == 1 ? npages()
503 : nrows(),
504 r1.get_extent());
505
506 Matrix out(mdata, r0, r1);
507 ARTS_ASSERT(size() == out.size(),
508 "Can only reduce size on same size input");
509 mdata = nullptr;
510 return out;
511 }
512
514 template <std::size_t dim0, std::size_t dim1, std::size_t dim2>
516 static_assert(dim2 < 4, "Bad Dimension, Out-of-Bounds");
517 static_assert(dim0 < dim1, "Bad Dimensions, dim1 must be larger than dim0");
518 static_assert(dim1 < dim2, "Bad Dimensions, dim2 must be larger than dim1");
519
520 const Range r2(0, dim2 == 2 ? nrows() : ncols());
521 const Range r1(0, dim1 == 1 ? npages() : nrows(), r2.get_extent());
522 const Range r0(
523 0, dim0 == 0 ? nbooks() : npages(), r1.get_extent() * r2.get_extent());
524
525 Tensor3 out(mdata, r0, r1, r2);
526 ARTS_ASSERT(size() == out.size(),
527 "Can only reduce size on same size input");
528 mdata = nullptr;
529 return out;
530 }
531
532 template <class F>
533 void transform_elementwise(F&& func) {
534 std::transform(mdata, mdata + size(), mdata, func);
535 }
536};
537
538// Function declarations:
539// ----------------------
540
541void copy(ConstIterator4D origin,
542 const ConstIterator4D& end,
543 Iterator4D target);
544
545void copy(Numeric x, Iterator4D target, const Iterator4D& end);
546
547void transform(Tensor4View y, double (&my_func)(double), ConstTensor4View x);
548
550
552
553std::ostream& operator<<(std::ostream& os, const ConstTensor4View& v);
554
556// Helper function for debugging
557#ifndef NDEBUG
558
560 Tensor4View& tv, Index b, Index p, Index r, Index c);
561
562#endif
564
565#endif // matpackIV_h
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:1050
Index size() const noexcept
Definition: matpackI.h:1065
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:232
Range mrr
The row range of mdata that is actually used.
Definition: matpackIII.h:228
Range mcr
The column range of mdata that is actually used.
Definition: matpackIII.h:230
Index size() const noexcept
Definition: matpackIII.h:149
Range mpr
The page range of mdata that is actually used.
Definition: matpackIII.h:226
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:274
ConstTensor4View()=default
ConstTensor4View & operator=(const ConstTensor4View &)=default
Range mbr
The book range of mdata that is actually used.
Definition: matpackIV.h:270
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:276
Numeric * mdata
Pointer to the plain C array that holds the data.
Definition: matpackIV.h:278
constexpr ConstTensor4View(ConstTensor4View &&)=default
Range mpr
The page range of mdata that is actually used.
Definition: matpackIV.h:272
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:517
Index size() const noexcept
Definition: matpackI.h:544
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:1169
The Matrix class.
Definition: matpackI.h:1270
The range class.
Definition: matpackI.h:166
Index mstart
The start index.
Definition: matpackI.h:368
constexpr Index get_extent() const noexcept
Returns the extent of the range.
Definition: matpackI.h:349
Index mstride
The stride.
Definition: matpackI.h:377
Index mextent
The number of elements.
Definition: matpackI.h:375
The Tensor3View class.
Definition: matpackIII.h:244
The Tensor3 class.
Definition: matpackIII.h:344
The Tensor4View class.
Definition: matpackIV.h:290
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:354
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:337
The Tensor4 class.
Definition: matpackIV.h:427
Matrix reduce_rank() &&ARTS_NOEXCEPT
Definition: matpackIV.h:495
Tensor4(Tensor4 &&v) noexcept
Definition: matpackIV.h:435
Tensor4(Numeric *d, const Range &r0, const Range &r1, const Range &r2, const Range &r3) ARTS_NOEXCEPT
Definition: matpackIV.h:450
Tensor3 reduce_rank() &&ARTS_NOEXCEPT
Definition: matpackIV.h:515
void transform_elementwise(F &&func)
Definition: matpackIV.h:533
Vector reduce_rank() &&ARTS_NOEXCEPT
Definition: matpackIV.h:477
Tensor4()=default
The Tensor5View class.
Definition: matpackV.h:341
The Tensor6View class.
Definition: matpackVI.h:630
The Tensor7View class.
Definition: matpackVII.h:1301
The VectorView class.
Definition: matpackI.h:658
The Vector class.
Definition: matpackI.h:908
#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
std::ostream & operator<<(std::ostream &os, const ConstTensor4View &v)
Output operator.
Definition: matpackIV.cc:420
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
#define d
#define v
#define a
#define c
#define b
Helper shape class.
Definition: matpackI.h:382