BSplinebasis
Loading...
Searching...
No Matches
CompoundOperators.h
1/*
2 * ########################################################################
3 * The contents of this file is free and unencumbered software released into the
4 * public domain. For more information, please refer to <http://unlicense.org/>
5 * ########################################################################
6 */
7
8#ifndef BSPLINE_OPERATORS_COMPOUNDOPERATORS_H
9#define BSPLINE_OPERATORS_COMPOUNDOPERATORS_H
10
11#include <bspline/operators/GenericOperators.h>
12
20template <typename O1, typename O2,
21 std::enable_if_t<are_operators_v<O1, O2>, bool> = true>
23 private:
25 O1 _o1;
27 O2 _o2;
28
29 public:
36 OperatorProduct(O1 o1, O2 o2) : _o1(std::move(o1)), _o2(std::move(o2)){};
37
44 static constexpr size_t outputOrder(size_t inputOrder) {
45 return O1::outputOrder(O2::outputOrder(inputOrder));
46 }
47
63 template <typename T, size_t size>
64 std::array<T, outputOrder(size - 1) + 1> transform(
65 const std::array<T, size> &input, const support::Grid<T> &grid,
66 size_t intervalIndex) const {
67 return _o1.transform(_o2.transform(input, grid, intervalIndex), grid,
69 }
70};
71
82template <typename O1, typename O2,
83 std::enable_if_t<are_operators_v<O1, O2>, bool> = true>
85 return OperatorProduct(std::forward<O1>(o1), std::forward<O2>(o2));
86}
87
88// ######################### OperatorProduct #############################
89// #######################################################################
90
91// #######################################################################
92// ########################### OperatorSum ###############################
93
100};
101
109template <AdditionOperation operation>
110inline constexpr bool is_valid_operation_v = (operation ==
112 (operation ==
114
124template <
125 typename O1, typename O2, AdditionOperation operation,
126 std::enable_if_t<are_operators_v<O1, O2> && is_valid_operation_v<operation>,
127 bool> = true>
128class OperatorSum final : public Operator {
129 private:
131 O1 _o1;
133 O2 _o2;
134
148 template <typename T, size_t sizea, size_t sizeb>
149 static std::array<T, std::max(sizea, sizeb)> &add(std::array<T, sizea> &a,
150 std::array<T, sizeb> &b) {
151 if constexpr (sizeb > sizea) {
152 return add(b, a);
153 } else {
154 for (size_t i = 0; i < sizeb; i++) {
155 a[i] += b[i];
156 }
157 return a;
158 }
159 }
160
161 public:
168 OperatorSum(O1 o1, O2 o2) : _o1(std::move(o1)), _o2(std::move(o2)){};
169
176 static constexpr size_t outputOrder(size_t inputOrder) {
177 return std::max(O1::outputOrder(inputOrder), O2::outputOrder(inputOrder));
178 }
179
195 template <typename T, size_t size>
196 std::array<T, outputOrder(size - 1) + 1> transform(
197 const std::array<T, size> &input, const support::Grid<T> &grid,
198 size_t intervalIndex) const {
199 auto a = _o1.transform(input, grid, intervalIndex);
200 auto b = _o2.transform(input, grid, intervalIndex);
201
202 // Negate b if subtraction is requested.
203 if constexpr (operation == AdditionOperation::SUBTRACTION) {
204 for (T &el : b) {
205 el *= static_cast<T>(-1);
206 }
207 }
208
209 return add(a, b);
210 }
211};
212
224template <typename O1, typename O2,
225 std::enable_if_t<are_operators_v<O1, O2>, bool> = true>
226auto operator+(O1 &&o1, O2 &&o2) {
228 std::forward<O2>(o2));
229}
230
242template <typename O1, typename O2,
243 std::enable_if_t<are_operators_v<O1, O2>, bool> = true>
244auto operator-(O1 &&o1, O2 &&o2) {
246 std::forward<O1>(o1), std::forward<O2>(o2));
247}
248
249} // namespace bspline::operators
250#endif // BSPLINE_OPERATORS_COMPOUNDOPERATORS_H
Represents the product of two operators.
Definition CompoundOperators.h:22
std::array< T, outputOrder(size - 1)+1 > transform(const std::array< T, size > &input, const support::Grid< T > &grid, size_t intervalIndex) const
Applies operator to one interval.
Definition CompoundOperators.h:64
OperatorProduct(O1 o1, O2 o2)
Creates an OperatorProduct from two operators.
Definition CompoundOperators.h:36
static constexpr size_t outputOrder(size_t inputOrder)
Returns the order of the output spline for a given input order.
Definition CompoundOperators.h:44
Operator sum. Represents the sum or difference of two operators.
Definition CompoundOperators.h:128
OperatorSum(O1 o1, O2 o2)
Creates an OperatorSum from two operators.
Definition CompoundOperators.h:168
static constexpr size_t outputOrder(size_t inputOrder)
Returns the order of the output spline for a given input order.
Definition CompoundOperators.h:176
std::array< T, outputOrder(size - 1)+1 > transform(const std::array< T, size > &input, const support::Grid< T > &grid, size_t intervalIndex) const
Applies opertor to interval.
Definition CompoundOperators.h:196
Marker interface for operators.
Definition GenericOperators.h:23
Represents a global Grid.
Definition Grid.h:27
Operator definitions.
Definition CompoundOperators.h:13
auto operator-(O1 &&o1, O2 &&o2)
Operator difference.
Definition CompoundOperators.h:244
auto operator+(O1 &&o1, O2 &&o2)
Operator sum.
Definition CompoundOperators.h:226
constexpr bool is_valid_operation_v
Validates operation.
Definition CompoundOperators.h:110
OperatorProduct< O1, O2 > operator*(O1 &&o1, O2 &&o2)
Operator multiplication operator.
Definition CompoundOperators.h:84
AdditionOperation
Definition CompoundOperators.h:95