SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 
40  SGSparseMatrix(SGSparseVector<T>* vecs, index_t num_feat,
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 
50  SGSparseMatrix(SGMatrix<T> dense);
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, "Provided row index %d negative!\n", i_row);
109  REQUIRE(i_col>=0, "Provided column index %d negative!\n", i_col);
110  REQUIRE(i_row<num_features, "Provided row index (%d) is larger than number of rows (%d)\n",
111  i_row, num_features);
112  REQUIRE(i_col<num_vectors, "Provided column index (%d) is larger than number of columns (%d)\n",
113  i_col, num_vectors);
114 
115  for (index_t i=0; i<sparse_matrix[i_col].num_feat_entries; ++i)
116  {
117  if (i_row==sparse_matrix[i_col].features[i].feat_index)
118  return sparse_matrix[i_col].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, "Provided row index %d negative!\n", i_row);
130  REQUIRE(i_col>=0, "Provided column index %d negative!\n", i_col);
131  REQUIRE(i_row<num_features, "Provided row index (%d) is larger than number of rows (%d)\n",
132  i_row, num_features);
133  REQUIRE(i_col<num_vectors, "Provided column index (%d) is larger than number of columns (%d)\n",
134  i_col, num_vectors);
135 
136  for (index_t i=0; i<sparse_matrix[i_col].num_feat_entries; ++i)
137  {
138  if (i_row==sparse_matrix[i_col].features[i].feat_index)
139  return sparse_matrix[i_col].features[i].entry;
140  }
141  index_t j=sparse_matrix[i_col].num_feat_entries;
142  sparse_matrix[i_col].num_feat_entries=j+1;
143  sparse_matrix[i_col].features=SG_REALLOC(SGSparseVectorEntry<T>,
144  sparse_matrix[i_col].features, j, j+1);
145  sparse_matrix[i_col].features[j].feat_index=i_row;
146  sparse_matrix[i_col].features[j].entry=static_cast<T>(0);
147  return sparse_matrix[i_col].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 
185  void from_dense(SGMatrix<T> full);
186 
188  void sort_features();
189 
190 protected:
191 
193  virtual void copy_data(const SGReferencedData& orig);
194 
196  virtual void init_data();
197 
199  virtual void free_data();
200 
201 public:
202 
205 
208 
211 
212 };
213 }
214 #endif // __SGSPARSEMATRIX_H__
void load(CFile *loader)
T & operator()(index_t i_row, index_t i_col)
template class SGSparseMatrix
Definition: Parameter.h:27
int32_t index_t
Definition: common.h:62
#define REQUIRE(x,...)
Definition: SGIO.h:206
SGVector< float64_t > load_with_labels(CLibSVMFile *libsvm_file, bool do_sort_features=true)
shogun matrix
Definition: Parameter.h:26
SGSparseMatrix< T > get_transposed()
index_t vlen
Definition: SGVector.h:494
const T operator()(index_t i_row, index_t i_col) const
index_t num_features
total number of features
shogun vector
Definition: Parameter.h:28
shogun reference count managed data
const SGSparseVector< T > & operator[](index_t index) const
A File access base class.
Definition: File.h:34
void from_dense(SGMatrix< T > full)
SGSparseVector< T > * sparse_matrix
array of sparse vectors of size num_vectors
void save(CFile *saver)
const SGVector< T > operator*(SGVector< T > v) const
virtual void copy_data(const SGReferencedData &orig)
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
read sparse real valued features in svm light format e.g. -1 1:10.0 2:100.2 1000:1.3 with -1 == (optional) label and dim 1 - value 10.0 dim 2 - value 100.2 dim 1000 - value 1.3
Definition: LibSVMFile.h:34
template class SGSparseVectorEntry
Definition: File.h:23
SGSparseVector< T > & operator[](index_t index)
template class SGSparseVector The assumtion is that the stored SGSparseVectorEntry* vector is orde...
Definition: Parameter.h:29
void save_with_labels(CLibSVMFile *saver, SGVector< float64_t > labels)
index_t num_vectors
total number of vectors

SHOGUN Machine Learning Toolbox - Documentation