/** 
* @file KernelRBF.cpp
* @brief Interface for the popular exponential mercer kernels
* @author Erik Rodner
* @date 10/24/2007

*/

#include <iostream>

#include <math.h>
#include "KernelRBF.h"

using namespace OBJREC;

using namespace std;
using namespace NICE;



KernelRBF::KernelRBF( double loggamma, double _noise )
{
    gammasq = exp(2*loggamma);
	noise = _noise;
}

KernelRBF::KernelRBF ( const KernelRBF & src )
{
	gammasq = src.gammasq;
	noise = src.noise;
}

KernelRBF::~KernelRBF()
{
}

KernelRBF *KernelRBF::clone(void) const
{
	return new KernelRBF ( *this );
}

	
double KernelRBF::K (const NICE::Vector & x, const NICE::Vector & y) const
{
	if ( x.size() != y.size() ) {
		cerr << "KernelRBF: dimensions: " << x.size() << " vs " << y.size() << endl;
		fthrow(Exception, "Vector dimensions do not match!");
	}

	double dist = 0.0;
	for ( size_t i = 0 ; i < x.size() ; i++ )
	{
		double d = x[i] - y[i];
		dist += d*d;
	}
    return exp( - dist / gammasq );
}

void KernelRBF::updateKernelData ( KernelData *kernelData ) const
{
	const NICE::Matrix & quadraticDistances = kernelData->getCachedMatrix ( KernelData::QUADRATIC_DISTANCES );
	NICE::Matrix & kernelMatrix = kernelData->getKernelMatrix();
	kernelMatrix.resize ( quadraticDistances.rows(), quadraticDistances.cols() );

	for ( size_t i = 0 ; i < kernelMatrix.rows(); i++ )
		for ( size_t j = 0 ; j < kernelMatrix.cols(); j++ ) 
		{
			double dist = quadraticDistances(i,j);
			kernelMatrix(i,j) = exp ( - dist / gammasq  );
		}

	kernelMatrix.addIdentity ( noise );
}

void KernelRBF::calcKernelData ( const VVector & X, KernelData *kernelData ) const
{
	CachedQuadraticDistances::calcKernelData ( X, kernelData );
	if ( noise != 0.0 ) {
		cerr << "KernelRBF: adding some noise" << endl;
		kernelData->getKernelMatrix().addIdentity(noise);
	}
}

void KernelRBF::getKernelJacobi ( size_t parameter, const NICE::Vector & parameters, const KernelData *kernelData, NICE::Matrix & jacobiMatrix ) const
{
	// double _gammasq = exp(2*parameters[0]);
	const NICE::Matrix & kernelMatrix = kernelData->getKernelMatrix();

	jacobiMatrix.resize ( kernelMatrix.rows(), kernelMatrix.cols() );
	if ( parameter == 0 ) {
		for ( size_t i = 0 ; i < kernelMatrix.rows(); i++ )
			for ( size_t j = 0 ; j < kernelMatrix.cols(); j++ ) 
			{
				double K = kernelMatrix(i,j);
				if ( almostZero(K) ) 
					jacobiMatrix(i,j) = 0.0;
				else
					jacobiMatrix(i,j) = - K * 2.0 * log (K);
			}
	} else{
		fthrow(Exception, "Wrong parameter index!");
	}
}
void KernelRBF::setParameters( const NICE::Vector & newParameters ) 
{
	gammasq = exp(2*newParameters[0]);
}

void KernelRBF::getParameters( NICE::Vector & newParameters ) const
{
	newParameters.resize(1);
	newParameters[0] = 0.5 * log(gammasq);
}
