LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
GaussianEliminationAlg.cxx
Go to the documentation of this file.
1 
11 #include "GaussianEliminationAlg.h"
12 #include <cmath>
13 #include <iostream>
14 #include <limits>
15 #include <stdexcept>
16 
18 {
19  fDistanceStepSize = step;
20  fDistanceMax = max;
21 
22  if (fDistanceStepSize < 0 || fDistanceMax < 0)
23  throw std::runtime_error(
24  "Error in GaussianEliminationAlg: Cannot construct with negative step or max.");
25 
27 }
28 
30 {
31 
32  fDistanceLookupTable.clear();
33  fDistanceLookupTable.reserve(std::ceil(fDistanceMax / fDistanceStepSize) + 2);
34 
35  //let's be extra safe, and make sure we have more sampling points than we will need
36  double x_val = 0.0;
37  while (x_val <= fDistanceMax + fDistanceStepSize) {
38  fDistanceLookupTable.push_back(std::exp(x_val * x_val * 0.5 * -1));
39  x_val += fDistanceStepSize;
40  }
41  //do one more to be sure to push beyond...
42  fDistanceLookupTable.push_back(std::exp(x_val * x_val * 0.5 * -1));
43 }
44 
46 {
47  double d_abs = std::abs(d);
48  if (d_abs > fDistanceMax) return 0.0;
49 
50  size_t low_bin = std::floor(d_abs / fDistanceStepSize);
51  return fDistanceLookupTable[low_bin] -
52  (d_abs / fDistanceStepSize - (double)low_bin) *
53  (fDistanceLookupTable[low_bin] - fDistanceLookupTable[low_bin + 1]);
54 }
55 
57  const std::vector<float>& meanVector,
58  const std::vector<float>& sigmaVector,
59  const std::vector<float>& heightVector)
60 {
61 
62  if (meanVector.size() != sigmaVector.size() || meanVector.size() != heightVector.size())
63  throw std::runtime_error("Error in GaussianEliminationAlg: Vector sizes not equal.");
64 
65  FillAugmentedMatrix(meanVector, sigmaVector, heightVector);
67 
68  return fSolutions;
69 }
70 
71 void util::GaussianEliminationAlg::FillAugmentedMatrix(const std::vector<float>& meanVector,
72  const std::vector<float>& sigmaVector,
73  const std::vector<float>& heightVector)
74 {
75 
76  fMatrix.resize(meanVector.size());
77  for (size_t i = 0; i < meanVector.size(); i++) {
78 
79  fMatrix[i].resize(meanVector.size() + 1);
80  for (size_t j = 0; j < meanVector.size(); j++) {
81  if (sigmaVector[j] < std::numeric_limits<float>::epsilon()) {
82  if (i == j)
83  fMatrix[i][j] = 1.0;
84  else
85  fMatrix[i][j] = 0.0;
86  }
87  else
88  fMatrix[i][j] = GetDistance((meanVector[i] - meanVector[j]) / sigmaVector[j]);
89  }
90  fMatrix[i][meanVector.size()] = heightVector[i];
91  }
92 }
93 
95 {
96 
97  fSolutions.resize(fMatrix.size(), 0.0);
98 
99  for (size_t i = 0; i < fMatrix.size(); i++) {
100 
101  for (size_t j = i + 1; j < (fMatrix[i].size() - 1); j++) {
102  float scale_value = fMatrix[j][i] / fMatrix[i][i];
103  for (size_t k = i; k < fMatrix[i].size(); k++)
104  fMatrix[j][k] -= fMatrix[i][k] * scale_value;
105  } //end column loop
106 
107  } //end row loop
108 
109  for (int i = fMatrix.size() - 1; i >= 0; i--) {
110  fSolutions[i] = fMatrix[i].back();
111 
112  for (size_t j = i + 1; j < fMatrix.size(); j++)
113  fSolutions[i] -= fMatrix[i][j] * fSolutions[j];
114 
115  fSolutions[i] /= fMatrix[i][i];
116  }
117 }
118 
120 {
121  std::cout << "GaussianEliminationAlg." << std::endl;
122 
123  std::cout << "\tLookup table (step=" << fDistanceStepSize << ", max=" << fDistanceMax << ")"
124  << std::endl;
125  for (size_t i = 0; i < fDistanceLookupTable.size(); i++)
126  std::cout << "\t\tGaussian(" << fDistanceStepSize * i << ") = " << fDistanceLookupTable[i]
127  << std::endl;
128 
129  std::cout << "\tAugmented matrix " << std::endl;
130  for (size_t i = 0; i < fMatrix.size(); i++) {
131  std::cout << "\t\t | ";
132  for (size_t j = 0; j < fMatrix[i].size() - 1; j++)
133  std::cout << fMatrix[i][j] << " ";
134  std::cout << " | " << fMatrix[i][fMatrix[i].size() - 1] << " |" << std::endl;
135  }
136 
137  std::cout << "\tSolutions" << std::endl;
138  for (size_t i = 0; i < fSolutions.size(); i++)
139  std::cout << "\t\t" << i << " " << fSolutions[i] << std::endl;
140 }
constexpr auto abs(T v)
Returns the absolute value of the argument.
void FillAugmentedMatrix(const std::vector< float > &meanVector, const std::vector< float > &sigmaVector, const std::vector< float > &heightVector)
std::vector< std::vector< double > > fMatrix
Float_t d
Definition: plot.C:235
const std::vector< float > & SolveEquations(const std::vector< float > &meanVector, const std::vector< float > &sigmaVector, const std::vector< float > &heightVector)
std::vector< double > fDistanceLookupTable