Alexandria  2.19
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
30 namespace Euclid {
31 namespace MathUtils {
32 
33 double integrate(const Function& function, const double min, const double max,
34  std::unique_ptr<NumericalIntegrationScheme> numericalIntegrationScheme) {
35  const Integrable* integrable = dynamic_cast<const Integrable*>(&function);
36  if (integrable) {
37  return integrable->integrate(min, max);
38  }
39 
40  if (numericalIntegrationScheme != nullptr) {
41  return (*numericalIntegrationScheme)(function, min, max);
42  }
43 
44  throw Elements::Exception() << "Numerical integration of non-Integrable Functions "
45  << "requiere that you provide a NumericalIntegrationScheme";
46 }
47 
49 public:
50  DefaultMultiplication(const Function& f1, const Function& f2) : m_f1{f1.clone()}, m_f2{f2.clone()} {}
51  double operator()(const double x) const override {
52  return (*m_f1)(x) * (*m_f2)(x);
53  }
54  std::unique_ptr<Function> clone() const override {
56  }
57 
58 private:
61 };
62 
64  // First we check if we have specific function for multiplying the two types with each other
65  auto iter = multiplySpecificSpecificMap.find(std::pair<std::type_index, std::type_index>(typeid(f1), typeid(f2)));
66  if (iter != multiplySpecificSpecificMap.end()) {
67  return iter->second(f1, f2);
68  }
70  if (iter != multiplySpecificSpecificMap.end()) {
71  return iter->second(f2, f1);
72  }
73  // Now we check if we have a specific function for multiplying one of the
74  // parameters with a generic function
75  auto iter2 = multiplySpecificGenericMap.find(typeid(f1));
76  if (iter2 != multiplySpecificGenericMap.end()) {
77  return iter2->second(f1, f2);
78  }
79  iter2 = multiplySpecificGenericMap.find(typeid(f2));
80  if (iter2 != multiplySpecificGenericMap.end()) {
81  return iter2->second(f2, f1);
82  }
83  // We couldn't find any specific function for handling the multiplication of
84  // the f1 and f2, so return the default
86 }
87 
88 } // namespace MathUtils
89 } // end of namespace Euclid
ELEMENTS_API std::unique_ptr< Function > multiply(const Function &f1, const Function &f2)
Interface class representing a function.
Definition: Function.h:46
ELEMENTS_API std::map< std::type_index, MultiplyFunction > multiplySpecificGenericMap
Interface representing an integrable function.
Definition: Integrable.h:44
std::unique_ptr< Function > clone() const override
ELEMENTS_API std::map< std::pair< std::type_index, std::type_index >, MultiplyFunction > multiplySpecificSpecificMap
ELEMENTS_API double integrate(const Function &function, const double min, const double max, std::unique_ptr< NumericalIntegrationScheme > numericalIntegrationScheme=nullptr)
virtual double integrate(const double a, const double b) const =0
DefaultMultiplication(const Function &f1, const Function &f2)
STL class.
virtual std::unique_ptr< Function > clone() const =0
double operator()(const double x) const override