SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
eigen3.cpp
浏览该文件的文档.
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
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:73
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 机器学习工具包 - 项目文档