SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SparseMatrixOperator.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) 2013 Soumyajit De
8  */
9 
10 #ifndef SPARSE_MATRIX_OPERATOR_H_
11 #define SPARSE_MATRIX_OPERATOR_H_
12 
13 #include <shogun/lib/config.h>
17 
18 namespace shogun
19 {
20 template<class T> class SGVector;
21 template<class T> class SGSparseMatrix;
22 
29 {
32 
40  SparsityStructure(index_t* row_offsets, index_t* column_indices,
41  index_t num_rows)
42  : m_num_rows(num_rows),
43  m_ptr(new int32_t*[num_rows]())
44  {
45  for (index_t i=0; i<m_num_rows; ++i)
46  {
47  index_t current_index=row_offsets[i];
48  index_t new_index=row_offsets[i+1];
49  index_t length_row=(new_index-current_index);
50 
51  m_ptr[i]=new int32_t[length_row+1]();
52  m_ptr[i][0]=length_row;
53 
54  for (index_t j=1; j<=length_row; ++j)
55  m_ptr[i][j]=column_indices[current_index++];
56  }
57  }
58 
61  {
62  for (index_t i=0; i<m_num_rows; ++i)
63  delete [] m_ptr[i];
64  delete [] m_ptr;
65  }
66 
69  {
70  for (index_t i=0; i<m_num_rows; ++i)
71  {
72  index_t nnzs=m_ptr[i][0];
73  SG_SPRINT("Row number %d. Number of Non-zeros %d. Colums ", i, nnzs);
74  for(index_t j=1; j<=nnzs; ++j)
75  {
76  SG_SPRINT("%d", m_ptr[i][j]);
77  if (j<nnzs)
78  SG_SPRINT(", ");
79  }
80  SG_SPRINT("\n");
81  }
82  }
83 
86 
88  int32_t **m_ptr;
89 };
90 
91 
98 template<class T> class CSparseMatrixOperator : public CMatrixOperator<T>
99 {
101 typedef bool supports_complex128_t;
102 
103 public:
106 
113 
120 
123 
130  virtual SGVector<T> apply(SGVector<T> b) const;
131 
137  virtual void set_diagonal(SGVector<T> diag);
138 
144  virtual SGVector<T> get_diagonal() const;
145 
148 
150  SparsityStructure* get_sparsity_structure(int64_t power=1) const;
151 
155  template<class Scalar>
156  inline operator CSparseMatrixOperator<Scalar>*() const
157  {
158  REQUIRE(m_operator.sparse_matrix, "Matrix is not initialized!\n");
159  typedef SGSparseVector<Scalar> vector;
160  typedef SGSparseVectorEntry<Scalar> entry;
161 
162  vector* rows=SG_MALLOC(vector, m_operator.num_vectors);
163 
164  for (index_t i=0; i<m_operator.num_vectors; ++i)
165  {
166  entry* features=SG_MALLOC(entry, m_operator[i].num_feat_entries);
167  for (index_t j=0; j<m_operator[i].num_feat_entries; ++j)
168  {
169  features[j].feat_index=m_operator[i].features[j].feat_index;
170  features[j].entry=static_cast<Scalar>(m_operator[i].features[j].entry);
171  }
172  rows[i].features=features;
173  rows[i].num_feat_entries=m_operator[i].num_feat_entries;
174  }
175 
176  SGSparseMatrix<Scalar> casted_m;
177  casted_m.sparse_matrix=rows;
178  casted_m.num_vectors=m_operator.num_vectors;
179  casted_m.num_features= m_operator.num_features;
180 
181  return new CSparseMatrixOperator<Scalar>(casted_m);
182  }
183 
185  virtual const char* get_name() const
186  {
187  return "SparseMatrixOperator";
188  }
189 
190 private:
192  SGSparseMatrix<T> m_operator;
193 
195  void init();
196 
197 };
198 
199 }
200 #endif // SPARSE_MATRIX_OPERATOR_H_
SparsityStructure * get_sparsity_structure(int64_t power=1) const
template class SGSparseMatrix
SparsityStructure(index_t *row_offsets, index_t *column_indices, index_t num_rows)
SGSparseMatrix< T > get_matrix_operator() const
int32_t index_t
Definition: common.h:62
#define REQUIRE(x,...)
Definition: SGIO.h:206
virtual SGVector< T > get_diagonal() const
virtual const char * get_name() const
virtual SGVector< T > apply(SGVector< T > b) const
#define SG_SPRINT(...)
Definition: SGIO.h:180
index_t num_features
total number of features
shogun vector
SGSparseVector< T > * sparse_matrix
array of sparse vectors of size num_vectors
virtual void set_diagonal(SGVector< T > diag)
Struct that represents the sparsity structure of the Sparse Matrix in CRS. Implementation has been ad...
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
Class that represents a sparse-matrix linear operator. It computes matrix-vector product in its appl...
template class SGSparseVectorEntry
Definition: File.h:23
template class SGSparseVector The assumtion is that the stored SGSparseVectorEntry* vector is orde...
Abstract base class that represents a matrix linear operator. It provides an interface to computes ma...
index_t num_vectors
total number of vectors

SHOGUN Machine Learning Toolbox - Documentation