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

Further changes to functions, FunctionFactory and ParameterTies in order to be...

Further changes to functions, FunctionFactory and ParameterTies in order to be able to save ties to a string from functions and create functions with set ties via FunctionFactory. re #1121
parent 97fd6227
......@@ -414,22 +414,6 @@
RelativePath=".\test\AxisTest.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\BinaryOperation.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\CalculateTransmission.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\ChangeBinOffset.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\CheckWorkspacesMatch.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\CloneableAlgorithm.h"
>
......@@ -442,10 +426,6 @@
RelativePath=".\inc\MantidAPI\ColumnFactory.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\CommutativeBinaryOperation.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\CompositeFunction.h"
>
......@@ -454,90 +434,18 @@
RelativePath=".\test\CompositeFunctionTest.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\ConjoinWorkspaces.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\ConstraintFactory.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\ConvertFromDistribution.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\ConvertSpectrumAxis.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\ConvertToDistribution.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\ConvertUnits.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\CorrectToFile.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\CreateCalFileByNames.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\CreateSingleValuedWorkspace.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\CropWorkspace.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\CrossCorrelate.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\CylinderAbsorption.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\DeclareUserAlg.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\DetectorEfficiencyCor.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\DetectorEfficiencyVariation.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\DiffractionFocussing.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\DiffractionFocussing2.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\Divide.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\DllExport.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\EfficiencyScriptInput.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\ExponentialCorrection.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\Expression.h"
>
......@@ -546,38 +454,6 @@
RelativePath=".\test\ExpressionTest.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\ExtractSingleSpectrum.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\FFT.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\FFTSmooth.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\FindDeadDetectors.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\FindDetectorsOutsideLimits.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\FindPeaks.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\FindPeaks1D.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\FlatBackground.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\FrameworkManager.h"
>
......@@ -602,26 +478,6 @@
RelativePath=".\test\FunctionTest.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\GEMScriptInput.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\GeneralisedSecondDifference.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\GetDetectorOffsets.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\GetEi.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\GroupWorkspaces.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\IAlgorithm.h"
>
......@@ -654,10 +510,6 @@
RelativePath=".\inc\MantidAPI\ILocatedData.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\InputWSDetectorInfo.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\Instrument.h"
>
......@@ -698,18 +550,6 @@
RelativePath=".\inc\MantidAPI\LogParser.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\LOQScriptInput.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\MaskBins.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\MaskDetectorsIf.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\MatrixWorkspace.h"
>
......@@ -718,50 +558,10 @@
RelativePath=".\test\MatrixWorkspaceTest.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\Max.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\MedianDetectorTest.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\MemoryManager.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\MergeRuns.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\Minus.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\Multiply.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\MuonAsymmetryCalc.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\MuonRemoveExpDecay.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\NormaliseByCurrent.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\NormaliseToMonitor.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\OneMinusExponentialCor.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\PairedGroupAlgorithm.h"
>
......@@ -790,82 +590,14 @@
RelativePath=".\test\ParInstrumentTest.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\PlotAsymmetryByLogValue.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\Plus.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\PointByPointVCorrection.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\PoissonErrors.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\PolynomialCorrection.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\PowerLawCorrection.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\Progress.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\Q1D.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\Qxy.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\ReadGroupsFromFile.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\RealFFT.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\RebinPreserveValue.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\RebinToWorkspace.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\Rebunch.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\RefAxis.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\Regroup.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\RemoveBins.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\RenameWorkspace.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\ReplaceSpecialValues.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\Sample.h"
>
......@@ -874,22 +606,6 @@
RelativePath=".\test\SampleTest.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\SimpleIntegration.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\SimpleRebin.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\SmoothData.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\SolidAngle.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\SpectraDetectorMap.h"
>
......@@ -898,34 +614,10 @@
RelativePath=".\test\SpectraDetectorMapTest.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\StripPeaks.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\SumRowColumn.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\SumSpectra.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\TableRow.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\UnaryOperation.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\UnGroupWorkspaces.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\inc\MantidAlgorithms\Unwrap.h"
>
</File>
<File
RelativePath=".\inc\MantidAPI\Workspace.h"
>
......
......@@ -120,9 +120,9 @@ public:
void addConstraint(IConstraint* ic);
void setParametersToSatisfyConstraints();
/// Get first constraint
IConstraint* firstConstraint();
IConstraint* firstConstraint()const;
/// Get next constraint
IConstraint* nextConstraint();
IConstraint* nextConstraint()const;
/* CompositeFunction own methods */
......@@ -140,6 +140,8 @@ public:
int functionIndex(int i)const;
/// Get the function index
int functionIndexActive(int i)const;
/// Returns the index of parameter i as it declared in its function
int parameterLocalIndex(int i)const;
/// Returns the name of parameter i as it declared in its function
std::string parameterLocalName(int i)const;
/// Check the function.
......@@ -175,7 +177,7 @@ private:
/// Total number of parameters
int m_nParams;
/// Function counter to be used in nextConstraint
int m_iConstraintFunction;
mutable int m_iConstraintFunction;
//friend class ParameterTie;
};
......
......@@ -106,13 +106,17 @@ public:
virtual void applyTies();
/// Remove all ties
virtual void clearTies();
/// Removes i-th parameter's tie
virtual bool removeTie(int i);
/// Get the tie of i-th parameter
virtual ParameterTie* getTie(int i)const;
/// Add a constraint to function
virtual void addConstraint(IConstraint* ic);
/// Get first constraint
virtual IConstraint* firstConstraint();
virtual IConstraint* firstConstraint()const;
/// Get next constraint
virtual IConstraint* nextConstraint();
virtual IConstraint* nextConstraint()const;
void setParametersToSatisfyConstraints();
......@@ -127,10 +131,6 @@ protected:
/// Add a new tie
virtual void addTie(ParameterTie* tie);
/// Removes i-th parameter's tie
virtual bool removeTie(int i);
/// Get the tie of i-th parameter
virtual ParameterTie* getTie(int i)const;
/// Get the address of the parameter. For use in UserFunction with mu::Parser
virtual double* getParameterAddress(int i);
......@@ -151,7 +151,7 @@ private:
/// Flags of explicitly set parameters
std::vector<bool> m_explicitlySet;
/// Constraint counter for use in nextConstraint()
int m_iConstraint;
mutable int m_iConstraint;
};
} // namespace API
......
......@@ -96,6 +96,10 @@ namespace API
void addConstraint(IFunction* fun,const Expression& expr)const;
/// Add a constraint to the created function if str has a form of (min:max), or return false
bool tryAddConstraint(IFunction* fun,const std::string& parName,const std::string& str)const;
/// Add ties to the created function
void addTies(IFunction* fun,const Expression& expr)const;
/// Add a tie to the created function
void addTie(IFunction* fun,const Expression& expr)const;
///static reference to the logger class
Kernel::Logger& g_log;
......
......@@ -65,6 +65,9 @@ public:
/// it is set to that value and vice versa for if the param value is larger
/// than the upper boundary value.
virtual void setParamToSatisfyConstraint() = 0;
/// Return the string that can be used in this->initialize() to recreate this constraint
virtual std::string asString()const = 0;
};
......
......@@ -171,13 +171,18 @@ public:
virtual void removeTie(const std::string& parName);
/// Remove all ties
virtual void clearTies() = 0;
/// Removes i-th parameter's tie
virtual bool removeTie(int i) = 0;
/// Get the tie of i-th parameter
virtual ParameterTie* getTie(int i)const = 0;
/// Get
/// Add a constraint to function
virtual void addConstraint(IConstraint* ic) = 0;
/// Get first constraint
virtual IConstraint* firstConstraint() = 0;
virtual IConstraint* firstConstraint()const = 0;
/// Get next constraint
virtual IConstraint* nextConstraint() = 0;
virtual IConstraint* nextConstraint()const = 0;
/// Set the parameters of the function to satisfy the constraints of
/// of the function. For example
......@@ -208,12 +213,6 @@ protected:
virtual ParameterTie* createTie(const std::string& parName);
/// Add a new tie
virtual void addTie(ParameterTie* tie) = 0;
/// Removes i-th parameter's tie
virtual bool removeTie(int i) = 0;
/// Get the tie of i-th parameter
virtual ParameterTie* getTie(int i)const = 0;
/// Get the address of the parameter
//virtual double* getParameterAddress(int i) = 0;
friend class ParameterTie;
friend class CompositeFunction;
......
......@@ -56,6 +56,8 @@ public:
virtual void set(const std::string& expr);
/// Evaluate the expression
virtual double eval();
/// Return the string that can be used to recreate this tie
virtual std::string asString(const IFunction* fun = 0)const;
/// Check if the tie has any references to certain parameters
bool findParametersOf(const IFunction* fun)const;
......@@ -66,6 +68,9 @@ protected:
std::map<double*,ParameterReference> m_varMap;
/// Keep the function that was passed to the constructor
IFunction* m_function1;
/// Keep the template for the input string passed to this->set(...)
/// In the template CompositeFunction prefixes are replaced with placeholders
std::string m_expression;
private:
/// MuParser callback function
......
......@@ -52,7 +52,18 @@ void CompositeFunction::init()
{
}
/// Writes itself into a string
/**
* Writes itself into a string. Functions derived from CompositeFunction must
* override this method with something like this:
* std::string NewFunction::asString()const
* {
* ostr << "composite=" << this->name() << ';';
* // write NewFunction's own attributes and parameters
* ostr << CompositeFunction::asString();
* // write NewFunction's own ties and constraints
* // ostr << ";constraints=(" << ... <<")";
* }
*/
std::string CompositeFunction::asString()const
{
std::ostringstream ostr;
......@@ -61,8 +72,38 @@ std::string CompositeFunction::asString()const
IFunction* fun = getFunction(i);
bool isComp = dynamic_cast<CompositeFunction*>(fun) != 0;
if (isComp) ostr << '(';
ostr << fun->asString() << ';';
ostr << fun->asString();
if (isComp) ostr << ')';
if (i < nFunctions() - 1)
{
ostr << ';';
}
}
std::string ties;
for(int i=0;i<nParams();i++)
{
const ParameterTie* tie = getTie(i);
if (tie)
{
IFunction* fun = getFunction(functionIndex(i));
std::string tmp = tie->asString(fun);
if (tmp.empty())
{
tmp = tie->asString(this);
if (!tmp.empty())
{
if (!ties.empty())
{
ties += ",";
}
ties += tmp;
}
}
}
}
if (!ties.empty())
{
ostr << ";ties=(" << ties << ")";
}
return ostr.str();
}
......@@ -564,9 +605,10 @@ void CompositeFunction::replaceFunction(int i,IFunction* f)
*/
IFunction* CompositeFunction::getFunction(int i)const
{
if ( i >= nFunctions() )
if ( i >= nFunctions() || i < 0)
{
throw std::out_of_range("Function index out of range.");
}
return m_functions[i];
}
......@@ -576,8 +618,10 @@ IFunction* CompositeFunction::getFunction(int i)const
*/
int CompositeFunction::functionIndex(int i)const
{
if (i >= nParams())
if (i >= nParams() || i < 0)
{
throw std::out_of_range("Function parameter index out of range.");