SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SGSparseMatrix.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2012 Fernando José Iglesias García
8  * Written (W) 2010,2012 Soeren Sonnenburg
9  * Copyright (C) 2010 Berlin Institute of Technology
10  * Copyright (C) 2012 Soeren Sonnenburg
11  */
12 
13 #ifndef __SGSPARSEMATRIX_H__
14 #define __SGSPARSEMATRIX_H__
15 
16 #include <shogun/lib/config.h>
17 
18 #include <shogun/lib/common.h>
20 #include <shogun/lib/SGVector.h>
21 #include <shogun/io/SGIO.h>
22 
23 namespace shogun
24 {
25 
26 template <class T> class SGSparseVector;
27 template <class ST> struct SGSparseVectorEntry;
28 template<class T> class SGMatrix;
29 class CFile;
30 class CLibSVMFile;
31 
33 template <class T> class SGSparseMatrix : public SGReferencedData
34 {
35  public:
38 
41  index_t num_vec, bool ref_counting=true);
42 
44  SGSparseMatrix(index_t num_feat, index_t num_vec, bool ref_counting=true);
45 
51 
53  SGSparseMatrix(const SGSparseMatrix &orig);
54 
56  virtual ~SGSparseMatrix();
57 
59  inline const SGSparseVector<T>& operator[](index_t index) const
60  {
61  return sparse_matrix[index];
62  }
63 
66  {
67  return sparse_matrix[index];
68  }
69 
75  inline SGSparseMatrix<T> get()
76  {
77  return *this;
78  }
79 
85  {
86  SGVector<T> result(num_vectors);
88  "Dimension mismatch! %d vs %d\n",
89  v.vlen, num_features);
90  for (index_t i=0; i<num_vectors; ++i)
91  result[i]=sparse_matrix[i].dense_dot(1.0, v.vector, v.vlen, 0.0);
92 
93  return result;
94  }
95 
100  template<class ST> const SGVector<T> operator*(SGVector<ST> v) const;
101 
106  inline const T operator()(index_t i_row, index_t i_col) const
107  {
108  REQUIRE(i_row>=0, "index %d negative!\n", i_row);
109  REQUIRE(i_col>=0, "index %d negative!\n", i_col);
110  REQUIRE(i_row<num_vectors, "index should be less than %d, %d provided!\n",
111  num_vectors, i_row);
112  REQUIRE(i_col<num_features, "index should be less than %d, %d provided!\n",
113  num_features, i_col);
114 
115  for (index_t i=0; i<sparse_matrix[i_row].num_feat_entries; ++i)
116  {
117  if (i_col==sparse_matrix[i_row].features[i].feat_index)
118  return sparse_matrix[i_row].features[i].entry;
119  }
120  return 0;
121  }
122 
127  inline T& operator()(index_t i_row, index_t i_col)
128  {
129  REQUIRE(i_row>=0, "index %d negative!\n", i_row);
130  REQUIRE(i_col>=0, "index %d negative!\n", i_col);
131  REQUIRE(i_row<num_vectors, "index should be less than %d, %d provided!\n",
132  num_vectors, i_row);
133  REQUIRE(i_col<num_features, "index should be less than %d, %d provided!\n",
134  num_features, i_col);
135 
136  for (index_t i=0; i<sparse_matrix[i_row].num_feat_entries; ++i)
137  {
138  if (i_col==sparse_matrix[i_row].features[i].feat_index)
139  return sparse_matrix[i_row].features[i].entry;
140  }
141  index_t j=sparse_matrix[i_row].num_feat_entries;
142  sparse_matrix[i_row].num_feat_entries=j+1;
143  sparse_matrix[i_row].features=SG_REALLOC(SGSparseVectorEntry<T>,
144  sparse_matrix[i_row].features, j, j+1);
145  sparse_matrix[i_row].features[j].feat_index=i_col;
146  sparse_matrix[i_row].features[j].entry=static_cast<T>(0);
147 
148  return sparse_matrix[i_row].features[j].entry;
149  }
150 
155  void load(CFile* loader);
156 
164  SGVector<float64_t> load_with_labels(CLibSVMFile* libsvm_file, bool do_sort_features=true);
165 
170  void save(CFile* saver);
171 
177  void save_with_labels(CLibSVMFile* saver, SGVector<float64_t> labels);
178 
181 
186  void from_dense(SGMatrix<T> full);
187 
189  void sort_features();
190 
191 protected:
192 
194  virtual void copy_data(const SGReferencedData& orig);
195 
197  virtual void init_data();
198 
200  virtual void free_data();
201 
202 public:
203 
206 
209 
212 
213 };
214 }
215 #endif // __SGSPARSEMATRIX_H__

SHOGUN Machine Learning Toolbox - Documentation