ARTS  2.4.0(git:4fb77825)
raw.cc
Go to the documentation of this file.
1 /* Copyright (C) 2020
2  * Richard Larsson <larsson@mps.mpg.de>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2, or (at your option) any
7  * later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17  * USA. */
18 
27 #include <algorithm>
28 
29 #include "constants.h"
30 #include "raw.h"
31 
32 void linalg::avg(VectorView y, const ArrayOfVector& ys, const Index start, const Index end_tmp)
33 {
34  // True end
35  const Index end = end_tmp >= 0 ? end_tmp : 1 + ys.nelem() + end_tmp;
36 
37  // Scale
38  const Numeric scale = 1 / Numeric(end - start);
39 
40  // Reset
41  y = 0;
42 
43  // Compute
44  for (Index k=start; k<end; k++)
45  for (Index i=0; i<y.nelem(); i++)
46  y[i] += ys[k][i] * scale;
47 }
48 
49 void linalg::var(VectorView var, const Vector& y, const ArrayOfVector& ys, const Index start, const Index end_tmp)
50 {
51  // True end
52  const Index end = end_tmp >= 0 ? end_tmp : 1 + ys.nelem() + end_tmp;
53 
54  // Scale
55  const Numeric scale = 1 / Numeric(end - start);
56 
57  // Reset
58  var = 0;
59 
60  // Compute
61  for (Index k=start; k<end; k++)
62  for (Index i=0; i<y.nelem(); i++)
63  var[i] += Constant::pow2(ys[k][i] - y[i]) * scale;
64 }
65 
66 void linalg::std(VectorView std, const Vector& y, const ArrayOfVector& ys, const Index start, const Index end_tmp)
67 {
68  var(std, y, ys, start, end_tmp);
69  std::transform(std.begin(), std.end(), std.begin(), [](const auto& x){return std::sqrt(x);});
70 }
71 
72 void linalg::cov(MatrixView cov, const Vector& y, const ArrayOfVector& ys, const Index start, const Index end_tmp)
73 {
74  // True end
75  const Index end = end_tmp >= 0 ? end_tmp : 1 + ys.nelem() + end_tmp;
76 
77  // Scale
78  const Numeric scale = 1 / Numeric(end - start - 1);
79 
80  // Reset
81  cov = 0;
82 
83  // Compute
84  for (Index k=start; k<end; k++)
85  for (Index i=0; i<y.nelem(); i++)
86  for (Index j=0; j<y.nelem(); j++)
87  cov(i, j) += (ys[k][i] - y[i]) * (ys[k][j] - y[j]) * scale;
88 }
89 
91 {
92  // Size of problem
93  const Index n = pos.nelem() ? pos.nelem() : v.nelem();
94 
95  // Create a vector to sort
96  ArrayOfNumeric calc(n);
97  for (Index i=0; i<n; i++)
98  if (pos.nelem())
99  calc[i] = v[pos[i]];
100  else
101  calc[i] = v[i];
102 
103  // Sort the vector
104  std::sort(calc.begin(), calc.end());
105 
106  // Return the median
107  if (n % 2)
108  return calc[n/2];
109  else
110  return (calc[(n-1)/2] + calc[n/2]) / 2;
111 }
transform
void transform(VectorView y, double(&my_func)(double), ConstVectorView x)
A generic transform function for vectors, which can be used to implement mathematical functions opera...
Definition: matpackI.cc:1476
MatrixView
The MatrixView class.
Definition: matpackI.h:1093
linalg::std
void std(VectorView std, const Vector &y, const ArrayOfVector &ys, const Index start=0, const Index end=-1)
Compute the standard deviation of the ranged ys.
Definition: raw.cc:66
ARTS::Var::y
Vector y(Workspace &ws) noexcept
Definition: autoarts.h:7401
raw.h
Stuff related to generating y-data from raw data.
Array< Vector >
linalg::median
Numeric median(const ConstVectorView v, const ArrayOfIndex &pos=ArrayOfIndex{})
Get the median of the vector in the range.
Definition: raw.cc:90
ConstVectorView::nelem
Index nelem() const
Returns the number of elements.
Definition: matpackI.cc:51
VectorView
The VectorView class.
Definition: matpackI.h:610
Numeric
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
linalg::var
void var(VectorView var, const Vector &y, const ArrayOfVector &ys, const Index start=0, const Index end=-1)
Compute the variance of the ranged ys.
Definition: raw.cc:49
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
linalg::avg
void avg(VectorView y, const ArrayOfVector &ys, const Index start=0, const Index end=-1)
Compute the average of the ranged ys.
Definition: raw.cc:32
constants.h
Constants of physical expressions as constexpr.
Zeeman::start
constexpr Rational start(Rational Ju, Rational Jl, Polarization type) noexcept
Gives the lowest M for a polarization type of this transition.
Definition: zeemandata.h:77
Constant::pow2
constexpr T pow2(T x)
power of two
Definition: constants.h:64
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
Vector
The Vector class.
Definition: matpackI.h:860
ConstVectorView
A constant view of a Vector.
Definition: matpackI.h:476
Array::nelem
Index nelem() const
Number of elements.
Definition: array.h:195
linalg::cov
void cov(MatrixView cov, const Vector &y, const ArrayOfVector &ys, const Index start=0, const Index end=-1)
Compute the covariance matrix of the ranged ys.
Definition: raw.cc:72