Alexandria  2.16
Please provide a description of the project.
function_tools.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2020 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,
34  const double min,
35  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 
51 public:
52  DefaultMultiplication(const Function& f1, const Function& f2) : m_f1{f1.clone()}, m_f2{f2.clone()} {}
53  double operator()(const double x) const override {
54  return (*m_f1)(x) * (*m_f2)(x);
55  }
56  std::unique_ptr<Function> clone() const override {
57  return std::unique_ptr<Function> {new DefaultMultiplication{*m_f1, *m_f2}};
58  }
59 private:
60  std::unique_ptr<Function> m_f1;
61  std::unique_ptr<Function> m_f2;
62 };
63 
64 std::unique_ptr<Function> multiply(const Function& f1, const Function& f2) {
65  // First we check if we have specific function for multiplying the two types with each other
66  auto iter = multiplySpecificSpecificMap.find(std::pair<std::type_index,std::type_index>(typeid(f1),typeid(f2)));
67  if (iter != multiplySpecificSpecificMap.end()) {
68  return iter->second(f1,f2);
69  }
70  iter = multiplySpecificSpecificMap.find(std::pair<std::type_index,std::type_index>(typeid(f2),typeid(f1)));
71  if (iter != multiplySpecificSpecificMap.end()) {
72  return iter->second(f2,f1);
73  }
74  // Now we check if we have a specific function for multiplying one of the
75  // parameters with a generic function
76  auto iter2 = multiplySpecificGenericMap.find(typeid(f1));
77  if (iter2 != multiplySpecificGenericMap.end()) {
78  return iter2->second(f1,f2);
79  }
80  iter2 = multiplySpecificGenericMap.find(typeid(f2));
81  if (iter2 != multiplySpecificGenericMap.end()) {
82  return iter2->second(f2,f1);
83  }
84  // We couldn't find any specific function for handling the multiplication of
85  // the f1 and f2, so return the default
86  return std::unique_ptr<Function> {new DefaultMultiplication(f1, f2)};
87 }
88 
89 } // End of MathUtils
90 } // 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)
virtual std::unique_ptr< Function > clone() const =0
double operator()(const double x) const override