ARTS  2.4.0(git:4fb77825)
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 
34 class Iterator3D {
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:
118 class 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  assert(0 <= p);
170  assert(0 <= r);
171  assert(0 <= c);
172  assert(p < mpr.mextent);
173  assert(r < mrr.mextent);
174  assert(c < mcr.mextent);
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
243  using ConstTensor3View::end;
244  using ConstTensor3View::operator();
245  using ConstTensor3View::get;
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  assert(0 <= p);
265  assert(0 <= r);
266  assert(0 <= c);
267  assert(p < mpr.mextent);
268  assert(r < mrr.mextent);
269  assert(c < mcr.mextent);
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;
282  Numeric* get_c_array();
283 
284  // Functions returning iterators:
285  Iterator3D begin();
286  Iterator3D end();
287 
288  // Assignment operators:
290  Tensor3View& operator=(const Tensor3View& v);
291  Tensor3View& operator=(const Tensor3& v);
293 
294  // Other operators:
299 
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 
339 class 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 
351  // Assignment operators:
352  Tensor3& operator=(const Tensor3& x);
353  Tensor3& operator=(Tensor3&& x) noexcept;
355 
356  // Resize function:
357  void resize(Index p, Index r, Index c);
358 
359  // Swap function:
360  friend void swap(Tensor3& t1, Tensor3& t2);
361 
362  // Destructor:
363  virtual ~Tensor3();
364 };
365 
366 // Function declarations:
367 // ----------------------
368 
369 void copy(ConstIterator3D origin,
370  const ConstIterator3D& end,
371  Iterator3D target);
372 
373 void copy(Numeric x, Iterator3D target, const Iterator3D& end);
374 
375 void transform(Tensor3View y, double (&my_func)(double), ConstTensor3View x);
376 
377 Numeric max(const ConstTensor3View& x);
378 
379 Numeric min(const ConstTensor3View& x);
380 
381 std::ostream& operator<<(std::ostream& os, const ConstTensor3View& v);
382 
384 // Helper function for debugging
385 #ifndef NDEBUG
386 
388 
389 #endif
390 
392 void mult(Tensor3View A, const ConstVectorView B, const ConstMatrixView C);
393 
394 #endif // matpackIII_h
Iterator3D::operator!=
bool operator!=(const Iterator3D &other) const
Not equal operator, needed for algorithms like copy.
Definition: matpackIII.h:53
copy
void copy(ConstIterator3D origin, const ConstIterator3D &end, Iterator3D target)
Copy data between begin and end to target.
Definition: matpackIII.cc:538
Tensor7View
The Tensor7View class.
Definition: matpackVII.h:1286
MatrixView
The MatrixView class.
Definition: matpackI.h:1093
transform
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:718
Tensor3View::operator()
Numeric & operator()(Index p, Index r, Index c)
Plain non-const index operator.
Definition: matpackIII.h:262
ConstTensor3View::operator()
ConstTensor3View operator()(const Range &p, const Range &r, const Range &c) const
Const index operator for subrange.
Definition: matpackIII.cc:45
ConstIterator3D::mstride
Index mstride
Stride.
Definition: matpackIII.h:114
Tensor3
The Tensor3 class.
Definition: matpackIII.h:339
ConstTensor3View::ConstTensor3View
constexpr ConstTensor3View(const ConstTensor3View &)=default
ConstTensor7View
A constant view of a Tensor7.
Definition: matpackVII.h:147
Tensor3View::Tensor3View
constexpr Tensor3View(const Tensor3View &)=default
ConstIterator3D
Const version of Iterator3D.
Definition: matpackIII.h:76
ConstMatrixView::mdata
Numeric * mdata
Pointer to the plain C array that holds the data.
Definition: matpackI.h:1081
ARTS::Var::y
Vector y(Workspace &ws) noexcept
Definition: autoarts.h:7401
Tensor3View::operator()
Tensor3View operator()(const Range &p, const Range &r, const Range &c)
Index operator for subrange.
Definition: matpackIII.cc:218
data
G0 G2 FVC Y DV Numeric Numeric Numeric Zeeman LowerQuantumNumbers void * data
Definition: arts_api_classes.cc:232
ConstTensor3View::ConstTensor3View
constexpr ConstTensor3View(ConstTensor3View &&)=default
Tensor3::~Tensor3
virtual ~Tensor3()
Destructor for Tensor3.
Definition: matpackIII.cc:697
Range::mstart
Index mstart
The start index.
Definition: matpackI.h:346
ConstMatrixView::mcr
Range mcr
The column range of mdata that is actually used.
Definition: matpackI.h:1079
Tensor3View::end
Iterator3D end()
Return iterator behind last page.
Definition: matpackIII.cc:337
ConstIterator3D::operator*
const ConstMatrixView & operator*() const
Dereferencing.
Definition: matpackIII.h:108
Tensor3::resize
void resize(Index p, Index r, Index c)
Resize function.
Definition: matpackIII.cc:664
Tensor3View::operator-=
Tensor3View & operator-=(Numeric x)
Subtraction of scalar.
Definition: matpackIII.cc:425
ConstTensor3View::npages
Index npages() const
Returns the number of pages.
Definition: matpackIII.h:144
ConstMatrixView::mrr
Range mrr
The row range of mdata that is actually used.
Definition: matpackI.h:1077
min
Numeric min(const ConstTensor3View &x)
Min function, tensor version.
Definition: matpackIII.cc:754
ConstIterator4D
Const version of Iterator4D.
Definition: matpackIV.h:77
ConstTensor4View
A constant view of a Tensor4.
Definition: matpackIV.h:133
matpackI.h
Implementation of Matrix, Vector, and such stuff.
Tensor3View
The Tensor3View class.
Definition: matpackIII.h:239
Iterator3D
Implementation of Tensors of Rank 3.
Definition: matpackIII.h:34
Iterator3D::operator*
MatrixView & operator*()
Dereferencing.
Definition: matpackIII.h:66
ConstTensor3View::begin
ConstIterator3D begin() const
Return const iterator to first page.
Definition: matpackIII.cc:139
Tensor3View::Tensor3View
Tensor3View()=default
Tensor3View::begin
Iterator3D begin()
Return iterator to first page.
Definition: matpackIII.cc:332
ConstIterator3D::ConstIterator3D
ConstIterator3D()=default
Default constructor.
ConstTensor3View::mrr
Range mrr
The row range of mdata that is actually used.
Definition: matpackIII.h:223
ConstTensor3View::mcr
Range mcr
The column range of mdata that is actually used.
Definition: matpackIII.h:225
debug_tensor3view_get_elem
Numeric debug_tensor3view_get_elem(Tensor3View &tv, Index p, Index r, Index c)
Helper function to access tensor elements.
Definition: matpackIII.cc:790
VectorView
The VectorView class.
Definition: matpackI.h:610
Iterator3D::operator++
Iterator3D & operator++()
Prefix increment operator.
Definition: matpackIII.h:47
Iterator3D::Iterator3D
Iterator3D(const MatrixView &x, Index stride)
Explicit constructor.
Definition: matpackIII.h:41
ConstTensor3View::ConstTensor3View
ConstTensor3View()=default
Tensor3View::get
Numeric get(Index p, Index r, Index c) const
Get element implementation without assertions.
Definition: matpackIII.h:180
ConstTensor3View::mpr
Range mpr
The page range of mdata that is actually used.
Definition: matpackIII.h:221
ConstTensor6View
A constant view of a Tensor6.
Definition: matpackVI.h:149
Numeric
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
Tensor5View
The Tensor5View class.
Definition: matpackV.h:333
ConstTensor3View::mdata
Numeric * mdata
Pointer to the plain C array that holds the data.
Definition: matpackIII.h:227
Tensor3View::operator/=
Tensor3View & operator/=(Numeric x)
Division by scalar.
Definition: matpackIII.cc:407
Iterator3D::msv
MatrixView msv
Current position.
Definition: matpackIII.h:70
Zeeman::end
constexpr Rational end(Rational Ju, Rational Jl, Polarization type) noexcept
Gives the largest M for a polarization type of this transition.
Definition: zeemandata.h:108
ConstTensor3View::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackIII.h:147
ConstMatrixView
A constant view of a Matrix.
Definition: matpackI.h:982
Tensor3View::operator*=
Tensor3View & operator*=(Numeric x)
Multiplication by scalar.
Definition: matpackIII.cc:398
Iterator3D::mstride
Index mstride
Stride.
Definition: matpackIII.h:72
Tensor3::Tensor3
Tensor3()=default
ConstTensor3View::end
ConstIterator3D end() const
Return const iterator behind last page.
Definition: matpackIII.cc:145
ConstTensor3View::get
Numeric get(Index p, Index r, Index c) const
Get element implementation without assertions.
Definition: matpackIII.h:180
max
Numeric max(const ConstTensor3View &x)
Max function, tensor version.
Definition: matpackIII.cc:735
Range
The range class.
Definition: matpackI.h:160
Range::mextent
Index mextent
The number of elements.
Definition: matpackI.h:353
Tensor3::swap
friend void swap(Tensor3 &t1, Tensor3 &t2)
Swaps two objects.
Definition: matpackIII.cc:688
ConstIterator3D::msv
ConstMatrixView msv
Current position.
Definition: matpackIII.h:112
ConstTensor3View::operator=
ConstTensor3View & operator=(const ConstTensor3View &)=default
Iterator4D
Implementation of Tensors of Rank 4.
Definition: matpackIV.h:38
ConstTensor3View::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackIII.h:150
ConstTensor3View::~ConstTensor3View
virtual ~ConstTensor3View()=default
Destructor.
ConstTensor3View
A constant view of a Tensor3.
Definition: matpackIII.h:132
Tensor4View
The Tensor4View class.
Definition: matpackIV.h:284
operator<<
std::ostream & operator<<(std::ostream &os, const ConstTensor3View &v)
Output operator.
Definition: matpackIII.cc:194
Tensor3View::get
Numeric & get(Index p, Index r, Index c)
Get element implementation without assertions.
Definition: matpackIII.h:275
ConstIterator3D::operator++
ConstIterator3D & operator++()
Prefix increment operator.
Definition: matpackIII.h:89
Tensor3::operator=
Tensor3 & operator=(const Tensor3 &x)
Assignment operator from another tensor.
Definition: matpackIII.cc:630
ConstTensor3View::operator=
ConstTensor3View & operator=(ConstTensor3View &&)=default
Tensor3View::operator+=
Tensor3View & operator+=(Numeric x)
Addition of scalar.
Definition: matpackIII.cc:416
Tensor3::Tensor3
Tensor3(Tensor3 &&v) noexcept
Definition: matpackIII.h:347
ARTS::Var::x
Vector x(Workspace &ws) noexcept
Definition: autoarts.h:7346
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
ConstIterator3D::operator!=
bool operator!=(const ConstIterator3D &other) const
Not equal operator, needed for algorithms like copy.
Definition: matpackIII.h:95
Tensor3View::operator=
Tensor3View & operator=(const ConstTensor3View &v)
Assignment operator.
Definition: matpackIII.cc:347
ConstTensor3View::empty
bool empty() const
Check if variable is empty.
Definition: matpackIII.cc:38
ConstIterator3D::operator->
const ConstMatrixView * operator->() const
The -> operator is needed, so that we can write i->begin() to get the 1D iterators.
Definition: matpackIII.h:105
Tensor3View::get_c_array
const Numeric * get_c_array() const
Conversion to plain C-array.
Definition: matpackIII.cc:321
ConstIterator3D::ConstIterator3D
ConstIterator3D(const ConstMatrixView &x, Index stride)
Explicit constructor.
Definition: matpackIII.h:83
Iterator3D::operator->
MatrixView * operator->()
The -> operator is needed, so that we can write i->begin() to get the 1D iterators.
Definition: matpackIII.h:63
Range::mstride
Index mstride
The stride.
Definition: matpackI.h:355
ConstVectorView
A constant view of a Vector.
Definition: matpackI.h:476
Iterator3D::Iterator3D
Iterator3D()=default
Default constructor.
ConstTensor3View::operator()
Numeric operator()(Index p, Index r, Index c) const
Plain const index operator.
Definition: matpackIII.h:166
ConstTensor5View
A constant view of a Tensor5.
Definition: matpackV.h:143
mult
void mult(Tensor3View A, const ConstVectorView B, const ConstMatrixView C)
mult Tensor3
Definition: matpackIII.cc:813
Tensor6View
The Tensor6View class.
Definition: matpackVI.h:621
Tensor3View::~Tensor3View
virtual ~Tensor3View()=default
Destructor.