Machine Learning Library
CSparseVector.h
Go to the documentation of this file.
1 /*
2  COPYRIGHT (C) 2003 APPLIED NEUROINFORMATIC GROUP - UNIVERSITY OF BIELEFELD.
3 
4  ALL RIGHTS RESERVED.
5 
6  REDISTRIBUTION AND USE IN SOURCE AND BINARY FORM, WITH OR WITHOUT
7  MODIFICATION, REQUIRE THE PERMISSION OF THE COPYRIGHT HOLDERS.
8 
9  COMMERCIAL USE WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT HOLDERS
10  IS FORBIDDEN
11 
12  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
13  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
20  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
21  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 */
23 
24 
25 
26 #ifndef CSPARSEVECTOR_H
27 #define CSPARSEVECTOR_H
28 
29 #include <iostream>
30 #include <utility>
31 
32 #include "CVector.h"
33 #include "keyed_iterator.hpp"
34 
35 
36 
41 template <class Type>
42 class CSparseVector : public CVector<Type>
43 {
44 public:
45  typedef pair<int, Type> element_type;
48 
49 private:
50  pointer_type elements;
51  int dim;
52  int size;
53  static Type nullElement;
54 
55  // forbidden
56  CSparseVector& operator= (const CSparseVector &rhs);
57 
58 public:
59  CSparseVector(int dim, int size, Type elements[], int pos[]);
60  CSparseVector(int dim, int size, vector<Type> elements, vector<int> pos);
61  CSparseVector(int dim, int size);
63  CSparseVector(const CVector<Type>& o);
64  CVector<Type>* clone() const;
65  virtual ~CSparseVector();
66 
67  const Type& operator[] (int index) const;
68  int dimension() const { return dim; }
69  Type getElement(int iIndex) const;
70  void setElement(int index, pair<int, Type> value);
71  int getNumNonZero() const { return size; }
72  bool isNaN() const;
73  Type euclideanLength(bool bQuad=false) const;
74  virtual string className() const { return string("CSparseVector");};
75  CVector<double>* toDouble() const;
76  CVector<float>* toFloat() const;
78 
81  return *this;
82  }
85  return *this;
86  }
88  virtual CVector<Type>& operator*=(const Type&);
90  virtual CVector<Type>& operator/=(const Type&);
91 
92  // fix to be <const Type>
94  return keyed_iterator<Type>(elements, size, dim);
95  }
97  return keyed_iterator<Type>(elements, size, dim);
98  }
100  return keyed_iterator<Type>(elements + size, 0, dim);
101  }
103  return keyed_iterator<Type>(elements + size, 0, dim);
104  }
105 
106 
107 };
108 
109 // operator definitions
110 
111 template<class Type>
112 std::ostream& operator << (std::ostream& stream, const CSparseVector<Type>& rhs)
113 {
114  stream << "SparseVector[dim=" << rhs.dimension()
115  << ", size=" << rhs.getNumNonZero() << ", pairs={";
116 
117  for(typename CSparseVector<Type>::iterator i = rhs.begin();
118  i != rhs.end(); ++i) {
119  stream << "(" << i.key() << ", " << i.val() << ") ";
120  }
121  stream << "}]";
122 
123  return stream;
124 }
125 
126 template <class Type>
127 CSparseVector<Type> operator* (const CSparseVector<Type>& vec, const Type& s);
128 template <class Type>
129 CSparseVector<Type> operator* (const Type& s, const CSparseVector<Type> &vec);
130 template<class Type>
132 template<class Type>
134 
135 #endif // CSPARSEVECTOR_H
int key() const
Definition: keyed_iterator.hpp:107
keyed_iterator< Type > begin() const
Definition: CSparseVector.h:93
keyed_iterator< Type > iterator
Definition: CSparseVector.h:47
bool isNaN() const
returns true if vector contains nan elements
CVector< Type > * clone() const
destructor
CSparseVector< Type > elementMul(const CSparseVector< Type > &, const CVector< Type > &)
Type euclideanLength(bool bQuad=false) const
returns euclidean length of vector
CSparseVector< Type > operator/(const CSparseVector< Type > &, const Type &)
Type getElement(int iIndex) const
return copy of n'th vector element
CVector< Type > & operator/=(const CVector< Type > &v2)
Definition: CSparseVector.h:83
element_type * pointer_type
Definition: CSparseVector.h:46
virtual ~CSparseVector()
void setElement(int index, pair< int, Type > value)
CSparseVector< Type > operator*(const CSparseVector< Type > &vec, const Type &s)
CSparseVector(int dim, int size, Type elements[], int pos[])
Definition: CDenseVector.h:35
CVector< double > * toDouble() const
create a copy with every element stored as 'double'
CVector< float > * toFloat() const
create a copy with every element stored as 'float'
pair< int, Type > element_type
Definition: CSparseVector.h:45
CVector< Type > & operator*=(const CVector< Type > &)
multiply vectors elementwise
Sparse, immutable vector representation.
Definition: CSparseVector.h:42
CVector< Type > & operator/=(const CVector< Type > &)
divide vectors elementwise
keyed_iterator< Type > end()
Definition: CSparseVector.h:102
int dimension() const
return vector dimension
Definition: CSparseVector.h:68
const Type & operator[](int index) const
return constant reference to n'th vector element
CVector< Type > & operator*=(const CVector< Type > &v2)
Definition: CSparseVector.h:79
Template object for vectors of single and double precision and integer.
Definition: CDenseVector.h:37
int getNumNonZero() const
returns the number of non-zero entries
Definition: CSparseVector.h:71
templatized vector for numerical applications
Definition: CMatrix.h:39
keyed_iterator< Type > begin()
Definition: CSparseVector.h:96
virtual string className() const
Returns the class name.
Definition: CSparseVector.h:74
CDenseVector< int > sort()
keyed_iterator< Type > end() const
Definition: CSparseVector.h:99