Alexandria 2.31.0
SDC-CH common library for the Euclid project
Loading...
Searching...
No Matches
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
32namespace Euclid {
33namespace MathUtils {
34
35double integrate(const Function& function, const double min, const double max,
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 << "requieres that you provide a NumericalIntegrationScheme";
48}
49
51public:
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
66
67private:
70};
71
73 // First we check if we have specific function for multiplying the two types with each other
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));
86 return iter2->second(f1, f2);
87 }
88 iter2 = multiplySpecificGenericMap.find(typeid(f2));
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
std::unique_ptr< Function > clone() const override
double operator()(const double x) const override
DefaultMultiplication(const Function &f1, const Function &f2)
Interface representing an integrable function.
Definition Integrable.h:44
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)
std::array< std::vector< double >, N > Coordinates
Used to pass the grid coordinates to interpn. Internally will make a copy of the required values.
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)