SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 
12 #ifdef HAVE_EIGEN3
16 
17 using namespace Eigen;
18 
19 namespace shogun
20 {
21 
22 template<typename T>
24  {
25  REQUIRE(sg_matrix.num_vectors>0,
26  "EigenSparseUtil::toEigenSparse(): \
27  Number of rows must be positive!\n");
28  REQUIRE(sg_matrix.num_features>0,
29  "EigenSparseUtil::toEigenSparse(): \
30  Number of cols must be positive!\n");
31  REQUIRE(sg_matrix.sparse_matrix,
32  "EigenSparseUtil::toEigenSparse(): \
33  sg_matrix is not initialized!\n");
34 
35  index_t num_rows=sg_matrix.num_vectors;
36  index_t num_cols=sg_matrix.num_features;
37 
38  typedef Eigen::Triplet<T> SparseTriplet;
39 
40  std::vector<SparseTriplet> tripletList;
41  for (index_t i=0; i<num_rows; ++i)
42  {
43  for (index_t k=0; k<sg_matrix[i].num_feat_entries; ++k)
44  {
45  index_t &index_i=i;
46  index_t &index_j=sg_matrix[i].features[k].feat_index;
47  T &val=sg_matrix[i].features[k].entry;
48  tripletList.push_back(SparseTriplet(index_i, index_j, val));
49  }
50  }
51 
52 #ifdef EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
53  DynamicSparseMatrix<T> dM(num_rows, num_cols);
54  dM.reserve(tripletList.size());
55 
56  for (typename std::vector<SparseTriplet>::iterator it=tripletList.begin();
57  it!=tripletList.end(); ++it )
58  dM.coeffRef(it->row(), it->col())+=it->value();
59 
60  SparseMatrix<T> M(dM);
61 #else // EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
62  SparseMatrix<T> M(num_rows, num_cols);
63  M.setFromTriplets(tripletList.begin(), tripletList.end());
64 #endif // EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
65 
66  return M;
67  }
68 
69 template class EigenSparseUtil<bool>;
70 template class EigenSparseUtil<float64_t>;
71 template class EigenSparseUtil<complex128_t>;
72 }
73 #endif //HAVE_EIGEN3

SHOGUN Machine Learning Toolbox - Documentation