-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a polynomial background function. Refs #5306.
- Loading branch information
Showing
5 changed files
with
232 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
Code/Mantid/Framework/CurveFitting/inc/MantidCurveFitting/HighOrderPolynomialBackground.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 © 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_ */ |
74 changes: 74 additions & 0 deletions
74
Code/Mantid/Framework/CurveFitting/src/HighOrderPolynomialBackground.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
Code/Mantid/Framework/CurveFitting/test/HighOrderPolynomialBackgroundTest.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ */ |