VectorBaseOperators.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_VECTOR_BASE_OPERATORS_HPP
45 #define GNSSTK_VECTOR_BASE_OPERATORS_HPP
46 
47 #include <fstream> // for copyfmt
48 #include <vector>
49 #include <iomanip>
50 #include "VectorBase.hpp"
51 
52 // to solve the conflict with windows.h
53 #if defined(min)
54 #undef min
55 #endif
56 
57 #if defined(max)
58 #undef max
59 #endif
60 
61 namespace gnsstk
62 {
63 
65 
66 
68  template <class T, class E>
69  std::ostream& operator<<(std::ostream& s, const ConstVectorBase<T, E>& a)
70  {
71  std::ofstream savefmt;
72  savefmt.copyfmt(s);
73  size_t i;
74  for (i=0; i< a.size(); i++) {
75  s << std::setw(1) << ' ';
76  s.copyfmt(savefmt);
77  s << a[i];
78  }
79  return s;
80  }
81 
83  template <class T, class BaseClass>
85  {
86  T total(0);
87  size_t i;
88  for (i = 0; i < l.size(); i++)
89  total += l[i];
90  return total;
91  }
92 
96  template <class T, class BaseClass>
98  {
99  if (l.size() == 0)
100  {
101  VectorException e("Can't find the minabs of an empty vector");
102  GNSSTK_THROW(e);
103  }
104  T min = l[0];
105  size_t i;
106  for (i = 1; i < l.size(); i++)
107  if (ABS(l[i]) < ABS(min))
108  min = l[i];
109  return min;
110  }
111 
115  template <class T, class BaseClass>
117  {
118  if (l.size() == 0)
119  {
120  VectorException e("Can't find the min of an empty vector");
121  GNSSTK_THROW(e);
122  }
123  T min = l[0];
124  size_t i;
125  for (i = 1; i < l.size(); i++)
126  if (l[i] < min)
127  min = l[i];
128  return min;
129  }
130 
132  template <class T, class BaseClass>
134  {
135  if (l.size() == 0)
136  {
137  VectorException e("Can't find the maxabs of an empty vector");
138  GNSSTK_THROW(e);
139  }
140  T max = l[0];
141  size_t i;
142  for (i = 1; i < l.size(); i++)
143  if (ABS(l[i]) > ABS(max))
144  max = l[i];
145  return max;
146  }
147 
149  template <class T, class BaseClass>
151  {
152  if (l.size() == 0)
153  {
154  VectorException e("Can't find the max of an empty vector");
155  GNSSTK_THROW(e);
156  }
157  T max = l[0];
158  size_t i;
159  for (i = 1; i < l.size(); i++)
160  if (l[i] > max)
161  max = l[i];
162  return max;
163  }
164 
166  template <class T, class BaseClass, class BaseClass2>
169  {
170  T sum(0);
171  size_t i,n=(l.size() > r.size() ? r.size() : l.size());
172  for (i = 0; i < n; i++)
173  {
174  sum += l[i] * r[i];
175  }
176  return sum;
177  }
178 
180  template <class T, class BaseClass>
181  inline T dot(const ConstVectorBase<T, BaseClass>& l, const T r)
182  {
183  T sum(0);
184  size_t i;
185  for (i = 0; i < l.size(); i++)
186  {
187  sum += l[i] * r;
188  }
189  return sum;
190  }
191 
193  template <class T, class BaseClass>
194  inline T dot(const T l, const ConstVectorBase<T, BaseClass>& r)
195  {
196  T sum(0);
197  size_t i;
198  for (i = 0; i < r.size(); i++)
199  {
200  sum += l * r[i];
201  }
202  return sum;
203  }
204 
206  template <class T, class BaseClass>
208  {
209  T mag=T(0);
210  if(v.size()==0) return mag;
211  mag = ABS(v(0));
212  for(size_t i=1; i<v.size(); i++) {
213  if(mag > ABS(v(i)))
214  mag *= SQRT(T(1)+(v(i)/mag)*(v(i)/mag));
215  else if(ABS(v(i)) > mag)
216  mag = ABS(v(i))*SQRT(T(1)+(mag/v(i))*(mag/v(i)));
217  else
218  mag *= SQRT(T(2));
219  }
220  return mag;
221  }
222 
224  template <class T, class BaseClass, class BaseClass2>
227  {
228  if (v.size()<4 || w.size()<4)
229  {
230  VectorException e("Minkowski requires vector length 4");
231  GNSSTK_THROW(e);
232  }
233  return (v(0)*w(0)+v(1)*w(1)+v(2)*w(2)-v(3)*w(3));
234  }
235 
237  template <class T, class BaseClass1, class BaseClass2>
240  {
241  T na=norm(a), nb=norm(b), c(0);
242  size_t i,n=(b.size() > a.size() ? a.size() : b.size());
243  for(i=0; i<n; i++) c += (a(i)/na)*(b(i)/nb);
244  return c;
245  }
246 
247 // shortwire equality operators - compares each individual
248 // element in the vector but returns one 'true' or 'false'
249 // for the whole comparison. note this only compares
250 // the smaller of the size of the two vectors
251 #define VecShortwireComparisonOperator(func, op) \
252  \
253  template <class T, class BaseClass, class BaseClass2> \
254  inline bool func(const ConstVectorBase<T, BaseClass>& l, \
255  const ConstVectorBase<T, BaseClass2>& r) \
256  { \
257  size_t len = (l.size() < r.size()) ? l.size() : r.size(); \
258  size_t i; \
259  for(i = 0; i < len; i++) \
260  if ( !(l[i] op r[i]) ) \
261  return false; \
262  return true; \
263  } \
264  \
265  template <class T, class BaseClass> \
266  inline bool func(const ConstVectorBase<T, BaseClass>& l, const T r) \
267  { \
268  size_t len = l.size(); \
269  size_t i; \
270  for(i = 0; i < len; i++) \
271  if ( !(l[i] op r) ) \
272  return false; \
273  return true; \
274  } \
275  \
276  template <class T, class BaseClass> \
277  inline bool func(const T l, const ConstVectorBase<T, BaseClass>& r) \
278  { \
279  size_t len = r.size(); \
280  size_t i; \
281  for(i = 0; i < len; i++) \
282  if ( !(l op r[i]) ) \
283  return false; \
284  return true; \
285  }
286 
293 
295 
296 } // namespace gnsstk
297 
298 #endif // GNSSTK_VECTOR_BASE_OPERATORS_HPP
SQRT
#define SQRT(x)
Definition: MathBase.hpp:74
gnsstk::VecShortwireComparisonOperator
VecShortwireComparisonOperator(eq,==) VecShortwireComparisonOperator(ne
gnsstk::dot
T dot(const SparseVector< T > &SL, const SparseVector< T > &SR)
dot (SparseVector, SparseVector)
Definition: SparseVector.hpp:789
gnsstk::max
T max(const SparseMatrix< T > &SM)
Maximum element - return 0 if empty.
Definition: SparseMatrix.hpp:881
gnsstk::sum
T sum(const ConstVectorBase< T, BaseClass > &l)
Definition: VectorBaseOperators.hpp:84
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
gnsstk::maxabs
T maxabs(const SparseMatrix< T > &SM)
Maximum absolute value - return 0 if empty.
Definition: SparseMatrix.hpp:927
gnsstk::minabs
T minabs(const SparseMatrix< T > &SM)
Minimum absolute value - return 0 if empty.
Definition: SparseMatrix.hpp:904
gnsstk::operator<<
std::ostream & operator<<(std::ostream &s, const ObsEpoch &oe) noexcept
Definition: ObsEpochMap.cpp:54
gnsstk::Minkowski
T Minkowski(const ConstVectorBase< T, BaseClass > &v, const ConstVectorBase< T, BaseClass2 > &w)
Definition: VectorBaseOperators.hpp:225
gnsstk::min
T min(const SparseMatrix< T > &SM)
Maximum element - return 0 if empty.
Definition: SparseMatrix.hpp:858
gnsstk::cosVec
T cosVec(const SparseVector< T > &S1, const SparseVector< T > &S2)
Definition: SparseVector.hpp:736
gnsstk::ConstVectorBase::size
size_t size() const
Returns the size of the base class.
Definition: VectorBase.hpp:112
ABS
#define ABS(x)
Definition: MathBase.hpp:73
gnsstk::ConstVectorBase
Definition: VectorBase.hpp:105
GNSSTK_THROW
#define GNSSTK_THROW(exc)
Definition: Exception.hpp:366
gnsstk::norm
T norm(const SparseVector< T > &SV)
Definition: SparseVector.hpp:705
VectorBase.hpp


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