/
MostLikelyMean.cpp
76 lines (64 loc) · 2.96 KB
/
MostLikelyMean.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidAlgorithms/MostLikelyMean.h"
#include "MantidKernel/ArrayLengthValidator.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/MultiThreaded.h"
#include "MantidKernel/PropertyWithValue.h"
#include "boost/multi_array.hpp"
namespace Mantid::Algorithms {
using Mantid::Kernel::ArrayLengthValidator;
using Mantid::Kernel::ArrayProperty;
using Mantid::Kernel::Direction;
using Mantid::Kernel::PropertyWithValue;
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(MostLikelyMean)
//----------------------------------------------------------------------------------------------
/// Algorithms name for identification. @see Algorithm::name
const std::string MostLikelyMean::name() const { return "MostLikelyMean"; }
/// Algorithm's version for identification. @see Algorithm::version
int MostLikelyMean::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string MostLikelyMean::category() const { return "Arithmetic"; }
/// Algorithm's summary for use in the GUI and help. @see Algorithm::summary
const std::string MostLikelyMean::summary() const {
return "Computes the most likely mean of the array by minimizing the taxicab "
"distance of the elements from the rest.";
}
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void MostLikelyMean::init() {
auto lengthValidator = std::make_shared<ArrayLengthValidator<double>>();
lengthValidator->setLengthMin(1);
declareProperty(std::make_unique<ArrayProperty<double>>("InputArray", lengthValidator, Direction::Input),
"An input array.");
declareProperty(std::make_unique<PropertyWithValue<double>>("Output", 0., Direction::Output), "The output (mean).");
}
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void MostLikelyMean::exec() {
const std::vector<double> input = getProperty("InputArray");
const auto size = static_cast<int>(input.size());
boost::multi_array<double, 2> cov(boost::extents[size][size]);
PARALLEL_FOR_NO_WSP_CHECK()
for (int i = 0; i < size; ++i) {
for (int j = 0; j <= i; ++j) {
double diff = sqrt(fabs(input[i] - input[j]));
cov[i][j] = diff;
cov[j][i] = diff;
}
}
std::vector<double> sums(size);
for (int i = 0; i < size; ++i) {
sums[i] = std::accumulate(cov[i].begin(), cov[i].end(), 0.);
}
const auto minIndex = std::min_element(sums.cbegin(), sums.cend());
setProperty("Output", input[std::distance(sums.cbegin(), minIndex)]);
}
} // namespace Mantid::Algorithms