15 #include <boost/thread.hpp>
17 #include "CommonTypes.h"
21 #include "Representer.h"
34 typedef typename Representer<T>::PointType PointType;
36 typedef typename DomainType::DomainPointsListType DomainPointsListType;
39 return new Nystrom(representer, kernel, numEigenfunctions, numberOfPointsForApproximation);
53 MatrixType kxi = MatrixType::Zero(kernelDim, m_nystromPoints.size() * kernelDim);
55 for (
unsigned j = 0; j < m_nystromPoints.size(); j++) {
56 kxi.block(0, j * kernelDim, kernelDim, kernelDim) = m_kernel(pt, m_nystromPoints[j]);
60 MatrixType resMat = MatrixType::Zero(kernelDim, m_numEigenfunctions);
61 for (
unsigned j = 0; j < m_numEigenfunctions; j++) {
62 MatrixType x = (kxi * m_nystromMatrix.col(j));
63 resMat.block(0, j, kernelDim, 1) = x;
81 : m_representer(representer), m_kernel(kernel), m_numEigenfunctions(numEigenfunctions) {
83 DomainType domain = m_representer->GetDomain();
84 m_nystromPoints = getNystromPoints(domain, numberOfPointsForApproximation);
92 computeKernelMatrixDecomposition(&kernel, m_nystromPoints, numEigenfunctions, U, D);
96 float normFactor =
static_cast<float>(m_nystromPoints.size()) / static_cast<float>(numDomainPoints);
97 m_nystromMatrix = std::sqrt(normFactor) * (U.leftCols(numEigenfunctions)
98 * D.topRows(numEigenfunctions).asDiagonal().inverse());
100 m_eigenvalues = (1.0f / normFactor) * D.topRows(numEigenfunctions);
111 std::vector<PointType> getNystromPoints(
DomainType& domain,
unsigned numberOfPoints)
const {
115 std::vector<PointType> shuffledDomainPoints = domain.
GetDomainPoints();
116 std::random_shuffle ( shuffledDomainPoints.begin(), shuffledDomainPoints.end() );
118 return std::vector<PointType>(shuffledDomainPoints.begin(), shuffledDomainPoints.begin() + numberOfPoints);
129 void computeKernelMatrixDecomposition(
const MatrixValuedKernel<PointType>* kernel,
130 const std::vector<PointType>& xs,
unsigned numComponents,
131 MatrixType& U, VectorType& D)
const {
132 unsigned kernelDim = kernel->GetDimension();
134 unsigned n = xs.size();
135 MatrixTypeDoublePrecision K = MatrixTypeDoublePrecision::Zero(
136 n * kernelDim, n * kernelDim);
137 for (
unsigned i = 0; i < n; ++i) {
138 for (
unsigned j = i; j < n; ++j) {
139 MatrixType k_xixj = (*kernel)(xs[i], xs[j]);
140 for (
unsigned d1 = 0; d1 < kernelDim; d1++) {
141 for (
unsigned d2 = 0; d2 < kernelDim; d2++) {
142 double elem_d1d2 = k_xixj(d1, d2);
143 K(i * kernelDim + d1, j * kernelDim + d2) = elem_d1d2;
144 K(j * kernelDim + d2, i * kernelDim + d1) = elem_d1d2;
150 typedef RandSVD<double> SVDType;
151 SVDType svd(K, numComponents * kernelDim);
159 Nystrom<T>& operator=(
const Nystrom<T>& rhs);
160 Nystrom(
const Nystrom<T>& orig);
167 const Representer<T>* m_representer;
168 MatrixType m_nystromMatrix;
169 VectorType m_eigenvalues;
170 std::vector<PointType> m_nystromPoints;
171 const MatrixValuedKernel<PointType>& m_kernel;
172 unsigned m_numEigenfunctions;
MatrixType computeEigenfunctionsAtPoint(const PointType &pt) const
Definition: Nystrom.h:46
const unsigned GetNumberOfPoints() const
Definition: Domain.h:72
virtual unsigned GetDimension() const
Definition: Kernels.h:82
const DomainPointsListType & GetDomainPoints() const
Definition: Domain.h:67
float ScalarType
the type that is used for all vector and matrices throughout the library.
Definition: CommonTypes.h:60
const VectorType & getEigenvalues() const
Definition: Nystrom.h:72