Alexandria 2.31.0
SDC-CH common library for the Euclid project
Loading...
Searching...
No Matches
multiplication.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2012-2022 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
31#include <memory>
32#include <set>
33#include <vector>
34
35namespace Euclid {
36namespace MathUtils {
37
40 const auto& p1 = dynamic_cast<const Polynomial&>(f1);
41 const auto& p2 = dynamic_cast<const Polynomial&>(f2);
42 std::vector<double> c1 = p1.getCoefficients();
43 std::vector<double> c2 = p2.getCoefficients();
45 for (size_t i = 0; i < c1.size(); ++i)
46 for (size_t j = 0; j < c2.size(); ++j)
47 resultCoef[i + j] += c1[i] * c2[j];
49}
50
62
63template <typename Iter>
65 for (auto i1 = start1, i2 = start2; i1 != end1 && i2 != end2;) {
66 if (*i1 < *i2) {
67 ++i1;
68 if (i1 != end1 && *i1 > *i2) {
69 return {i1, i2};
70 }
71 } else {
72 ++i2;
73 if (i2 != end2 && *i1 < *i2) {
74 return {i1, i2};
75 }
76 }
77 }
78 return {end1, end2};
79}
80
99
100// Multiply two Piecewise Function%s by multiplying all their sub-functions
102 const auto& p1 = dynamic_cast<const Piecewise&>(f1);
103 const auto& p2 = dynamic_cast<const Piecewise&>(f2);
104
105 auto knots = overlappingKnots(p1.getKnots(), p2.getKnots());
106
107 if (knots.empty()) {
109 }
110
112 auto& p1func = p1.getFunctions();
113 auto& p2func = p2.getFunctions();
114 int i1{};
115 int i2{};
116 for (double knot : knots) {
117 if (knot == knots.back()) {
118 break;
119 }
120 while (p1.getKnots()[i1 + 1] <= knot) {
121 ++i1;
122 }
123 while (p2.getKnots()[i2 + 1] <= knot) {
124 ++i2;
125 }
127 }
128
130}
131
135
138
139} // namespace MathUtils
140} // end of namespace Euclid
T back(T... args)
T begin(T... args)
Interface class representing a function with an arbitrary number of parameters.
Definition Function.h:104
Represents a piecewise function.
Definition Piecewise.h:87
Represents a polynomial function.
Definition Polynomial.h:43
T empty(T... args)
T end(T... args)
T move(T... args)
std::vector< double > overlappingKnots(const std::vector< double > &knots1, const std::vector< double > &knots2)
Returns a vector of the overlapping knots from the given vectors.
ELEMENTS_API std::map< std::pair< std::type_index, std::type_index >, MultiplyFunction > multiplySpecificSpecificMap
std::unique_ptr< Function >(* MultiplyFunction)(const Function &, const Function &)
Alias of a function which multiplies Function objects.
ELEMENTS_API std::map< std::type_index, MultiplyFunction > multiplySpecificGenericMap
static std::pair< Iter, Iter > overlappingStart(Iter start1, Iter end1, Iter start2, Iter end2)
std::unique_ptr< Function > multiplyPolynomials(const Function &f1, const Function &f2)
Function for multiplying two Polynomials. It multiplies their coefficients.
std::array< std::vector< double >, N > Coordinates
Used to pass the grid coordinates to interpn. Internally will make a copy of the required values.
std::unique_ptr< Function > multiplyPiecewiseWithGeneric(const Function &f1, const Function &f2)
std::unique_ptr< Function > multiplyPiecewises(const Function &f1, const Function &f2)
ELEMENTS_API std::unique_ptr< Function > multiply(const Function &f1, const Function &f2)
T size(T... args)
T tie(T... args)
T transform(T... args)