Commit ae1a114b authored by Roman Tolchenov's avatar Roman Tolchenov
Browse files

Re #19904. Added method hasParameter() to IFunction.

parent d894b9af
......@@ -96,6 +96,8 @@ public:
const std::string &description) override;
/// Get parameter by name.
double getParameter(const std::string &name) const override;
/// Check if function has a parameter with this name.
bool hasParameter(const std::string &name) const;
/// Total number of parameters
size_t nParams() const override;
/// Returns the index of parameter name
......
......@@ -67,6 +67,8 @@ public:
const std::string &description) override;
/// Get parameter by name.
double getParameter(const std::string &name) const override;
/// Check if function has a parameter with this name.
bool hasParameter(const std::string &name) const;
/// Total number of parameters
size_t nParams() const override;
/// Returns the index of parameter name
......
......@@ -76,6 +76,8 @@ public:
/// Get parameter of decorated function by name.
double getParameter(const std::string &name) const override;
/// Check if the decorated function has a parameter with this name.
bool hasParameter(const std::string &name) const;
/// Total number of parameters of decorated function.
size_t nParams() const override;
/// Returns the index of parameter of decorated function name.
......
......@@ -396,6 +396,8 @@ public:
const std::string &description) = 0;
/// Get parameter by name.
virtual double getParameter(const std::string &name) const = 0;
/// Check if function has a parameter with this name.
virtual bool hasParameter(const std::string &name) const = 0;
/// Total number of parameters
virtual size_t nParams() const = 0;
/// Returns the index of parameter name
......
......@@ -66,6 +66,8 @@ public:
const std::string &description) override;
/// Get parameter by name.
double getParameter(const std::string &name) const override;
/// Check if function has a parameter with this name.
bool hasParameter(const std::string &name) const;
/// Total number of parameters
size_t nParams() const override { return m_parameters.size(); }
/// Returns the index of parameter name
......
......@@ -198,6 +198,23 @@ double CompositeFunction::getParameter(size_t i) const {
return m_functions[iFun]->getParameter(i - m_paramOffsets[iFun]);
}
/**
* Check if function has a parameter with a particular name.
* @param name :: A name of a parameter.
* @return True if the parameter exists.
*/
bool CompositeFunction::hasParameter(const std::string &name) const {
try {
std::string pname;
size_t index;
parseName(name, index, pname);
return index < m_functions.size() ? m_functions[index]->hasParameter(pname)
: false;
} catch (std::invalid_argument &) {
return false;
}
}
/**
* Sets a new value to a parameter by name.
* @param name :: The name of the parameter.
......
......@@ -49,6 +49,12 @@ double FunctionGenerator::getParameter(size_t i) const {
: m_target->getParameter(i - m_nOwnParams);
}
/// Check if function has a parameter with a particular name.
bool FunctionGenerator::hasParameter(const std::string &name) const {
checkTargetFunction();
return m_source->hasParameter(name) || m_target->hasParameter(name);
}
/// Set parameter by name.
void FunctionGenerator::setParameter(const std::string &name,
const double &value, bool explicitlySet) {
......
......@@ -109,6 +109,11 @@ double FunctionParameterDecorator::getParameter(const std::string &name) const {
return m_wrappedFunction->getParameter(name);
}
bool FunctionParameterDecorator::hasParameter(const std::string &name) const {
throwIfNoFunctionSet();
return m_wrappedFunction->hasParameter(name);
}
size_t FunctionParameterDecorator::nParams() const {
if (!m_wrappedFunction) {
return 0;
......
......@@ -137,6 +137,16 @@ double ParamFunction::getParameter(const std::string &name) const {
return parvalue;
}
/**
* Check if function has a parameter with a particular name.
* @param name :: A name of a parameter.
* @return True if the parameter exists.
*/
bool ParamFunction::hasParameter(const std::string &name) const {
return std::find(m_parameterNames.cbegin(), m_parameterNames.cend(), name) !=
m_parameterNames.end();
}
/**
* Returns the index of the parameter named name.
* @param name :: The name of the parameter.
......
......@@ -153,6 +153,17 @@ public:
TS_ASSERT_EQUALS(fn->nParams(), decoratedFunction->nParams());
}
void testHasParameter() {
TestableFunctionParameterDecorator fn;
fn.setDecoratedFunction("FunctionWithParameters");
TS_ASSERT(fn.hasParameter("Height"));
TS_ASSERT(fn.hasParameter("PeakCentre"));
TS_ASSERT(fn.hasParameter("Sigma"));
TS_ASSERT(!fn.hasParameter("Hello"));
}
void testGetSetParameter() {
TestableFunctionParameterDecorator invalidFn;
TS_ASSERT_THROWS(invalidFn.setParameter(0, 2.0), std::runtime_error);
......
......@@ -147,6 +147,31 @@ public:
"curvefitting.peakRadius", m_preSetupPeakRadius);
}
void test_has_parameter() {
auto mfun = boost::make_shared<CompositeFunction>();
auto g = boost::make_shared<CurveFittingGauss>();
auto g1 = boost::make_shared<CurveFittingGauss>();
auto g2 = boost::make_shared<CurveFittingGauss>();
auto bk =boost::make_shared<CurveFittingLinear>();
auto c = boost::make_shared<CompositeFunction>();
mfun->addFunction(bk);
mfun->addFunction(g);
c->addFunction(g1);
c->addFunction(g2);
mfun->addFunction(c);
TS_ASSERT(mfun->hasParameter("f0.a"));
TS_ASSERT(mfun->hasParameter("f0.b"));
TS_ASSERT(mfun->hasParameter("f1.h"));
TS_ASSERT(mfun->hasParameter("f1.c"));
TS_ASSERT(mfun->hasParameter("f2.f0.c"));
TS_ASSERT(mfun->hasParameter("f2.f1.h"));
TS_ASSERT(!mfun->hasParameter("h"));
TS_ASSERT(!mfun->hasParameter("f0.h"));
TS_ASSERT(!mfun->hasParameter("f2.f3.a"));
}
void testFit() {
boost::shared_ptr<CompositeFunction> mfun =
boost::make_shared<CompositeFunction>();
......
......@@ -86,6 +86,15 @@ public:
TS_ASSERT_DELTA(fun.getParameter("f2.Amplitude"), 0.4298 * c_mbsr,
1e-3 * c_mbsr);
TS_ASSERT_DELTA(fun.getParameter("f2.FWHM"), 1.5, 1e-3);
TS_ASSERT(fun.hasParameter("B20"));
TS_ASSERT(fun.hasParameter("B42"));
TS_ASSERT(fun.hasParameter("f0.Amplitude"));
TS_ASSERT(fun.hasParameter("f0.FWHM"));
TS_ASSERT(fun.hasParameter("f2.Amplitude"));
TS_ASSERT(fun.hasParameter("f2.PeakCentre"));
TS_ASSERT(!fun.hasParameter("Hello"));
TS_ASSERT(!fun.hasParameter("f0.Hello"));
}
void test_evaluate() {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment