21 :
m_data(), m_errors(), m_image(), m_dataCalc(), m_background(1.0), m_angle(-1.), m_chisq(-1.), m_directionsIm(),
22 m_coeffs(), m_entropy(
std::move(entropy)), m_transform(
std::move(transform)) {}
37 throw std::runtime_error(
"Cannot compute gradient of Chi");
41 size_t sizeDat =
m_data.size();
50 std::vector<double> cgrad(sizeDatCalc, 0.);
51 auto dpoints =
static_cast<double>(sizeDat);
52 for (
size_t i = 0; i < sizeDat; i++) {
68 throw std::runtime_error(
"No data were loaded");
81 throw std::runtime_error(
"No image was loaded");
93 throw std::runtime_error(
"Search directions have not been calculated");
106 throw std::runtime_error(
"Quadratic coefficients have not been calculated");
119 throw std::runtime_error(
"Angle has not been calculated");
131 throw std::runtime_error(
"Chisq has not been calculated");
156 const std::vector<double> &image,
double background,
157 const std::vector<double> &linearAdjustments,
158 const std::vector<double> &constAdjustments) {
160 if (data.empty() || errors.empty() || (data.size() != errors.size())) {
161 throw std::invalid_argument(
"Cannot calculate quadratic coefficients: invalid data");
164 throw std::invalid_argument(
"Cannot calculate quadratic coefficients: invalid image");
166 if (background == 0) {
167 throw std::invalid_argument(
"Cannot calculate quadratic coefficients: invalid background");
180 if (!linearAdjustments.empty()) {
181 if (linearAdjustments.size() <
m_dataCalc.size()) {
182 throw std::invalid_argument(
"Cannot adjust calculated data: too few linear adjustments");
184 for (
size_t j = 0; j <
m_dataCalc.size() / 2; ++j) {
187 m_dataCalc[2 * j] = yr * linearAdjustments[2 * j] - yi * linearAdjustments[2 * j + 1];
188 m_dataCalc[2 * j + 1] = yi * linearAdjustments[2 * j] + yr * linearAdjustments[2 * j + 1];
191 if (!constAdjustments.empty()) {
192 if (constAdjustments.size() <
m_dataCalc.size()) {
193 throw std::invalid_argument(
"Cannot adjust calculated data: too few constant adjustments");
195 for (
size_t i = 0; i <
m_dataCalc.size(); ++i) {
201 const size_t dim = 2;
203 size_t npoints =
m_image.size();
212 if (cgrad.size() != npoints || sgrad.size() != npoints || metric.size() != npoints)
213 throw std::runtime_error(
"Cannot calculate quadratic coefficients: invalid image space");
222 for (
size_t i = 0; i < npoints; i++) {
223 auto metric2 = metric[i] * metric[i];
224 cnorm += cgrad[i] * cgrad[i] * metric2;
225 snorm += sgrad[i] * sgrad[i] * metric2;
226 csnorm += cgrad[i] * sgrad[i] * metric2;
238 m_angle = sqrt(0.5 * (1. - csnorm / snorm / cnorm));
248 m_directionsIm = std::vector<std::vector<double>>(2, std::vector<double>(npoints, 0.));
250 for (
size_t i = 0; i < npoints; i++) {
257 auto directionsDat = std::vector<std::vector<double>>(2, std::vector<double>(npoints, 0.));
262 double factor =
getChisq() * double(npoints) / 2;
263 auto resolutionFactor =
static_cast<double>(
m_dataCalc.size() /
m_data.size());
270 for (
size_t k = 0; k < dim; k++) {
272 for (
size_t i = 0; i < npoints; i++) {
281 for (
size_t k = 0; k < dim; k++) {
282 for (
size_t l = 0; l < k + 1; l++) {
284 for (
size_t i = 0; i < npoints; i++) {
292 for (
size_t k = 0; k < dim; k++) {
293 for (
size_t l = 0; l < k + 1; l++) {
295 for (
size_t i = 0; i < npoints; i++) {
299 m_coeffs.
c2[k][l] *= 2.0 / factor * resolutionFactor;
304 for (
size_t k = 0; k < dim; k++) {
305 for (
size_t l = k + 1; l < dim; l++) {
317 throw std::runtime_error(
"Cannot calculate chi-square");
325 size_t npoints =
m_data.size();
326 auto dpoints =
static_cast<double>(npoints);
329 for (
size_t i = 0; i < npoints; i++) {
331 chisq += term * term;
333 return chisq / dpoints;
const std::vector< Type > & m_data
std::vector< std::vector< double > > getSearchDirections() const
Returns the search directions (in image space)
std::vector< double > m_data
void calculateChisq()
Calculates chi-square.
QuadraticCoefficients m_coeffs
std::vector< double > m_errors
std::vector< double > calculateData(const std::vector< double > &image) const
MaxentTransform_sptr m_transform
double getAngle() const
Returns the angle between the gradient of chi-square and the gradient of the entropy (calculated and ...
MaxentCalculator()=delete
std::vector< double > getReconstructedData() const
Returns the reconstructed (calculated) data.
std::vector< std::vector< double > > m_directionsIm
QuadraticCoefficients getQuadraticCoefficients() const
Returns the quadratic coefficients.
std::vector< double > calculateChiGrad() const
Calculates the gradient of chi-square using the experimental data, calculated data and errors.
MaxentEntropy_sptr m_entropy
void iterate(const std::vector< double > &data, const std::vector< double > &errors, const std::vector< double > &image, double background, const std::vector< double > &linearAdjustments, const std::vector< double > &constAdjustments)
Performs an iteration and calculates everything: search directions (SB.
std::vector< double > m_dataCalc
double getChisq()
Returns chi-square.
double calculateChiSquared(const std::vector< double > &data) const
Calculate ChiSq = 1 / N sum_i [ data_i - dataCalc_i ]^2 / [ error_i ]^2.
std::vector< double > getImage() const
Returns the (reconstructed) image.
std::vector< double > m_image
std::vector< double > calculateImage(const std::vector< double > &data) const
std::pair< size_t, size_t > size() const
Access matrix sizes.
std::shared_ptr< MaxentTransform > MaxentTransform_sptr
std::shared_ptr< MaxentEntropy > MaxentEntropy_sptr
Mantid::Kernel::Matrix< double > DblMatrix
Mantid::Kernel::DblMatrix s2
Mantid::Kernel::DblMatrix s1
Mantid::Kernel::DblMatrix c1
Mantid::Kernel::DblMatrix c2