SHOGUN  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
eigen3.cpp
Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2013 Soumyajit De
00008  */
00009 
00010 #include <shogun/lib/common.h>
00011 
00012 #ifdef HAVE_EIGEN3
00013 #include <shogun/lib/SGSparseMatrix.h>
00014 #include <shogun/lib/SGSparseVector.h>
00015 #include <shogun/mathematics/eigen3.h>
00016 
00017 using namespace Eigen;
00018 
00019 namespace shogun
00020 {
00021 
00022 template<typename T>
00023 SparseMatrix<T> EigenSparseUtil<T>::toEigenSparse(SGSparseMatrix<T> sg_matrix)
00024     {
00025         REQUIRE(sg_matrix.num_vectors>0,
00026             "EigenSparseUtil::toEigenSparse(): \
00027             Number of rows must be positive!\n");
00028         REQUIRE(sg_matrix.num_features>0,
00029             "EigenSparseUtil::toEigenSparse(): \
00030             Number of cols must be positive!\n");
00031         REQUIRE(sg_matrix.sparse_matrix,
00032             "EigenSparseUtil::toEigenSparse(): \
00033             sg_matrix is not initialized!\n");
00034 
00035         index_t num_rows=sg_matrix.num_vectors;
00036         index_t num_cols=sg_matrix.num_features;
00037 
00038         typedef Eigen::Triplet<T> SparseTriplet;
00039 
00040         std::vector<SparseTriplet> tripletList;
00041         for (index_t i=0; i<num_rows; ++i)
00042         {
00043             for (index_t k=0; k<sg_matrix[i].num_feat_entries; ++k)
00044             {
00045                 index_t &index_i=i;
00046                 index_t &index_j=sg_matrix[i].features[k].feat_index;
00047                 T &val=sg_matrix[i].features[k].entry;
00048                 tripletList.push_back(SparseTriplet(index_i, index_j, val));
00049             }
00050         }
00051 
00052 #ifdef EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
00053         DynamicSparseMatrix<T> dM(num_rows, num_cols);
00054         dM.reserve(tripletList.size());
00055 
00056         for (typename std::vector<SparseTriplet>::iterator it=tripletList.begin();
00057             it!=tripletList.end(); ++it )
00058             dM.coeffRef(it->row(), it->col())+=it->value();
00059 
00060         SparseMatrix<T> M(dM);
00061 #else // EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
00062         SparseMatrix<T> M(num_rows, num_cols);
00063         M.setFromTriplets(tripletList.begin(), tripletList.end());
00064 #endif // EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
00065 
00066         return M;
00067     }
00068 
00069 template class EigenSparseUtil<bool>;
00070 template class EigenSparseUtil<float64_t>;
00071 template class EigenSparseUtil<complex128_t>;
00072 }
00073 #endif //HAVE_EIGEN3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation