Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #include "MOR_SingularValuesHelpers.hpp"
00008
00009 #include <algorithm>
00010 #include <numeric>
00011 #include <iterator>
00012 #include <functional>
00013 #include <cmath>
00014
00015 namespace MOR {
00016
00017 namespace Detail {
00018
00019 struct square : public std::unary_function<double, double> {
00020 double operator()(double x) const { return x * x; }
00021 };
00022
00023 class relative_magnitude_from_square : public std::unary_function<double, double> {
00024 public:
00025 explicit relative_magnitude_from_square(double x2_ref) :
00026 x2_ref_(x2_ref)
00027 {}
00028
00029 double operator()(double x2) const { return std::sqrt(x2 / x2_ref_); }
00030
00031 private:
00032 double x2_ref_;
00033 };
00034
00035 }
00036
00037 Teuchos::Array<double> computeDiscardedEnergyFractions(Teuchos::ArrayView<const double> singularValues)
00038 {
00039 Teuchos::Array<double> result;
00040
00041 if (singularValues.begin() != singularValues.end()) {
00042 result.reserve(singularValues.size());
00043 std::transform(
00044 singularValues.begin(), singularValues.end(),
00045 std::back_inserter(result),
00046 Detail::square());
00047
00048 std::partial_sum(result.rbegin(), result.rend(), result.rbegin());
00049
00050 std::transform(result.begin() + 1, result.end(),
00051 result.begin(),
00052 Detail::relative_magnitude_from_square(result.front()));
00053 result.back() = 0.0;
00054 }
00055
00056 return result;
00057 }
00058
00059 }