ARTS 2.5.4 (git: 31ce4f0e)
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
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 return false;
58 }
59
62 MatrixView* operator->() { return &msv; }
63
65 MatrixView& operator*() { return msv; }
66
67 private:
72};
73
76 public:
77 // Constructors:
79 ConstIterator3D() = default;
80
83 : msv(x), mstride(stride) { /* Nothing to do here. */
84 }
85
86 // Operators:
89 msv.mdata += mstride;
90 return *this;
91 }
92
94 bool operator!=(const ConstIterator3D& other) const {
95 if (msv.mdata + msv.mrr.mstart + msv.mcr.mstart !=
96 other.msv.mdata + other.msv.mrr.mstart + other.msv.mcr.mstart)
97 return true;
98 return false;
99 }
100
103 const ConstMatrixView* operator->() const { return &msv; }
104
106 const ConstMatrixView& operator*() const { return msv; }
107
108 private:
113};
114
115// Declare class Tensor3:
116class Tensor3;
117
131 public:
132 constexpr ConstTensor3View(const ConstTensor3View&) = default;
133 constexpr ConstTensor3View(ConstTensor3View&&) = default;
136
137 // Member functions:
138
140 [[nodiscard]] Index npages() const { return mpr.mextent; }
141
143 [[nodiscard]] Index nrows() const { return mrr.mextent; }
144
146 [[nodiscard]] Index ncols() const { return mcr.mextent; }
147
148 // Total size
149 [[nodiscard]] Index size() const noexcept {
150 return npages() * nrows() * ncols();
151 }
152 [[nodiscard]] bool empty() const noexcept { return size() == 0; }
153
155 [[nodiscard]] Shape<3> shape() const { return {npages(), nrows(), ncols()}; }
156
157 // Const index operators:
159 const Range& r,
160 const Range& c) const;
161
162 ConstMatrixView operator()(const Range& p, const Range& r, Index c) const;
163 ConstMatrixView operator()(const Range& p, Index r, const Range& c) const;
164 ConstMatrixView operator()(Index p, const Range& r, const Range& c) const;
165
166 ConstVectorView operator()(Index p, Index r, const Range& c) const;
167 ConstVectorView operator()(Index p, const Range& r, Index c) const;
168 ConstVectorView operator()(const Range& p, Index r, Index c) const;
169
172 Index r,
173 Index c) const { // Check if indices are valid:
174 ARTS_ASSERT(0 <= p);
175 ARTS_ASSERT(0 <= r);
176 ARTS_ASSERT(0 <= c);
180
181 return get(p, r, c);
182 }
183
185 [[nodiscard]] Numeric get(Index p, Index r, Index c) const {
186 return *(mdata + mpr.mstart + p * mpr.mstride + mrr.mstart +
187 r * mrr.mstride + mcr.mstart + c * mcr.mstride);
188 }
189
190 // Functions returning iterators:
191 [[nodiscard]] ConstIterator3D begin() const;
192 [[nodiscard]] ConstIterator3D end() const;
193
195 virtual ~ConstTensor3View() = default;
196
197 // Friends:
198 friend class Tensor3View;
199 friend class ConstIterator4D;
200 friend class ConstTensor4View;
201 friend class ConstTensor5View;
202 friend class ConstTensor6View;
203 friend class ConstTensor7View;
204
205 friend std::ostream& operator<<(std::ostream& os, const ConstTensor3View& v);
206
207 // Special constructor to make a Tensor3 view of a matrix.
209
210 protected:
211 // Constructors:
212 ConstTensor3View() = default;
214 const Range& p,
215 const Range& r,
216 const Range& c);
218 const Range& pp,
219 const Range& pr,
220 const Range& pc,
221 const Range& np,
222 const Range& nr,
223 const Range& nc);
224
225 // Data members:
226 // -------------
228 Range mpr{0, 0, 1};
230 Range mrr{0, 0, 1};
232 Range mcr{0, 0, 1};
234 Numeric* mdata{nullptr};
235};
236
247 public:
248 // Make const methods visible from base class
251 using ConstTensor3View::operator();
253
254 constexpr Tensor3View(const Tensor3View&) = default;
255
256 // Non-const index operators:
257
258 Tensor3View operator()(const Range& p, const Range& r, const Range& c);
259
260 MatrixView operator()(const Range& p, const Range& r, Index c);
261 MatrixView operator()(const Range& p, Index r, const Range& c);
262 MatrixView operator()(Index p, const Range& r, const Range& c);
263
264 VectorView operator()(Index p, Index r, const Range& c);
265 VectorView operator()(Index p, const Range& r, Index c);
266 VectorView operator()(const Range& p, Index r, Index c);
267
270 // Check if indices are valid:
271 ARTS_ASSERT(0 <= p);
272 ARTS_ASSERT(0 <= r);
273 ARTS_ASSERT(0 <= c);
277
278 return get(p, r, c);
279 }
280
283 return *(mdata + mpr.mstart + p * mpr.mstride + mrr.mstart +
284 r * mrr.mstride + mcr.mstart + c * mcr.mstride);
285 }
286
287 // Conversion to a plain C-array
288 [[nodiscard]] const Numeric* get_c_array() const ARTS_NOEXCEPT;
290
291 // Functions returning iterators:
293 Iterator3D end();
294
295 // Assignment operators:
296 Tensor3View& operator=(const ConstTensor3View& v);
297 Tensor3View& operator=(const Tensor3View& v);
298 Tensor3View& operator=(const Tensor3& v);
299 Tensor3View& operator=(Numeric x);
300
301 // Other operators:
302 Tensor3View& operator*=(Numeric x);
303 Tensor3View& operator/=(Numeric x);
304 Tensor3View& operator+=(Numeric x);
305 Tensor3View& operator-=(Numeric x);
306
307 Tensor3View& operator*=(const ConstTensor3View& x);
308 Tensor3View& operator/=(const ConstTensor3View& x);
309 Tensor3View& operator+=(const ConstTensor3View& x);
310 Tensor3View& operator-=(const ConstTensor3View& x);
311
313 virtual ~Tensor3View() = default;
314
315 // Friends:
316 friend class Iterator4D;
317 friend class Tensor4View;
318 friend class Tensor5View;
319 friend class Tensor6View;
320 friend class Tensor7View;
321
322 // Special constructor to make a Tensor3 view of a matrix.
323 Tensor3View(const MatrixView& a);
324
325 protected:
326 // Constructors:
327 Tensor3View() = default;
328 Tensor3View(Numeric* data, const Range& p, const Range& r, const Range& c);
329 Tensor3View(Numeric* data,
330 const Range& pp,
331 const Range& pr,
332 const Range& pc,
333 const Range& np,
334 const Range& nr,
335 const Range& nc);
336};
337
346class Tensor3 : public Tensor3View {
347 public:
348 // Constructors:
349 Tensor3() = default;
350 Tensor3(Index p, Index r, Index c);
351 Tensor3(Index p, Index r, Index c, Numeric fill);
352 Tensor3(const ConstTensor3View& v);
353 Tensor3(const Tensor3& v);
354 Tensor3(Tensor3&& v) noexcept : Tensor3View(std::forward<Tensor3View>(v)) {
355 v.mdata = nullptr;
356 }
357
368 Tensor3(Numeric* d, const Range& r0, const Range& r1, const Range& r2)
369 ARTS_NOEXCEPT : Tensor3View(d, r0, r1, r2) {
370 ARTS_ASSERT(not(r0.get_extent() < 0), "Must have size");
371 ARTS_ASSERT(not(r1.get_extent() < 0), "Must have size");
372 ARTS_ASSERT(not(r2.get_extent() < 0), "Must have size");
373 }
374
375 // Assignment operators:
376 Tensor3& operator=(const Tensor3& x);
377 Tensor3& operator=(Tensor3&& x) noexcept;
379
380 // Resize function:
381 void resize(Index p, Index r, Index c);
382
383 // Swap function:
384 friend void swap(Tensor3& t1, Tensor3& t2);
385
386 // Destructor:
387 virtual ~Tensor3();
388
390 template <std::size_t dim0>
392 static_assert(dim0 < 3, "Bad Dimension, Out-of-Bounds");
393
394 Range r0(0, dim0 == 0 ? npages() : dim0 == 1 ? nrows() : ncols());
395
396 Vector out(mdata, r0);
397 ARTS_ASSERT(size() == out.size(),
398 "Can only reduce size on same size input");
399 mdata = nullptr;
400 return out;
401 }
402
404 template <std::size_t dim0, std::size_t dim1>
406 static_assert(dim1 < 3, "Bad Dimension, Out-of-Bounds");
407 static_assert(dim0 < dim1, "Bad Dimensions, dim1 must be larger than dim0");
408
409 const Range r1(0, dim1 == 1 ? nrows() : ncols());
410 const Range r0(0, dim0 == 0 ? npages() : nrows(), r1.get_extent());
411
412 Matrix out(mdata, r0, r1);
413 ARTS_ASSERT(size() == out.size(),
414 "Can only reduce size on same size input");
415 mdata = nullptr;
416 return out;
417 }
418
419 template <class F>
420 void transform_elementwise(F&& func) {
421 std::transform(mdata, mdata + size(), mdata, func);
422 }
423};
424
425// Function declarations:
426// ----------------------
427
428void copy(ConstIterator3D origin,
429 const ConstIterator3D& end,
430 Iterator3D target);
431
432void copy(Numeric x, Iterator3D target, const Iterator3D& end);
433
434void transform(Tensor3View y, double (&my_func)(double), ConstTensor3View x);
435
437
439
441// Helper function for debugging
442#ifndef NDEBUG
443
445
446#endif
448
449void mult(Tensor3View A, const ConstVectorView B, const ConstMatrixView C);
450
453
455
456#endif // matpackIII_h
This can be used to make arrays out of anything.
Definition: array.h:48
Const version of Iterator3D.
Definition: matpackIII.h:75
ConstMatrixView msv
Current position.
Definition: matpackIII.h:110
ConstIterator3D & operator++()
Prefix increment operator.
Definition: matpackIII.h:88
Index mstride
Stride.
Definition: matpackIII.h:112
ConstIterator3D(const ConstMatrixView &x, Index stride)
Explicit constructor.
Definition: matpackIII.h:82
const ConstMatrixView * operator->() const
The -> operator is needed, so that we can write i->begin() to get the 1D iterators.
Definition: matpackIII.h:103
const ConstMatrixView & operator*() const
Dereferencing.
Definition: matpackIII.h:106
bool operator!=(const ConstIterator3D &other) const
Not equal operator, needed for algorithms like copy.
Definition: matpackIII.h:94
ConstIterator3D()=default
Default constructor.
Const version of Iterator4D.
Definition: matpackIV.h:76
A constant view of a Matrix.
Definition: matpackI.h:1043
Range mrr
The row range of mdata that is actually used.
Definition: matpackI.h:1148
Index size() const noexcept
Definition: matpackI.h:1061
Numeric * mdata
Pointer to the plain C array that holds the data.
Definition: matpackI.h:1152
Range mcr
The column range of mdata that is actually used.
Definition: matpackI.h:1150
A constant view of a Tensor3.
Definition: matpackIII.h:130
ConstTensor3View operator()(const Range &p, const Range &r, const Range &c) const
Const index operator for subrange.
Definition: matpackIII.cc:36
Numeric get(Index p, Index r, Index c) const
Get element implementation without assertions.
Definition: matpackIII.h:185
bool empty() const noexcept
Definition: matpackIII.h:152
virtual ~ConstTensor3View()=default
Destructor.
Shape< 3 > shape() const
Definition: matpackIII.h:155
Index npages() const
Returns the number of pages.
Definition: matpackIII.h:140
ConstIterator3D begin() const
Return const iterator to first page.
Definition: matpackIII.cc:130
Numeric * mdata
Pointer to the plain C array that holds the data.
Definition: matpackIII.h:234
ConstTensor3View & operator=(const ConstTensor3View &)=default
ConstIterator3D end() const
Return const iterator behind last page.
Definition: matpackIII.cc:136
ConstTensor3View()=default
Numeric operator()(Index p, Index r, Index c) const
Plain const index operator.
Definition: matpackIII.h:171
Range mrr
The row range of mdata that is actually used.
Definition: matpackIII.h:230
constexpr ConstTensor3View(ConstTensor3View &&)=default
Range mcr
The column range of mdata that is actually used.
Definition: matpackIII.h:232
Index size() const noexcept
Definition: matpackIII.h:149
friend std::ostream & operator<<(std::ostream &os, const ConstTensor3View &v)
Output operator.
Definition: matpackIII.cc:185
Index nrows() const
Returns the number of rows.
Definition: matpackIII.h:143
Range mpr
The page range of mdata that is actually used.
Definition: matpackIII.h:228
Index ncols() const
Returns the number of columns.
Definition: matpackIII.h:146
ConstTensor3View & operator=(ConstTensor3View &&)=default
constexpr ConstTensor3View(const ConstTensor3View &)=default
A constant view of a Tensor4.
Definition: matpackIV.h:131
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 3.
Definition: matpackIII.h:34
Iterator3D & operator++()
Prefix increment operator.
Definition: matpackIII.h:47
Iterator3D(const MatrixView &x, Index stride)
Explicit constructor.
Definition: matpackIII.h:41
bool operator!=(const Iterator3D &other) const
Not equal operator, needed for algorithms like copy.
Definition: matpackIII.h:53
MatrixView & operator*()
Dereferencing.
Definition: matpackIII.h:65
MatrixView msv
Current position.
Definition: matpackIII.h:69
Iterator3D()=default
Default constructor.
MatrixView * operator->()
The -> operator is needed, so that we can write i->begin() to get the 1D iterators.
Definition: matpackIII.h:62
Index mstride
Stride.
Definition: matpackIII.h:71
Implementation of Tensors of Rank 4.
Definition: matpackIV.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
Iterator3D begin()
Return iterator to first page.
Definition: matpackIII.cc:329
constexpr Tensor3View(const Tensor3View &)=default
Tensor3View & operator=(const ConstTensor3View &v)
Assignment operator.
Definition: matpackIII.cc:344
Numeric & operator()(Index p, Index r, Index c)
Plain non-const index operator.
Definition: matpackIII.h:269
const Numeric * get_c_array() const ARTS_NOEXCEPT
Conversion to plain C-array.
Definition: matpackIII.cc:315
Tensor3View operator()(const Range &p, const Range &r, const Range &c)
Index operator for subrange.
Definition: matpackIII.cc:209
Tensor3View()=default
Numeric & get(Index p, Index r, Index c)
Get element implementation without assertions.
Definition: matpackIII.h:282
Iterator3D end()
Return iterator behind last page.
Definition: matpackIII.cc:334
The Tensor3 class.
Definition: matpackIII.h:346
Matrix reduce_rank() &&ARTS_NOEXCEPT
Definition: matpackIII.h:405
Tensor3(Tensor3 &&v) noexcept
Definition: matpackIII.h:354
Tensor3(Numeric *d, const Range &r0, const Range &r1, const Range &r2) ARTS_NOEXCEPT
Definition: matpackIII.h:368
void transform_elementwise(F &&func)
Definition: matpackIII.h:420
Vector reduce_rank() &&ARTS_NOEXCEPT
Definition: matpackIII.h:391
Tensor3()=default
The Tensor4View class.
Definition: matpackIV.h:292
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 mult(Tensor3View A, const ConstVectorView B, const ConstMatrixView C)
mult Tensor3
Definition: matpackIII.cc:810
Numeric debug_tensor3view_get_elem(Tensor3View &tv, Index p, Index r, Index c)
Helper function to access tensor elements.
Definition: matpackIII.cc:787
Numeric min(const ConstTensor3View &x)
Min function, tensor version.
Definition: matpackIII.cc:751
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:715
void copy(ConstIterator3D origin, const ConstIterator3D &end, Iterator3D target)
Copy data between begin and end to target.
Definition: matpackIII.cc:535
Numeric max(const ConstTensor3View &x)
Max function, tensor version.
Definition: matpackIII.cc:732
Implementation of Matrix, Vector, and such stuff.
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