[mlpack-git] master: sparse svec (c47ddf1)
gitdub at big.cc.gt.atl.ga.us
gitdub at big.cc.gt.atl.ga.us
Mon Feb 2 15:16:26 EST 2015
Repository : https://github.com/mlpack/mlpack
On branch : master
Link : https://github.com/mlpack/mlpack/compare/bb6e5c56aab07e6449d86021246b52a9e323f3a0...bd6cb33f8d8270b02a84e81e38727679bb6c319a
>---------------------------------------------------------------
commit c47ddf1f815b2a7517fee228a56d8cb80ba908d4
Author: Stephen Tu <stephent at berkeley.edu>
Date: Wed Jan 21 12:05:49 2015 -0800
sparse svec
>---------------------------------------------------------------
c47ddf1f815b2a7517fee228a56d8cb80ba908d4
src/mlpack/core/math/lin_alg.cpp | 22 ++++++++++++++++++++++
src/mlpack/core/math/lin_alg.hpp | 9 +++++++++
src/mlpack/tests/lin_alg_test.cpp | 25 +++++++++++++++++++++++++
3 files changed, 56 insertions(+)
diff --git a/src/mlpack/core/math/lin_alg.cpp b/src/mlpack/core/math/lin_alg.cpp
index a4ae426..b15c119 100644
--- a/src/mlpack/core/math/lin_alg.cpp
+++ b/src/mlpack/core/math/lin_alg.cpp
@@ -232,6 +232,28 @@ void mlpack::math::Svec(const arma::mat& input, arma::vec& output)
}
}
+void mlpack::math::Svec(const arma::sp_mat& input, arma::sp_mat& output)
+{
+ const size_t n = input.n_rows;
+ const size_t n2bar = n * (n + 1) / 2;
+
+ output.zeros(n2bar, 1);
+
+ size_t idx = 0;
+
+ for (auto it = input.begin(); it != input.end(); ++it)
+ {
+ const size_t i = it.row();
+ const size_t j = it.col();
+ if (i > j)
+ continue;
+ if (i == j)
+ output(SvecIndex(i, j, n)) = *it;
+ else
+ output(SvecIndex(i, j, n)) = sq2 * (*it);
+ }
+}
+
void mlpack::math::Smat(const arma::vec& input, arma::mat& output)
{
const size_t n = static_cast<size_t>(ceil((-1. + sqrt(1. + 8. * input.n_elem))/2.));
diff --git a/src/mlpack/core/math/lin_alg.hpp b/src/mlpack/core/math/lin_alg.hpp
index 1930a53..0e0998e 100644
--- a/src/mlpack/core/math/lin_alg.hpp
+++ b/src/mlpack/core/math/lin_alg.hpp
@@ -88,6 +88,15 @@ void RemoveRows(const arma::mat& input,
void Svec(const arma::mat& input, arma::vec& output);
/**
+ * Svec for sparse matrices.
+ * NOTE: armadillo doesn't have an sp_vec type, so the output type is sp_mat.
+ *
+ * @param input sparse A symmetric matrix
+ * @param output
+ */
+void Svec(const arma::sp_mat& input, arma::sp_mat& output);
+
+/**
* The inverse of Svec. That is, Smat(Svec(A)) == A.
*
* @param input
diff --git a/src/mlpack/tests/lin_alg_test.cpp b/src/mlpack/tests/lin_alg_test.cpp
index e368b65..ebea714 100644
--- a/src/mlpack/tests/lin_alg_test.cpp
+++ b/src/mlpack/tests/lin_alg_test.cpp
@@ -211,6 +211,31 @@ BOOST_AUTO_TEST_CASE(TestSvecSmat)
BOOST_REQUIRE_CLOSE(X(i, j), Xtest(i, j), 1e-7);
}
+BOOST_AUTO_TEST_CASE(TestSparseSvec)
+{
+ arma::sp_mat X;
+ X.zeros(3, 3);
+ X(1, 0) = X(0, 1) = 1;
+
+ arma::sp_mat sx;
+ Svec(X, sx);
+
+ const double sq2 = sqrt(2.);
+ const double v0 = sx(0, 0);
+ const double v1 = sx(1, 0);
+ const double v2 = sx(2, 0);
+ const double v3 = sx(3, 0);
+ const double v4 = sx(4, 0);
+ const double v5 = sx(5, 0);
+
+ BOOST_REQUIRE_CLOSE(v0, 0, 1e-7);
+ BOOST_REQUIRE_CLOSE(v1, sq2 * 1., 1e-7);
+ BOOST_REQUIRE_CLOSE(v2, 0, 1e-7);
+ BOOST_REQUIRE_CLOSE(v3, 0, 1e-7);
+ BOOST_REQUIRE_CLOSE(v4, 0, 1e-7);
+ BOOST_REQUIRE_CLOSE(v5, 0, 1e-7);
+}
+
BOOST_AUTO_TEST_CASE(TestSymKronIdSimple)
{
arma::mat A(3, 3);
More information about the mlpack-git
mailing list