Mantid
Loading...
Searching...
No Matches
MostLikelyMean.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
12
13#include "boost/multi_array.hpp"
14
15namespace Mantid::Algorithms {
16
21
22// Register the algorithm into the AlgorithmFactory
23DECLARE_ALGORITHM(MostLikelyMean)
24
25//----------------------------------------------------------------------------------------------
26
27
28const std::string MostLikelyMean::name() const { return "MostLikelyMean"; }
29
31int MostLikelyMean::version() const { return 1; }
32
34const std::string MostLikelyMean::category() const { return "Arithmetic"; }
35
37const std::string MostLikelyMean::summary() const {
38 return "Computes the most likely mean of the array by minimizing the taxicab "
39 "distance of the elements from the rest.";
40}
41
42//----------------------------------------------------------------------------------------------
46 auto lengthValidator = std::make_shared<ArrayLengthValidator<double>>();
47 lengthValidator->setLengthMin(1);
48 declareProperty(std::make_unique<ArrayProperty<double>>("InputArray", lengthValidator, Direction::Input),
49 "An input array.");
50 declareProperty(std::make_unique<PropertyWithValue<double>>("Output", 0., Direction::Output), "The output (mean).");
51}
52
53//----------------------------------------------------------------------------------------------
57 const std::vector<double> input = getProperty("InputArray");
58 const auto size = static_cast<int>(input.size());
59#if defined(_WIN32)
60// disable warning about multi_array
61#pragma warning(disable : 4996)
62#endif
63 boost::multi_array<double, 2> cov(boost::extents[size][size]);
65 for (int i = 0; i < size; ++i) {
66 for (int j = 0; j <= i; ++j) {
67 double diff = sqrt(fabs(input[i] - input[j]));
68 cov[i][j] = diff;
69 cov[j][i] = diff;
70 }
71 }
72 std::vector<double> sums(size);
73 for (int i = 0; i < size; ++i) {
74 sums[i] = std::accumulate(cov[i].begin(), cov[i].end(), 0.);
75 }
76 const auto minIndex = std::min_element(sums.cbegin(), sums.cend());
77 setProperty("Output", input[std::distance(sums.cbegin(), minIndex)]);
78}
79
80} // namespace Mantid::Algorithms
std::string name
Definition Run.cpp:60
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
#define fabs(x)
Definition Matrix.cpp:22
#define PARALLEL_FOR_NO_WSP_CHECK()
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
MostLikelyMean : Computes the most likely mean of the array by minimizing the taxicab distance of the...
void init() override
Initialize the algorithm's properties.
const std::string category() const override
Algorithm's category for identification.
void exec() override
Execute the algorithm.
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
int version() const override
Algorithm's version for identification.
ArrayLenghtValidator : Validate length of an array property.
Support for a property that holds an array of values.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
The concrete, templated class for properties.
STL namespace.
Describes the direction (within an algorithm) of a Property.
Definition Property.h:50
@ Input
An input workspace.
Definition Property.h:53
@ Output
An output workspace.
Definition Property.h:54