ARTS 2.5.0 (git: 9ee3ac6c)
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 else
58 return false;
59 }
60
63 MatrixView* operator->() { return &msv; }
64
66 MatrixView& operator*() { return msv; }
67
68 private:
73};
74
77 public:
78 // Constructors:
80 ConstIterator3D() = default;
81
84 : msv(x), mstride(stride) { /* Nothing to do here. */
85 }
86
87 // Operators:
90 msv.mdata += mstride;
91 return *this;
92 }
93
95 bool operator!=(const ConstIterator3D& other) const {
96 if (msv.mdata + msv.mrr.mstart + msv.mcr.mstart !=
97 other.msv.mdata + other.msv.mrr.mstart + other.msv.mcr.mstart)
98 return true;
99 else
100 return false;
101 }
102
105 const ConstMatrixView* operator->() const { return &msv; }
106
108 const ConstMatrixView& operator*() const { return msv; }
109
110 private:
115};
116
117// Declare class Tensor3:
118class Tensor3;
119
133 public:
134 constexpr ConstTensor3View(const ConstTensor3View&) = default;
135 constexpr ConstTensor3View(ConstTensor3View&&) = default;
138
139 // Member functions:
140
141 bool empty() const;
142
144 Index npages() const { return mpr.mextent; }
145
147 Index nrows() const { return mrr.mextent; }
148
150 Index ncols() const { return mcr.mextent; }
151
152 // Const index operators:
154 const Range& r,
155 const Range& c) const;
156
157 ConstMatrixView operator()(const Range& p, const Range& r, Index c) const;
158 ConstMatrixView operator()(const Range& p, Index r, const Range& c) const;
159 ConstMatrixView operator()(Index p, const Range& r, const Range& c) const;
160
161 ConstVectorView operator()(Index p, Index r, const Range& c) const;
162 ConstVectorView operator()(Index p, const Range& r, Index c) const;
163 ConstVectorView operator()(const Range& p, Index r, Index c) const;
164
167 Index r,
168 Index c) const { // Check if indices are valid:
169 ARTS_ASSERT(0 <= p);
170 ARTS_ASSERT(0 <= r);
171 ARTS_ASSERT(0 <= c);
175
176 return get(p, r, c);
177 }
178
180 Numeric get(Index p, Index r, Index c) const {
181 return *(mdata + mpr.mstart + p * mpr.mstride + mrr.mstart +
182 r * mrr.mstride + mcr.mstart + c * mcr.mstride);
183 }
184
185 // Functions returning iterators:
186 ConstIterator3D begin() const;
187 ConstIterator3D end() const;
188
190 virtual ~ConstTensor3View() = default;
191
192 // Friends:
193 friend class Tensor3View;
194 friend class ConstIterator4D;
195 friend class ConstTensor4View;
196 friend class ConstTensor5View;
197 friend class ConstTensor6View;
198 friend class ConstTensor7View;
199
200 // Special constructor to make a Tensor3 view of a matrix.
202
203 protected:
204 // Constructors:
205 ConstTensor3View() = default;
207 const Range& p,
208 const Range& r,
209 const Range& c);
211 const Range& pp,
212 const Range& pr,
213 const Range& pc,
214 const Range& np,
215 const Range& nr,
216 const Range& nc);
217
218 // Data members:
219 // -------------
221 Range mpr{0, 0, 1};
223 Range mrr{0, 0, 1};
225 Range mcr{0, 0, 1};
227 Numeric* mdata{nullptr};
228};
229
240 public:
241 // Make const methods visible from base class
244 using ConstTensor3View::operator();
246
247 constexpr Tensor3View(const Tensor3View&) = default;
248
249 // Non-const index operators:
250
251 Tensor3View operator()(const Range& p, const Range& r, const Range& c);
252
253 MatrixView operator()(const Range& p, const Range& r, Index c);
254 MatrixView operator()(const Range& p, Index r, const Range& c);
255 MatrixView operator()(Index p, const Range& r, const Range& c);
256
257 VectorView operator()(Index p, Index r, const Range& c);
258 VectorView operator()(Index p, const Range& r, Index c);
259 VectorView operator()(const Range& p, Index r, Index c);
260
263 // Check if indices are valid:
264 ARTS_ASSERT(0 <= p);
265 ARTS_ASSERT(0 <= r);
266 ARTS_ASSERT(0 <= c);
270
271 return get(p, r, c);
272 }
273
276 return *(mdata + mpr.mstart + p * mpr.mstride + mrr.mstart +
277 r * mrr.mstride + mcr.mstart + c * mcr.mstride);
278 }
279
280 // Conversion to a plain C-array
281 const Numeric* get_c_array() const ARTS_NOEXCEPT;
283
284 // Functions returning iterators:
286 Iterator3D end();
287
288 // Assignment operators:
289 Tensor3View& operator=(const ConstTensor3View& v);
290 Tensor3View& operator=(const Tensor3View& v);
291 Tensor3View& operator=(const Tensor3& v);
292 Tensor3View& operator=(Numeric x);
293
294 // Other operators:
295 Tensor3View& operator*=(Numeric x);
296 Tensor3View& operator/=(Numeric x);
297 Tensor3View& operator+=(Numeric x);
298 Tensor3View& operator-=(Numeric x);
299
300 Tensor3View& operator*=(const ConstTensor3View& x);
301 Tensor3View& operator/=(const ConstTensor3View& x);
302 Tensor3View& operator+=(const ConstTensor3View& x);
303 Tensor3View& operator-=(const ConstTensor3View& x);
304
306 virtual ~Tensor3View() = default;
307
308 // Friends:
309 friend class Iterator4D;
310 friend class Tensor4View;
311 friend class Tensor5View;
312 friend class Tensor6View;
313 friend class Tensor7View;
314
315 // Special constructor to make a Tensor3 view of a matrix.
316 Tensor3View(const MatrixView& a);
317
318 protected:
319 // Constructors:
320 Tensor3View() = default;
321 Tensor3View(Numeric* data, const Range& p, const Range& r, const Range& c);
323 const Range& pp,
324 const Range& pr,
325 const Range& pc,
326 const Range& np,
327 const Range& nr,
328 const Range& nc);
329};
330
339class Tensor3 : public Tensor3View {
340 public:
341 // Constructors:
342 Tensor3() = default;
343 Tensor3(Index p, Index r, Index c);
344 Tensor3(Index p, Index r, Index c, Numeric fill);
345 Tensor3(const ConstTensor3View& v);
346 Tensor3(const Tensor3& v);
347 Tensor3(Tensor3&& v) noexcept : Tensor3View(std::forward<Tensor3View>(v)) {
348 v.mdata = nullptr;
349 }
350
361 Tensor3(Numeric* d, const Range& r0, const Range& r1, const Range& r2) ARTS_NOEXCEPT
362 : Tensor3View(d, r0, r1, r2) {
363 ARTS_ASSERT (not (r0.get_extent() < 0), "Must have size");
364 ARTS_ASSERT (not (r1.get_extent() < 0), "Must have size");
365 ARTS_ASSERT (not (r2.get_extent() < 0), "Must have size");
366 }
367
368 // Assignment operators:
369 Tensor3& operator=(const Tensor3& x);
370 Tensor3& operator=(Tensor3&& x) noexcept;
372
373 // Resize function:
374 void resize(Index p, Index r, Index c);
375
376 // Swap function:
377 friend void swap(Tensor3& t1, Tensor3& t2);
378
379 // Destructor:
380 virtual ~Tensor3();
381
382 // Total size
383 Index size() const noexcept {return npages() * nrows() * ncols();}
384
386 template <std::size_t dim0>
388 static_assert(dim0 < 3, "Bad Dimension, Out-of-Bounds");
389
390 Range r0(0, dim0 == 0 ? npages() : dim0 == 1 ? nrows() : ncols());
391
392 Vector out(mdata, r0);
393 ARTS_ASSERT (size() == out.size(), "Can only reduce size on same size input");
394 mdata = nullptr;
395 return out;
396 }
397
399 template <std::size_t dim0, std::size_t dim1>
401 static_assert(dim1 < 3, "Bad Dimension, Out-of-Bounds");
402 static_assert(dim0 < dim1, "Bad Dimensions, dim1 must be larger than dim0");
403
404 const Range r1(0, dim1 == 1 ? nrows() : ncols());
405 const Range r0(0, dim0 == 0 ? npages() : nrows(), r1.get_extent());
406
407 Matrix out(mdata, r0, r1);
408 ARTS_ASSERT (size() == out.size(), "Can only reduce size on same size input");
409 mdata = nullptr;
410 return out;
411 }
412};
413
414// Function declarations:
415// ----------------------
416
417void copy(ConstIterator3D origin,
418 const ConstIterator3D& end,
419 Iterator3D target);
420
421void copy(Numeric x, Iterator3D target, const Iterator3D& end);
422
423void transform(Tensor3View y, double (&my_func)(double), ConstTensor3View x);
424
426
428
429std::ostream& operator<<(std::ostream& os, const ConstTensor3View& v);
430
432// Helper function for debugging
433#ifndef NDEBUG
434
436
437#endif
439
440void mult(Tensor3View A, const ConstVectorView B, const ConstMatrixView C);
441
442#endif // matpackIII_h
void * data
Const version of Iterator3D.
Definition: matpackIII.h:76
ConstMatrixView msv
Current position.
Definition: matpackIII.h:112
ConstIterator3D & operator++()
Prefix increment operator.
Definition: matpackIII.h:89
Index mstride
Stride.
Definition: matpackIII.h:114
ConstIterator3D(const ConstMatrixView &x, Index stride)
Explicit constructor.
Definition: matpackIII.h:83
const ConstMatrixView * operator->() const
The -> operator is needed, so that we can write i->begin() to get the 1D iterators.
Definition: matpackIII.h:105
const ConstMatrixView & operator*() const
Dereferencing.
Definition: matpackIII.h:108
bool operator!=(const ConstIterator3D &other) const
Not equal operator, needed for algorithms like copy.
Definition: matpackIII.h:95
ConstIterator3D()=default
Default constructor.
Const version of Iterator4D.
Definition: matpackIV.h:77
A constant view of a Matrix.
Definition: matpackI.h:1014
Range mrr
The row range of mdata that is actually used.
Definition: matpackI.h:1109
Numeric * mdata
Pointer to the plain C array that holds the data.
Definition: matpackI.h:1113
Range mcr
The column range of mdata that is actually used.
Definition: matpackI.h:1111
A constant view of a Tensor3.
Definition: matpackIII.h:132
ConstTensor3View operator()(const Range &p, const Range &r, const Range &c) const
Const index operator for subrange.
Definition: matpackIII.cc:43
Numeric get(Index p, Index r, Index c) const
Get element implementation without assertions.
Definition: matpackIII.h:180
virtual ~ConstTensor3View()=default
Destructor.
Index npages() const
Returns the number of pages.
Definition: matpackIII.h:144
ConstIterator3D begin() const
Return const iterator to first page.
Definition: matpackIII.cc:137
Numeric * mdata
Pointer to the plain C array that holds the data.
Definition: matpackIII.h:227
ConstTensor3View & operator=(const ConstTensor3View &)=default
ConstIterator3D end() const
Return const iterator behind last page.
Definition: matpackIII.cc:143
ConstTensor3View()=default
Numeric operator()(Index p, Index r, Index c) const
Plain const index operator.
Definition: matpackIII.h:166
Range mrr
The row range of mdata that is actually used.
Definition: matpackIII.h:223
constexpr ConstTensor3View(ConstTensor3View &&)=default
Range mcr
The column range of mdata that is actually used.
Definition: matpackIII.h:225
Index nrows() const
Returns the number of rows.
Definition: matpackIII.h:147
Range mpr
The page range of mdata that is actually used.
Definition: matpackIII.h:221
Index ncols() const
Returns the number of columns.
Definition: matpackIII.h:150
bool empty() const
Check if variable is empty.
Definition: matpackIII.cc:36
ConstTensor3View & operator=(ConstTensor3View &&)=default
constexpr ConstTensor3View(const ConstTensor3View &)=default
A constant view of a Tensor4.
Definition: matpackIV.h:133
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 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:66
MatrixView msv
Current position.
Definition: matpackIII.h:70
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:63
Index mstride
Stride.
Definition: matpackIII.h:72
Implementation of Tensors of Rank 4.
Definition: matpackIV.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
Iterator3D begin()
Return iterator to first page.
Definition: matpackIII.cc:326
constexpr Tensor3View(const Tensor3View &)=default
Tensor3View & operator=(const ConstTensor3View &v)
Assignment operator.
Definition: matpackIII.cc:341
Numeric & operator()(Index p, Index r, Index c)
Plain non-const index operator.
Definition: matpackIII.h:262
const Numeric * get_c_array() const ARTS_NOEXCEPT
Conversion to plain C-array.
Definition: matpackIII.cc:317
Tensor3View operator()(const Range &p, const Range &r, const Range &c)
Index operator for subrange.
Definition: matpackIII.cc:216
Tensor3View()=default
Numeric & get(Index p, Index r, Index c)
Get element implementation without assertions.
Definition: matpackIII.h:275
Iterator3D end()
Return iterator behind last page.
Definition: matpackIII.cc:331
The Tensor3 class.
Definition: matpackIII.h:339
Matrix reduce_rank() &&ARTS_NOEXCEPT
Definition: matpackIII.h:400
Tensor3(Tensor3 &&v) noexcept
Definition: matpackIII.h:347
Tensor3(Numeric *d, const Range &r0, const Range &r1, const Range &r2) ARTS_NOEXCEPT
Definition: matpackIII.h:361
Vector reduce_rank() &&ARTS_NOEXCEPT
Definition: matpackIII.h:387
Tensor3()=default
Index size() const noexcept
Definition: matpackIII.h:383
The Tensor4View class.
Definition: matpackIV.h:284
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 mult(Tensor3View A, const ConstVectorView B, const ConstMatrixView C)
mult Tensor3
Definition: matpackIII.cc:807
Numeric debug_tensor3view_get_elem(Tensor3View &tv, Index p, Index r, Index c)
Helper function to access tensor elements.
Definition: matpackIII.cc:784
Numeric min(const ConstTensor3View &x)
Min function, tensor version.
Definition: matpackIII.cc:748
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:712
void copy(ConstIterator3D origin, const ConstIterator3D &end, Iterator3D target)
Copy data between begin and end to target.
Definition: matpackIII.cc:532
std::ostream & operator<<(std::ostream &os, const ConstTensor3View &v)
Output operator.
Definition: matpackIII.cc:192
Numeric max(const ConstTensor3View &x)
Max function, tensor version.
Definition: matpackIII.cc:729
Implementation of Matrix, Vector, and such stuff.
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