ARTS 2.5.10 (git: 2f1c442c)
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 <utility>
35
36#include "matpackIII.h"
37
41 public:
42 // Constructors:
44 Iterator4D() = default;
45
47 Iterator4D(const Tensor3View& x, Index stride)
48 : msv(x), mstride(stride) { /* Nothing to do here. */
49 }
50
51 // Operators:
54 msv.mdata += mstride;
55 return *this;
56 }
57
59 bool operator!=(const Iterator4D& other) const {
61 other.msv.mdata + other.msv.mpr.mstart + other.msv.mrr.mstart +
62 other.msv.mcr.mstart)
63 return true;
64 return false;
65 }
66
69
70 private:
75};
76
79 public:
80 // Constructors:
81 // Functions for ConstIterator4D
82 // -----------------------------
83
85 ConstIterator4D() = default;
86
89 : msv(std::move(x)), mstride(stride) { /* Nothing to do here. */
90 }
91
92 // Operators:
95 msv.mdata += mstride;
96 return *this;
97 }
98
100 bool operator!=(const ConstIterator4D& other) const {
102 other.msv.mdata + other.msv.mpr.mstart + other.msv.mrr.mstart +
103 other.msv.mcr.mstart)
104 return true;
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 static constexpr bool matpack_type{true};
136
137 constexpr ConstTensor4View(const ConstTensor4View&) = default;
138 constexpr ConstTensor4View(ConstTensor4View&&) = default;
141
142 // Member functions:
143 [[nodiscard]] Index nbooks() const noexcept { return mbr.mextent; }
144 [[nodiscard]] Index npages() const noexcept { return mpr.mextent; }
145 [[nodiscard]] Index nrows() const noexcept { return mrr.mextent; }
146 [[nodiscard]] Index ncols() const noexcept { return mcr.mextent; }
147
148 // Total size
149 [[nodiscard]] Index size() const noexcept {
150 return nbooks() * npages() * nrows() * ncols();
151 }
152 [[nodiscard]] bool empty() const noexcept { return size() == 0; }
153
155 [[nodiscard]] Shape<4> shape() const {
156 return {nbooks(), npages(), nrows(), ncols()};
157 }
158
159 // Const index operators:
161 const Range& p,
162 const Range& r,
163 const Range& c) const;
164
166 const Range& p,
167 const Range& r,
168 Index c) const;
170 const Range& p,
171 Index r,
172 const Range& c) const;
174 Index p,
175 const Range& r,
176 const Range& c) const;
178 const Range& p,
179 const Range& r,
180 const Range& c) const;
181
183 const Range& p,
184 Index r,
185 Index c) const;
187 Index p,
188 const Range& r,
189 Index c) const;
191 Index p,
192 Index r,
193 const Range& c) const;
195 const Range& p,
196 Index r,
197 const Range& c) const;
199 const Range& p,
200 const Range& r,
201 Index c) const;
203 Index p,
204 const Range& r,
205 const Range& c) const;
206
207 ConstVectorView operator()(const Range& b, Index p, Index r, Index c) const;
208 ConstVectorView operator()(Index b, const Range& p, Index r, Index c) const;
209 ConstVectorView operator()(Index b, Index p, const Range& r, Index c) const;
210 ConstVectorView operator()(Index b, Index p, Index r, const Range& c) const;
211
214 Index p,
215 Index r,
216 Index c) const { // Check if indices are valid:
217 ARTS_ASSERT(0 <= b);
218 ARTS_ASSERT(0 <= p);
219 ARTS_ASSERT(0 <= r);
220 ARTS_ASSERT(0 <= c);
225
226 return get(b, p, r, c);
227 }
228
230 [[nodiscard]] Numeric get(Index b, Index p, Index r, Index c) const {
231 return *(mdata + mbr.mstart + b * mbr.mstride + mpr.mstart +
232 p * mpr.mstride + mrr.mstart + r * mrr.mstride + mcr.mstart +
233 c * mcr.mstride);
234 }
235
236 // Functions returning iterators:
237 [[nodiscard]] ConstIterator4D begin() const;
238 [[nodiscard]] ConstIterator4D end() const;
239
241 virtual ~ConstTensor4View() = default;
242
243 // Friends:
244 friend class Tensor4View;
245 friend class ConstIterator5D;
246 friend class ConstTensor5View;
247 friend class ConstTensor6View;
248 friend class ConstTensor7View;
249
250 friend std::ostream& operator<<(std::ostream& os, const ConstTensor4View& v);
251
252 // Special constructor to make a Tensor4 view of a Tensor3.
254
255 protected:
256 // Constructors:
257 ConstTensor4View() = default;
259 const Range& b,
260 const Range& p,
261 const Range& r,
262 const Range& c);
264 const Range& pb,
265 const Range& pp,
266 const Range& pr,
267 const Range& pc,
268 const Range& nb,
269 const Range& np,
270 const Range& nr,
271 const Range& nc);
272
273 // Data members:
274 // -------------
276 Range mbr{0, 0, 1};
278 Range mpr{0, 0, 1};
280 Range mrr{0, 0, 1};
282 Range mcr{0, 0, 1};
284 Numeric* mdata{nullptr};
285};
286
297 public:
298 // Make const methods visible from base class
301 using ConstTensor4View::operator();
303
304 constexpr Tensor4View(const Tensor4View&) = default;
305
306 // Non-const index operators:
307
309 const Range& p,
310 const Range& r,
311 const Range& c);
312
314 const Range& p,
315 const Range& r,
316 Index c);
318 const Range& p,
319 Index r,
320 const Range& c);
322 Index p,
323 const Range& r,
324 const Range& c);
326 const Range& p,
327 const Range& r,
328 const Range& c);
329
330 MatrixView operator()(const Range& b, const Range& p, Index r, Index c);
331 MatrixView operator()(const Range& b, Index p, const Range& r, Index c);
332 MatrixView operator()(const Range& b, Index p, Index r, const Range& c);
333 MatrixView operator()(Index b, const Range& p, Index r, const Range& c);
334 MatrixView operator()(Index b, const Range& p, const Range& r, Index c);
335 MatrixView operator()(Index b, Index p, const Range& r, const Range& c);
336
337 VectorView operator()(const Range& b, Index p, Index r, Index c);
338 VectorView operator()(Index b, const Range& p, Index r, Index c);
339 VectorView operator()(Index b, Index p, const Range& r, Index c);
340 VectorView operator()(Index b, Index p, Index r, const Range& c);
341
342#define GETFUN(b, p, r, c) \
343 *(mdata + mbr.mstart + b * mbr.mstride + mpr.mstart + p * mpr.mstride + \
344 mrr.mstart + r * mrr.mstride + mcr.mstart + c * mcr.mstride)
347 Index p,
348 Index r,
349 Index c) { // Check if indices are valid:
350 ARTS_ASSERT(0 <= b);
351 ARTS_ASSERT(0 <= p);
352 ARTS_ASSERT(0 <= r);
353 ARTS_ASSERT(0 <= c);
358
359 return GETFUN(b, p, r, c);
360 }
361
364 return GETFUN(b, p, r, c);
365 }
366#undef GETFUN
367
368 // Conversion to a plain C-array
369 [[nodiscard]] const Numeric* get_c_array() const ARTS_NOEXCEPT;
371
372 // Functions returning iterators:
374 Iterator4D end();
375
376 // Assignment operators:
377 Tensor4View& operator=(const ConstTensor4View& v);
378 Tensor4View& operator=(const Tensor4View& v);
379 Tensor4View& operator=(const Tensor4& v);
380 Tensor4View& operator=(Numeric x);
381
382 // Other operators:
383 Tensor4View& operator*=(Numeric x);
384 Tensor4View& operator/=(Numeric x);
385 Tensor4View& operator+=(Numeric x);
386 Tensor4View& operator-=(Numeric x);
387
388 Tensor4View& operator*=(const ConstTensor4View& x);
389 Tensor4View& operator/=(const ConstTensor4View& x);
390 Tensor4View& operator+=(const ConstTensor4View& x);
391 Tensor4View& operator-=(const ConstTensor4View& x);
392
394 ~Tensor4View() override = default;
395
396 // Friends:
397 // friend class VectorView;
398 // friend ConstTensor4View transpose(ConstTensor4View m);
399 // friend Tensor4View transpose(Tensor4View m);
400 friend class Iterator5D;
401 friend class Tensor5View;
402 friend class Tensor6View;
403 friend class Tensor7View;
404
405 // Special constructor to make a Tensor4 view of a Tensor3.
406 Tensor4View(const Tensor3View& a);
407
408 protected:
409 // Constructors:
410 Tensor4View() = default;
411 Tensor4View(Numeric* data,
412 const Range& b,
413 const Range& p,
414 const Range& r,
415 const Range& c);
416 Tensor4View(Numeric* data,
417 const Range& pb,
418 const Range& pp,
419 const Range& pr,
420 const Range& pc,
421 const Range& nb,
422 const Range& np,
423 const Range& nr,
424 const Range& nc);
425};
426
435class Tensor4 : public Tensor4View {
436 public:
437 // Constructors:
438 Tensor4() = default;
439 Tensor4(Index b, Index p, Index r, Index c);
440 Tensor4(Index b, Index p, Index r, Index c, Numeric fill);
441 Tensor4(const ConstTensor4View& v);
442 Tensor4(const Tensor4& v);
443 Tensor4(Tensor4&& v) noexcept : Tensor4View(std::forward<Tensor4View>(v)) {
444 v.mdata = nullptr;
445 }
446
449 : Tensor4(matpack::book_size(init), matpack::page_size(init),
450 matpack::row_size(init), matpack::column_size(init)) {
451 *this = init;
452 }
453
456 if (const auto s = matpack::shape<Index, 4>(init); shape().data not_eq s)
457 resize(s[0], s[1], s[2], s[3]);
458
459 auto [I, J, K, L] = shape().data;
460 for (Index i = 0; i < I; i++)
461 for (Index j = 0; j < J; j++)
462 for (Index k = 0; k < K; k++)
463 for (Index x = 0; x < L; x++)
464 operator()(i, j, k, x) = init(i, j, k, x);
465
466 return *this;
467 }
468
481 const Range& r0,
482 const Range& r1,
483 const Range& r2,
484 const Range& r3) ARTS_NOEXCEPT : Tensor4View(d, r0, r1, r2, r3) {
485 ARTS_ASSERT(not(r0.get_extent() < 0), "Must have size");
486 ARTS_ASSERT(not(r1.get_extent() < 0), "Must have size");
487 ARTS_ASSERT(not(r2.get_extent() < 0), "Must have size");
488 ARTS_ASSERT(not(r3.get_extent() < 0), "Must have size");
489 }
490
491 // Assignment operators:
492 Tensor4& operator=(const Tensor4& x);
495
496 // Resize function:
497 void resize(Index b, Index p, Index r, Index c);
498
499 // Swap function:
500 friend void swap(Tensor4& t1, Tensor4& t2) noexcept;
501
502 // Destructor:
503 ~Tensor4() noexcept override;
504
505 // Returns data as a Vector
506 Vector flatten() && ARTS_NOEXCEPT;
507
509 template <std::size_t dim0>
510 Vector reduce_rank() && ARTS_NOEXCEPT {
511 static_assert(dim0 < 4, "Bad Dimension, Out-of-Bounds");
512
513 Range r0(0,
514 dim0 == 0 ? nbooks()
515 : dim0 == 1 ? npages()
516 : dim0 == 2 ? nrows()
517 : ncols());
518
519 Vector out(mdata, r0);
520 ARTS_ASSERT(size() == out.size(),
521 "Can only reduce size on same size input");
522 mdata = nullptr;
523 return out;
524 }
525
527 template <std::size_t dim0, std::size_t dim1>
529 static_assert(dim1 < 4, "Bad Dimension, Out-of-Bounds");
530 static_assert(dim0 < dim1, "Bad Dimensions, dim1 must be larger than dim0");
531
532 const Range r1(0, dim1 == 1 ? npages() : dim1 == 2 ? nrows() : ncols());
533 const Range r0(0,
534 dim0 == 0 ? nbooks()
535 : dim0 == 1 ? npages()
536 : nrows(),
537 r1.get_extent());
538
539 Matrix out(mdata, r0, r1);
540 ARTS_ASSERT(size() == out.size(),
541 "Can only reduce size on same size input");
542 mdata = nullptr;
543 return out;
544 }
545
547 template <std::size_t dim0, std::size_t dim1, std::size_t dim2>
549 static_assert(dim2 < 4, "Bad Dimension, Out-of-Bounds");
550 static_assert(dim0 < dim1, "Bad Dimensions, dim1 must be larger than dim0");
551 static_assert(dim1 < dim2, "Bad Dimensions, dim2 must be larger than dim1");
552
553 const Range r2(0, dim2 == 2 ? nrows() : ncols());
554 const Range r1(0, dim1 == 1 ? npages() : nrows(), r2.get_extent());
555 const Range r0(
556 0, dim0 == 0 ? nbooks() : npages(), r1.get_extent() * r2.get_extent());
557
558 Tensor3 out(mdata, r0, r1, r2);
559 ARTS_ASSERT(size() == out.size(),
560 "Can only reduce size on same size input");
561 mdata = nullptr;
562 return out;
563 }
564
565 template <class F>
566 void transform_elementwise(F&& func) {
567 std::transform(mdata, mdata + size(), mdata, func);
568 }
569};
570
571// Function declarations:
572// ----------------------
573
574void copy(ConstIterator4D origin,
575 const ConstIterator4D& end,
576 Iterator4D target);
577
578void copy(Numeric x, Iterator4D target, const Iterator4D& end);
579
580void transform(Tensor4View y, double (&my_func)(double), ConstTensor4View x);
581
583
585
587// Helper function for debugging
588#ifndef NDEBUG
589
591 Tensor4View& tv, Index b, Index p, Index r, Index c);
592
593#endif
595
598
600
601#endif // matpackIV_h
base max(const Array< base > &x)
Max function.
Definition: array.h:145
base min(const Array< base > &x)
Min function.
Definition: array.h:161
This can be used to make arrays out of anything.
Definition: array.h:48
Const version of Iterator4D.
Definition: matpackIV.h:78
Index mstride
Stride.
Definition: matpackIV.h:115
ConstIterator4D & operator++()
Prefix increment operator.
Definition: matpackIV.h:94
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:113
ConstIterator4D()=default
Default constructor.
bool operator!=(const ConstIterator4D &other) const
Not equal operator, needed for algorithms like copy.
Definition: matpackIV.h:100
ConstIterator4D(ConstTensor3View x, Index stride)
Explicit constructor.
Definition: matpackIV.h:88
Const version of Iterator5D.
Definition: matpackV.h:87
A constant view of a Matrix.
Definition: matpackI.h:1065
Index size() const noexcept
Definition: matpackI.h:1085
A constant view of a Tensor3.
Definition: matpackIII.h:133
Numeric * mdata
Pointer to the plain C array that holds the data.
Definition: matpackIII.h:239
Range mrr
The row range of mdata that is actually used.
Definition: matpackIII.h:235
Range mcr
The column range of mdata that is actually used.
Definition: matpackIII.h:237
Index size() const noexcept
Definition: matpackIII.h:154
Range mpr
The page range of mdata that is actually used.
Definition: matpackIII.h:233
A constant view of a Tensor4.
Definition: matpackIV.h:133
static constexpr bool matpack_type
Definition: matpackIV.h:135
Index ncols() const noexcept
Definition: matpackIV.h:146
Range mrr
The row range of mdata that is actually used.
Definition: matpackIV.h:280
ConstTensor4View()=default
ConstTensor4View & operator=(const ConstTensor4View &)=default
Range mbr
The book range of mdata that is actually used.
Definition: matpackIV.h:276
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:149
bool empty() const noexcept
Definition: matpackIV.h:152
constexpr ConstTensor4View(const ConstTensor4View &)=default
Range mcr
The column range of mdata that is actually used.
Definition: matpackIV.h:282
Numeric * mdata
Pointer to the plain C array that holds the data.
Definition: matpackIV.h:284
constexpr ConstTensor4View(ConstTensor4View &&)=default
Range mpr
The page range of mdata that is actually used.
Definition: matpackIV.h:278
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:230
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:145
Shape< 4 > shape() const
Definition: matpackIV.h:155
Index nbooks() const noexcept
Definition: matpackIV.h:143
Numeric operator()(Index b, Index p, Index r, Index c) const
Plain const index operator.
Definition: matpackIV.h:213
Index npages() const noexcept
Definition: matpackIV.h:144
A constant view of a Tensor5.
Definition: matpackV.h:144
A constant view of a Tensor6.
Definition: matpackVI.h:150
A constant view of a Tensor7.
Definition: matpackVII.h:148
A constant view of a Vector.
Definition: matpackI.h:521
Index size() const noexcept
Definition: matpackI.h:550
Implementation of Tensors of Rank 4.
Definition: matpackIV.h:40
Iterator4D & operator++()
Prefix increment operator.
Definition: matpackIV.h:53
Index mstride
Stride.
Definition: matpackIV.h:74
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:59
Tensor3View & operator*()
Dereferencing.
Definition: matpackIV.cc:34
Tensor3View msv
Current position.
Definition: matpackIV.h:72
Iterator4D(const Tensor3View &x, Index stride)
Explicit constructor.
Definition: matpackIV.h:47
Implementation of Tensors of Rank 5.
Definition: matpackV.h:41
The MatrixView class.
Definition: matpackI.h:1188
The Matrix class.
Definition: matpackI.h:1285
The range class.
Definition: matpackI.h:160
Index mstart
The start index.
Definition: matpackI.h:377
constexpr Index get_extent() const noexcept
Returns the extent of the range.
Definition: matpackI.h:343
Index mstride
The stride.
Definition: matpackI.h:382
Index mextent
The number of elements.
Definition: matpackI.h:380
The Tensor3View class.
Definition: matpackIII.h:251
The Tensor3 class.
Definition: matpackIII.h:352
The Tensor4View class.
Definition: matpackIV.h:296
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:363
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:346
The Tensor4 class.
Definition: matpackIV.h:435
Tensor4 & operator=(const matpack::tensor4_like_not_tensor4 auto &init)
Set from a tensor type.
Definition: matpackIV.h:455
Matrix reduce_rank() &&ARTS_NOEXCEPT
Definition: matpackIV.h:528
Tensor4(Tensor4 &&v) noexcept
Definition: matpackIV.h:443
Tensor4(Numeric *d, const Range &r0, const Range &r1, const Range &r2, const Range &r3) ARTS_NOEXCEPT
Definition: matpackIV.h:480
Tensor3 reduce_rank() &&ARTS_NOEXCEPT
Definition: matpackIV.h:548
Tensor4(const matpack::tensor4_like_not_tensor4 auto &init)
Initialization from a tensor type.
Definition: matpackIV.h:448
void transform_elementwise(F &&func)
Definition: matpackIV.h:566
Tensor4()=default
The Tensor5View class.
Definition: matpackV.h:348
The Tensor6View class.
Definition: matpackVI.h:635
The Tensor7View class.
Definition: matpackVII.h:1308
The VectorView class.
Definition: matpackI.h:674
The Vector class.
Definition: matpackI.h:910
A concept precluding Arts types but allowing the tensor-like object.
#define ARTS_NOEXCEPT
Definition: debug.h:99
#define ARTS_ASSERT(condition,...)
Definition: debug.h:102
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:1190
#define GETFUN(b, p, r, c)
Definition: matpackIV.h:342
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:1116
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) noexcept
Swaps two objects.
Helper shape class.
Definition: matpackI.h:387
#define d
#define v
#define a
#define c
#define b