SHOGUN  v3.0.0
 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/common.h>
17 #include <shogun/lib/DataType.h>
20 #include <shogun/io/LibSVMFile.h>
21 
22 namespace shogun
23 {
24 
25 template <class T> class SGSparseVector;
26 template<class T> class SGMatrix;
27 class CFile;
28 class CLibSVMFile;
29 class CRegressionLabels;
30 
32 template <class T> class SGSparseMatrix : public SGReferencedData
33 {
34  public:
37 
40  index_t num_vec, bool ref_counting=true);
41 
43  SGSparseMatrix(index_t num_feat, index_t num_vec, bool ref_counting=true);
44 
50 
52  SGSparseMatrix(const SGSparseMatrix &orig);
53 
55  virtual ~SGSparseMatrix();
56 
58  inline const SGSparseVector<T>& operator[](index_t index) const
59  {
60  return sparse_matrix[index];
61  }
62 
65  {
66  return sparse_matrix[index];
67  }
68 
74  inline SGSparseMatrix<T> get()
75  {
76  return *this;
77  }
78 
84  {
85  SGVector<T> result(num_vectors);
87  "Dimension mismatch! %d vs %d\n",
88  v.vlen, num_features);
89  for (index_t i=0; i<num_vectors; ++i)
90  result[i]=sparse_matrix[i].dense_dot(1.0, v.vector, v.vlen, 0.0);
91 
92  return result;
93  }
94 
99  template<class ST> const SGVector<T> operator*(SGVector<ST> v) const;
100 
105  inline const T operator()(index_t i_row, index_t i_col) const
106  {
107  REQUIRE(i_row>=0, "index %d negative!\n", i_row);
108  REQUIRE(i_col>=0, "index %d negative!\n", i_col);
109  REQUIRE(i_row<num_vectors, "index should be less than %d, %d provided!\n",
110  num_vectors, i_row);
111  REQUIRE(i_col<num_features, "index should be less than %d, %d provided!\n",
112  num_features, i_col);
113 
114  for (index_t i=0; i<sparse_matrix[i_row].num_feat_entries; ++i)
115  {
116  if (i_col==sparse_matrix[i_row].features[i].feat_index)
117  return sparse_matrix[i_row].features[i].entry;
118  }
119  return 0;
120  }
121 
126  inline T& operator()(index_t i_row, index_t i_col)
127  {
128  REQUIRE(i_row>=0, "index %d negative!\n", i_row);
129  REQUIRE(i_col>=0, "index %d negative!\n", i_col);
130  REQUIRE(i_row<num_vectors, "index should be less than %d, %d provided!\n",
131  num_vectors, i_row);
132  REQUIRE(i_col<num_features, "index should be less than %d, %d provided!\n",
133  num_features, i_col);
134 
135  for (index_t i=0; i<sparse_matrix[i_row].num_feat_entries; ++i)
136  {
137  if (i_col==sparse_matrix[i_row].features[i].feat_index)
138  return sparse_matrix[i_row].features[i].entry;
139  }
140  index_t j=sparse_matrix[i_row].num_feat_entries;
141  sparse_matrix[i_row].num_feat_entries=j+1;
142  sparse_matrix[i_row].features=SG_REALLOC(SGSparseVectorEntry<T>,
143  sparse_matrix[i_row].features, j, j+1);
144  sparse_matrix[i_row].features[j].feat_index=i_col;
145  sparse_matrix[i_row].features[j].entry=static_cast<T>(0);
146 
147  return sparse_matrix[i_row].features[j].entry;
148  }
149 
154  void load(CFile* loader);
155 
163  SGVector<float64_t> load_with_labels(CLibSVMFile* libsvm_file, bool do_sort_features=true);
164 
169  void save(CFile* saver);
170 
176  void save_with_labels(CLibSVMFile* saver, SGVector<float64_t> labels);
177 
180 
181  void from_dense(SGMatrix<T> full);
182 
184  void sort_features();
185 
186 protected:
187 
189  virtual void copy_data(const SGReferencedData& orig);
190 
192  virtual void init_data();
193 
195  virtual void free_data();
196 
197 public:
198 
201 
204 
207 
208 };
209 }
210 #endif // __SGSPARSEMATRIX_H__

SHOGUN Machine Learning Toolbox - Documentation