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 boost::multi_array<double, 2> cov(boost::extents[size][size]);
61 for (int i = 0; i < size; ++i) {
62 for (int j = 0; j <= i; ++j) {
63 double diff = sqrt(fabs(input[i] - input[j]));
64 cov[i][j] = diff;
65 cov[j][i] = diff;
66 }
67 }
68 std::vector<double> sums(size);
69 for (int i = 0; i < size; ++i) {
70 sums[i] = std::accumulate(cov[i].begin(), cov[i].end(), 0.);
71 }
72 const auto minIndex = std::min_element(sums.cbegin(), sums.cend());
73 setProperty("Output", input[std::distance(sums.cbegin(), minIndex)]);
74}
75
76} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
#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.
Definition: Algorithm.cpp:1913
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
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.
Definition: ArrayProperty.h:28
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