13 #ifndef KERNELCOMBINATORS_H
14 #define KERNELCOMBINATORS_H
16 #include <boost/scoped_ptr.hpp>
17 #include <boost/thread/mutex.hpp>
18 #include <boost/unordered_map.hpp>
20 #include "CommonTypes.h"
23 #include "Representer.h"
30 template<
class TPo
int>
44 "Kernels in SumKernel must have the same dimensionality");
48 MatrixType
operator()(
const TPoint& x,
const TPoint& y)
const {
49 return (*m_lhs)(x, y) + (*m_rhs)(x, y);
53 std::ostringstream os;
59 const MatrixValuedKernelType* m_lhs;
60 const MatrixValuedKernelType* m_rhs;
69 template<
class TPo
int>
82 "Kernels in SumKernel must have the same dimensionality");
87 MatrixType
operator()(
const TPoint& x,
const TPoint& y)
const {
88 return (*m_lhs)(x, y) * (*m_rhs)(x, y);
92 std::ostringstream os;
98 const MatrixValuedKernelType* m_lhs;
99 const MatrixValuedKernelType* m_rhs;
107 template<
class TPo
int>
116 double scalingFactor) :
120 MatrixType
operator()(
const TPoint& x,
const TPoint& y)
const {
121 return (*m_kernel)(x, y) * m_scalingFactor;
124 std::ostringstream os;
125 os << (*m_kernel).GetKernelInfo() <<
" * " << m_scalingFactor;
130 const MatrixValuedKernelType* m_kernel;
131 double m_scalingFactor;
140 template<
class TPo
int>
148 unsigned dimension) :
150 m_ident(MatrixType::Identity(dimension, dimension)) {
153 MatrixType
operator()(
const TPoint& x,
const TPoint& y)
const {
155 return m_ident * (*m_kernel)(x, y);
162 std::ostringstream os;
163 os <<
"UncorrelatedMatrixValuedKernel(" << (*m_kernel).GetKernelInfo()
164 <<
", " << this->m_dimension <<
")";
179 template <
class TPo
int>
182 virtual double operator()(
const TPoint& pt)
const = 0;
196 typedef boost::unordered_map<statismo::VectorType, statismo::MatrixType> CacheType;
200 typedef Representer<T> RepresenterType;
201 typedef typename RepresenterType::PointType PointType;
214 : m_representer(representer),
216 m_nystrom(
Nystrom<T>::Create(representer, kernel, numEigenfunctions, numberOfPointsForApproximation == 0 ? numEigenfunctions * 2 : numberOfPointsForApproximation)),
217 m_eigenvalues(m_nystrom->getEigenvalues()),
218 m_cacheValues(cacheValues),
222 inline MatrixType operator()(
const PointType& x,
const PointType& y)
const {
224 MatrixType sum = MatrixType::Zero(this->m_dimension, this->m_dimension);
226 float eta_x = m_eta(x);
227 float eta_y = m_eta(y);
230 statismo::MatrixType phisAtX = phiAtPoint(x);
231 statismo::MatrixType phisAtY = phiAtPoint(y);
233 double largestTemperedEigenvalue = std::pow(m_eigenvalues(0), (eta_x + eta_y)/2);
235 for (
unsigned i = 0; i < m_eigenvalues.size(); ++i) {
237 float temperedEigenvalue = std::pow(m_eigenvalues(i), (eta_x + eta_y)/2);
241 if (temperedEigenvalue / largestTemperedEigenvalue < 1e-6) {
244 sum += phisAtX.col(i) * phisAtY.col(i).transpose() * temperedEigenvalue;
248 float normalizationFactor = largestTemperedEigenvalue / m_eigenvalues(0);
249 sum *= 1.0 / normalizationFactor;
258 std::ostringstream os;
259 os <<
"SpatiallyVaryingKernel";
269 const statismo::MatrixType phiAtPoint(
const PointType& pt)
const {
271 statismo::MatrixType v;
275 const VectorType ptAsVec = this->m_representer->PointToVector(pt);
276 _phiCacheLock.lock();
277 typename CacheType::const_iterator got = m_phiCache.find (ptAsVec);
278 _phiCacheLock.unlock();
279 if (got == m_phiCache.end()) {
280 v = m_nystrom->computeEigenfunctionsAtPoint(pt);
281 _phiCacheLock.lock();
282 m_phiCache.insert(std::make_pair(ptAsVec, v));
283 _phiCacheLock.unlock();
288 v = m_nystrom->computeEigenfunctionsAtPoint(pt);
298 boost::scoped_ptr<Nystrom<T> > m_nystrom;
299 statismo::VectorType m_eigenvalues;
300 const TemperingFunction<PointType>& m_eta;
302 mutable CacheType m_phiCache;
303 mutable boost::mutex _phiCacheLock;
310 #endif // KERNELCOMBINATORS_H
Definition: KernelCombinators.h:194
virtual unsigned GetDimension() const
Definition: Kernels.h:82
Definition: KernelCombinators.h:70
A trivial representer, that does no representation at all, but works directly with vectorial data...
Definition: TrivialVectorialRepresenter.h:83
std::string GetKernelInfo() const
Definition: KernelCombinators.h:257
virtual std::string GetKernelInfo() const =0
MatrixType operator()(const TPoint &x, const TPoint &y) const
Definition: KernelCombinators.h:87
Generic Exception class for the statismo Library.
Definition: Exceptions.h:68
MatrixType operator()(const TPoint &x, const TPoint &y) const
Definition: KernelCombinators.h:120
SpatiallyVaryingKernel(const RepresenterType *representer, const MatrixValuedKernel< PointType > &kernel, const TemperingFunction< PointType > &eta, unsigned numEigenfunctions, unsigned numberOfPointsForApproximation=0, bool cacheValues=true)
Make a given kernel spatially varying according to the given tempering function.
Definition: KernelCombinators.h:213
MatrixType operator()(const TPoint &x, const TPoint &y) const
Definition: KernelCombinators.h:48
std::string GetKernelInfo() const
Definition: KernelCombinators.h:91
Definition: KernelCombinators.h:180
std::string GetKernelInfo() const
Definition: KernelCombinators.h:123
std::string GetKernelInfo() const
Definition: KernelCombinators.h:52
Definition: KernelCombinators.h:108
Definition: KernelCombinators.h:31