PolyFit.hpp
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 
44 #ifndef GNSSTK_POLYFIT_HPP
45 #define GNSSTK_POLYFIT_HPP
46 
47 #include "Matrix.hpp"
48 
49 namespace gnsstk
50 {
52 
53 
72  template <class T>
73  class PolyFit
74  {
75  public:
77  PolyFit() : n_(0), Npts(0), Inverted(false), Singular(true) {}
79  PolyFit(unsigned int n) : n_(n), Npts(0), Inverted(false), Singular(true)
80  {
81  InfData.resize(n_,T(0));
82  X.resize(n_);
83  InfMatrix.resize(n_,n_,T(0));
84  Cov.resize(n_,n_);
85  }
86 
91  void Reset(unsigned int n=0)
92  {
93  if(n != 0 && n_ != n) {
94  InfData.resize(n,T(0));
95  X.resize(n);
96  InfMatrix.resize(n,n,T(0));
97  Cov.resize(n,n);
98  n_ = n;
99  }
100  Npts = 0;
101  InfData = T(0);
102  X = T(0);
103  InfMatrix = T(0);
104  Cov = T(0);
105  Singular = true;
106  Inverted = false;
107  }
108 
110  void Add(T d, T t, T w=T(1))
111  {
112  // n_-1
113  // Equation is sum[t^i * X(i)] = d OR P * X = d
114  // i=0 1xn_ n_ 1
115  Vector<T> P(n_);
116  T tt=T(1);
117  for(size_t i=0; i<n_; i++) { P(i)=tt; tt *= t; }
118  Npts++;
119  Matrix<T> PP;
120  PP = outer(P,P);
121  PP *= w;
122  InfMatrix += PP;
123  P *= d*w;
124  InfData += P;
125  Inverted = false;
126  }
127 
129  void Add(const Vector<T>& d, const Vector<T>& t)
130  {
131  size_t m=d.size();
132  if(t.size() < m) m=t.size();
133 
134  Matrix<T> P(m,n_);
135  Vector<T> D(d);
136  D.resize(m);
137 
138  for(size_t j=0; j<m; j++) {
139  T tt=T(1);
140  for(size_t i=0; i<n_; i++) { P(j,i)=tt; tt *= t(j); }
141  }
142  Npts += m;
143  Matrix<T> PTP,PT;
144  PT = transpose(P);
145  PTP = PT*P;
146  InfMatrix += PTP;
147  InfData += PT*D;
148  Inverted = false;
149  }
150 
152  void Add(const std::vector<T>& d, const std::vector<T>& t)
153  {
154  size_t m=d.size();
155  if(t.size() < m) m=t.size();
156 
157  Matrix<T> P(m,n_);
158  Vector<T> D;
159  D.resize(m);
160  for(int i=0; i<m; i++) D(i)=d[i];
161 
162  for(size_t j=0; j<m; j++) {
163  T tt=T(1);
164  for(size_t i=0; i<n_; i++) { P(j,i)=tt; tt *= t[j]; }
165  }
166  Npts += m;
167  Matrix<T> PTP,PT;
168  PT = transpose(P);
169  PTP = PT*P;
170  InfMatrix += PTP;
171  InfData += PT*D;
172  Inverted = false;
173  }
174 
177  T Evaluate(T t)
178  {
179  if(n_ <= 0) { Singular=true; return T(0); }
180  Solve();
181  if(Singular) return T(0);
182 
183  T d,tn;
184  d = X(0);
185  tn = t;
186  for(size_t i=1; i<X.size(); i++) {
187  d += X(i)*tn;
188  tn *= t;
189  }
190  return d;
191  }
192 
196  {
197  Vector<T> R;
198  if(n_ <= 0) { Singular=true; return R; }
199  Solve();
200  if(Singular) return R;
201 
202  T tn;
203  R = Vector<T>(Vt.size());
204  for(size_t j=0; j<Vt.size(); j++) {
205  R(j) = X(0);
206  tn = Vt(j);
207  for(size_t i=1; i<X.size(); i++) {
208  R(j) += X(i)*tn;
209  tn *= Vt(j);
210  }
211  }
212  return R;
213  }
214 
216  inline bool isSingular(void) { Solve(); return Singular; }
218  inline Vector<T> Solution(void) { Solve(); return X; }
220  inline Matrix<T> Covariance(void) { Solve(); return Cov; }
222  inline unsigned int Degree(void) const { return n_; }
224  inline unsigned int N(void) const { return Npts; }
225 
226  private:
228  inline void Solve(void)
229  {
230  if(Inverted) return;
231  try { Cov=inverse(InfMatrix); }
232  catch (gnsstk::Exception& e) { Singular=true; return; }
233  Singular = false;
234  X = Cov * InfData;
235  Inverted = true;
236  }
237 
239  unsigned int n_;
241  unsigned int Npts;
247  bool Inverted;
249  bool Singular;
254 
255  }; // end class PolyFit
256 
258 
259 } // namespace gnsstk
260 
261 #endif
gnsstk::PolyFit::Add
void Add(const Vector< T > &d, const Vector< T > &t)
Add a gnsstk::Vector of data to the estimation.
Definition: PolyFit.hpp:129
gnsstk::PolyFit::Solve
void Solve(void)
Invert the equation.
Definition: PolyFit.hpp:228
gnsstk::PolyFit::Add
void Add(T d, T t, T w=T(1))
Add a single (optional: weighted) datum to the estimation.
Definition: PolyFit.hpp:110
gnsstk::PolyFit::PolyFit
PolyFit()
Empty constructor.
Definition: PolyFit.hpp:77
gnsstk::PolyFit::n_
unsigned int n_
degree of polynomial to be fit (dimension of state).
Definition: PolyFit.hpp:239
gnsstk::PolyFit::Evaluate
T Evaluate(T t)
Definition: PolyFit.hpp:177
gnsstk::PolyFit::Evaluate
Vector< T > Evaluate(const Vector< T > &Vt)
Definition: PolyFit.hpp:195
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
gnsstk::PolyFit::N
unsigned int N(void) const
get the number of data points processed
Definition: PolyFit.hpp:224
gnsstk::Vector::resize
Vector & resize(const size_t index)
Definition: Vector.hpp:262
gnsstk::Exception
Definition: Exception.hpp:151
gnsstk::PolyFit::Npts
unsigned int Npts
number of data points added to the estimation so far.
Definition: PolyFit.hpp:241
gnsstk::PolyFit::PolyFit
PolyFit(unsigned int n)
Constructor given an initial size.
Definition: PolyFit.hpp:79
gnsstk::PolyFit
Definition: PolyFit.hpp:73
gnsstk::transpose
SparseMatrix< T > transpose(const SparseMatrix< T > &M)
transpose
Definition: SparseMatrix.hpp:829
gnsstk::Matrix
Definition: Matrix.hpp:72
gnsstk::PolyFit::Covariance
Matrix< T > Covariance(void)
get the covariance matrix
Definition: PolyFit.hpp:220
gnsstk::PolyFit::isSingular
bool isSingular(void)
is the problem singular?
Definition: PolyFit.hpp:216
gnsstk::PolyFit::InfMatrix
Matrix< T > InfMatrix
information matrix = inverse(Cov) = sum[transpose(P)*P], P=partials.
Definition: PolyFit.hpp:243
gnsstk::Vector
Definition: Vector.hpp:67
gnsstk::TrackingCode::P
@ P
Legacy GPS precise code.
gnsstk::PolyFit::Solution
Vector< T > Solution(void)
get the solution vector (coefficients)
Definition: PolyFit.hpp:218
gnsstk::Vector::size
size_t size() const
STL size.
Definition: Vector.hpp:207
gnsstk::PolyFit::InfData
Vector< T > InfData
information data = inverse(Cov)*X = sum[transpose(P)*data].
Definition: PolyFit.hpp:245
gnsstk::PolyFit::Add
void Add(const std::vector< T > &d, const std::vector< T > &t)
Add a std::vector of data to the estimation.
Definition: PolyFit.hpp:152
gnsstk::PolyFit::Cov
Matrix< T > Cov
Covariance matrix.
Definition: PolyFit.hpp:253
gnsstk::PolyFit::Singular
bool Singular
flag indicating problem is singular.
Definition: PolyFit.hpp:249
Matrix.hpp
gnsstk::PolyFit::X
Vector< T > X
State vector (array of polynomial coefficients) of size n_.
Definition: PolyFit.hpp:251
gnsstk::PolyFit::Degree
unsigned int Degree(void) const
get the degree of the polynomial
Definition: PolyFit.hpp:222
gnsstk::inverse
SparseMatrix< T > inverse(const SparseMatrix< T > &A)
Definition: SparseMatrix.hpp:1890
gnsstk::PolyFit::Inverted
bool Inverted
flag indicating problem has been inverted.
Definition: PolyFit.hpp:247
gnsstk::outer
Matrix< T > outer(const ConstVectorBase< T, BaseClass > &v, const ConstVectorBase< T, BaseClass > &w)
Definition: MatrixOperators.hpp:933
gnsstk::PolyFit::Reset
void Reset(unsigned int n=0)
Definition: PolyFit.hpp:91


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