[mlpack-git] master: Add a function to create a random basis. (b959363)
gitdub at big.cc.gt.atl.ga.us
gitdub at big.cc.gt.atl.ga.us
Mon Nov 2 12:19:27 EST 2015
Repository : https://github.com/mlpack/mlpack
On branch : master
Link : https://github.com/mlpack/mlpack/compare/f86acf8be2c01568d8b3dcd2e529ee9f20f7585e...156787dd4f372a7fd740f733127ac200ea2564b7
>---------------------------------------------------------------
commit b959363b531a720a38cd590e58463d63e0f6452d
Author: Ryan Curtin <ryan at ratml.org>
Date: Mon Nov 2 17:15:54 2015 +0000
Add a function to create a random basis.
>---------------------------------------------------------------
b959363b531a720a38cd590e58463d63e0f6452d
src/mlpack/core.hpp | 1 +
src/mlpack/core/math/CMakeLists.txt | 2 ++
src/mlpack/core/math/random_basis.cpp | 44 +++++++++++++++++++++++++++++++++++
src/mlpack/core/math/random_basis.hpp | 27 +++++++++++++++++++++
4 files changed, 74 insertions(+)
diff --git a/src/mlpack/core.hpp b/src/mlpack/core.hpp
index 96bb674..d2bb50a 100644
--- a/src/mlpack/core.hpp
+++ b/src/mlpack/core.hpp
@@ -162,6 +162,7 @@
#include <mlpack/core/data/normalize_labels.hpp>
#include <mlpack/core/math/clamp.hpp>
#include <mlpack/core/math/random.hpp>
+#include <mlpack/core/math/random_basis.hpp>
#include <mlpack/core/math/lin_alg.hpp>
#include <mlpack/core/math/range.hpp>
#include <mlpack/core/math/round.hpp>
diff --git a/src/mlpack/core/math/CMakeLists.txt b/src/mlpack/core/math/CMakeLists.txt
index 50e6635..88e294d 100644
--- a/src/mlpack/core/math/CMakeLists.txt
+++ b/src/mlpack/core/math/CMakeLists.txt
@@ -7,6 +7,8 @@ set(SOURCES
lin_alg.cpp
random.hpp
random.cpp
+ random_basis.hpp
+ random_basis.cpp
range.hpp
range_impl.hpp
round.hpp
diff --git a/src/mlpack/core/math/random_basis.cpp b/src/mlpack/core/math/random_basis.cpp
new file mode 100644
index 0000000..a1b56c0
--- /dev/null
+++ b/src/mlpack/core/math/random_basis.cpp
@@ -0,0 +1,44 @@
+/**
+ * @file random_basis.cpp
+ * @author Ryan Curtin
+ *
+ * Generate a random d-dimensional basis.
+ */
+#include "random_basis.hpp"
+
+using namespace arma;
+
+namespace mlpack {
+namespace math {
+
+void RandomBasis(mat& basis, const size_t d)
+{
+ while(true)
+ {
+ // [Q, R] = qr(randn(d, d));
+ // Q = Q * diag(sign(diag(R)));
+ mat r;
+ if (qr(basis, r, randn<mat>(d, d)))
+ {
+ vec rDiag(r.n_rows);
+ for (size_t i = 0; i < rDiag.n_elem; ++i)
+ {
+ if (r(i, i) < 0)
+ rDiag(i) = -1;
+ else if (r(i, i) > 0)
+ rDiag(i) = 1;
+ else
+ rDiag(i) = 0;
+ }
+
+ basis *= diagmat(rDiag);
+
+ // Check if the determinant is positive.
+ if (det(basis) >= 0)
+ break;
+ }
+ }
+}
+
+} // namespace math
+} // namespace mlpack
diff --git a/src/mlpack/core/math/random_basis.hpp b/src/mlpack/core/math/random_basis.hpp
new file mode 100644
index 0000000..35a355c
--- /dev/null
+++ b/src/mlpack/core/math/random_basis.hpp
@@ -0,0 +1,27 @@
+/**
+ * @file random_basis.hpp
+ * @author Ryan Curtin
+ *
+ * Generate a random d-dimensional basis.
+ */
+#ifndef __MLPACK_CORE_MATH_RANDOM_BASIS_HPP
+#define __MLPACK_CORE_MATH_RANDOM_BASIS_HPP
+
+#include <mlpack/prereqs.hpp>
+
+namespace mlpack {
+namespace math {
+
+/**
+ * Create a random d-dimensional orthogonal basis, storing it in the given
+ * matrix.
+ *
+ * @param basis Matrix to store basis in.
+ * @param d Desired number of dimensions in the basis.
+ */
+void RandomBasis(arma::mat& basis, const size_t d);
+
+} // namespace math
+} // namespace mlpack
+
+#endif
More information about the mlpack-git
mailing list