BSplinebasis
Loading...
Searching...
No Matches
LinearForm.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_INTEGRATION_LINEARFORM_H
9#define BSPLINE_INTEGRATION_LINEARFORM_H
10
11#include <bspline/Spline.h>
12#include <bspline/operators/GenericOperators.h>
13
14namespace bspline::integration {
15
25template <typename O,
26 std::enable_if_t<operators::is_operator_v<O>, bool> = true>
28 private:
30 O _o;
31
41 template <typename T, size_t size>
42 static T evaluateInterval(const std::array<T, size> &a, const T &dxhalf) {
43 // Use Horner's scheme to evaluate.
44 constexpr int endIndex =
45 static_cast<int>(size) - static_cast<int>(size % 2 == 0 ? 2 : 1);
46 static_assert(endIndex >= 0);
47
48 T result = a.at(endIndex) / static_cast<T>(endIndex + 1);
49 const T dxhalf_squared = dxhalf * dxhalf;
50
51 for (int i = endIndex - 2; i >= 0; i -= 2) {
52 result = dxhalf_squared * result + a[i] / static_cast<T>(i + 1);
53 }
54 return static_cast<T>(2) * dxhalf * result;
55 }
56
57 public:
63 explicit LinearForm(O o) : _o(std::move(o)){};
64
68 LinearForm() : _o(O{}){};
69
78 template <typename T, size_t order>
79 T evaluate(const Spline<T, order> &a) const {
80 const size_t nintervals = a.getSupport().numberOfIntervals();
81
82 T result = static_cast<T>(0);
83
84 for (size_t i = 0; i < nintervals; i++) {
85 const size_t absIndex = a.getSupport().absoluteFromRelative(i);
86 const T dxhalf =
87 (a.getSupport()[i + 1] - a.getSupport()[i]) / static_cast<T>(2);
88
89 result +=
90 evaluateInterval(_o.transform(a.getCoefficients()[i],
91 a.getSupport().getGrid(), absIndex),
92 dxhalf);
93 }
94 return result;
95 }
96
101 template <typename T, size_t order>
103 return evaluate(a);
104 }
105};
106
115
116} // namespace bspline::integration
117#endif // BSPLINE_INTEGRATION_LINEARFORM_H
A linear form with a user-provided operator.
Definition LinearForm.h:27
T operator()(const Spline< T, order > &a) const
Definition LinearForm.h:102
LinearForm()
Default constructor constructing a LinearForm.
Definition LinearForm.h:68
LinearForm(O o)
Constructor constructing a LinearForm from an operator.
Definition LinearForm.h:63
T evaluate(const Spline< T, order > &a) const
Evaluates the linear form for a particular spline.
Definition LinearForm.h:79
Multiplicative identity operator.
Definition GenericOperators.h:95
Represents a global Grid.
Definition Grid.h:27
const T & at(size_t i) const
Gives access to the i-th element of the Grid.
Definition Grid.h:218
Integration routines for Splines.
Definition BilinearForm.h:21