Alexandria  2.22.0
Please provide a description of the project.
function_tools.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2021 Euclid Science Ground Segment
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 3.0 of the License, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
29 #include <algorithm>
30 #include <functional>
31 
32 namespace Euclid {
33 namespace MathUtils {
34 
35 double integrate(const Function& function, const double min, const double max,
36  std::unique_ptr<NumericalIntegrationScheme> numericalIntegrationScheme) {
37  const Integrable* integrable = dynamic_cast<const Integrable*>(&function);
38  if (integrable) {
39  return integrable->integrate(min, max);
40  }
41 
42  if (numericalIntegrationScheme != nullptr) {
43  return (*numericalIntegrationScheme)(function, min, max);
44  }
45 
46  throw Elements::Exception() << "Numerical integration of non-Integrable Functions "
47  << "requiere that you provide a NumericalIntegrationScheme";
48 }
49 
50 class DefaultMultiplication final : public Function {
51 public:
52  DefaultMultiplication(const Function& f1, const Function& f2) : m_f1{f1.clone()}, m_f2{f2.clone()} {}
53 
54  double operator()(const double x) const override {
55  return (*m_f1)(x) * (*m_f2)(x);
56  }
57 
58  void operator()(const std::vector<double>& xs, std::vector<double>& out) const override {
59  out.resize(xs.size());
60  std::transform(xs.begin(), xs.end(), out.begin(), std::cref(*this));
61  }
62 
63  std::unique_ptr<Function> clone() const override {
65  }
66 
67 private:
70 };
71 
73  // First we check if we have specific function for multiplying the two types with each other
74  auto iter = multiplySpecificSpecificMap.find(std::pair<std::type_index, std::type_index>(typeid(f1), typeid(f2)));
75  if (iter != multiplySpecificSpecificMap.end()) {
76  return iter->second(f1, f2);
77  }
79  if (iter != multiplySpecificSpecificMap.end()) {
80  return iter->second(f2, f1);
81  }
82  // Now we check if we have a specific function for multiplying one of the
83  // parameters with a generic function
84  auto iter2 = multiplySpecificGenericMap.find(typeid(f1));
85  if (iter2 != multiplySpecificGenericMap.end()) {
86  return iter2->second(f1, f2);
87  }
88  iter2 = multiplySpecificGenericMap.find(typeid(f2));
89  if (iter2 != multiplySpecificGenericMap.end()) {
90  return iter2->second(f2, f1);
91  }
92  // We couldn't find any specific function for handling the multiplication of
93  // the f1 and f2, so return the default
95 }
96 
97 } // namespace MathUtils
98 } // end of namespace Euclid
T begin(T... args)
void operator()(const std::vector< double > &xs, std::vector< double > &out) const override
double operator()(const double x) const override
std::unique_ptr< Function > clone() const override
DefaultMultiplication(const Function &f1, const Function &f2)
Interface representing an integrable function.
Definition: Integrable.h:44
virtual double integrate(const double a, const double b) const =0
Interface class representing a function with an arbitrary number of parameters.
Definition: Function.h:104
T end(T... args)
ELEMENTS_API std::map< std::pair< std::type_index, std::type_index >, MultiplyFunction > multiplySpecificSpecificMap
ELEMENTS_API std::map< std::type_index, MultiplyFunction > multiplySpecificGenericMap
ELEMENTS_API double integrate(const Function &function, const double min, const double max, std::unique_ptr< NumericalIntegrationScheme > numericalIntegrationScheme=nullptr)
ELEMENTS_API std::unique_ptr< Function > multiply(const Function &f1, const Function &f2)
T cref(T... args)
T resize(T... args)
T size(T... args)
T transform(T... args)