KrisLibrary  1.0.0
SparseVectorTemplate.h
1 #ifndef MATH_SPARSE_VECTOR_TEMPLATE_H
2 #define MATH_SPARSE_VECTOR_TEMPLATE_H
3 
4 #include <KrisLibrary/Logger.h>
5 #include "VectorTemplate.h"
6 #include <KrisLibrary/structs/SparseArray.h>
7 
8 namespace Math {
9 
10 template <class T>
12 
13 template <class T>
15 {
16 public:
18  typedef SparseArray<T> BaseT;
19  typedef typename BaseT::iterator iterator;
20  typedef typename BaseT::const_iterator const_iterator;
21  typedef VectorTemplate<T> VectorT;
22 
24  SparseVectorTemplate(size_t n):BaseT(n) {}
25  SparseVectorTemplate(const BaseT& v):BaseT(v) {}
26 
27  void print(std::ostream&) const;
28 
29  SparseVectorAccessor<T> operator() (int i);
30  SparseVectorAccessor<T> operator[] (int i) { return operator()(i); }
31  T operator() (int i) const { return get(i); }
32  T operator[] (int i) const { return get(i); }
33  T get(int i) const;
34  inline void set(int i,const T& t) { BaseT::insert(i,t); }
35 
36  inline void setZero() { BaseT::entries.clear(); }
37  inline void set(const BaseT& v) { BaseT::operator = (v); }
38  void set(const VectorT&,T zeroTol=Zero);
39  void set(const T*,int n,T zeroTol=Zero);
40  void get(T*) const;
41  void get(VectorT&) const;
42  void inplaceNegative();
43  void inplaceMul(T s);
44  void inplaceDiv(T s);
45 
46  void copy(const MyT&);
47  void copySubVector(int i,const MyT&);
48  void copySubVector(int i,const VectorT&,T zeroTol=0);
49  void swap(MyT&);
50  void add(const MyT&, const MyT&);
51  void sub(const MyT&, const MyT&);
52  void mul(const MyT&, T s);
53  void div(const MyT&, T s);
54 
55  T dot(const VectorT&) const;
56  T dot(const MyT&) const;
57  T norm() const;
58  T normSquared() const;
59  T distance(const MyT&) const;
60  T distanceSquared(const MyT&) const;
61 
62  inline bool isEmpty() const { return BaseT::empty(); }
63  inline bool hasDims(size_t _n) const { return BaseT::n==_n; }
64 
65  T minElement(int* index=NULL) const;
66  T maxElement(int* index=NULL) const;
67  T minAbsElement(int* index=NULL) const;
68  T maxAbsElement(int* index=NULL) const;
69 };
70 
71 template <class T>
73 {
74 public:
76  typedef VectorTemplate<T> VectorT;
77 
79  SparseVectorCompressed(int n, int num_entries);
80  SparseVectorCompressed(const MyT&);
82  void init(int n, int num_entries);
83  void resize(int n, int num_entries);
84  void makeSimilar(const MyT&);
85  void cleanup();
86 
87  bool Read(File&);
88  bool Write(File&) const;
89  void print(std::ostream&) const;
90 
91  const MyT& operator =(const MyT&);
92  T operator() (int i) const;
93 
94  void setZero();
95  void set(const MyT&);
96  void set(const VectorT&,T zeroTol=Zero);
97  void set(const T*,int n,T zeroTol=Zero);
98  void get(T*) const;
99  void get(VectorT&) const;
100  void inplaceNegative();
101  void inplaceMul(T s);
102  void inplaceDiv(T s);
103 
104  void add(const MyT&, const MyT&);
105  void sub(const MyT&, const MyT&);
106  void mul(const MyT&, T s);
107  void div(const MyT&, T s);
108 
109  T dot(const VectorT&) const;
110  T dot(const MyT&) const;
111  T norm() const;
112  T normSquared() const;
113  T distance(const MyT&) const;
114  T distanceSquared(const MyT&) const;
115 
116  bool isValid() const;
117  inline bool isEmpty() const { return n == 0; }
118  inline bool hasDims(int _n) const { return n==_n; }
119  inline bool isValidIndex(int i) const { return 0<=i&&i<n; }
120 
121  T minElement(int* index=NULL) const;
122  T maxElement(int* index=NULL) const;
123  T minAbsElement(int* index=NULL) const;
124  T maxAbsElement(int* index=NULL) const;
125 
126  /***********************************************************
127  * Compressed vector format:
128  * There are num_entries nonzero entries .
129  * The index of entry i is indices[i] (0<=indices[i]<indices[i+1]<n)
130  * The value of entry i is vals[i]
131  **********************************************************/
132  int* indices;
133  T* vals;
134  int num_entries;
135  int n;
136 };
137 
138 template <class T>
140 {
142  :vec(_vec),index(_index)
143  {}
145  :vec(rhs.vec),index(rhs.index)
146  {}
147  operator T () const
148  {
149  return vec->get(index);
150  }
151  const SparseVectorAccessor<T>& operator = (const T& rhs) {
152  vec->set(index,rhs);
153  return *this;
154  }
155 
157  int index;
158 };
159 
161 template <class T>
162 inline bool IsFinite(const SparseVectorTemplate<T>& x)
163 {
164  for(typename SparseVectorTemplate<T>::const_iterator i=x.begin();i!=x.end();i++)
165  if(!IsFinite(i->second)) return false;
166  return true;
167 }
168 
170 template <class T>
171 inline bool HasNaN(const SparseVectorTemplate<T>& x)
172 {
173  for(typename SparseVectorTemplate<T>::const_iterator i=x.begin();i!=x.end();i++)
174  if(IsNaN(i->second)) return false;
175  return true;
176 }
177 
179 template <class T>
180 inline bool HasInf(const SparseVectorTemplate<T>& x)
181 {
182  for(typename SparseVectorTemplate<T>::const_iterator i=x.begin();i!=x.end();i++)
183  if(IsInf(i->second)) return IsInf(i->second);
184  return 0;
185 }
186 
187 class Complex;
191 
192 
193 } //namespace Math
194 
195 #endif
A sparse 1D array class.
Definition: SparseArray.h:13
int IsInf(double x)
Returns +1 if x is +inf, -1 if x is -inf, and 0 otherwise.
Definition: infnan.cpp:92
int HasInf(const MatrixTemplate< T > &A)
returns nonzero if any element of A is infinite
Definition: MatrixTemplate.h:262
Definition: SparseVectorTemplate.h:72
Definition: SparseVectorTemplate.h:14
Complex number class (x + iy).
Definition: complex.h:17
bool HasNaN(const MatrixTemplate< T > &A)
returns true if any element of A is NaN
Definition: MatrixTemplate.h:252
int IsNaN(double x)
Returns nonzero if x is not-a-number (NaN)
Definition: infnan.cpp:61
Definition: SparseVectorTemplate.h:11
The logging system used in KrisLibrary.
Contains all definitions in the Math package.
Definition: WorkspaceBound.h:12
A vector over the field T.
Definition: function.h:9
A cross-platform class for reading/writing binary data.
Definition: File.h:47
int IsFinite(double x)
Returns nonzero unless x is infinite or a NaN.
Definition: infnan.cpp:75