Пример #1
0
	/** @brief	Overloading the test fixture set up. */
	virtual void SetUp()
	{
		// Call the parent set up.
		TestFunctionDataSetting::SetUp();

		// Set the hyperparameters.
		logHyp.cov(0) = log(ell);
		logHyp.cov(1) = log(sigma_f);
		logHyp.lik(0) = log(sigma_n);

		// Some constants
		N					= trainingData.N();

		pInvSqrtD		= invSqrtD(logHyp.lik, trainingData);
		pL1				= choleskyFactor(logHyp.cov, logHyp.lik, trainingData);
		pL2				= choleskyFactor(logHyp.cov, trainingData, pInvSqrtD);
		pY_M				= y_m(logHyp.mean, trainingData);

		pAlpha1			= alpha(pInvSqrtD, pL1, pY_M);
		pAlpha2			= alpha(logHyp.lik, pL1, pY_M);
		pQ1				= q(pInvSqrtD, pL1, pAlpha1);
		pQ2				= q(logHyp.lik, pL1, pAlpha2);
		dnlZWRTLikHyp1 = dnlZWRTLikHyp(logHyp.lik, trainingData, pQ1);
		dnlZWRTLikHyp2 = dnlZWRTLikHyp(logHyp.lik, pQ2);
	}
Пример #2
0
BSplineFit<T>::BSplineFit( int iDimension, int iSampleQuantity, const T* afSampleData, int iDegree, int iControlQuantity )
    : m_kBasis( iControlQuantity, iDegree )
{
	if( iControlQuantity <= iDegree + 1 ) iControlQuantity = iDegree + 2;
	if( iControlQuantity > iSampleQuantity ) iControlQuantity = iSampleQuantity;
	iDegree = constrain( iDegree, 1, iControlQuantity - 1 );	

	assert(iDimension >= 1);
	assert(1 <= iDegree && iDegree < iControlQuantity);
	assert(iControlQuantity <= iSampleQuantity);

	m_iDimension = iDimension;
	m_iSampleQuantity = iSampleQuantity;
	m_afSampleData = afSampleData;
	m_iDegree = iDegree;
	m_iControlQuantity = iControlQuantity;
	m_afControlData = new T[m_iDimension*iControlQuantity];

	// Fit the data points with a B-spline curve using a least-squares error
	// metric.  The problem is of the form A^T*A*X = A^T*B.
	BSplineFitBasisd kDBasis(m_iControlQuantity,m_iDegree);
	double dTMultiplier = 1.0/(double)(m_iSampleQuantity - 1);
	double dT;
	int i0, i1, i2, iMin, iMax, j;

	// Construct the matrix A (depends only on the output basis function).
	BandedMatrixd* pkAMat = new BandedMatrixd( m_iControlQuantity, m_iDegree+1, m_iDegree + 1 );

	for (i0 = 0; i0 < m_iControlQuantity; i0++) {
		for ( i1 = 0; i1 < i0; i1++ ) {
			(*pkAMat)(i0,i1) = (*pkAMat)(i1,i0);
		}

		int i1Max = i0 + m_iDegree;
		if (i1Max >= m_iControlQuantity)
		{
			i1Max = m_iControlQuantity - 1;
		}

		for (i1 = i0; i1 <= i1Max; i1++)
		{
			double dValue = 0.0;
			for (i2 = 0; i2 < m_iSampleQuantity; i2++)
			{
				dT = dTMultiplier*(double)i2;
				kDBasis.compute(dT,iMin,iMax);
				if (iMin <= i0 && i0 <= iMax && iMin <= i1 && i1 <= iMax)
				{
					double dB0 = kDBasis.getValue(i0 - iMin);
					double dB1 = kDBasis.getValue(i1 - iMin);
					dValue += dB0*dB1;
				}
			}
			(*pkAMat)(i0,i1) = dValue;
		}
	}

	// Construct the matrix B.
	double** aadBMat;
	aadBMat = new double*[m_iControlQuantity];
	aadBMat[0] = new double[m_iControlQuantity*m_iSampleQuantity];
	for( int iRow = 1; iRow < m_iControlQuantity; iRow++ ) {
		aadBMat[iRow] = &aadBMat[0][m_iSampleQuantity*iRow];
	}

	memset( aadBMat[0],0,m_iControlQuantity*m_iSampleQuantity*sizeof(double) );
	for (i0 = 0; i0 < m_iControlQuantity; i0++)
	{
		for (i1 = 0; i1 < m_iSampleQuantity; i1++)
		{
			dT = dTMultiplier*(double)i1;
			kDBasis.compute(dT,iMin,iMax);
			if (iMin <= i0 && i0 <= iMax)
			{
				aadBMat[i0][i1] = kDBasis.getValue(i0 - iMin);
			}
		}
	}

	// Construct the control points for the least-squares curve.
	double* adControlData = new double[m_iDimension*m_iControlQuantity];
	memset( adControlData,0,m_iDimension*m_iControlQuantity*sizeof(double) );
	double* pdBaseTarget = adControlData;
	for (i0 = 0; i0 < m_iControlQuantity; i0++)
	{
		const T* pfSource = m_afSampleData;
		double* adTarget = pdBaseTarget;
		for (i1 = 0; i1 < m_iSampleQuantity; i1++)
		{
			double dBValue = aadBMat[i0][i1];
			for (j = 0; j < m_iDimension; j++)
			{
				adTarget[j] += dBValue*(double)(*pfSource++);
			}
		}
		pdBaseTarget += m_iDimension;
	}

	// Solve A^T*A*ControlData = A^T*B*SampleData.
	bool bSolved = choleskyFactor(*pkAMat);
	assert(bSolved);
	bSolved = solveLower(*pkAMat,adControlData);
	assert(bSolved);
	bSolved = solveUpper(*pkAMat,adControlData);
	assert(bSolved);

	// Set the B-spline control points.
	T* pfTarget = m_afControlData;
	const double* pdSource = adControlData;
	for (i0 = 0; i0 < m_iDimension*m_iControlQuantity; i0++)
	{
		*pfTarget++ = (T)(*pdSource++);
	}

	// Set the first and last output control points to match the first and
	// last input samples.  This supports the application of fitting keyframe
	// data with B-spline curves.  The user expects that the curve passes
	// through the first and last positions in order to support matching two
	// consecutive keyframe sequences.
	T* pfCEnd0 = m_afControlData;
	const T* pfSEnd0 = m_afSampleData;
	T* pfCEnd1 = &m_afControlData[m_iDimension*(m_iControlQuantity-1)];
	const T* pfSEnd1 = &m_afSampleData[m_iDimension*(m_iSampleQuantity-1)];
	for (j = 0; j < m_iDimension; j++)
	{
		*pfCEnd0++ = *pfSEnd0++;
		*pfCEnd1++ = *pfSEnd1++;
	}

	delete [] adControlData;
	if( aadBMat ) {
        delete [] aadBMat[0];
        delete [] aadBMat;
        aadBMat = 0;
    }
	delete pkAMat;
}