14#include "MantidHistogramData/HistogramY.h"
31using namespace CurveFitting;
32using namespace Constraints;
33using namespace HistogramData;
66 :
API::
ParamFunction(), CalcVxx(false), CalcVyy(false), CalcVxy(false), NCells(0), CalcVariances(false), mIx(0.0),
67 mx(0.0), mIy(0.0), my(0.0), SIxx(0.0), SIyy(0.0), SIxy(0.0), Sxx(0.0), Syy(0.0), Sxy(0.0), TotI(0.0), TotN(0.0),
68 Varx0(-1.0), Vary0(-1.0), expVals(nullptr), uu(0.0), coefNorm(0.0), expCoeffx2(0.0), expCoeffy2(0.0),
86 for (
size_t i = 0; i < nData; i++)
96 const auto &D = ws->y(0);
97 const auto &
X = ws->y(1);
98 const auto &
Y = ws->y(2);
106 double badParams =
initCoeff(D,
X,
Y,
coefNorm,
expCoeffx2,
expCoeffy2,
expCoeffxy,
NCells, Varxx, Varxy, Varyy);
108 std::ostringstream inf;
109 inf <<
"F Parameters=";
110 for (
size_t k = 0; k <
nParams(); k++)
113 inf <<
"," << Varxx <<
"," << Varyy <<
"," << Varxy;
116 NCells = std::min<int>(
static_cast<int>(nData),
NCells);
123 double DDD = std::min<double>(10, 10 * std::max<double>(0, -Background));
130 for (
int i = 0; i <
NCells; i++) {
137 double dx =
X[i] - Xmean;
138 double dy =
Y[i] - Ymean;
141 out[
x] = out[
x] + DDD;
143 if (out[
x] != out[
x]) {
148 double diff = out[
x] - D[
x];
149 chiSq += diff * diff;
154 for (
size_t i = 0; i <
nParams(); i++) {
157 inf << i <<
"=" << constr->
check() <<
";";
159 inf <<
"\n\n chiSq =" << chiSq <<
" nData " << nData <<
'\n';
170 std::ostringstream inf;
171 inf <<
"***penalty(" << penDeriv <<
"),Parameters=";
172 for (
size_t k = 0; k < 7; k++)
177 std::vector<double> outf(nData, 0.0);
188 const auto &
X = ws->y(1);
189 const auto &
Y = ws->y(2);
193 double penaltyDeriv = penDeriv;
200 if (penaltyDeriv <= 0)
212 if (penaltyDeriv <= 0)
225 if (penaltyDeriv <= 0)
242 if (penaltyDeriv <= 0)
264 if (penaltyDeriv <= 0)
288 if (penaltyDeriv <= 0)
303 out->
set(
x,
IVXX, penaltyDeriv + SIVXX);
324 out->
set(
x,
IVYY, penaltyDeriv + SIVYY);
344 out->
set(
x,
IVXY, penaltyDeriv + SIVXY);
384 bool ParamsOK =
true;
385 bool CommonsOK =
true;
390 const auto &D = ws->y(0);
391 const auto &
X = ws->y(1);
392 const auto &
Y = ws->y(2);
395 NCells =
static_cast<int>(std::min<size_t>(D.size(), std::min<size_t>(
X.size(),
Y.size())));
399 double Attrib[12] = {0.0};
401 double MinX, MinY, MaxX, MaxY, MaxD, MinD;
408 for (
int i = 0; i <
NCells; i++) {
409 Attrib[
S_int] += D[i];
418 Attrib[
S_x2] +=
X[i] *
X[i];
419 Attrib[
S_y2] +=
Y[i] *
Y[i];
420 Attrib[
S_xy] +=
X[i] *
Y[i];
459 addConstraint((std::make_unique<BoundaryConstraint>(
this,
"Background", 0, Attrib[
S_int] / Attrib[
S_1])));
462 double maxIntensity = Attrib[
S_int] + 3 * sqrt(Attrib[
S_int]);
464 if (maxIntensity < 100)
468 addConstraint(std::make_unique<BoundaryConstraint>(
this,
"Intensity", 0, maxIntensity));
471 double minMeany = MinY * .9 + .1 * MaxY;
472 double maxMeany = MinY * .1 + .9 * MaxY;
475 addConstraint(std::make_unique<BoundaryConstraint>(
this,
"Mrow", minMeany, maxMeany));
478 double minMeanx = MinX * .9 + .1 * MaxX;
479 double maxMeanx = MinX * .1 + .9 * MaxX;
481 addConstraint(std::make_unique<BoundaryConstraint>(
this,
"Mcol", minMeanx, maxMeanx));
485 std::ostringstream ssxx, ssyy, ssxy;
487 ssyy << std::string(
"(") << (
SIyy) <<
"+(Mrow-" << (
mIy) <<
")*(Mrow-" << (
mIy) <<
")*" << Attrib[
S_int]
488 <<
"-Background*" << (
Syy) <<
"-Background*(Mrow-" << (
my) <<
")*(Mrow-" << (
my) <<
")*" << Attrib[
S_1]
489 <<
")/(" << (Attrib[
S_int]) <<
"-Background*" << (Attrib[
S_1]) <<
")";
492 tie(
"SSrow", ssyy.str());
496 ssxx << std::string(
"(") << (
SIxx) <<
"+(Mcol-" << (
mIx) <<
")*(Mcol-" << (
mIx) <<
")*" << Attrib[
S_int]
497 <<
"-Background*" << (
Sxx) <<
"-Background*(Mcol-" << (
mx) <<
")*(Mcol-" << (
mx) <<
")*" << Attrib[
S_1]
498 <<
")/(" << (Attrib[
S_int]) <<
"-Background*" << (Attrib[
S_1]) <<
")";
501 tie(
"SScol", ssxx.str());
505 ssxy << std::string(
"(") << (
SIxy) <<
"+(Mcol-" << (
mIx) <<
")*(Mrow-" << (
mIy) <<
")*" << Attrib[
S_int]
506 <<
"-Background*" << (
Sxy) <<
"-Background*(Mcol-" << (
mx) <<
")*(Mrow-" << (
my) <<
")*" << Attrib[
S_1]
507 <<
")/(" << (Attrib[
S_int]) <<
"-Background*" << (Attrib[
S_1]) <<
")";
510 tie(
"SSrc", ssxy.str());
522 for (
size_t i = 0; i <
nParams() && ParamsOK; i++)
528 for (
size_t i = 0; i <
nParams(); i++)
532 if (!CommonsOK || !ParamsOK) {
535 double Varxx, Varxy, Varyy;
537 Varxx = Varxy = Varyy = -1;
538 penalty =
initCoeff(D,
X,
Y,
coefNorm,
expCoeffx2,
expCoeffy2,
expCoeffxy, NCells1, Varxx, Varxy, Varyy);
540 if (
Varx0 < 0 && penalty <= 0) {
552 for (
int i = 0; i <
NCells; i++) {
563 double &expCoeffx2,
double &expCoeffy2,
double &expCoeffxy,
int &NCells,
564 double &Varxx,
double &Varxy,
double &Varyy)
const {
567 bool zeroDenom =
false;
570 else if (
TotI - Background *
TotN <= 0)
579 Varxx = std::min<double>(Varxx, 1.21 *
Varx0);
580 Varxx = std::max<double>(Varxx, .79 *
Varx0);
595 Varyy = std::min<double>(Varyy, 1.21 *
Vary0);
596 Varyy = std::max<double>(Varyy, .79 *
Vary0);
612 double uu = Varxx * Varyy - Varxy * Varxy;
617 penalty = std::max<double>(0, -Varxx + .01) + std::max<double>(0, -Varyy + .01) + std::max<double>(0, -
uu + .01);
626 NCells =
static_cast<int>(std::min<size_t>(D.size(), std::min<size_t>(
X.size(),
Y.size())));
#define DECLARE_FUNCTION(classname)
Macro for declaring a new type of function to be used with the FunctionFactory.
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
An interface to a constraint.
virtual double check()=0
Returns a penalty number which is bigger than or equal to zero If zero it means that the constraint i...
virtual void setPenaltyFactor(const double &c)=0
set the penalty factor for the constraint Set panelty factor.
static Kernel::Logger g_log
Logger instance.
std::shared_ptr< const API::MatrixWorkspace > getMatrixWorkspace() const
Get shared pointer to the workspace.
virtual void tie(const std::string &parName, const std::string &expr, bool isDefault=false)
Tie a parameter to other parameters (or a constant)
virtual ParameterTie * getTie(size_t i) const
Get the tie of i-th parameter.
virtual void addConstraint(std::unique_ptr< IConstraint > ic)
Add a constraint to function.
virtual IConstraint * getConstraint(size_t i) const
Get constraint of i-th parameter.
Represents the Jacobian in IFitFunction::functionDeriv.
virtual double get(size_t iY, size_t iP)=0
Get the value to a Jacobian matrix element.
virtual void set(size_t iY, size_t iP, double value)=0
Set a value to a Jacobian matrix element.
Implements the part of IFunction interface dealing with parameters.
void declareParameter(const std::string &name, double initValue=0, const std::string &description="") override
Declare a new parameter.
size_t nParams() const override
Total number of parameters.
double getParameter(size_t i) const override
Get i-th parameter.
bool CalcVariances
from experimental data versus fit the (Co)Variances
double LastParams[9]
Saves previous/this set of parameters.
double * expVals
Save common exponential values for each cell.
void function1D(double *out, const double *xValues, const size_t nData) const override
Function you want to fit to.
void functionDeriv1D(API::Jacobian *out, const double *xValues, const size_t nData) override
Derivatives of function with respect to active parameters.
double initCommon()
Check for changes in parameters, etc.
void init() override
Function initialization. Declare function parameters in this method.
double initCoeff(const HistogramData::HistogramY &D, const HistogramData::HistogramY &X, const HistogramData::HistogramY &Y, double &coefNorm, double &expCoeffx2, double &expCoeffy2, double &expCoeffxy, int &NCells, double &Varxx, double &Varxy, double &Varyy) const
common values
~BivariateNormal() override
Destructor.
The Logger class is in charge of the publishing messages from the framework through various channels.
void debug(const std::string &msg)
Logs at debug level.
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)