SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
eigen3.cpp
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 #include <shogun/lib/common.h>
11 
15 
16 using namespace Eigen;
17 
18 namespace shogun
19 {
20 
21 template<typename T>
23  {
24  REQUIRE(sg_matrix.num_vectors>0,
25  "EigenSparseUtil::toEigenSparse(): \
26  Number of rows must be positive!\n");
27  REQUIRE(sg_matrix.num_features>0,
28  "EigenSparseUtil::toEigenSparse(): \
29  Number of cols must be positive!\n");
30  REQUIRE(sg_matrix.sparse_matrix,
31  "EigenSparseUtil::toEigenSparse(): \
32  sg_matrix is not initialized!\n");
33 
34  index_t num_rows=sg_matrix.num_vectors;
35  index_t num_cols=sg_matrix.num_features;
36 
37  typedef Eigen::Triplet<T> SparseTriplet;
38 
39  std::vector<SparseTriplet> tripletList;
40  for (index_t i=0; i<num_rows; ++i)
41  {
42  for (index_t k=0; k<sg_matrix[i].num_feat_entries; ++k)
43  {
44  index_t &index_i=i;
45  index_t &index_j=sg_matrix[i].features[k].feat_index;
46  T &val=sg_matrix[i].features[k].entry;
47  tripletList.push_back(SparseTriplet(index_i, index_j, val));
48  }
49  }
50 
51 #ifdef EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
52  DynamicSparseMatrix<T> dM(num_rows, num_cols);
53  dM.reserve(tripletList.size());
54 
55  for (typename std::vector<SparseTriplet>::iterator it=tripletList.begin();
56  it!=tripletList.end(); ++it )
57  dM.coeffRef(it->row(), it->col())+=it->value();
58 
59  SparseMatrix<T> M(dM);
60 #else // EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
61  SparseMatrix<T> M(num_rows, num_cols);
62  M.setFromTriplets(tripletList.begin(), tripletList.end());
63 #endif // EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
64 
65  return M;
66  }
67 
68 template class EigenSparseUtil<bool>;
69 template class EigenSparseUtil<float64_t>;
70 template class EigenSparseUtil<complex128_t>;
71 }
template class SGSparseMatrix
int32_t index_t
Definition: common.h:62
Definition: SGMatrix.h:20
#define REQUIRE(x,...)
Definition: SGIO.h:206
index_t num_features
total number of features
SGSparseVector< T > * sparse_matrix
array of sparse vectors of size num_vectors
This class contains some utilities for Eigen3 Sparse Matrix integration with shogun. Currently it provides a method for converting SGSparseMatrix to Eigen3 SparseMatrix.
Definition: eigen3.h:79
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
index_t num_vectors
total number of vectors

SHOGUN Machine Learning Toolbox - Documentation