ARTS 2.5.10 (git: 2f1c442c)
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 <utility>
31
32#include "matpackI.h"
33#include "matpack_concepts.h"
34
38 public:
39 // Constructors:
41 Iterator3D() = default;
42
44 Iterator3D(const MatrixView& x, Index stride)
45 : msv(x), mstride(stride) { /* Nothing to do here. */
46 }
47
48 // Operators:
51 msv.mdata += mstride;
52 return *this;
53 }
54
56 bool operator!=(const Iterator3D& other) const {
57 if (msv.mdata + msv.mrr.mstart + msv.mcr.mstart !=
58 other.msv.mdata + other.msv.mrr.mstart + other.msv.mcr.mstart)
59 return true;
60 return false;
61 }
62
65 MatrixView* operator->() { return &msv; }
66
68 MatrixView& operator*() { return msv; }
69
70 private:
75};
76
79 public:
80 // Constructors:
82 ConstIterator3D() = default;
83
86 : msv(std::move(x)), mstride(stride) { /* Nothing to do here. */
87 }
88
89 // Operators:
92 msv.mdata += mstride;
93 return *this;
94 }
95
97 bool operator!=(const ConstIterator3D& other) const {
98 if (msv.mdata + msv.mrr.mstart + msv.mcr.mstart !=
99 other.msv.mdata + other.msv.mrr.mstart + other.msv.mcr.mstart)
100 return true;
101 return false;
102 }
103
106 const ConstMatrixView* operator->() const { return &msv; }
107
109 const ConstMatrixView& operator*() const { return msv; }
110
111 private:
116};
117
118// Declare class Tensor3:
119class Tensor3;
120
134 public:
135 static constexpr bool matpack_type{true};
136
137 constexpr ConstTensor3View(const ConstTensor3View&) = default;
138 constexpr ConstTensor3View(ConstTensor3View&&) = default;
141
142 // Member functions:
143
145 [[nodiscard]] Index npages() const { return mpr.mextent; }
146
148 [[nodiscard]] Index nrows() const { return mrr.mextent; }
149
151 [[nodiscard]] Index ncols() const { return mcr.mextent; }
152
153 // Total size
154 [[nodiscard]] Index size() const noexcept {
155 return npages() * nrows() * ncols();
156 }
157 [[nodiscard]] bool empty() const noexcept { return size() == 0; }
158
160 [[nodiscard]] Shape<3> shape() const { return {npages(), nrows(), ncols()}; }
161
162 // Const index operators:
164 const Range& r,
165 const Range& c) const;
166
167 ConstMatrixView operator()(const Range& p, const Range& r, Index c) const;
168 ConstMatrixView operator()(const Range& p, Index r, const Range& c) const;
169 ConstMatrixView operator()(Index p, const Range& r, const Range& c) const;
170
171 ConstVectorView operator()(Index p, Index r, const Range& c) const;
172 ConstVectorView operator()(Index p, const Range& r, Index c) const;
173 ConstVectorView operator()(const Range& p, Index r, Index c) const;
174
177 Index r,
178 Index c) const { // Check if indices are valid:
179 ARTS_ASSERT(0 <= p);
180 ARTS_ASSERT(0 <= r);
181 ARTS_ASSERT(0 <= c);
185
186 return get(p, r, c);
187 }
188
190 [[nodiscard]] Numeric get(Index p, Index r, Index c) const {
191 return *(mdata + mpr.mstart + p * mpr.mstride + mrr.mstart +
192 r * mrr.mstride + mcr.mstart + c * mcr.mstride);
193 }
194
195 // Functions returning iterators:
196 [[nodiscard]] ConstIterator3D begin() const;
197 [[nodiscard]] ConstIterator3D end() const;
198
200 virtual ~ConstTensor3View() = default;
201
202 // Friends:
203 friend class Tensor3View;
204 friend class ConstIterator4D;
205 friend class ConstTensor4View;
206 friend class ConstTensor5View;
207 friend class ConstTensor6View;
208 friend class ConstTensor7View;
209
210 friend std::ostream& operator<<(std::ostream& os, const ConstTensor3View& v);
211
212 // Special constructor to make a Tensor3 view of a matrix.
214
215 protected:
216 // Constructors:
217 ConstTensor3View() = default;
219 const Range& p,
220 const Range& r,
221 const Range& c);
223 const Range& pp,
224 const Range& pr,
225 const Range& pc,
226 const Range& np,
227 const Range& nr,
228 const Range& nc);
229
230 // Data members:
231 // -------------
233 Range mpr{0, 0, 1};
235 Range mrr{0, 0, 1};
237 Range mcr{0, 0, 1};
239 Numeric* mdata{nullptr};
240};
241
252 public:
253 // Make const methods visible from base class
256 using ConstTensor3View::operator();
258
259 constexpr Tensor3View(const Tensor3View&) = default;
260
261 // Non-const index operators:
262
263 Tensor3View operator()(const Range& p, const Range& r, const Range& c);
264
265 MatrixView operator()(const Range& p, const Range& r, Index c);
266 MatrixView operator()(const Range& p, Index r, const Range& c);
267 MatrixView operator()(Index p, const Range& r, const Range& c);
268
269 VectorView operator()(Index p, Index r, const Range& c);
270 VectorView operator()(Index p, const Range& r, Index c);
271 VectorView operator()(const Range& p, Index r, Index c);
272
273#define GETFUN(p, r, c) \
274 *(mdata + mpr.mstart + p * mpr.mstride + mrr.mstart + r * mrr.mstride + \
275 mcr.mstart + c * mcr.mstride)
278 // Check if indices are valid:
279 ARTS_ASSERT(0 <= p);
280 ARTS_ASSERT(0 <= r);
281 ARTS_ASSERT(0 <= c);
285
286 return GETFUN(p, r, c);
287 }
288
290 Numeric& get(Index p, Index r, Index c) { return GETFUN(p, r, c); }
291#undef GETFUN
292
293 // Conversion to a plain C-array
294 [[nodiscard]] const Numeric* get_c_array() const ARTS_NOEXCEPT;
296
297 // Functions returning iterators:
299 Iterator3D end();
300
301 // Assignment operators:
302 Tensor3View& operator=(const ConstTensor3View& v);
303 Tensor3View& operator=(const Tensor3View& v);
304 Tensor3View& operator=(const Tensor3& v);
305 Tensor3View& operator=(Numeric x);
306
307 // Other operators:
308 Tensor3View& operator*=(Numeric x);
309 Tensor3View& operator/=(Numeric x);
310 Tensor3View& operator+=(Numeric x);
311 Tensor3View& operator-=(Numeric x);
312
313 Tensor3View& operator*=(const ConstTensor3View& x);
314 Tensor3View& operator/=(const ConstTensor3View& x);
315 Tensor3View& operator+=(const ConstTensor3View& x);
316 Tensor3View& operator-=(const ConstTensor3View& x);
317
319 ~Tensor3View() override = default;
320
321 // Friends:
322 friend class Iterator4D;
323 friend class Tensor4View;
324 friend class Tensor5View;
325 friend class Tensor6View;
326 friend class Tensor7View;
327
328 // Special constructor to make a Tensor3 view of a matrix.
329 Tensor3View(const MatrixView& a);
330
331 protected:
332 // Constructors:
333 Tensor3View() = default;
334 Tensor3View(Numeric* data, const Range& p, const Range& r, const Range& c);
335 Tensor3View(Numeric* data,
336 const Range& pp,
337 const Range& pr,
338 const Range& pc,
339 const Range& np,
340 const Range& nr,
341 const Range& nc);
342};
343
352class Tensor3 : public Tensor3View {
353 public:
354 // Constructors:
355 Tensor3() = default;
356 Tensor3(Index p, Index r, Index c);
357 Tensor3(Index p, Index r, Index c, Numeric fill);
358 Tensor3(const ConstTensor3View& v);
359 Tensor3(const Tensor3& v);
360 Tensor3(Tensor3&& v) noexcept : Tensor3View(std::forward<Tensor3View>(v)) {
361 v.mdata = nullptr;
362 }
363
366 : Tensor3(matpack::page_size(init), matpack::row_size(init),
367 matpack::column_size(init)) {
368 *this = init;
369 }
370
373 if (const auto s = matpack::shape<Index, 3>(init); shape().data not_eq s)
374 resize(s[0], s[1], s[2]);
375
376 auto [I, J, K] = shape().data;
377 for (Index i = 0; i < I; i++)
378 for (Index j = 0; j < J; j++)
379 for (Index k = 0; k < K; k++)
380 operator()(i, j, k) = init(i, j, k);
381
382 return *this;
383 }
384
395 Tensor3(Numeric* d, const Range& r0, const Range& r1, const Range& r2)
396 ARTS_NOEXCEPT : Tensor3View(d, r0, r1, r2) {
397 ARTS_ASSERT(not(r0.get_extent() < 0), "Must have size");
398 ARTS_ASSERT(not(r1.get_extent() < 0), "Must have size");
399 ARTS_ASSERT(not(r2.get_extent() < 0), "Must have size");
400 }
401
402 // Assignment operators:
403 Tensor3& operator=(const Tensor3& x);
406
407 // Resize function:
408 void resize(Index p, Index r, Index c);
409
410 // Swap function:
411 friend void swap(Tensor3& t1, Tensor3& t2) noexcept;
412
413 // Destructor:
414 ~Tensor3() noexcept override;
415
417 template <std::size_t dim0>
418 Vector reduce_rank() && ARTS_NOEXCEPT {
419 static_assert(dim0 < 3, "Bad Dimension, Out-of-Bounds");
420
421 Range r0(0, dim0 == 0 ? npages() : dim0 == 1 ? nrows() : ncols());
422
423 Vector out(mdata, r0);
424 ARTS_ASSERT(size() == out.size(),
425 "Can only reduce size on same size input");
426 mdata = nullptr;
427 return out;
428 }
429
431 template <std::size_t dim0, std::size_t dim1>
433 static_assert(dim1 < 3, "Bad Dimension, Out-of-Bounds");
434 static_assert(dim0 < dim1, "Bad Dimensions, dim1 must be larger than dim0");
435
436 const Range r1(0, dim1 == 1 ? nrows() : ncols());
437 const Range r0(0, dim0 == 0 ? npages() : nrows(), r1.get_extent());
438
439 Matrix out(mdata, r0, r1);
440 ARTS_ASSERT(size() == out.size(),
441 "Can only reduce size on same size input");
442 mdata = nullptr;
443 return out;
444 }
445
446 template <class F>
447 void transform_elementwise(F&& func) {
448 std::transform(mdata, mdata + size(), mdata, func);
449 }
450};
451
452// Function declarations:
453// ----------------------
454
455void copy(ConstIterator3D origin,
456 const ConstIterator3D& end,
457 Iterator3D target);
458
459void copy(Numeric x, Iterator3D target, const Iterator3D& end);
460
461void transform(Tensor3View y, double (&my_func)(double), ConstTensor3View x);
462
464
466
468// Helper function for debugging
469#ifndef NDEBUG
470
472
473#endif
475
476void mult(Tensor3View A, const ConstVectorView B, const ConstMatrixView C);
477
480
482
483#endif // matpackIII_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 Iterator3D.
Definition: matpackIII.h:78
ConstMatrixView msv
Current position.
Definition: matpackIII.h:113
ConstIterator3D & operator++()
Prefix increment operator.
Definition: matpackIII.h:91
Index mstride
Stride.
Definition: matpackIII.h:115
const ConstMatrixView * operator->() const
The -> operator is needed, so that we can write i->begin() to get the 1D iterators.
Definition: matpackIII.h:106
const ConstMatrixView & operator*() const
Dereferencing.
Definition: matpackIII.h:109
ConstIterator3D(ConstMatrixView x, Index stride)
Explicit constructor.
Definition: matpackIII.h:85
bool operator!=(const ConstIterator3D &other) const
Not equal operator, needed for algorithms like copy.
Definition: matpackIII.h:97
ConstIterator3D()=default
Default constructor.
Const version of Iterator4D.
Definition: matpackIV.h:78
A constant view of a Matrix.
Definition: matpackI.h:1065
Range mrr
The row range of mdata that is actually used.
Definition: matpackI.h:1172
Index size() const noexcept
Definition: matpackI.h:1085
Numeric * mdata
Pointer to the plain C array that holds the data.
Definition: matpackI.h:1176
Range mcr
The column range of mdata that is actually used.
Definition: matpackI.h:1174
A constant view of a Tensor3.
Definition: matpackIII.h:133
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:190
static constexpr bool matpack_type
Definition: matpackIII.h:135
bool empty() const noexcept
Definition: matpackIII.h:157
virtual ~ConstTensor3View()=default
Destructor.
Shape< 3 > shape() const
Definition: matpackIII.h:160
Index npages() const
Returns the number of pages.
Definition: matpackIII.h:145
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:239
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:176
Range mrr
The row range of mdata that is actually used.
Definition: matpackIII.h:235
constexpr ConstTensor3View(ConstTensor3View &&)=default
Range mcr
The column range of mdata that is actually used.
Definition: matpackIII.h:237
Index size() const noexcept
Definition: matpackIII.h:154
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:148
Range mpr
The page range of mdata that is actually used.
Definition: matpackIII.h:233
Index ncols() const
Returns the number of columns.
Definition: matpackIII.h:151
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: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 3.
Definition: matpackIII.h:37
Iterator3D & operator++()
Prefix increment operator.
Definition: matpackIII.h:50
Iterator3D(const MatrixView &x, Index stride)
Explicit constructor.
Definition: matpackIII.h:44
bool operator!=(const Iterator3D &other) const
Not equal operator, needed for algorithms like copy.
Definition: matpackIII.h:56
MatrixView & operator*()
Dereferencing.
Definition: matpackIII.h:68
MatrixView msv
Current position.
Definition: matpackIII.h:72
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:65
Index mstride
Stride.
Definition: matpackIII.h:74
Implementation of Tensors of Rank 4.
Definition: matpackIV.h:40
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
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:277
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:290
Iterator3D end()
Return iterator behind last page.
Definition: matpackIII.cc:334
The Tensor3 class.
Definition: matpackIII.h:352
Matrix reduce_rank() &&ARTS_NOEXCEPT
Definition: matpackIII.h:432
Tensor3(Tensor3 &&v) noexcept
Definition: matpackIII.h:360
Tensor3(const matpack::tensor3_like_not_tensor3 auto &init)
Initialization from a tensor type.
Definition: matpackIII.h:365
Tensor3 & operator=(const matpack::tensor3_like_not_tensor3 auto &init)
Set from a tensor type.
Definition: matpackIII.h:372
Tensor3(Numeric *d, const Range &r0, const Range &r1, const Range &r2) ARTS_NOEXCEPT
Definition: matpackIII.h:395
void transform_elementwise(F &&func)
Definition: matpackIII.h:447
Tensor3()=default
The Tensor4View class.
Definition: matpackIV.h:296
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 mult(Tensor3View A, const ConstVectorView B, const ConstMatrixView C)
mult Tensor3
Definition: matpackIII.cc:811
Numeric debug_tensor3view_get_elem(Tensor3View &tv, Index p, Index r, Index c)
Helper function to access tensor elements.
Definition: matpackIII.cc:788
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:716
void copy(ConstIterator3D origin, const ConstIterator3D &end, Iterator3D target)
Copy data between begin and end to target.
Definition: matpackIII.cc:535
#define GETFUN(p, r, c)
Definition: matpackIII.h:273
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) noexcept
Swaps two objects.
Helper shape class.
Definition: matpackI.h:387
#define d
#define v
#define a
#define c