Skip to content

Commit

Permalink
Add a polynomial background function. Refs #5306.
Browse files Browse the repository at this point in the history
  • Loading branch information
wdzhou committed Jul 20, 2012
1 parent c55b710 commit a91e9d0
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 5 deletions.
6 changes: 3 additions & 3 deletions Code/Mantid/Framework/CurveFitting/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ set ( SRC_FILES
src/Bk2BkExpConvPV.cpp
src/BoundaryConstraint.cpp
src/Chebyshev.cpp
src/ChebyshevPolynomialBackground.cpp
# src/ChebyshevPolynomialBackground.cpp
src/Convolution.cpp
src/CostFuncFitting.cpp
src/CostFuncLeastSquares.cpp
Expand Down Expand Up @@ -81,7 +81,7 @@ set ( INC_FILES
inc/MantidCurveFitting/Bk2BkExpConvPV.h
inc/MantidCurveFitting/BoundaryConstraint.h
inc/MantidCurveFitting/Chebyshev.h
inc/MantidCurveFitting/ChebyshevPolynomialBackground.h
# inc/MantidCurveFitting/ChebyshevPolynomialBackground.h
inc/MantidCurveFitting/CompositeValues.h
inc/MantidCurveFitting/Convolution.h
inc/MantidCurveFitting/CostFuncFitting.h
Expand Down Expand Up @@ -154,7 +154,7 @@ set ( TEST_FILES
test/BivariateNormalTest.h
test/Bk2BkExpConvPVTest.h
test/BoundaryConstraintTest.h
test/ChebyshevPolynomialBackgroundTest.h
# test/ChebyshevPolynomialBackgroundTest.h
test/ChebyshevTest.h
test/CompositeFunctionTest.h
test/ConvolutionTest.h
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#ifndef MANTID_CURVEFITTING_HIGHORDERPOLYNOMIALBACKGROUND_H_
#define MANTID_CURVEFITTING_HIGHORDERPOLYNOMIALBACKGROUND_H_

#include "MantidKernel/System.h"
#include "MantidCurveFitting/BackgroundFunction.h"
#include <cmath>


namespace Mantid
{
namespace CurveFitting
{

/** HighOrderPolynomialBackground : TODO: DESCRIPTION
Copyright &copy; 2012 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport HighOrderPolynomialBackground : public BackgroundFunction
{
public:
HighOrderPolynomialBackground();
virtual ~HighOrderPolynomialBackground();

std::string name()const{return "HighOrderPolynomialBackground";}
virtual void function1D(double* out, const double* xValues, const size_t nData)const;
virtual void functionDeriv1D(API::Jacobian* out, const double* xValues, const size_t nData);

private:
void init();

};


} // namespace CurveFitting
} // namespace Mantid

#endif /* MANTID_CURVEFITTING_HIGHORDERPOLYNOMIALBACKGROUND_H_ */
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "MantidCurveFitting/HighOrderPolynomialBackground.h"
#include "MantidAPI/FunctionFactory.h"


using namespace Mantid::Kernel;
using namespace Mantid::API;

namespace Mantid
{
namespace CurveFitting
{

DECLARE_FUNCTION(HighOrderPolynomialBackground)

//----------------------------------------------------------------------------------------------
/** Constructor
*/
HighOrderPolynomialBackground::HighOrderPolynomialBackground()
{
}

//----------------------------------------------------------------------------------------------
/** Destructor
*/
HighOrderPolynomialBackground::~HighOrderPolynomialBackground()
{
}

void HighOrderPolynomialBackground::init()
{
declareParameter("A0", 0.0);
declareParameter("A1", 0.0);
declareParameter("A2", 0.0);
declareParameter("A3", 0.0);
declareParameter("A4", 0.0);
}


/*
*
*/
void HighOrderPolynomialBackground::function1D(double* out, const double* xValues, const size_t nData)const
{
const double& a0 = getParameter("A0");
const double& a1 = getParameter("A1");
const double& a2 = getParameter("A2");
const double& a3 = getParameter("A3");
const double& a4 = getParameter("A4");

for (size_t i = 0; i < nData; i++)
{
out[i] = a0+a1*xValues[i]+a2*xValues[i]*xValues[i]+a3*xValues[i]*xValues[i]*xValues[i]+
a4*xValues[i]*xValues[i]*xValues[i]*xValues[i];
}

return;
}

void HighOrderPolynomialBackground::functionDeriv1D(API::Jacobian* out, const double* xValues, const size_t nData)
{
for (size_t i = 0; i < nData; i++)
{
out->set(i, 0, 1);
out->set(i, 1, xValues[i]);
out->set(i, 2, xValues[i]*xValues[i]);
out->set(i, 3, xValues[i]*xValues[i]*xValues[i]);
out->set(i, 4, xValues[i]*xValues[i]*xValues[i]*xValues[i]);
}
}



} // namespace CurveFitting
} // namespace Mantid
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@ DECLARE_FUNCTION(QuadraticBackground)
const double& a1 = getParameter("A1");
const double& a2 = getParameter("A2");

for (size_t i = 0; i < nData; i++) {
for (size_t i = 0; i < nData; i++)
{
out[i] = a0+a1*xValues[i]+a2*xValues[i]*xValues[i];
}
}

void QuadraticBackground::functionDeriv1D(API::Jacobian* out, const double* xValues, const size_t nData)
{
for (size_t i = 0; i < nData; i++) {
for (size_t i = 0; i < nData; i++)
{
out->set(i, 0, 1);
out->set(i, 1, xValues[i]);
out->set(i, 2, xValues[i]*xValues[i]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#ifndef MANTID_CURVEFITTING_HIGHORDERPOLYNOMIALBACKGROUNDTEST_H_
#define MANTID_CURVEFITTING_HIGHORDERPOLYNOMIALBACKGROUNDTEST_H_

#include <cxxtest/TestSuite.h>

#include "MantidCurveFitting/HighOrderPolynomialBackground.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidCurveFitting/Fit.h"

using Mantid::CurveFitting::HighOrderPolynomialBackground;
using namespace Mantid;
using namespace API;
using namespace Kernel;

class HighOrderPolynomialBackgroundTest : public CxxTest::TestSuite
{
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static HighOrderPolynomialBackgroundTest *createSuite() { return new HighOrderPolynomialBackgroundTest(); }
static void destroySuite( HighOrderPolynomialBackgroundTest *suite ) { delete suite; }

void testForCategories()
{
HighOrderPolynomialBackground forCat;
const std::vector<std::string> categories = forCat.categories();
TS_ASSERT( categories.size() == 1 );
TS_ASSERT( categories[0] == "Background" );
}

void test_HighOrderPolynomialBackground()
{
// create mock data to test against
std::string wsName = "QuadraticBackgroundTest";
int histogramNumber = 1;
int timechannels = 5;
DataObjects::Workspace2D_sptr ws2D =
boost::dynamic_pointer_cast<DataObjects::Workspace2D>(
API::WorkspaceFactory::Instance().create("Workspace2D", histogramNumber, timechannels, timechannels));
for (int i = 0; i < timechannels; i++)
{
ws2D->dataX(0)[i] = i + 1;
ws2D->dataY(0)[i] = (i + 1)*(i + 1) + 2*(i+1) + 3.0;
ws2D->dataE(0)[i] = 1.0;
}

//put this workspace in the data service
TS_ASSERT_THROWS_NOTHING(AnalysisDataService::Instance().add(wsName, ws2D));

CurveFitting::Fit alg2;
TS_ASSERT_THROWS_NOTHING(alg2.initialize());
TS_ASSERT( alg2.isInitialized() );

// set up fitting function
IFunction_sptr quadB(new HighOrderPolynomialBackground());
quadB->initialize();

quadB->setParameter("A0", 0.0);
quadB->setParameter("A1", 1.0);
quadB->setParameter("A2", 0.0);
quadB->setParameter("A3", 0.0);

//alg2.setFunction(linB);
alg2.setProperty("Function", quadB);

// Set which spectrum to fit against and initial starting values
alg2.setPropertyValue("InputWorkspace", wsName);
alg2.setPropertyValue("WorkspaceIndex", "0");

// execute fit
TS_ASSERT_THROWS_NOTHING(TS_ASSERT( alg2.execute() ))

TS_ASSERT( alg2.isExecuted() );

// test the output from fit is what you expect
double dummy = alg2.getProperty("OutputChi2overDoF");

TS_ASSERT_DELTA( dummy, 0.0,0.1);
IFunction_sptr out = alg2.getProperty("Function");
TS_ASSERT_DELTA( out->getParameter("A0"), 3.0, 0.01);
TS_ASSERT_DELTA( out->getParameter("A1"), 2.0, 0.0003);
TS_ASSERT_DELTA( out->getParameter("A2"), 1.0, 0.01);

// check its categories
const std::vector<std::string> categories = out->categories();
TS_ASSERT( categories.size() == 1 );
TS_ASSERT( categories[0] == "Background" );

return;
}

};


#endif /* MANTID_CURVEFITTING_HIGHORDERPOLYNOMIALBACKGROUNDTEST_H_ */

0 comments on commit a91e9d0

Please sign in to comment.