25 #include "libmesh/statistics.h" 26 #include "libmesh/int_range.h" 27 #include "libmesh/libmesh_logging.h" 40 const dof_id_type n = cast_int<dof_id_type>(this->size());
42 normsq += ((*
this)[i] * (*this)[i]);
44 return std::sqrt(normsq);
51 LOG_SCOPE (
"minimum()",
"StatisticsVector");
53 const T min = *(std::min_element(this->begin(), this->end()));
64 LOG_SCOPE (
"maximum()",
"StatisticsVector");
66 const T max = *(std::max_element(this->begin(), this->end()));
77 LOG_SCOPE (
"mean()",
"StatisticsVector");
79 const dof_id_type n = cast_int<dof_id_type>(this->size());
85 the_mean += (
static_cast<Real>((*this)[i]) - the_mean ) /
86 static_cast<Real>(i + 1);
98 const dof_id_type n = cast_int<dof_id_type>(this->size());
103 LOG_SCOPE (
"median()",
"StatisticsVector");
105 std::sort(this->begin(), this->end());
115 the_median =
static_cast<Real>((*this)[lhs]);
120 the_median = (
static_cast<Real>((*this)[lhs]) +
121 static_cast<Real>((*
this)[rhs]) ) / 2.0;
130 template <
typename T>
141 template <
typename T>
144 const dof_id_type n = cast_int<dof_id_type>(this->size());
146 LOG_SCOPE (
"variance()",
"StatisticsVector");
148 Real the_variance = 0;
152 const Real delta = (
static_cast<Real>((*this)[i]) - mean_in );
153 the_variance += (delta * delta - the_variance) /
154 static_cast<Real>(i + 1);
158 the_variance *=
static_cast<Real>(n) / static_cast<Real>(n - 1);
164 template <
typename T>
167 const dof_id_type n = cast_int<dof_id_type>(this->size());
168 const Real max = this->maximum();
171 (*
this)[i] =
static_cast<T
>((*this)[i] / max);
178 template <
typename T>
185 const dof_id_type n = cast_int<dof_id_type>(this->size());
187 std::sort(this->begin(), this->end());
191 Real min =
static_cast<Real>(this->minimum());
192 Real max =
static_cast<Real>(this->maximum());
193 Real bin_size = (max - min) / static_cast<Real>(n_bins);
195 LOG_SCOPE (
"histogram()",
"StatisticsVector");
197 std::vector<Real> bin_bounds(n_bins+1);
199 bin_bounds[i] = min +
Real(i) * bin_size;
204 bin_bounds.back() += 1.e-6 * bin_size;
207 bin_members.resize(n_bins);
213 unsigned int unbinned=0;
224 Real current_val =
static_cast<Real>( (*this)[i] );
229 if (current_val < min)
241 if (current_val > bin_bounds[j+1])
258 libmesh_assert_equal_to(j, bin_members.size()-1);
265 const dof_id_type n_binned = std::accumulate(bin_members.begin(),
268 std::plus<dof_id_type>());
270 if (n-unbinned != n_binned)
272 libMesh::out <<
"Warning: The number of binned entries, n_binned=" 274 <<
", did not match the total number of binnable entries, n=" 275 << n-unbinned <<
"." << std::endl;
284 template <
typename T>
286 const std::string & filename,
290 std::vector<dof_id_type> bin_members;
291 this->histogram(bin_members, n_bins);
294 T min = this->minimum();
295 T max = this->maximum();
296 T bin_size = (max - min) / static_cast<T>(n_bins);
301 std::ofstream out_stream (filename.c_str());
303 out_stream <<
"clear all\n";
304 out_stream <<
"clf\n";
311 out_stream << min + (
Real(i)+0.5)*bin_size <<
" ";
313 out_stream <<
"];\n";
316 for (
auto bmi : bin_members)
318 out_stream << bmi <<
" ";
320 out_stream <<
"];\n";
321 out_stream <<
"bar(x,y);\n";
327 template <
typename T>
329 unsigned int n_bins)
const 333 return sv.
histogram(bin_members, n_bins);
339 template <
typename T>
342 LOG_SCOPE (
"cut_below()",
"StatisticsVector");
344 const dof_id_type n = cast_int<dof_id_type>(this->size());
346 std::vector<dof_id_type> cut_indices;
347 cut_indices.reserve(n/2);
351 if ((*
this)[i] < cut)
353 cut_indices.push_back(i);
363 template <
typename T>
366 LOG_SCOPE (
"cut_above()",
"StatisticsVector");
368 const dof_id_type n = cast_int<dof_id_type>(this->size());
370 std::vector<dof_id_type> cut_indices;
371 cut_indices.reserve(n/2);
374 if ((*
this)[i] > cut)
375 cut_indices.push_back(i);
387 #ifdef LIBMESH_DEFAULT_TRIPLE_PRECISION 390 #ifdef LIBMESH_DEFAULT_QUADRUPLE_PRECISION virtual T maximum() const
virtual Real mean() const
virtual Real l2_norm() const
void normalize()
Divides all entries by the largest entry and stores the result.
The StatisticsVector class is derived from the std::vector<> and therefore has all of its useful feat...
virtual std::vector< dof_id_type > cut_above(Real cut) const
The libMesh namespace provides an interface to certain functionality in the library.
void plot_histogram(const processor_id_type my_procid, const std::string &filename, unsigned int n_bins)
Generates a Matlab/Octave style file which can be used to make a plot of the histogram having the des...
uint8_t processor_id_type
virtual void histogram(std::vector< dof_id_type > &bin_members, unsigned int n_bins=10)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual std::vector< dof_id_type > cut_below(Real cut) const
virtual T minimum() const
virtual Real variance() const
auto index_range(const T &sizable)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...