15#include "MantidHistogramData/HistogramY.h"
32using namespace CurveFitting;
33using namespace Constraints;
34using namespace HistogramData;
67 :
API::
ParamFunction(), CalcVxx(false), CalcVyy(false), CalcVxy(false), NCells(0), CalcVariances(false), mIx(0.0),
68 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),
69 Varx0(-1.0), Vary0(-1.0), expVals(nullptr), uu(0.0), coefNorm(0.0), expCoeffx2(0.0), expCoeffy2(0.0),
87 for (
size_t i = 0; i < nData; i++)
91 double coefficientNorm, exponentialCoeffx2, exponentialCoeffy2, exponentialCoeffxy, Varxx, Varxy, Varyy;
97 const auto &D = ws->y(0);
98 const auto &
X = ws->y(1);
99 const auto &
Y = ws->y(2);
107 double badParams =
initCoeff(D,
X,
Y, coefficientNorm, exponentialCoeffx2, exponentialCoeffy2, exponentialCoeffxy,
108 numberOfCells, Varxx, Varxy, Varyy);
110 std::ostringstream inf;
111 inf <<
"F Parameters=";
112 for (
size_t k = 0; k <
nParams(); k++)
115 inf <<
"," << Varxx <<
"," << Varyy <<
"," << Varxy;
118 numberOfCells = std::min<int>(
static_cast<int>(nData), numberOfCells);
125 double DDD = std::min<double>(10, 10 * std::max<double>(0, -Background));
132 for (
int i = 0; i < numberOfCells; i++) {
139 double dx =
X[i] - Xmean;
140 double dy =
Y[i] - Ymean;
141 out[
x] = Background +
142 coefficientNorm * Intensity *
143 exp(exponentialCoeffx2 * dx * dx + exponentialCoeffxy * dx * dy + exponentialCoeffy2 * dy * dy);
144 out[
x] = out[
x] + DDD;
146 if (out[
x] != out[
x]) {
151 double diff = out[
x] - D[
x];
152 chiSq += diff * diff;
157 for (
size_t i = 0; i <
nParams(); i++) {
160 inf << i <<
"=" << constr->
check() <<
";";
162 inf <<
"\n\n chiSq =" << chiSq <<
" nData " << nData <<
'\n';
173 std::ostringstream inf;
174 inf <<
"***penalty(" << penDeriv <<
"),Parameters=";
175 for (
size_t k = 0; k < 7; k++)
180 std::vector<double> outf(nData, 0.0);
191 const auto &
X = ws->y(1);
192 const auto &
Y = ws->y(2);
196 double penaltyDeriv = penDeriv;
203 if (penaltyDeriv <= 0)
215 if (penaltyDeriv <= 0)
228 if (penaltyDeriv <= 0)
245 if (penaltyDeriv <= 0)
267 if (penaltyDeriv <= 0)
291 if (penaltyDeriv <= 0)
300 else if (paramUU < 0)
306 out->
set(
x,
IVXX, penaltyDeriv + SIVXX);
327 out->
set(
x,
IVYY, penaltyDeriv + SIVYY);
347 out->
set(
x,
IVXY, penaltyDeriv + SIVXY);
387 bool ParamsOK =
true;
388 bool CommonsOK =
true;
393 const auto &D = ws->y(0);
394 const auto &
X = ws->y(1);
395 const auto &
Y = ws->y(2);
398 NCells =
static_cast<int>(std::min<size_t>(D.size(), std::min<size_t>(
X.size(),
Y.size())));
402 double Attrib[12] = {0.0};
404 double MinX, MinY, MaxX, MaxY, MaxD, MinD;
411 for (
int i = 0; i <
NCells; i++) {
412 Attrib[
S_int] += D[i];
421 Attrib[
S_x2] +=
X[i] *
X[i];
422 Attrib[
S_y2] +=
Y[i] *
Y[i];
423 Attrib[
S_xy] +=
X[i] *
Y[i];
462 addConstraint((std::make_unique<BoundaryConstraint>(
this,
"Background", 0, Attrib[
S_int] / Attrib[
S_1])));
465 double maxIntensity = Attrib[
S_int] + 3 * sqrt(Attrib[
S_int]);
467 if (maxIntensity < 100)
471 addConstraint(std::make_unique<BoundaryConstraint>(
this,
"Intensity", 0, maxIntensity));
475 double minMeany = MinY * .9 + .1 * MaxY;
476 double maxMeany = MinY * .1 + .9 * MaxY;
477 addConstraint(std::make_unique<BoundaryConstraint>(
this,
"Mrow", minMeany, maxMeany));
481 double minMeanx = MinX * .9 + .1 * MaxX;
482 double maxMeanx = MinX * .1 + .9 * MaxX;
483 addConstraint(std::make_unique<BoundaryConstraint>(
this,
"Mcol", minMeanx, maxMeanx));
487 std::ostringstream ssxx, ssyy, ssxy;
489 ssyy << std::string(
"(") << (
SIyy) <<
"+(Mrow-" << (
mIy) <<
")*(Mrow-" << (
mIy) <<
")*" << Attrib[
S_int]
490 <<
"-Background*" << (
Syy) <<
"-Background*(Mrow-" << (
my) <<
")*(Mrow-" << (
my) <<
")*" << Attrib[
S_1]
491 <<
")/(" << (Attrib[
S_int]) <<
"-Background*" << (Attrib[
S_1]) <<
")";
494 tie(
"SSrow", ssyy.str());
498 ssxx << std::string(
"(") << (
SIxx) <<
"+(Mcol-" << (
mIx) <<
")*(Mcol-" << (
mIx) <<
")*" << Attrib[
S_int]
499 <<
"-Background*" << (
Sxx) <<
"-Background*(Mcol-" << (
mx) <<
")*(Mcol-" << (
mx) <<
")*" << Attrib[
S_1]
500 <<
")/(" << (Attrib[
S_int]) <<
"-Background*" << (Attrib[
S_1]) <<
")";
503 tie(
"SScol", ssxx.str());
507 ssxy << std::string(
"(") << (
SIxy) <<
"+(Mcol-" << (
mIx) <<
")*(Mrow-" << (
mIy) <<
")*" << Attrib[
S_int]
508 <<
"-Background*" << (
Sxy) <<
"-Background*(Mcol-" << (
mx) <<
")*(Mrow-" << (
my) <<
")*" << Attrib[
S_1]
509 <<
")/(" << (Attrib[
S_int]) <<
"-Background*" << (Attrib[
S_1]) <<
")";
512 tie(
"SSrc", ssxy.str());
524 for (
size_t i = 0; i <
nParams() && ParamsOK; i++)
530 for (
size_t i = 0; i <
nParams(); i++)
534 if (!CommonsOK || !ParamsOK) {
537 double Varxx, Varxy, Varyy;
539 Varxx = Varxy = Varyy = -1;
540 penalty =
initCoeff(D,
X,
Y,
coefNorm,
expCoeffx2,
expCoeffy2,
expCoeffxy, NCells1, Varxx, Varxy, Varyy);
542 if (
Varx0 < 0 && penalty <= 0) {
554 for (
int i = 0; i <
NCells; i++) {
565 double &expCoeffx2,
double &expCoeffy2,
double &expCoeffxy,
int &NCells,
566 double &Varxx,
double &Varxy,
double &Varyy)
const {
569 bool zeroDenom =
false;
572 else if (
TotI - Background *
TotN <= 0)
581 Varxx = std::min<double>(Varxx, 1.21 *
Varx0);
582 Varxx = std::max<double>(Varxx, .79 *
Varx0);
597 Varyy = std::min<double>(Varyy, 1.21 *
Vary0);
598 Varyy = std::max<double>(Varyy, .79 *
Vary0);
614 double paramUU = Varxx * Varyy - Varxy * Varxy;
620 std::max<double>(0, -Varxx + .01) + std::max<double>(0, -Varyy + .01) + std::max<double>(0, -paramUU + .01);
622 if (
fabs(paramUU) < .01) {
629 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.
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)