PowerSum.cpp
Go to the documentation of this file.
1 //==============================================================================
2 //
3 // This file is part of GNSSTk, the ARL:UT GNSS Toolkit.
4 //
5 // The GNSSTk is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation; either version 3.0 of the License, or
8 // any later version.
9 //
10 // The GNSSTk is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with GNSSTk; if not, write to the Free Software Foundation,
17 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 //
19 // This software was developed by Applied Research Laboratories at the
20 // University of Texas at Austin.
21 // Copyright 2004-2022, The Board of Regents of The University of Texas System
22 //
23 //==============================================================================
24 
25 //==============================================================================
26 //
27 // This software was developed by Applied Research Laboratories at the
28 // University of Texas at Austin, under contract to an agency or agencies
29 // within the U.S. Department of Defense. The U.S. Government retains all
30 // rights to use, duplicate, distribute, disclose, or release this software.
31 //
32 // Pursuant to DoD Directive 523024
33 //
34 // DISTRIBUTION STATEMENT A: This software has been approved for public
35 // release, distribution is unlimited.
36 //
37 //==============================================================================
38 
39 #include "PowerSum.hpp"
40 #include <math.h>
41 
42 namespace gnsstk
43 {
44  void PowerSum::clear() noexcept
45  {
46  for (int i=1; i<=order; i++)
47  s[i]=0.0;
48  n=0;
49  }
50 
51  void PowerSum::add(double x) noexcept
52  {
53  n++;
54  double px=x;
55  for (int i=1; i<=order; i++, px*=x)
56  s[i] += px;
57  }
58 
59  void PowerSum::subtract(double x) noexcept
60  {
61  n--;
62  double px=x;
63  for (int i=1; i<=order; i++, px*=x)
64  s[i] -= px;
65  }
66 
68  {
69  dlc_iterator i;
70  for (i=b; i != e; i++)
71  add(*i);
72  }
73 
75  {
76  dlc_iterator i;
77  for (i=b; i != e; i++)
78  subtract(*i);
79  }
80 
83  double PowerSum::moment(int i) const noexcept
84  {
85  if ( i > order || i >= n)
86  return 0;
87 
88  double m=0;
89  double ni=1.0/n;
90  double s12 = s[1]*s[1];
91 
92  if (i==1 && n>0)
93  m = ni*s[1];
94  if (i==2 && n>1)
95  m = ni*(s[2] - ni*s12);
96  else if (i==3 && n>2)
97  m = ni*(s[3] + ni*(-3*s[1]*s[2] + ni*(2*s12*s[1])));
98  else if (i==4 && n>3)
99  m = ni*(s[4] + ni*(-4*s[1]*s[3] + ni*(6*s12*s[2] + ni*(-3*s12*s12))));
100  else if (i==5 && n>4)
101  m = ni*(s[5] + ni*(-5*s[1]*s[4] +ni*(10*s12*s[3]
102  + ni*(-10*s12*s[1]*s[2] + ni*(4*s12*s12*s[1])))));
103 
104  return m;
105  }
106 
107  double PowerSum::average() const noexcept
108  {
109  if (n<1)
110  return 0;
111  return s[1]/n;
112  }
113 
114 
115  double PowerSum::variance() const noexcept
116  {
117  if (n<2)
118  return 0;
119  return moment(2);
120  }
121 
122  double PowerSum::skew() const noexcept
123  {
124  if (n<3)
125  return 0;
126  return moment(3)/pow(moment(2),1.5);
127  }
128 
129  double PowerSum::kurtosis() const noexcept
130  {
131  if (n<4)
132  return 0;
133  double m2 = moment(2);
134  return moment(4)/(m2*m2);
135  }
136 
137  void PowerSum::dump(std::ostream& str) const noexcept
138  {
139  str << "n:" << n;
140  for (int i=1; i<=order; i++)
141  str << " s" << i << ":" << s[i];
142  str << std::endl;
143 
144  str << "m1:" << moment(1)
145  << " m2:" << moment(2)
146  << " m3:" << moment(3)
147  << " m4:" << moment(4)
148  << std::endl;
149 
150  str << "average:" << average()
151  << " stddev:" << sqrt(variance())
152  << " skew:" << skew()
153  << " kurtosis:" << kurtosis()
154  << std::endl;
155  }
156 }
gnsstk::PowerSum::clear
void clear() noexcept
Reset all sums.
Definition: PowerSum.cpp:44
gnsstk::PowerSum::s
double s[order+1]
Definition: PowerSum.hpp:101
gnsstk::PowerSum::moment
double moment(int i) const noexcept
Computes the ith order central moment.
Definition: PowerSum.cpp:83
const
#define const
Definition: getopt.c:43
gnsstk::PowerSum::subtract
void subtract(double x) noexcept
Definition: PowerSum.cpp:59
gnsstk::PowerSum::variance
double variance() const noexcept
Definition: PowerSum.cpp:115
gnsstk::PowerSum::add
void add(double x) noexcept
Add a single value to the sums.
Definition: PowerSum.cpp:51
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
PowerSum.hpp
gnsstk::PowerSum::dump
void dump(std::ostream &str) const noexcept
Definition: PowerSum.cpp:137
gnsstk::PowerSum::n
long n
Definition: PowerSum.hpp:102
gnsstk::PowerSum::dlc_iterator
std::list< double >::const_iterator dlc_iterator
Definition: PowerSum.hpp:77
gnsstk::PowerSum::kurtosis
double kurtosis() const noexcept
Definition: PowerSum.cpp:129
gnsstk::PowerSum::order
const static int order
Definition: PowerSum.hpp:62
gnsstk::PowerSum::skew
double skew() const noexcept
Definition: PowerSum.cpp:122
gnsstk::PowerSum::average
double average() const noexcept
Computes the indicated value.
Definition: PowerSum.cpp:107


gnsstk
Author(s):
autogenerated on Wed Oct 25 2023 02:40:40