15#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++)
90 double coefficientNorm, exponentialCoeffx2, exponentialCoeffy2, exponentialCoeffxy, Varxx, Varxy, Varyy;
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, coefficientNorm, exponentialCoeffx2, exponentialCoeffy2, exponentialCoeffxy,
107 numberOfCells, Varxx, Varxy, Varyy);
109 std::ostringstream inf;
110 inf <<
"F Parameters=";
111 for (
size_t k = 0; k <
nParams(); k++)
114 inf <<
"," << Varxx <<
"," << Varyy <<
"," << Varxy;
117 numberOfCells = std::min<int>(
static_cast<int>(nData), numberOfCells);
124 double DDD = std::min<double>(10, 10 * std::max<double>(0, -Background));
131 for (
int i = 0; i < numberOfCells; i++) {
138 double dx =
X[i] - Xmean;
139 double dy =
Y[i] - Ymean;
140 out[
x] = Background +
141 coefficientNorm * Intensity *
142 exp(exponentialCoeffx2 * dx * dx + exponentialCoeffxy * dx * dy + exponentialCoeffy2 * dy * dy);
143 out[
x] = out[
x] + DDD;
145 if (out[
x] != out[
x]) {
150 double diff = out[
x] - D[
x];
151 chiSq += diff * diff;
156 for (
size_t i = 0; i <
nParams(); i++) {
159 inf << i <<
"=" << constr->
check() <<
";";
161 inf <<
"\n\n chiSq =" << chiSq <<
" nData " << nData <<
'\n';
172 std::ostringstream inf;
173 inf <<
"***penalty(" << penDeriv <<
"),Parameters=";
174 for (
size_t k = 0; k < 7; k++)
179 std::vector<double> outf(nData, 0.0);
190 const auto &
X = ws->y(1);
191 const auto &
Y = ws->y(2);
195 double penaltyDeriv = penDeriv;
202 if (penaltyDeriv <= 0)
214 if (penaltyDeriv <= 0)
227 if (penaltyDeriv <= 0)
244 if (penaltyDeriv <= 0)
266 if (penaltyDeriv <= 0)
290 if (penaltyDeriv <= 0)
299 else if (paramUU < 0)
305 out->
set(
x,
IVXX, penaltyDeriv + SIVXX);
326 out->
set(
x,
IVYY, penaltyDeriv + SIVYY);
346 out->
set(
x,
IVXY, penaltyDeriv + SIVXY);
386 bool ParamsOK =
true;
387 bool CommonsOK =
true;
392 const auto &D = ws->y(0);
393 const auto &
X = ws->y(1);
394 const auto &
Y = ws->y(2);
397 NCells =
static_cast<int>(std::min<size_t>(D.size(), std::min<size_t>(
X.size(),
Y.size())));
401 double Attrib[12] = {0.0};
403 double MinX, MinY, MaxX, MaxY, MaxD, MinD;
410 for (
int i = 0; i <
NCells; i++) {
411 Attrib[
S_int] += D[i];
420 Attrib[
S_x2] +=
X[i] *
X[i];
421 Attrib[
S_y2] +=
Y[i] *
Y[i];
422 Attrib[
S_xy] +=
X[i] *
Y[i];
461 addConstraint((std::make_unique<BoundaryConstraint>(
this,
"Background", 0, Attrib[
S_int] / Attrib[
S_1])));
464 double maxIntensity = Attrib[
S_int] + 3 * sqrt(Attrib[
S_int]);
466 if (maxIntensity < 100)
470 addConstraint(std::make_unique<BoundaryConstraint>(
this,
"Intensity", 0, maxIntensity));
474 double minMeany = MinY * .9 + .1 * MaxY;
475 double maxMeany = MinY * .1 + .9 * MaxY;
476 addConstraint(std::make_unique<BoundaryConstraint>(
this,
"Mrow", minMeany, maxMeany));
480 double minMeanx = MinX * .9 + .1 * MaxX;
481 double maxMeanx = MinX * .1 + .9 * MaxX;
482 addConstraint(std::make_unique<BoundaryConstraint>(
this,
"Mcol", minMeanx, maxMeanx));
486 std::ostringstream ssxx, ssyy, ssxy;
488 ssyy << std::string(
"(") << (
SIyy) <<
"+(Mrow-" << (
mIy) <<
")*(Mrow-" << (
mIy) <<
")*" << Attrib[
S_int]
489 <<
"-Background*" << (
Syy) <<
"-Background*(Mrow-" << (
my) <<
")*(Mrow-" << (
my) <<
")*" << Attrib[
S_1]
490 <<
")/(" << (Attrib[
S_int]) <<
"-Background*" << (Attrib[
S_1]) <<
")";
493 tie(
"SSrow", ssyy.str());
497 ssxx << std::string(
"(") << (
SIxx) <<
"+(Mcol-" << (
mIx) <<
")*(Mcol-" << (
mIx) <<
")*" << Attrib[
S_int]
498 <<
"-Background*" << (
Sxx) <<
"-Background*(Mcol-" << (
mx) <<
")*(Mcol-" << (
mx) <<
")*" << Attrib[
S_1]
499 <<
")/(" << (Attrib[
S_int]) <<
"-Background*" << (Attrib[
S_1]) <<
")";
502 tie(
"SScol", ssxx.str());
506 ssxy << std::string(
"(") << (
SIxy) <<
"+(Mcol-" << (
mIx) <<
")*(Mrow-" << (
mIy) <<
")*" << Attrib[
S_int]
507 <<
"-Background*" << (
Sxy) <<
"-Background*(Mcol-" << (
mx) <<
")*(Mrow-" << (
my) <<
")*" << Attrib[
S_1]
508 <<
")/(" << (Attrib[
S_int]) <<
"-Background*" << (Attrib[
S_1]) <<
")";
511 tie(
"SSrc", ssxy.str());
523 for (
size_t i = 0; i <
nParams() && ParamsOK; i++)
529 for (
size_t i = 0; i <
nParams(); i++)
533 if (!CommonsOK || !ParamsOK) {
536 double Varxx, Varxy, Varyy;
538 Varxx = Varxy = Varyy = -1;
539 penalty =
initCoeff(D,
X,
Y,
coefNorm,
expCoeffx2,
expCoeffy2,
expCoeffxy, NCells1, Varxx, Varxy, Varyy);
541 if (
Varx0 < 0 && penalty <= 0) {
553 for (
int i = 0; i <
NCells; i++) {
564 double &expCoeffx2,
double &expCoeffy2,
double &expCoeffxy,
int &NCells,
565 double &Varxx,
double &Varxy,
double &Varyy)
const {
568 bool zeroDenom =
false;
571 else if (
TotI - Background *
TotN <= 0)
580 Varxx = std::min<double>(Varxx, 1.21 *
Varx0);
581 Varxx = std::max<double>(Varxx, .79 *
Varx0);
596 Varyy = std::min<double>(Varyy, 1.21 *
Vary0);
597 Varyy = std::max<double>(Varyy, .79 *
Vary0);
613 double paramUU = Varxx * Varyy - Varxy * Varxy;
619 std::max<double>(0, -Varxx + .01) + std::max<double>(0, -Varyy + .01) + std::max<double>(0, -paramUU + .01);
621 if (
fabs(paramUU) < .01) {
628 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)